javascript - 如何优化此 css 代码,以免在 html 中多次编写相同的 css 代码?
问题描述
我有一个输入框。这些值已经在其中定义或将在其他一些规则中生成,这就是为什么它们都有单独 的 id 的原因。
html代码:
<input class="box" type="text" style="width:8%; text-align:center" id="a" value="A" readonly="true"/>
<input class="box" type="text" style="width:8%; text-align:center" id="b" value="B" readonly="true"/>
<input class="box" type="text" style="width:8%; text-align:center" id="c" value="C" readonly="true"/>
<input class="box" type="text" style="width:8%; text-align:center" id="d" value="D" readonly="true"/>
<input class="box" type="text" style="width:8%; text-align:center" id="e" value="E" readonly="true"/>
现在假设,我必须将宽度8减小到 6。然后我必须写width:6%'
5 次,这是多余的,因此我不想使用它。有什么办法可以改进或为此编写任何循环吗?
解决方案
您的 HTML 元素已经具有 CSS 类属性,因此您可以像这样设置它们的样式:
.box {
width: 6%;
text-align:center;
}
<input class="box" type="text" id="a" value="A" readonly="true"/>
<input class="box" type="text" id="b" value="B" readonly="true"/>
<input class="box" type="text" id="c" value="C" readonly="true"/>
<input class="box" type="text" id="d" value="D" readonly="true"/>
<input class="box" type="text" id="e" value="E" readonly="true"/>
推荐阅读
- visual-studio-code - Visual Studio Code 中的 Tcl 语法高亮显示损坏
- linux - 将 Google Colab 中已安装的包移动到环境的 PATH
- css - 如何更改属性值取决于用于查看页面的设备
- xcode - 升级到 Big Sur 后使用 altool validate-app 时无法验证
- sql - 基于字符串表达式的SQL提取
- python - 离线环境安装Python包找不到安装的依赖
- python - 第二次单击导航按钮时,如何阻止 Tkinter 窗口销毁?
- html - Angular - 包含整数对象的排序列
- r - 在 R 中读取 LTSpice .step 数据
- python - Heroku:推送失败的python