Search code examples
cssinline-styles

Should I style an element which appears only once inline or in CSS?


I have a particular element which only appears once in the whole website. It is never likely to appear again anywhere else (it is a small site).

Are there any benefits obtained from styling this with CSS instead of a style attribute on the element itself?


Solution

  • If your element styles need to respond to different situations (as Alan mentions), you will not be able to do this with an inline style at all, as inline styles don't support pseudo-class states or media states or indeed, any form of state change.

    Having said that, if this is necessary, you can simply place your styles in an internal <style> element within the page containing this element. You get the best of both worlds: localized styles and a full Cascading Style Sheet.

    Whether you do this in an inline style attribute or an internal stylesheet the only other meaningful benefit will be perceived by you as an author: it depends on whether you prefer having all your styles in one place (the main stylesheet), or whether you prefer unique element styles not polluting your main stylesheet (or your theme stylesheet or whatever your stylesheet is intended for, assuming it is externally linked).

    As it is a small site, performance won't be an issue.