0

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.

html code:

<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. Is there any way I can improve this or write any loop for this?

4
  • Can you use css or js ? Commented May 19, 2020 at 14:48
  • 2
    For cases like this (where you have styles for multiple elements), it's best to use an external stylesheet or a <style> tag; then you can use a CSS selector like .box {} or #a, #b, #c, #d, #e {} to apply styles to all of your inputs Commented May 19, 2020 at 14:49
  • 1
    Why can't you use CSS classes? Commented May 19, 2020 at 14:49
  • .box { width: 6%; text-align:center; } instead of inline styles Commented May 19, 2020 at 14:49

2 Answers 2

3

Your HTML elements already have a CSS class attribute, so you can style them like this:

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

Sign up to request clarification or add additional context in comments.

Comments

0

The whole point of CSS classes is exactly to handle this situation. 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.

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. INPUT.box means "all INPUT elements with the class box"

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

2 Comments

Property values don't work that way - you're supposed to specify the values without quotes.
Good Lord, it really IS Monday. Thanks, fixing my answer. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.