Showing posts with label CSS Advanced. Show all posts
Showing posts with label CSS Advanced. Show all posts


CSS Grouping and Nesting Selectors

Grouping Selectors

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:

Example

<html>
<head>
<style type="text/css">
h1,h2,p
{
color:green;
}
</style>
</head>

<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:

Hello World!

Smaller heading!

This is a paragraph.

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, 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":

Example

<html>
<head>
<style type="text/css">
p
{
color:blue;
text-align:center;
}
.marked
{
background-color:red;
}
.marked p
{
color:white;
}
</style>
</head>

<body>
<p>This is a blue, center-aligned paragraph.</p>
<div class="marked">
<p>This p element should not be blue.</p>
</div>
<p>p elements inside a "marked" classed element keeps the alignment style, but has a different text color.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

The CSS dimension properties allow you to control the height and width of an element.


Try it Yourself - Examples

Set the height of elements
This example demonstrates how to set the height of different elements.
Example
<html>
<head>
<style type="text/css">
img.normal
{
height:auto;
}
img.big
{
height:120px;
}
p.ex
{
height:100px;
width:100px;
}
</style>
</head>


<body>
<img class="normal" src="your_image.gif" width="95" height="84" /><br />
<img class="big" src="your_image.gif" width="95" height="84" />
<p class="ex">The height and width of this paragraph is 100px.</p>
<p>This is some text in a paragraph. This is some text in a paragraph.
This is some text in a paragraph. This is some text in a paragraph.
This is some text in a paragraph. This is some text in a paragraph.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:

The height and width of this paragraph is 100px.
This is some text in a paragraph. This is some text in a paragraph. This is some text in a paragraph. This is some text in a paragraph. This is some text in a paragraph. This is some text in a paragraph.

Set the height of an image using percentThis example demonstrates how to set the height of an element using a percent value.
Example

<html>
<head>
<style type="text/css">
img.normal {height:auto;}
img.big {height:50%;}
img.small {height:10%;}
</style>
</head>


<body>
<img class="normal" src="your_image.gif" width="95" height="84" /><br />
<img class="big" src="your_image.gif" width="95" height="84" /><br />
<img class="small" src="your_image.gif" width="95" height="84" />
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

Result:



Set the width of an element using a pixel valueThis example demonstrates how to set the width of an element using a pixel value.
Example

<html>
<head>
<style type="text/css">
img {width:200px;}
</style>
</head>
<body>


<img src="your_image.gif" width="95" height="84" />


</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:


Set the maximum height of an elementThis example demonstrates how to set the maximum height of an element.
Example

<html>
<head>
<style type="text/css">
p
{
max-height:50px;
background-color:yellow;
}
</style>
</head>


<body>
<p>The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The 
maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px. The maximum height of this paragraph is set to 50px.

Set the maximum width of an element using percentThis example demonstrates how to set the maximum width of an element using a percent value.
Example

<html>
<head>
<style type="text/css">
p
{
max-width:20%;
background-color:yellow;
}
</style>
</head>
<body>


<p>This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.</p>


</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
This is some text. 
This is some text. 
This is some text. 
This is some text. 
This is some text. 
This is some text. 
This is some text. 
This is some text. 
This is some text.


Set the minimum height of an elementThis example demonstrates how to set the minimum height of an element.
Example

<html>
<head>
<style type="text/css">
p
{
min-height:100px;
background-color:yellow;
}
</style>
</head>


<body>
<p>The minimum height of this paragraph is set to 100px.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
The minimum height of this paragraph is set to 100px.                                                                          
                                                                                                                                                              
                                                                                                                                                              
                                                                                                                                                              


Set the minimum width of an element using a pixel valueThis example demonstrates how to set the minimum width of an element using a pixel value.
Example

<html>
<head>
<style type="text/css">
p
{
min-width:150px;
background-color:yellow;
}
</style>
</head>


<body>
<p>The minimum width of this paragraph is set to 150px.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
The minimum width of this paragraph is set to 150px.                                                                            



All CSS Dimension Properties

The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
PropertyDescriptionValuesCSS
heightSets the height of an elementauto
length
%
inherit
1
max-heightSets the maximum height of an elementnone
length
%
inherit
2
max-widthSets the maximum width of an elementnone
length
%
inherit
2
min-heightSets the minimum height of an elementlength
%
inherit
2
min-widthSets the minimum width of an elementlength
%
inherit
2
widthSets the width of an elementauto
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.

Box 1

Box 2
 
Box 3
 

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

<html>
<head>
<style type="text/css">
h1.hidden {visibility:hidden;}
</style>
</head>

<body>
<h1>This is a visible heading</h1>
<h1 class="hidden">This is a hidden heading</h1>
<p>Notice that the hidden heading still takes up space.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
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

<html>
<head>
<style type="text/css">
h1.hidden {display:none;}
</style>
</head>

<body>
<h1>This is a visible heading</h1>
<h1 class="hidden">This is a hidden heading</h1>
<p>Notice that the hidden heading does not take up space.</p>
</body>

</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.


CSS 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

<html>
<head>
<style type="text/css">
li{display:inline;}
</style>
</head>
<body>

<p>Display this link list as a horizontal menu:</p>

<ul>
<li><a href="http://amaderitlearnhtml.blogspot.com/2012/01/html-tutorial.html" target="_blank">HTML</a></li>
<li><a href="your_link_here" target="_blank">CSS</a></li>
<li><a href="your_link_here" target="_blank">JavaScript</a></li>
<li><a href="your_link_here" target="_blank">XML</a></li>
</ul>

</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
Display this link list as a horizontal menu:
  • HTML
  •  
  • CSS
  •  
  • JavaScript
  •  
  • XML

The following example displays span elements as block elements:

Example

<html>
<head>
<style type="text/css">
span
{
display:block;
}
</style>
</head>
<body>

<h2>Nirvana</h2>
<span>Record: MTV Unplugged in New York</span>
<span>Year: 1993</span>
<h2>Radiohead</h2>
<span>Record: OK Computer</span>
<span>Year: 1997</span>

</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:

Nirvana

Record: MTV Unplugged in New YorkYear: 1993

Radiohead

Record: OK ComputerYear: 1997

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.

More Examples

How to display an element as an inline element.
This example demonstrates how to display an element as an inline element.
Example
<html>
<head>
<style type="text/css">
p {display:inline;}
</style>
</head>


<body>
<p>A display property with a value of "inline" results in</p>
<p>no distance between two elements.</p>
</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
A display property with a value of "inline" results in

no distance between two elements.


How to display an element as a block element
This example demonstrates how to display an element as a block element.
Example
<html>
<head>
<style type="text/css">
span
{
display:block;
}
</style>
</head>
<body>


<span>A display property with a value of "block" results in</span> <span>a line break between the two elements.</span>


</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
A display property with a value of "block" results ina line break between the two elements.



How to make a table element collapse
This example demonstrates how to make a table element collapse.
Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
tr.collapse {visibility:collapse;}
</style>
</head>
<body>

<table border="1">
<tr>
<td>Peter</td>
<td>Griffin</td>
</tr>
<tr class="collapse">
<td>Lois</td>
<td>Griffin</td>
</tr>
</table>

<p><b>Note:</b> IE8 and earlier support visibility:collapse only if a !DOCTYPE is specified.</p>

</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
PeterGriffin

Note: IE8 and earlier support visibility:collapse only if a !DOCTYPE is specified.


Positioning can be tricky sometimes!

Decide which element to display in front!

Elements can overlap!


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.

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.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<style type="text/css">
p.pos_fixed
{
position:fixed;
top:30px;
right:5px;
}
</style>
</head>
<body>

<p class="pos_fixed">Some more text</p>
<p><b>Note:</b> IE7 and IE8 supports the fixed value only if a 
!DOCTYPE is specified.</p>
<p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
Note: IE7 and IE8 supports the fixed value only if a !DOCTYPE is specified.
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text
Some text


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.

Relative Positioning

A relative positioned element is positioned relative to its normal position.

Example

<html>
<head>
<style type="text/css">
h2.pos_left
{
position:relative;
left:-20px;
}

h2.pos_right
{
position:relative;
left:20px;
}
</style>
</head>

<body>
<h2>This is a heading with no position</h2>
<h2 class="pos_left">This heading is moved left according to its normal position</h2>
<h2 class="pos_right">This heading is moved right according to its normal position</h2>
<p>Relative positioning moves an element RELATIVE to its original position.</p>
<p>The style "left:-20px" subtracts 20 pixels from the element's original left position.</p>
<p>The style "left:20px" adds 20 pixels to the element's original left position.</p>
</body>

</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:

This is a heading with no position

This heading is moved left according to its normal position

This heading is moved right according to its normal position

Relative positioning moves an element RELATIVE to its original position.
The style "left:-20px" subtracts 20 pixels from the element's original left position.
The style "left:20px" adds 20 pixels to the element's original left position.


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

<html>
<head>
<style type="text/css">
h2.pos_top
{
position:relative;
top:-50px;
}
</style>
</head>

<body>
<h2>This is a heading with no position</h2>
<h2 class="pos_top">This heading is moved upwards according to its normal position</h2>
<p><b>Note:</b> Even if the content of the relatively positioned element is moved, the reserved space for the element is still preserved in the normal flow.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:

This is a heading with no position

This heading is moved upwards according to its normal position

Note: Even if the content of the relatively positioned element is moved, the reserved space for the element is still preserved in the normal flow.

Relatively positioned elements are often used as container blocks for absolutely positioned elements.

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

<html>
<head>
<style type="text/css">
h2
{
position:absolute;
left:100px;
top:150px;
}
</style>
</head>

<body>
<h2>This is a heading with an absolute position</h2>
<p>With absolute positioning, an element can be placed anywhere on a page. The heading below is placed 100px from the left of the page and 150px from the top of the page.</p>
</body>

</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
With absolute positioning, an element can be placed anywhere on a page. The heading below is placed 100px from the left of the page and 150px from the top of the page.


This is a heading with an absolute position

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.

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


<html>
<head>
<style type="text/css">
img
{
position:absolute;
left:0px;
top:0px;
z-index:-1;
}
</style>
</head>


<body>
<h1>This is a heading</h1>
<img src="Your_image.gif" width="100" height="140" />
<p>Because the image has a z-index of -1, it will be placed behind the text.</p>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
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.

More Examples

Set the shape of an element
This example demonstrates how to set the shape of an element. The element is clipped into this shape, and displayed.
Example

<html>
<head>
<style type="text/css">
img 
{
position:absolute;
clip:rect(0px,60px,200px,0px);
}
</style>
</head>


<body>
<img src="w3css.gif" width="100" height="140" />
</body>
</html>
Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.

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.

Example

<html>
<head>
<style type="text/css">
div.scroll
{
background-color:#00FFFF;
width:100px;
height:100px;
overflow:scroll;
}


div.hidden 
{
background-color:#00FF00;
width:100px;
height:100px;
overflow:hidden;
}
</style>
</head>


<body>
<p>The overflow property specifies what to do if the content of an element exceeds the size of the element's box.</p>


<p>overflow:scroll</p>
<div class="scroll">You can use the overflow property when you want to have better control of the layout. The default value is visible.</div>


<p>overflow:hidden</p>
<div class="hidden">You can use the overflow property when you want to have better control of the layout. The default value is visible.</div>
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
The overflow property specifies what to do if the content of an element exceeds the size of the element's box.
overflow:scroll
You can use the overflow property when you want to have better control of the layout. The default value is visible.
overflow:hidden


This example demonstrates how to set the browser to automatically handle overflow.

Example

<html>
<head>
<style type="text/css">
div 
{
background-color:#00FFFF;
width:150px;
height:150px;
overflow:auto;
}
</style>
</head>


<body>
<p>The overflow property decides what to do if the content inside an element exceeds the given width and height properties.</p>


<div>
You can use the overflow property when you want to have better control of the layout. Try to change the overflow property to: visible, hidden, scroll, or inherit and see what happens. The default value is visible.
</div>
</body>


</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
The overflow property decides what to do if the content inside an element exceeds the given width and height properties.
You can use the overflow property when you want to have better control of the layout. Try to change the overflow property to: visible, hidden, scroll, or inherit and see what 


This example demonstrates how to change the cursor.

Example

<html>
<body>
<p>Mouse over the words to change the cursor.</p>
<span style="cursor:auto">auto</span><br />
<span style="cursor:crosshair">crosshair</span><br />
<span style="cursor:default">default</span><br />
<span style="cursor:e-resize">e-resize</span><br />
<span style="cursor:help">help</span><br />
<span style="cursor:move">move</span><br />
<span style="cursor:n-resize">n-resize</span><br />
<span style="cursor:ne-resize">ne-resize</span><br />
<span style="cursor:nw-resize">nw-resize</span><br />
<span style="cursor:pointer">pointer</span><br />
<span style="cursor:progress">progress</span><br />
<span style="cursor:s-resize">s-resize</span><br />
<span style="cursor:se-resize">se-resize</span><br />
<span style="cursor:sw-resize">sw-resize</span><br />
<span style="cursor:text">text</span><br />
<span style="cursor:w-resize">w-resize</span><br />
<span style="cursor:wait">wait</span><br />
</body>
</html>

Try it yourself in Notepad or Dreamweaver by coping and pasting the code above.
Result:
Mouse over the words to change the cursor.
auto
crosshair
default
e-resize
help
move
n-resize
ne-resize
nw-resize
pointer
progress
s-resize
se-resize
sw-resize
text
w-resize
wait

All CSS Positioning Properties

The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2).
PropertyDescriptionValuesCSS
bottomSets the bottom margin edge for a positioned boxauto
length
%
inherit
2
clipClips an absolutely positioned elementshapeauto
inherit
2
cursorSpecifies the type of cursor to be displayedurlauto
crosshair
default
pointer
move
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
text
wait
help
2
leftSets the left margin edge for a positioned boxauto
length
%
inherit
2
overflowSpecifies what happens if content overflows an element's boxauto
hidden
scroll
visible
inherit
2
positionSpecifies the type of positioning for an elementabsolute
fixed
relative
static
inherit
2
rightSets the right margin edge for a positioned boxauto
length
%
inherit
2
topSets the top margin edge for a positioned boxauto
length
%
inherit
2
z-indexSets the stack order of an elementnumberauto
inherit
2