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.
Answer from jmbertucci on Stack Overflowhtml - Inline tags vs. inline css properties - Stack Overflow
Inline styles for a:link, a:hover etc in an email newsletter - HTML & CSS - SitePoint Forums | Web Development & Design Community
css - What's the difference between inline styles vs classes? - Stack Overflow
html - Inline Styles with - Stack Overflow
Videos
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.
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.
First of all:
- If the HTML is built or generated independent of the overall site design (e.g. shared template code), then add reasonably-named classes and IDs, linked exclusively to external stylesheet(s). Use sufficient elements to allow for arbitrary CSS manipulation. For example, see the CSS Zen Garden. This applies to ALL CMSes, programs, scripts, and other dynamically-generated site content. The HTML output must contain absolutely no styling or layout of any sort at all. No exceptions.
Assuming you're dealing with static content, then:
If there's any way you can reuse the style, make it a class and link to a stylesheet.
If there's no way would ever reuse the style (it's a one-off thing that doesn't make sense anywhere else) then use a
<style>block that references the element's #id.If the CSS attribute only makes sense in the context of the surrounding HTML (e.g. some usages of
clear:) then I inline the style into the element.
A lot of people call this heresy, just like a lot of people denounce any use of goto in modern programming languages.
However, rather than subscribing to stylistic dogma, my view is you should choose the method based on your circumstances that decreases your overall workload the most. Stylesheets add a level of indirection that makes site-level changes easy and helps build consistency. But if you have several dozen classes on each page that are only used in one place, then you're actually increasing your workload, not decreasing it.
In other words, don't do something dumb and confusing just because people tell you it's the right way to do it.
There is a simple reason. The point of CSS is to separate the content (HTML) from the presentation (CSS). It's all about accessibility and code reuse.
You're missing closing quote " after font-size:24px;
<p style="font-weight:bold;font-size:24px;"><a href="<?php echo ROOT_PATH; ?>scp/"><?php echo __('sign in here'); ?>Sign in here</a></p>
- You need to close the quotes
". - You need to have a text inside
<a>some text here</a>the tag.
I've always been under the impression that you should avoid putting styling in your HTML unless you have no choice, but I'm far from a professional, so I'm curious if there are times when it's actually advantageous.
Having to change 100 lines of code when you want to make the site look different. That may not apply in your example, but if you're using inline css for things like
<div style ="font-size:larger; text-align:center; font-weight:bold">
on each page to denote a page header, it would be a lot easier to maintain as
<div class="pageheader">
if the pageheader is defined in a single stylesheet so that if you want to change how a page header looks across the entire site, you change the css in one place.
However, I'll be a heretic and say that in your example, I see no problem. You're targeting the behavior of a single image, which probably has to look right on a single page, so putting the actual css in a stylesheet would probably be overkill.
The advantage for having a different css file are
- Easy to maintain your html page
- Change to the Look and feel will be easy and you can have support for many themes on your pages.
- Your css file will be cached on the browser side. So you will contribute a little on internet traffic by not loading some kbs of data every time a the page is refreshed or user navigates your site.
I am relatively new to CSS. I understand the rationale for not needlessly cluttering your HTML with constant, repeated styles. Like, if I have a bazillion elements that need a set of shared values, okay, great, I'll make a class. Or, if I have an element that needs a bazillion styles, I'll make a class.
The part that frustrates me is when I have, for example, some random <p> tag that just needs a different color. Should I really be writing some crap like this:
.someclass .anotherclass > p { color: salmon; }
in order to target that ultra-specific <p> tag, or should I just chuck that color inline, i.e. <p style="color: salmon;"> and forget about it? Because the latter is definitely more convenient.