Why Avoid Inline Styling in HTML for Generic Styles?

csshtml

Given

<div>
   <textarea></textarea>
</div>

Why is this

<div>
   <textarea class="width90"></textarea>
</div>

.width90{
   width:90%;
}

any better than this?

<div>
   <textarea style="width:90%;"></textarea>
</div>

edit:
I updated my example better. My question should be more related to generic styles like widths, text align, etc.

Best Answer

Looking at your updated question,

Why is this

<div>
   <textarea class="width90"></textarea>
</div>

.width90{
   width:90%;
}

any better than this?

<div>
   <textarea style="width:90%;"></textarea>
</div>

My answer is It's not any better, and neither would pass a code review with me.

As noted in the comments, if you wanted to change the width from 90% to 85%, you'd still have to make changes in many places.

It would be far better to give your <textarea> tag a semantically meaningful class name such as 'InvoiceDescription', 'container', 'col-md-1', and so on. That way when someone later on wants to both change the width AND the font/background color/whatever, they can do it in one place.

Related Topic