Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline <style> tags vs. inline css properties

Tags:

html

css

What is the preferred method for setting CSS properties?

Inline style properties:

<div style="width:20px;height:20px;background-color:#ffcc00;"></div> 

Style properties in <style>...</style> tags:

<style>.gold{width:20px;height:20px;background-color:#ffcc00;}</style><div class="gold"></div> 
like image 226
Paul Avatar asked Aug 17 '12 21:08

Paul


People also ask

What is the difference between CSS and inline styling?

The main difference between inline CSS and external CSS is that inline CSS is processed faster as it only requires the browser to download 1 file while using external CSS will require downloading HTML and CSS files separately.

Why inline CSS is not recommendable?

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.

What is inline style tag?

An inline CSS is used to apply a unique style to a single HTML element. An inline CSS uses the style attribute of an HTML element.

Are inline styles faster than CSS?

An inline CSS will load faster if the CSS content size downloads faster than your server would respond to an external CSS file request (considering DNS time, server latency, etc).


2 Answers

Style rules can be attached using:

  • External Files
  • In-page Style Tags
  • Inline Style Attribute

Generally, I prefer to use linked style sheets because they:

  • can be cached by browsers for performance; and
  • are a lot easier to maintain for a development perspective.

However, your question is asking specifically about the style tag versus inline styles. Prefer to use the style tag, in this case, because it:

  • provides a clear separation of markup from styling;
  • produces cleaner HTML markup; and
  • is more efficient with selectors to apply rules to multiple elements on a page improving management as well as making your page size smaller.

Inline elements only affect their respective element.

An important difference between the style tag and the inline attribute is specificity. Specificity determines when one style overrides another. Generally, inline styles have a higher specificity.

Read CSS: Specificity Wars for an entertaining look at this subject.

I hope that helps!

like image 135
jmbertucci Avatar answered Sep 23 '22 00:09

jmbertucci


Here's one aspect that could rule the difference:

If you change an element's style in JavaScript, you are affecting the inline style. If there's already a style there, you overwrite it permanently. But, if the style were defined in an external sheet or in a <style> tag, then setting the inline one to "" restores the style from that source.

like image 39
Niet the Dark Absol Avatar answered Sep 21 '22 00:09

Niet the Dark Absol