Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

When is inline CSS a good idea?

I'm under the assumption never. But maybe not? Slacker Radio places it inline.

slacker.com

What are the pros/cons?

10 Answers

Chase Lee
Chase Lee
29,275 Points

It's considered good practice to keep your css and your html separate. By adding in-line styles, it makes it more difficult to revise the look of a site, it makes it more difficult for future programmers to modify your site, and is overall NOT the best way to go. If everything is in a CSS file, then you can change the entire design of your site without having to mess with the data (HTML) of the site. So my advice do NOT use inline styles.

Cathrine Alvin
Cathrine Alvin
12,184 Points

You have to use inline css for html email unfortunately. And many rules are not supported by various email clients. This is important to know if you are making newsletters. slacker.com also uses flash which is probably not necessary for what they wish to do...

Guil Hernandez
Guil Hernandez
Treehouse Teacher

Oh yes – those too :) Some email clients don't even support the shorthand properties either.

Guil Hernandez
STAFF
Guil Hernandez
Treehouse Teacher

Richard Rakin Inline styles can be used when creating quick HTML / CSS mockups or prototypes, or for temporary styles that are not meant to be shared with other elements. They can also be of use when you need a quick fix in production while you sort out the issues locally––then remove when all is fixed.

Since they're at the lowest level possible of the cascade, they'll override all other styles, so we need to be careful.

I believe I heard it's good to use inline css for setting height and width of images. Any comments on that?

Guil Hernandez
Guil Hernandez
Treehouse Teacher

Hi Ernest Grzybowski,

Adding the width and height dimensions of embedded images can help minimize page reflows. It allows the browser to reserve space for the image even before it has downloaded. We can add those in either the <img> tag as width=" " height=" ", or in the CSS.

Spen Taylor
Spen Taylor
13,027 Points

I'd say mostly never.

That said, each time a new file is called ( a .css file or .js file for example) there is of cause an extra http request going through your server.

For a website with multiple pages that share certain scripts, this would hardly be a problem.

For a simple one page website inline CSS would be absolutely fine, something like a simple brochure site for a mobile app, small business even up to a search engine.

If the total CSS rules that your site uses is only something like 3 or 4, having your server produce a whole separate .css file for a single page would be pointless, not to mention that referring to the .css file might actually mean having more code in your .html file than the inline CSS would have taken up.

Obviously those types of sites are fairly rare in comparison to your usual website though, but I think this is all worth keeping in mind if you find yourself making a very simple one pager :)

Brian MacDonald
Brian MacDonald
4,951 Points

Between HTML, CSS and scripts a page of source code can get pretty messy, really quick! Especially when dealing with large websites with many different types of content.

Using style sheets allows for clean source code, and keeping it well laid out. Imagine you are a new developer to a project; going in it would be much easier to figure out how to change, update or fix an issue on a well laid out and commented page - whereas if there is clutter, jumble and a lot of different areas to look at it may be much harder to identify, in some cases make it even worse by changing things you did not intend to!

That is just my take on it, I like clean and efficient! CSS and HTML separate helps me achieve that.

Johan Hernández
Johan Hernández
4,141 Points

I agree with @Brian MacDonald... The only moment I use inline CSS is for e-mailing (like newsletters to say something).

Kevin Korte
Kevin Korte
28,148 Points

The only time I've personally ever used inline CSS styles is when I wanted PHP to generate the CSS value. One instance it generating a random background URL on page load. There are other cases though where you might want a CSS value generated on the fly.

That's it for me. Otherwise, I use it in an external file. The other points here are still valid.

George Offley
PLUS
George Offley
Courses Plus Student 7,386 Points

I used to use inline styles when I initially made my pages, and then I would convert them to external style sheets. It was just easier to to use one file when making the page, I wouldn't use them when I published a site though.

To answer your original question, the only time I have ever seen it and have it work is when using them with images.

Thanks for all the great responses. So I'm drawing the impression that whoever wrote the code for slacker.com probably used sloppy design practice. I wonder if GUI website making programs might do that too. Like Dreamweaver, etc.

appbites
appbites
2,949 Points

Richard Rakin ,

Please avoid using bad words like Dreamweaver :P