Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is using the style-attribute in html bad?

Tags:

html

css

I have been told, as well as read that using the style attribute in html is considered bad/sloppy/poor form. Further, that all rendering specific bits should be divorced into css and other parts as appropriate. I am trying to understand why exactly this is.

I can see why you might want to keep the HTML a pure semantic DOM, that speaks about the structure of the document, but for practical pages, the importance is that the page looks right and functions appropriately.

Is there some more compelling reasons for this separation?

like image 813
meawoppl Avatar asked Mar 02 '13 03:03

meawoppl


People also ask

Is using style in HTML bad?

Inline styles, while they have a purpose, generally are not the best way to maintain your website. They go against every one of the best practices: Inline styles don't separate content from design: Inline styles are exactly the same as embedded font and other clunky design tags that modern developers rail against.

Are style tags bad?

No, it's not bad practice. Learning to differentiate the concept is the key here. Usually tags styling are used only to define/reset styles at the most generic base for your webpage. The most common case you can see is used for reseting the styling for cross-browser purpose.

Why is it bad to use inline styles?

One of the main reasons that inline styling is not a good choice for your application is because it does not support (or it has really poor support) for CSS features. Every application nowadays might have to end up using some selectors such as :hover , :active , :focused , etc.

Why don't we use embedded styles in HTML?

If styles are embedded directly into HTML elements using the style attribute the HTML can become hard to read (and thus maintain) and can present accessibility issues for some visitors to your website.

Why does HTML code need the style attribute?

HTML code needs the style attribute for web pages to be rendered in web browsers like Chrome, FireFox, IE for them to be displayed as they are intended to look. HTML tags can contain various information, and the style attribute controls the appearance of information in HTML blocks using inline styling.

Why inline styles in HTML is a bad practice?

Use of inline styles in HTML is a bad practice because browser doesn’t understand it well. If your website is small and you don’t want to use external style sheets, then you should go for internal styles as browsers can understand them easily.

How to implement style in HTML?

There are 3 ways of implementing style in HTML : 1 Inline Style : In this method, the style attribute is used inside the HTML start tag. 2 Embedded Style : In this method, the style element is used inside the <head> element of the document. 3 External Style Sheet : In this method the <link> element is used to point to an external CSS file.

What is the difference between CSS style and embedded style?

The style attribute includes a series of CSS property and value pairs. Each ‘ property : value ‘ pair is separated by a semicolon ( ; ). Embedded Style : Embedded or internal style sheets only affect the document they are embedded in. Embedded style sheets are defined in the <head> section of an HTML document using the <style> tag.


2 Answers

  • Separation of concerns This makes it easy to replace the styles without changing the markup, or vice versa. Plus you can have one person working on CSS and another working on content

  • Don't Repeat Yourself You can apply a style to many elements without having to repeat it over and over. Smaller pages means quicker load times using less bandwidth. Plus it's easier to modify later, because you can change it in one place in one file, instead of many places in many files.

  • Cachability If the same style sheet is used on every page of your site, browsers can download it once and then cache it, instead of downloading the styles with the content of every single page. And they won't have to re-download it whenever the content of those pages changes.

  • Multiple Versions It is easy to create multiple versions of the visual layout and appearance of your site since you just need to swap out the stylesheet file to change the appearance of every page. For instance, you can create a white-label version of a web application which your partners can re-skin to match their brand. See CSS Zen Garden for some great examples of how flexible this approach can be.

like image 54
Tim Goodman Avatar answered Oct 30 '22 11:10

Tim Goodman


Start with this code:

<ul>
    <li style="color: blue;">One</li>
    <li style="color: blue;">Two</li>
    <li style="color: blue;">Three</li>
    <li style="color: blue;">Four</li>
</ul>

Let's say that today, you decide to change the link color to red. Since these styles are inline, you tediously have to walk through each element and change the style attribute. Imagine doing this for 10, maybe 20 HTML pages and you'll see why this becomes a problem.

Using a stylesheet separates the content:

<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
</ul>

From the style:

ul li {
    color: blue;
}

Had you used a stylesheet from the beginning, changing the color is as simple as changing blue to red in your stylesheet.

Aside from making the document easier to style, there's also selector specificity. Imagine that you inherited the first code chunk from a previous developer and would like to change the color again, but you (being a nice developer) prefer stylesheets:

ul li {
    color: red;
}

You'll soon become frustrated and resort to using !important, as your selectors can't override the inline styles.

like image 27
Blender Avatar answered Oct 30 '22 13:10

Blender