stefan and zogo small Help Stefan Campaign
If you can spare $1 then help support this site and change someone's life forever? Learn how and why on the Help Stefan campaign page. Or donate $10 and get my guide to spices book as a gift for your donation!


Celtnet Information:

Introduction to CSS: Part 4

CeltNet CSS Logo

In this section you'll learn more about how CSS styles actually apply to HTML elements and how you can use this information to group related selectors and actively reduce the size of your CSS include file. In effect what you'll learn about here is how CSS elements interact with HTML tags and what happens when you nest tags with associated CSS formatting within one another.



Applying Multiple Classes to Elements

In actuality you're not limited to a single style for any HTML element. You can combine classes together. This is done within the class property of the HTML tag element using spaces, as show below:

<html>
<head>
<style type="text/css">
.marker {color:red;}
.yellowbg {background-color:#ff0;}
</style>

</head>

<body>
<p class="marker yellowbg">Both styles will apply to the <p> tag</p>
</body>

</html>

The effect of using this newly-defined <p> tag is as follows:

Both styles will apply to the <p> tag

HTML selectors are employed to redefine the genral look for an HTML tag in your entire website.

Applying Styles by id Property

As you have seen in part 3 HTML elements can have id properties as well as class names. This is an important feature to note, as properties inherited from an id will overwrite properties an element gets from its class (or from its HTML element type). This distinction is demonstrated by the example below:

The following example should make things clearer:

<html>
<head>
<style type="text/css">
#banner {color:green;}
.special {color:red; background-color:#ff9;}
div {color:blue; font-weight: bold;}
</style>

</head>

<body>
<div id="banner" class="special">This is text will come out green as the ID upercedes both the class and the element classes.</div>
</body>

</html>

The effect of combining these classes is as follows:

How CSS styles apply to nested elements

We've already seen how CSS styles can be combined, and how those CSS styles function depending on the elemet or class that they relate to. The following example gives a more complex example of using CSS with nested elements (ie elements defined within elements in HTML):

<html>
<head>
<style type="text/css">
    div {color:green;}
    .special {color:red; background-color:#ff9;}
    #banner {text-decoration:underline; font-weight:bold; color:blue;}
</style>

</head>

<body>
<div>
   This text is only inside the div...
   <p class="special">
      This text is inside the div and inside the paragraph...
      <span id="baner">
         This text is inside the div, paragraph with classname and span with id.
      <span>
   <p>
</div>

</body>
</html>

The result of these nested elements is as follows:

This text is only inside the div...

This text is inside the div and inside the paragraph...
This text is inside the div, paragraph with classname and span with id.

Admittedly, this is a fairly complex example, but what's going on here is that the first protion of text is green because int's inside the div.

The second block of text is coloured red as it's inside a class and this overrides the previous colour property on the HTML tag (it also gets the coloured background property defined within the class).

the third block of text lies inside the two previous elements but as it's id property overrules both the class property and the HTML tag property it's style overrides any conflicting style properties inherited from the other elements. (It inherits the background colour from the paragraph <p> class as the id style doesn't specify a background.

Grouping Selectors

We've already seen how various selectors behave when they're nested (and which selectors have dominance when there's a choice between them). Now we'll look at how selectors can be grouped and nested within your CSS file to reduce the size of the file (and the speed with which it uploads). Consider the example below:

.headlines{
   font-family:mabinogi; color:black; background:CCCCCC; font-size:14pt;
}

.sublines {
   font-family:mabinogi; color:black; background:CCCCCC; font-size:12pt;
}

.infotext {
   font-family:mabinogi; color:black; background:CCCCCC; font-size:10pt;
}

As you can see, all three of the selectors above are virtually identical. Indeed, the only vary in their font-size property. Which effectively means that some grouping-together can be done to reduce the verbosity of the file, as shown below:

.headlines, .sublines, .infotext {
    font-family:mabinogi; color:black; background:cccccc;
}

.headlines {font-size:14pt;}
.sublines {font-size:12pt;}
.infotext {font-size: 10pt;}

As you can see, the common elements of the selectors can be defined all at once and the individual attributes that distinguish the selectors from one another can be added subsequently. This is a very useful feature of CSS and you can use it to significantly optimize the size of your CSS files.

Context-dependent Selectors

Just as it's possible to have different behaviours dependent on the nexting of the selectors it's also possible to design selectors that only function in certain contexts. For example, it's possible to design a style for the embolden <b> tag that only functions if the text is not only emboldened, but also italicized. This is simplicity itself to implement, as shown below:

i b {font-size:16px; color:red; }

The definition above works only if the <b> tag is enclosed in an <i> tag, like this: <i><b>. Notice that for these combined selectors the selectors themselves are separated by spaces. Interestingly, as the combined context-dependent selectors effectively act like a single selector it's possible to group them with other selectors, as in the example below:

i b, .headline, b.footlines {font-size:16px; color:red;}

The example above works for all <b> tags enclosed in <i> tags, for all .headlines classes and for all <b> tags of class .footlines.

That concludes all the essential preliminaries, and it's now possible to look at some real-world examples of how CSS is actually employed to help build a website.

On to the next part...

Search: css html internet


In association with Amazon.co.uk
  

1.
Search : CSS: The Missing Manual
List Price: £24.99
Amazon.co.uk's Price: Â£17.49
You Save: £7.50 (30%)
Prices subject to change.
CSS: The Missing Manual
by: David McFarland
August 24, 2006
 
2.
Search : Dreamweaver CS3: The Missing Manual
List Price: £31.99
Amazon.co.uk's Price: Â£19.51
You Save: £12.48 (39%)
Prices subject to change.
Dreamweaver CS3: The Missing Manual
by: David McFarland
June 22, 2007
 
3.
Search : HTML for the World Wide Web: with XHTML and CSS: With XHTML and CSS (Visual QuickStart Guides)
List Price: £21.99
Amazon.co.uk's Price: Â£10.99
You Save: £11.00 (50%)
Prices subject to change.
HTML for the World Wide Web: with XHTML and CSS: With XHTML and CSS (Visual QuickStart Guides)
by: Elizabeth Castro
August 31, 2006
 
4.
Search : JavaScript: The Definitive Guide
List Price: £35.50
Amazon.co.uk's Price: Â£23.08
You Save: £12.42 (35%)
Prices subject to change.
JavaScript: The Definitive Guide
by: David Flanagan
August 17, 2006
 
5.
Search : HTML, XHTML, and CSS All-in-one Desk Reference for Dummies (For Dummies)
List Price: £22.99
Amazon.co.uk's Price: Â£11.49
You Save: £11.50 (50%)
Prices subject to change.
HTML, XHTML, and CSS All-in-one Desk Reference for Dummies (For Dummies)
by: Andy Harris, Chris McCulloh
May 09, 2008
 
6.
Search : Bulletproof Web Design: Improving Flexibility and Protecting Against Worst-Case Scenarios with XHTML and CSS
List Price: £28.99
Amazon.co.uk's Price: Â£14.49
You Save: £14.50 (50%)
Prices subject to change.
Bulletproof Web Design: Improving Flexibility and Protecting Against Worst-Case Scenarios with XHTML and CSS
by: Dan Cederholm
August 23, 2007
 
7.
Search : The CSS Anthology: 101 Essential Tips, Tricks and Hacks
List Price: £24.99
Amazon.co.uk's Price: Â£23.74
You Save: £1.25 ( 5%)
Prices subject to change.
The CSS Anthology: 101 Essential Tips, Tricks and Hacks
by: Rachel Andrew
August 01, 2007
 
8.
Search : HTML, XHTML and CSS for Dummies (For Dummies)
List Price: £19.99
Amazon.co.uk's Price: Â£9.99
You Save: £10.00 (50%)
Prices subject to change.
HTML, XHTML and CSS for Dummies (For Dummies)
by: Ed Tittel, Jeff Noble
June 06, 2008
 
9.
Search : Creating Web Sites: The Missing Manual
List Price: £20.95
Amazon.co.uk's Price: Â£14.67
You Save: £6.28 (30%)
Prices subject to change.
Creating Web Sites: The Missing Manual
by: Matthew MacDonald
October 26, 2005
 
10.
Search : Head First HTML with CSS & XHTML (Head First)
List Price: £28.50
Amazon.co.uk's Price: Â£18.53
You Save: £9.97 (35%)
Prices subject to change.
Head First HTML with CSS & XHTML (Head First)
by: Eric Freeman, Elisabeth Freeman
December 08, 2005
 
page 1 of  13
 1  2  3  4  5  6  7  8  9  10  11 
 



Domain Name Registration




Quick Links:
Make Money from the Web
Essential Web Tools
Generate your Website
Paid Surveys
Affiliate Programmes
Successful Affiliates
Maximizing Traffic
News Feeds
Using Minisites
Form-mail script
Sitemap
Links
Celtnet Information Home


Site Search





[Valid RSS] Register Domains
Copyright © 2005–2007, Dyfed Lloyd Evans