CSS Advanced Grouping Selectors
In style sheets there are often elements with the same style.
Buy Our Best Recommended Guide On CSS: The Missing Manual
| h1 { color:green; } h2 { color:green; } p { color:green; } |
To minimize the code, you can group selectors.
Separate each selector with a comma.
In the example below we have grouped the selectors
from the code above:
Example
|
CSS Advanced Nesting Selectors
It is possible to apply a style for a selector within a selector.
In the example below, one style is specified for all p elements, and a separate style is specified for p elements nested within the "marked" class:
Example
|
The CSS advanced dimension properties allow you to control the height and width of an element.
All CSS Advanced Dimension Properties
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
| Property | Description | Values | CSS |
|---|---|---|---|
| height | Sets the height of an element | auto length % inherit |
1 |
| max-height | Sets the maximum height of an element | none length % inherit |
2 |
| max-width | Sets the maximum width of an element | none length % inherit |
2 |
| min-height | Sets the minimum height of an element | length % inherit |
2 |
| min-width | Sets the minimum width of an element | length % inherit |
2 |
| width | Sets the width of an element | auto length % inherit |
1 |
The display property specifies if/how an element is displayed, and the visibility property specifies if an element should be visible or hidden.
Buy Another Great Guide On Cascading Style Sheets CSS: The Definitive Guide
Hiding an Element - display:none or visibility:hidden
Hiding an element can be done by setting the display property to "none" or the visibility property to "hidden". However, notice that these two methods produce different results:
visibility:hidden hides an element, but it will still take up the same space as before. The element will be hidden, but still affect the layout.
Example
|
display:none hides an element, and it will not take up any space. The element will be hidden, and the page will be displayed as the element is not there:
Example
|
CSS Advanced Display - Block and Inline Elements
A block element is an element that takes up the full width available, and has a line break before and after it.
Examples of block elements:
<h1>
<p>
<div>
An inline element only takes up as much width as necessary, and does not force line breaks.
Examples of inline elements:
<span>
<a>
Changing How an Element is Displayed
Changing an inline element to a block element, or vice versa, can be useful for making the page look a specific way, and still follow web standards.
The following example displays list items as inline elements:
Example
|
The following example displays span elements as block elements:
Example
|
Note: Changing the display type of an element changes only how the element is displayed, NOT what kind of element it is. For example: An inline element set to display:block is not allowed to have a block element nested inside of it.
CSS Advanced Positioning
The CSS positioning properties allow you to position an element. It can also place an element behind another, and specify what should happen when an element's content is too big.
Elements can be positioned using the top, bottom, left, and right properties. However, these properties will not work unless the position property is set first. They also work differently depending on the positioning method.
There are four different positioning methods.
You Can Also Choose To Buy Elesson: Mastering CSS & HTML Training Tutorial
CSS Advanced Static Positioning
HTML elements are positioned static by default. A static positioned element is always positioned according to the normal flow of the page.
Static positioned elements are not affected by the top, bottom, left, and right properties.
CSS Advanced Fixed Positioning
An element with fixed position is positioned relative to the browser window.
It will not move even if the window is scrolled:
Example
|
Note: IE7 and IE8 support the fixed value only if a !DOCTYPE is specified.
Fixed positioned elements are removed from the normal flow. The document and other elements behave like the fixed positioned element does not exist.
Fixed positioned elements can overlap other elements.
CSS Advanced Relative Positioning
A relative positioned element is positioned relative to its normal position.
Example
|
The content of relatively positioned elements can be moved and overlap other elements, but the reserved space for the element is still preserved in the normal flow.
Example
|
Relatively positioned elements are often used as container blocks for absolutely positioned elements.
CSS Advanced Absolute Positioning
An absolute position element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block is <html>:
Example
|
Absolutely positioned elements are removed from the normal flow. The document and other elements behave like the absolutely positioned element does not exist.
Absolutely positioned elements can overlap other elements.
CSS Advanced Overlapping Elements
When elements are positioned outside the normal flow, they can overlap other elements.
The z-index property specifies the stack order of an element (which element should be placed in front of, or behind, the others).
An element can have a positive or negative stack order:
Example
|
An element with greater stack order is always in front of an element with a lower stack order.
Note: If two positioned elements overlap, without a z-index specified, the element positioned last in the HTML code will be shown on top.
Create a professional website with Yola Silver for less than $10 a month - Easy to use and with unlimited website templates!All CSS Advanced Positioning Properties
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
| Property | Description | Values | CSS |
|---|---|---|---|
| bottom | Sets the bottom margin edge for a positioned box | auto length % inherit |
2 |
| clip | Clips an absolutely positioned element | shape auto inherit |
2 |
| cursor | Specifies the type of cursor to be displayed | url auto crosshair default pointer move e-resize ne-resize nw-resize n-resize se-resize sw-resize s-resize w-resize text wait help |
2 |
| left | Sets the left margin edge for a positioned box | auto length % inherit |
2 |
| overflow |
Specifies what happens if content overflows an element's box | auto hidden scroll visible inherit |
2 |
| position | Specifies the type of positioning for an element | absolute fixed relative static inherit |
2 |
| right | Sets the right margin edge for a positioned box | auto length % inherit |
2 |
| top | Sets the top margin edge for a positioned box | auto length % inherit |
2 |
| z-index | Sets the stack order of an element | number auto inherit |
2 |
What is CSS Float?
With CSS float, an element can be pushed to the left or right, allowing other elements to wrap around it.
Float is very often used for images, but it is also useful when working with layouts.
How Elements Float
Elements are floated horizontally, this means that an element can only be floated left or right, not up or down.
A floated element will move as far to the left or right as it can. Usually this means all the way to the left or right of the containing element.
The elements after the floating element will flow around it.
The elements before the floating element will not be affected.
If an image is floated to the right, a following text flows around it, to the left:
Example
|
Floating Elements Next to Each Other
If you place several floating elements after each other, they will float next to each other if there is room.
Here we have made an image gallery using the float property:
Example
|
Turning off Float - Using Clear
Elements after the floating element will flow around it. To avoid this, use the clear property.
The clear property specifies which sides of an element other floating elements are not allowed.
Add a text line into the image gallery, using the clear property:
Example
|
All CSS Advanced Float Properties
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
| Property | Description | Values | CSS |
|---|---|---|---|
| clear | Specifies which sides of an element where other floating elements are not allowed | left right both none inherit |
1 |
| float | Specifies whether or not a box should float | left right none inherit |
1 |
Aligning Block Elements
A block element is an element that takes up the full width available, and has a line break before and after it.
Examples of block elements:
<h1>
<p>
<div>
In this chapter we will show you how to horizontally align block elements for layout purposes.
Center Aligning Using the margin Property
Block elements can be aligned by setting the left and right margins to "auto".
Note: Using margin:auto will not work in IE8 and earlier, unless a !DOCTYPE is declared.
Setting the left and right margins to auto specifies that they should split the available margin equally. The result is a centered element:
Example
|
Tip: Aligning has no effect if the width is 100%.
Note: In IE5 there is a margin handling bug for block elements. To make the example above work in IE5, add some extra code.
Left and Right Aligning Using the position Property
One method of aligning elements is to use absolute positioning:
Example
|
Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.
Crossbrowser Compatibility Issues
When aligning elements like this, it is always a good idea to predefine margin and padding for the <body> element. This is to avoid visual differences in different browsers.
There is a problem with IE8 and earlier, when using the position property. If a container element (in our case <div class="container">) has a specified width, and the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. Always set the !DOCTYPE declaration when using the position property:
Example
|
Left and Right Aligning Using the float Property
One method of aligning elements is to use the float property:
Example
|
Crossbrowser Compatibility Issues
When aligning elements like this, it is always a good idea to predefine margin and padding for the <body> element. This is to avoid visual differences in different browsers.
There is a problem with IE8 and earlier when using the float property. If the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. Always set the !DOCTYPE declaration when using the float property:
Example
|
CSS pseudo-classes are used to add special effects to some selectors.
Buy below a very good recommended book on how to Build Your Own Web Site The Right Way Using HTML & CSS:
CSS Advanced Syntax
The syntax of pseudo-classes:
| selector:pseudo-class {property:value;} |
CSS classes can also be used with pseudo-classes:
| selector.class:pseudo-class {property:value;} |
CSS Advanced Anchor Pseudo-classes
Links can be displayed in different ways in a CSS-supporting browser:
Example
|
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective!!
Note: a:active MUST come after a:hover in the CSS definition in order to be effective!!
Note: Pseudo-class names are not case-sensitive.
Pseudo-classes and CSS Classes
Pseudo-classes can be combined with CSS classes:
| a.red:visited {color:#FF0000;} <a class="red" href="css_syntax.asp">CSS Syntax</a> |
If the link in the example above has been visited, it will be displayed in red.
CSS Advanced - The :first-child Pseudo-class
The :first-child pseudo-class matches a specified element that is the first child of another element.
Note: For :first-child to work in IE8 and earlier, a <!DOCTYPE> must be declared.
Match the first <p> element
In the following example, the selector matches any <p> element that is the first child of any element:
Example
|
Match the first <i> element in all <p> elements
In the following example, the selector matches the first <i> element in all <p> elements:
Example
|
Match all <i> elements in all first child <p> elements
In the following example, the selector matches all <i> elements in <p> elements that are the first child of another element:
Example
|
CSS - The :lang Pseudo-class
The :lang pseudo-class allows you to define special rules for different languages.
Note: IE8 supports the :lang pseudo-class only if a <!DOCTYPE> is specified.
In the example below, the :lang class defines the quotation marks for q elements with lang="no":
Example
|
All CSS Advanced Pseudo Classes/Elements
| Selector | Example | Example description |
|---|---|---|
| :link | a:link | Selects all unvisited links |
| :visited | a:visited | Selects all visited links |
| :active | a:active | Selects the active link |
| :hover | a:hover | Selects links on mouse over |
| :focus | input:focus | Selects the input element which has focus |
| :first-letter | p:first-letter | Selects the first letter of every <p> element |
| :first-line | p:first-line | Selects the first line of every <p> element |
| :first-child | p:first-child | Selects every <p> elements that is the first child of its parent |
| :before | p:before | Insert content before every <p> element |
| :after | p:after | Insert content after every <p> element |
| :lang(language) | p:lang(it) | Selects every <p> element with a lang attribute value starting with "it" |
CSS advanced pseudo-elements are used to add special effects to some selectors.
Syntax
The syntax of pseudo-elements:
| selector:pseudo-element {property:value;} |
CSS classes can also be used with pseudo-elements:
| selector.class:pseudo-element {property:value;} |
The :first-line Pseudo-element
The "first-line" pseudo-element is used to add a special style to the first line of a text.
In the following example the browser formats the first line of text in a p element according to the style in the "first-line" pseudo-element (where the browser breaks the line, depends on the size of the browser window):
Example
|
Note: The "first-line" pseudo-element can only be used with block-level elements.
Note: The following properties apply to the "first-line" pseudo-element:
font properties
color properties
background properties
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
line-height
clear
The :first-letter Pseudo-element
The "first-letter" pseudo-element is used to add a special style to the first letter of a text:
Example
|
Note: The "first-letter" pseudo-element can only be used with block-level elements.
Note: The following properties apply to the "first-letter" pseudo- element:
font properties
color properties
background properties
margin properties
padding properties
border properties
text-decoration
vertical-align (only if "float" is "none")
text-transform
line-height
float
clear
Pseudo-elements and CSS Advanced Classes
Pseudo-elements can be combined with CSS classes:
| p.article:first-letter {color:#ff0000;} <p class="article">A paragraph in an article</p> |
The example above will display the first letter of all paragraphs with class="article", in red.
Multiple Pseudo-elements
Several pseudo-elements can also be combined.
In the following example, the first letter of a paragraph will be red, in an xx-large font size. The rest of the first line will be blue, and in small-caps. The rest of the paragraph will be the default font size and color:
Example
|
CSS Advanced - The :before Pseudo-element
The ":before" pseudo-element can be used to insert some content before the content of an element.
The following example inserts an image before each <h1> element:
Example
|
CSS Advanced - The :after Pseudo-element
The ":after" pseudo-element can be used to insert some content after the content of an element.
The following example inserts an image after each <h1> element:
Example
|
All CSS Advanced Pseudo Classes/Elements
| Selector | Example | Example description |
|---|---|---|
| :link | a:link | Selects all unvisited links |
| :visited | a:visited | Selects all visited links |
| :active | a:active | Selects the active link |
| :hover | a:hover | Selects links on mouse over |
| :focus | input:focus | Selects the input element which has focus |
| :first-letter | p:first-letter | Selects the first letter of every <p> element |
| :first-line | p:first-line | Selects the first line of every <p> element |
| :first-child | p:first-child | Selects every <p> elements that is the first child of its parent |
| :before | p:before | Insert content before every <p> element |
| :after | p:after | Insert content after every <p> element |
| :lang(language) | p:lang(it) | Selects every <p> element with a lang attribute value starting with "it" |
Navigation Bars
Having easy-to-use navigation is important for any web site.
With CSS you can transform boring HTML menus into good-looking navigation bars.
Navigation Bar = List of Links
A navigation bar needs standard HTML as a base.
In our examples we will build the navigation bar from a standard HTML list.
A navigation bar is basically a list of links, so using the <ul> and <li> elements makes perfect sense:
Example
|
Now let's remove the bullets and the margins and padding from the list:
Example
|
Example explained:
1. list-style-type:none - Removes the bullets. A navigation bar does not need list markers
2. Setting margins and padding to 0 to remove browser default settings
The code in the example above is the standard code used in both vertical, and horizontal navigation bars.
Vertical Navigation Bar
To build a vertical navigation bar we only need to style the <a> elements, in addition to the code above:
Example
|
Example explained:
1. display:block - Displaying the links as block elements makes the whole link area clickable (not just the text), and it allows us to specify the width
2. width:60px - Block elements take up the full width available by default. We want to specify a 60 px width
Note: Always specify the width for <a> elements in a vertical navigation bar. If you omit the width, IE6 can produce unexpected results.
Horizontal Navigation Bar
There are two ways to create a horizontal navigation bar. Using inline or floating list items.
Both methods work fine, but if you want the links to be the same size, you have to use the floating method.
Inline List Items
One way to build a horizontal navigation bar is to specify the <li> elements as inline, in addition to the "standard" code above:
Example
|
Example explained:
display:inline; - By default, <li> elements are block elements. Here, we remove the line breaks before and after each list item, to display them on one line
Floating List Items
In the example above the links have different widths.
For all the links to have an equal width, float the <li> elements and specify a width for the <a> elements:
Example
|
Example explained:
float:left - use float to get block elements to slide next to each other
display:block - Displaying the links as block elements makes the whole link area clickable (not just the text), and it allows us to specify the width
width:60px - Since block elements take up the full width available, they cannot float next to each other. We specify the width of the links to 60px
Image Gallery
The following image gallery is created with CSS advanced code:
Example
|
Creating transparent images with CSS is easy.
Note: This is not yet a CSS standard. However, it works in all modern browsers, and is a part of the W3C CSS 3 recommendation.
Example 1 - Creating a Transparent Image
First we will show you how to create a transparent image with CSS.
Look at the following source code:
| <img src="klematis.jpg" width="150" height="113" alt="klematis" style="opacity:0.4;filter:alpha(opacity=40)" /> |
Firefox uses the property opacity:x for transparency, while IE uses filter:alpha(opacity=x).
Tip: The CSS3 syntax for transparency is opacity:x.
In Firefox (opacity:x) x can be a value from 0.0 - 1.0. A lower value makes the element more transparent.
In IE (filter:alpha(opacity=x)) x can be a value from 0 - 100. A lower value makes the element more transparent.
Example 2 - Image Transparency - Mouseover Effect
The source code looks like this:
<img src="klematis.jpg" style="opacity:0.4;filter:alpha(opacity=40)"
onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"
onmouseout="this.style.opacity=0.4;this.filters.alpha.opacity=40" />
<img src="klematis2.jpg" style="opacity:0.4;filter:alpha(opacity=40)"
onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"
onmouseout="this.style.opacity=0.4;this.filters.alpha.opacity=40" />
We see that the first line of the source code is similar to the source code in Example 1. In addition, we have added an onmouseover attribute and an onmouseout attribute. The onmouseover attribute defines what will happen when the mouse pointer moves over the image. In this case we want the image to NOT be transparent when we move the mouse pointer over it.
The syntax for this in Firefox is: this.style.opacity=1 and the syntax in IE is:this.filters.alpha.opacity=100.
When the mouse pointer moves away from the image, we want the image to be transparent again. This is done in the onmouseout attribute.
Example 3 - Text in Transparent Box
| <html> <head> <style type="text/css"> div.background { width:500px; height:250px; background:url(klematis.jpg) repeat; border:2px solid black; } div.transbox { width:400px; height:180px; margin:30px 50px; background-color:#ffffff; border:1px solid black; /* for IE */ filter:alpha(opacity=60); /* CSS3 standard */ opacity:0.6; } div.transbox p { margin:30px 40px; font-weight:bold; color:#000000; } </style> </head> <body> <div class="background"> <div class="transbox"> <p>This is some text that is placed in the transparent box. This is some text that is placed in the transparent box. This is some text that is placed in the transparent box. This is some text that is placed in the transparent box. This is some text that is placed in the transparent box. </p> </div> </div> </body> </html> |
First, we create a div element (class="background") with a fixed height and width, a background image, and a border. Then we create a smaller div (class="transbox") inside the first div element. The "transbox" div have a fixed width, a background color, and a border - and it is transparent. Inside the transparent div, we add some text inside a p element.
Image Sprites
An image sprite is a collection of images put into a single image.
A web page with many images can take a long time to load and generates multiple server requests.
Using image sprites will reduce the number of server requests and save bandwidth.
Image Sprites - Simple Example
Instead of using three separate images, we use this single image ("img_navsprites.gif"):
![]()
With CSS, we can show just the part of the image we need.
In the following example the CSS specifies which part of the "img_navsprites.gif" image to show:
Example
|
Example explained:
<img class="home" src="img_trans.gif" /> - Only defines a small transparent image because the src attribute cannot be empty. The displayed image will be the background image we specify in CSS
width:46px;height:44px; - Defines the portion of the image we want to use
background:url(img_navsprites.gif) 0 0; - Defines the background image and its position (left 0px, top 0px)
This is the easiest way to use image sprites, now we want to expand it by using links and hover effects.
Image Sprites - Create a Navigation List
We want to use the sprite image ("img_navsprites.gif") to create a navigation list.
We will use an HTML list, because it can be a link and also supports a background image:
Example
|
Example explained:
#navlist{position:relative;} - position is set to relative to allow absolute positioning inside it
#navlist li{margin:0;padding:0;list-style:none;position:absolute;top:0;} - margin and padding is set to 0, list-style is removed, and all list items are absolute positioned
#navlist li, #navlist a{height:44px;display:block;} - the height of all the images are 44px
Now start to position and style for each specific part:
#home{left:0px;width:46px;} - Positioned all the way to the left, and the width of the image is 46px
#home{background:url(img_navsprites.gif) 0 0;} - Defines the background image and its position (left 0px, top 0px)
#prev{left:63px;width:43px;} - Positioned 63px to the right (#home width 46px + some extra space between items), and the width is 43px.
#prev{background:url('img_navsprites.gif') -47px 0;} - Defines the background image 47px to the right (#home width 46px + 1px line divider)
#next{left:129px;width:43px;}- Positioned 129px to the right (start of #prev is 63px + #prev width 43px + extra space), and the width is 43px.
#next{background:url('img_navsprites.gif') no-repeat -91px 0;} - Defines the background image 91px to the right (#home width 46px + 1px line divider + #prev width 43px + 1px line divider )
Image Sprites - Hover Effect
Now we want to add a hover effect to our navigation list.
We only add three lines of code to add the hover effect:
Example
|
Example explained:
Since the list item contains a link, we can use the :hover pseudo-class
#home a:hover{background: transparent url(img_navsprites_hover.gif) 0 -45px;} - For all three hover images we specify the same background position, only 45px further down
Media Types allow you to specify how documents will be presented in different media. The document can be displayed differently on the screen, on the paper, with an aural browser, etc.
Media Types
Some CSS advanced properties are only designed for a certain media. For example the "voice-family" property is designed for aural user agents. Some other properties can be used for different media types. For example, the "font-size" property can be used for both screen and print media, but perhaps with different values. A document usually needs a larger font-size on a screen than on paper, and sans-serif fonts are easier to read on the screen, while serif fonts are easier to read on paper.
The @media Rule
The @media rule allows different style rules for different media in the same style sheet.
The style in the example below tells the browser to display a 14 pixels Verdana font on the screen. But if the page is printed, it will be in a 10 pixels Times font. Notice that the font-weight is set to bold, both on screen and on paper:
| <html> <head> <style> @media screen { p.test {font-family:verdana,sans-serif;font-size:14px;} } @media print { p.test {font-family:times,serif;font-size:10px;} } @media screen,print { p.test {font-weight:bold;} } </style> </head> <body> .... </body> </html> |
See it yourself ! If you are using Mozilla/Firefox or IE5+ and print this page, you will see that the paragraph under "Media Types" will be displayed in another font, and have a smaller font size than the rest of the text.
Different Media Types
Note: The media type names are not case-sensitive.
| Media Type | Description |
|---|---|
| all | Used for all media type devices |
| aural | Used for speech and sound synthesizers |
| braille | Used for braille tactile feedback devices |
| embossed | Used for paged braille printers |
| handheld | Used for small or handheld devices |
| Used for printers | |
| projection | Used for projected presentations, like slides |
| screen | Used for computer screens |
| tty | Used for media using a fixed-pitch character grid, like teletypes and terminals |
| tv | Used for television-type devices |
Style HTML Elements With Specific Attributes
It is possible to style HTML elements that have specific attributes, not just class and id.
Note: IE7 and IE8 support attribute selectors only if a !DOCTYPE is specified. Attribute selection is NOT supported in IE6 and lower.
Attribute Selector
The example below styles all elements with a title attribute:
Example
|
Attribute and Value Selector
The example below styles all elements with title="Yocto":
Example
|
Attribute and Value Selector - Multiple Values
The example below styles all elements with a title attribute that contains a specified value. This works even if the attribute has space separated values:
Example
|
The example below styles all elements with a lang attribute that contains a specified value. This works even if the attribute has hyphen ( - ) separated values:
Example
|
Styling Forms
The attribute selectors are particularly useful for styling forms without class or ID:
Example
|
Here are some technologies you should try to avoid when using CSS.
Internet Explorer Behaviors
What is it? Internet Explorer 5 introduced behaviors. Behaviors are a way to add behaviors to HTML elements with the use of CSS styles.
Why avoid it? The behavior attribute is only supported by Internet Explorer.
What to use instead? Use JavaScript and the HTML DOM instead.
Example 1 - Mouseover Highlight
The following HTML file has a <style> element that defines a behavior for the <h1> element:
| <html> <head> <style type="text/css"> h1 { behavior:url(behave.htc); } </style> </head> <body> <h1>Mouse over me!!!</h1> </body> </html> |
The XML document "behave.htc" is shown below:
| Example (IE5+ Only)
The behavior file contains a JavaScript and event handlers for the elements.
|
Example 2 - Typewriter Simulation
The following HTML file has a <style> element that defines a behavior for elements with an id of "typing":
| <html> <head> <style type="text/css"> #typing { behavior:url(behave_typing.htc); font-family:"courier new"; } </style> </head> <body> <span id="typing" speed="100">IE5 introduced DHTML behaviors. Behaviors are a way to add DHTML functionality to HTML elements with the ease of CSS.<br /><br />How do behaviors work?<br /> By using XML we can link behaviors to any element in a web page and manipulate that element.</p> </span> </body> </html> |
The XML document "typing.htc" is shown below:
Example (IE5+ Only)
|


