In style sheets there are often elements with the same style.
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:
h1,h2,p
{
color:green;
}
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, one style is specified for all elements with class="marked", and a third style is specified only for p elements within elements with class="marked":
p
{
color:blue;
text-align:center;
}
.marked
{
background-color:red;
}
.marked p
{
color:white;
}
The CSS dimension properties allow you to control the height and width of an element.
This example demonstrates how to set the height of different elements.
Set the height of an image using percent
This example demonstrates how to set the height of an element using a percent value.
Set the width of an element using a pixel value
This example demonstrates how to set the width of an element using a pixel value.
Set the maximum height of an element
This example demonstrates how to set the maximum height of an element.
Set the maximum width of an element using percent
This example demonstrates how to set the maximum width of an element using a percent value.
Set the minimum height of an element
This example demonstrates how to set the minimum height of an element.
Set the minimum width of an element using a pixel value
This example demonstrates how to set the minimum width of an element using a pixel value.
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
The display property specifies if/how an element is displayed, and the visibility property specifies if an element should be visible or hidden.
Box 1
Box 2
Box 3
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.
h1.hidden {visibility:hidden;}
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 if the element is not there:
h1.hidden {display:none;}
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:
An inline element only takes up as much width as necessary, and does not force line breaks.
Examples of inline elements:
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:
li {display:inline;}
The following example displays span elements as block elements:
span {display:block;}
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.
How to display an element as an inline element.
This example demonstrates how to display an element as an inline element.
How to display an element as a block element
This example demonstrates how to display an element as a block element.
How to make a table element collapse
This example demonstrates how to make a table element collapse.
Positioning can be tricky sometimes!
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.
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.
An element with fixed position is positioned relative to the browser window.
It will not move even if the window is scrolled:
p.pos_fixed
{
position:fixed;
top:30px;
right:5px;
}
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.
A relative positioned element is positioned relative to its normal position.
h2.pos_left
{
position:relative;
left:-20px;
}
h2.pos_right
{
position:relative;
left:20px;
}
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.
h2.pos_top
{
position:relative;
top:-50px;
}
Relatively positioned elements are often used as container blocks for absolutely positioned elements.
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>:
h2
{
position:absolute;
left:100px;
top:150px;
}
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.
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:
img
{
position:absolute;
left:0px;
top:0px;
z-index:-1;
}
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.
This example demonstrates how to set the shape of an element. The element is clipped into this shape, and displayed.
How to show overflow in an element using scroll
This example demonstrates how to set the overflow property to create a scroll bar when an element's content is too big to fit in a specified area.
How to set the browser to automatically handle overflow
This example demonstrates how to set the browser to automatically handle overflow.
This example demonstrates how to change the cursor.
The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).