03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

86 x CHAPTER 3 DESIGNING YOUR WEB PAGES<br />

Choos<strong>in</strong>g among External, Embedded, <strong>and</strong> Inl<strong>in</strong>e Style Sheets<br />

Because you have so many options to add style sheets to your site, what’s the best method to use? In<br />

general, you should give preference to external style sheets over embedded styles, which <strong>in</strong> turn are<br />

preferred over <strong>in</strong>l<strong>in</strong>e styles. External style sheets enable you to change the appearance of the entire<br />

site through a s<strong>in</strong>gle file. Make one change to your external style sheet file, <strong>and</strong> all pages that use<br />

this style sheet pick up the change automatically.<br />

However, it’s perfectly acceptable to use embedded <strong>and</strong> <strong>in</strong>l<strong>in</strong>e styles as well <strong>in</strong> certa<strong>in</strong> circumstances.<br />

If you want to change the look of a s<strong>in</strong>gle page, without affect<strong>in</strong>g other pages <strong>in</strong> your site,<br />

an embedded style sheet is your best choice. The same applies to <strong>in</strong>l<strong>in</strong>e styles: if you only want to<br />

change the behavior of a s<strong>in</strong>gle element <strong>in</strong> a s<strong>in</strong>gle page, <strong>and</strong> you’re pretty sure you’re not go<strong>in</strong>g to<br />

need the same declaration for other HTML elements, you could use an <strong>in</strong>l<strong>in</strong>e style.<br />

An important th<strong>in</strong>g to consider is the way that the various types of style sheets override each other.<br />

If you have multiple identical selectors with different property values, the one def<strong>in</strong>ed last takes precedence.<br />

For example, consider a rule def<strong>in</strong>ed <strong>in</strong> an external style sheet called Styles.css that sets<br />

the color of all tags to green:<br />

h1<br />

{<br />

color: Green;<br />

}<br />

Now imag<strong>in</strong>e you’re attach<strong>in</strong>g this style sheet <strong>in</strong> a page that also has an embedded rule for the same<br />

h1 but that sets a different color:<br />

<br />

<br />

h1<br />

{<br />

color: Blue;<br />

}<br />

<br />

With this code, the color of the actual tag <strong>in</strong> the page will be blue. This is because the embedded<br />

style sheet that sets the color to blue is def<strong>in</strong>ed later <strong>in</strong> the page, <strong>and</strong> thus overrides the sett<strong>in</strong>g <strong>in</strong><br />

the external file. If you turn the styles around like this,<br />

<br />

h1<br />

{<br />

color: Blue;<br />

}<br />

<br />

<br />

the head<strong>in</strong>g will be green, because the sett<strong>in</strong>g <strong>in</strong> the external style sheet now overrules that of the<br />

embedded style.<br />

The same pr<strong>in</strong>ciple applies to <strong>in</strong>l<strong>in</strong>e styles. Because they’re def<strong>in</strong>ed directly on the HTML elements,<br />

their sett<strong>in</strong>gs take precedence over embedded <strong>and</strong> external style sheets.<br />

It’s also good to know that CSS generally overrules attributes on HTML elements. For example, if<br />

you have a CSS rule that sets the width <strong>and</strong> height of an image, the height <strong>and</strong> width attributes

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!