javascript - 为什么这些按钮在 iPhone 上是矩形的,而在我的桌面浏览器上却是方形的?
问题描述
我正在尝试制作方形按钮,但它们在 iPhone 上是宽矩形。为什么会这样?我有 JS 样式它们:
var btn = document.createElement("button");
btn.setAttribute("id", "paletteColor");
btn.className = "paletteButton"
btn.style.width = '25px';
btn.style.height = 'px';
btn.style.lineHeight = '0px';
btn.style.fontSize = '26px';
btn.style.verticalAlign = 'top';
btn.style.backgroundColor = "#CCCCCC";
var t = document.createTextNode("");
btn.appendChild(t);
document.getElementById("palette").appendChild(btn);
CSS:
.paletteButton {
display: inline-block;
border: 1px outset black;
cursor:pointer;
-webkit-text-stroke: 1px white;
-webkit-appearance: none;
}
然后我有一个 id 为“调色板”的空 div。
解决方案
似乎 IOS Safari 正在根据声明的字体大小计算默认填充。(在 iPad 上的 IOS 14.4 上测试)
如果我们在左边和右边设置填充为 0,那么我们会得到一个正方形。当然,这意味着如果您将文本放入任何调色板条目中,您可能还需要放入一些填充。
这是填充左/右 0 的片段。
var btn = document.createElement("button");
btn.setAttribute("id", "paletteColor");
btn.className = "paletteButton";
btn.style.paddingLeft = '0px';
btn.style.paddingRight = '0px';
btn.style.width = '25px';
btn.style.height = '25px';
btn.style.lineHeight = '0px';
btn.style.fontSize = '26px';
btn.style.verticalAlign = 'top';
btn.style.backgroundColor = "#CCCCCC";
var t = document.createTextNode("");
btn.appendChild(t);
document.getElementById("palette").appendChild(btn);
.paletteButton {
display: inline-block;
border: 1px outset black;
cursor:pointer;
-webkit-text-stroke: 1px white;
-webkit-appearance: none;
}
<div id="palette"></div>
推荐阅读
- r - 如何从ff对象中随机采样
- html - 在 ion-textarea Ionic 4 中设置值
- android - 我正在尝试将我的 APK 上传到 Playstore,但它无法正常工作...... :(
- c++ - 绕行 ShellExecuteEx 的问题,有什么想法吗?
- swift - 在自动续订订阅中检查真实性时推荐使用的逻辑是什么
- c# - 如何获取特定进程连接的 IP 地址?(C#)
- azure - Azure expressroute 和 VPN 与第三方 NVA
- javascript - 如何在对象中创建表头键行?
- variables - Java 从文本文件中读取变量编号值并分配给声明的程序变量
- android - 使用 cordova 运行命令行时签署调试 APK 文件