good_evening
good_evening

Reputation: 21749

Should I create a style in css or just add style to the element. What's the better practise?

Ok, so let's say I am coding HTML/CSS template and I see that I need to create a text with certain color and text-shadow. Should I create a different style in css file or just add style to the element? This <span style="color: #f2f12; text-shadow: 0 2px 0 #f1f1f1">Text</span> or this <span class="styleforthistext">Text</span> is better practise? If I know that I will use this color only for this text (only once).

Upvotes: 2

Views: 95

Answers (3)

Dmytro Shevchenko
Dmytro Shevchenko

Reputation: 34591

Your html layout should only contain data. All styling should be done in separate css files. This is a good practice for many reasons.

  • First of all, your code becomes more clean and readable.
  • Also, you could easily substitute your styles with another ones, not even touching the html.
  • You could reuse your classes in multiple places (given that you name and structure them properly).
  • If you name classes basing on semantics, your code becomes even more readable and easier to maintain.
  • etc., etc.

Upvotes: 6

Chamila Chulatunga
Chamila Chulatunga

Reputation: 4914

Using a separate style in the CSS file is usually the better practice. It's the principle of separating your concerns - content is in the HTML, presentation (style) is in CSS (i.e. not inlined with the HTML) and behavior is in JavaScript (again, not inlined in the HTML).

Upvotes: 0

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

You should ask yourself this, what is the meaning carried by the differently styled text?

Is it an emphasized text? Maybe a strong text? Is it a <h3>title</h3>?

Based on that, create the appropriate element, and style that on your stylesheet. Very few cases justify the use of the style= attribute, this isn't one of those cases.

Upvotes: 1

Related Questions