How CSS styles apply to HTML elements - CSS3 Examples

How CSS styles apply to HTML elements

Written by jon on 4:22 AM

There are a few basic rules that govern which (separately-defined) styles apply to what elements. Fortunately, these are relatively simple and logical, once explained. I'll go through them one at a time.

1. Applying to HTML elements themselves

In CSS, if you want some styles to apply to HTML elements of a certain type, you write the HTML element type, followed by style definitions all contained between curly braces and separated with semicolons.

p {
font-weight:bold;
line-height:1.3em;
}

<p>The styles above will apply to me because I am a paragraph.</p>
<div>But they won't apply to me.</div>
The code above will apply bold text and slightly increased line-height to the contents of any element of type <p> (i.e. paragraphs).
2. Applying to elements with class names

You can give any HTML element one or more class names. In CSS, these are defined with a preceding period (full-stop).

Styles derived from classnames (normally) override any of the same styles defined for the HTML element type (more on this later).

<style type="text/css">
.custom {
color:red;
}
</style>

<p class="custom">Applies to me.</p>
<p>But does not apply to me.</p>

This is extremely useful, because you can definte style characteristics under a single class, and then apply those characteristics to multiple elements by giving them that class (even if they're different types of elements).

3. Elements can have multiple classes
HTML elements can have more than one class. Class names must be separated in the class property with spaces.
<style type="text/css">
.makered {
color:red;
}
.yellowbg {
background-color:#ff0;
}
</style>

<p class="makered yellowbg">Both styles will apply to me.</p>
4. Applying styles by id property

HTML elements can have id properties as well as class names.

While there can be multiple elements that share any class name, there should only be one element with a particular id property. This isn't important in CSS, but it is important in DHTML, so good practice to follow.

Styles are associated with particular ids in CSS by prefixing the name with a hash/pound sign (#).

Another thing to remember is that properties inherited from an id will overwrite properties an element gets from its class (or from its HTML element type).

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

<div id="banner" class="special">
This text would come out green, because the ID trumps the class and the element type.
</div>

Note that, in the example above, the font-weight:bold; and background-color:#ff9; styles still apply to the text, because it's only the color: property that is overwritten by the superior style definition.
5. CSS styles apply to elements within elements, unless overwritten

Taking the above example...

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

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

The first bit of text gets its green colour because it's inside the div. The second bit of text becomes red because it's inside something with class="special", which overrides the previous color property. It also gets the additional background-color. The third bit of text is inside all three elements, but the properties it gets from its id overrule any other conflicting style properties it gets from the other elements. Note that it has also inherited the background-color from the special paragraph, because the id's styles don't specify a background.

Related Posts by Categories



Widget by John | Interviewghost
  1. 0 comments: Responses to “ How CSS styles apply to HTML elements ”

RECENT COMMENTS

SUBSCRIBE TO CSS3 EXAMPLES

 Subscribe to css3 Examples via RSS
Or, subscribe via email:
Find CSS3 Examples Entries :