简体   繁体   English

如何优化此 css 代码,以便在 html 中多次不编写相同的 css 代码?

[英]How can I optimise this css code for not writing same css code several times in html?

I have an input box.我有一个输入框。 Where the values are already defined in or it will be generated in some other rules that's why they all have individual id's .这些值已经在其中定义或将在其他一些规则中生成,这就是为什么它们都有单独的 id 的原因。

html code: 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"/>

now suppose, i've to decrease the size of the width 8 to 6. Then I've to write width:6%' 5 times, which is redundant thus I don't wanna use it.现在假设,我必须将宽度8 的大小减小到 6。然后我必须写width:6%' 5 次,这是多余的,因此我不想使用它。 Is there any way I can improve this or write any loop for this?有什么办法可以改进或为此编写任何循环吗?

Your HTML elements already have a CSS class attribute, so you can style them like this:您的 HTML 元素已经具有 CSS class 属性,因此您可以像这样设置它们的样式:

 .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"/>

The whole point of CSS classes is exactly to handle this situation. CSS 类的重点就是处理这种情况。 Rather than put style="width:8%; text-align:center" in each tag, you could use your box class to change all of them at once.您可以使用您的框 class 一次更改所有标签,而不是将style="width:8%; text-align:center"放在每个标签中。

INPUT.box {
  width: 8%;
  text-align: center;
}

The trick, of course, is to use the right CSS selector to access the things you want to change as a group.当然,诀窍是使用正确的 CSS 选择器来访问您想要作为一个组更改的内容。 INPUT.box means "all INPUT elements with the class box" INPUT.box表示“带有 class 框的所有 INPUT 元素”

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何多次重用相同的代码 - html/css - How to reuse the same code multiple times - html/css 如何将JavaScript变量放入HTML和CSS代码 - How can I put JavaScript variables into HTML and CSS code 您如何管理同一页面中多次包含的相同代码的 CSS 选择器? - How can you manage CSS selectors for identical code included multiple times in the same page? 如何在 javascript 中制作 css 代码? - How can I make the css code in javascript? 相同的CSS和HTML代码,但在2页上看起来不同 - Same css and HTML code but looking different on 2 pages 多个元素使用相同的 css 类,如何仅更改其中一个元素的 css 属性 - Several elements use the same css class, how can I change the css properties of only one of those elements 如何从 css 变量中获取值并使用 javascript 多次(循环)自动更改它 - How can i get value from css variable and change it automatically in several times (loop) using javascript 如何将 HTML、CSS 和 Javascript 组合成 HTML 代码 - How to combine HTML, CSS, and Javascript into an HTML code 在html表单中填写表格时,如何使自定义CSS代码无效? - How can I make the custom css code for invalid values while filling the form in html? 如何使用css,javascript或jquery忽略html“代码”块中的前导和尾随换行符? - How can I ignore leading and trailing linebreaks within an html “code” block using css, javascript or jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM