CSS

Basics of CSS Part – #9

Today we’re going to talk about list styles. Lists are a vital commodity to web site creation. Most side-navigation menus and inline menus are actually lists that are simply listing the things we want them to. From there, we can add styles to these lists and manipulate them however we please.

Lists

In HTML there are two types of lists:

  • Ordered Lists <ol>
  • Unordered Lists <ul>

Ordered Lists are usually marked with numbers or letters whereas Unordered Lists are usually marked with bullets or squares.

List Type

We can change how a list looks by using the list-style-type property. Consider the following code:

ul {list-style-type:circle;}
ol {list-style-type:upper-roman;}

The CSS code above will make all ul elements have a list style type of a circle and all ol elements have a list style type of upper roman numerals. The output will look like the following:

  • Item 1
  • Item 2
  1. Item 1
  2. Item 2

Properties

We can have a few properties for list types and those properties operate the same way the background properties

Property Description Values
list-style This is the all inclusive property where you can define all of the values list-style-type
list-style-image
list-style-position
list-style-type This property will define type of list style you wish to have in the <li> element. none
disc
circle
square
decimal
decimal-leading-zero
armenian
georgian
lower-alpha
upper-alpha
lower-greek
lower-latin
upper-latin
lower-roman
upper-roman
hebrew
cjk-ideographic
hiragana
hiragana-iroha
katakana
katakana-iroha
list-style-image This property will allow you to define an image as the list marker. url(image url)
none
list-style-position This property will allow you to specify whether you want the list to be inside or outside the content flow. inside
outside

We can also narrow down the list-style and list-style-type properties for each list element.

Unordered Lists

For unordered lists we can use the following values:

  • circle
  • disc
  • square
  • none

Technically we can use any of the values listed in the table above, however, the purpose of the unordered list would cease to exist.

Ordered Lists

We can have many options for the ordered list. Out of the 23 values we posted for list-style-type, only three were solely for unordered lists. We can use the following values:

  1. decimal
  2. decimal-leading-zero
  3. armenian
  4. georgian
  5. lower-aplha
  6. upper-alpha
  7. lower-greek
  8. upper-greek
  9. lower-latin
  1. upper-latin
  2. lower-roman
  3. upper-roman
  4. hebrew
  5. cjk-ideographic
  6. hiragana
  7. hiragana-iroha
  8. katakana
  9. katakanap-iroha

Using images with lists

Using images can spice up just a standard web page by a lot. It is very easy to create your own bullet, circle, square, or a small arrow pointing toward the item you are showing in the list. We can use the all-inclusive,

list-style:url ("image/bullet.png");
/*OR*/
list-style-image:url ("image/bullet.png");

This will replace the default bullet in the unordered list with an image of our choice.

To see these properties in action feel free to download lists.zip(9.75KB)

Advanced CSS Part -#7

Today we’re going to create a top navigation menu and side menu in CSS. This is like any website menu that we would see on most websites.

Inline Menu

This is an example of a website menu:

Menu

We can see that the menu is entirely inline and is actually really just an unordered list. Consider the following code:

#menu {
     background-color:#4c4c4c;
     height:30px;
     font-size:30px;
     font-weight:700;
     padding:5px 0px;
}
#menu ul {list-style:none;}
#menu li {
     display:inline;
     padding:0px 10px;
}
<div id="menu">
     <ul>
          <li><a href="url">Link</a></li>
          <li><a href="url">Link</a></li>
          <li><a href="url">Link</a></li>
          <li><a href="url">Link</a></li>
     </ul>
</div>

The CSS is setting the styles for the navigation menu… We have our ID selector #menu that is creating a background of #4c4c4c (grey), a height of 30px, a font size of 30px, a font weight of 700 (in other words, bold), and a padding of 5px that applies to the top and bottom ONLY.

#menu ul is basically saying that all <ul> elements inside of the ID #menu will have a list-style of none.

#menu li is saying that all <li> elements within the ID #menu will be displayed as inline elements (no breaks before or after the element, refer to the Display Property for more information) and will apply a 10px padding to the left and right sides ONLY.

Vertical Menu

A vertical menu is a menu that is, well, vertical.

Side Navigation

Side Menus are easier to make than inline menus. Consider the following code:

#side-nav {
     background-color:#4c4c4c;
     display:block;
     width:150px;
}
#side-nav ul {
     list-style-image:url("images/bullet.png");
     margin:10px;
}
#side-nav li {
     padding:5px 0px;
     border-bottom:1px solid #3b3b3b;
}
#side-nav.no-border {border:none;}
<div id="side-nav">
     <ul>
          <li><a href="url">Link</a></li>
          <li><a href="url">Link</a></li>
          <li><a href="url">Link</a></li>
          <li class="no-border"><a href="url">Link</a></li>
     </ul>
</div>

So we have our ID selector side-nav that is creating the background color of #4c4c4c (grey), displaying the selector as a block element and a width of 150px. Note: since only the width is defined, we can have an unlimited height.

Next, we have our selector that is saying that all <ul> elements within the side-nav selector will have a list style image of “bullet.png” and will have a 10px margin around all sides.

Finally, we have our selector that is saying all <li> elements within the side-nav selector will have a top and bottom padding of 5px ONLY and a bottom border that is 1px wide, solid, and the color #3b3b3b. There is something interesting, however; there is an additional selector defined that states a border is none which we used on the last <li> element of our vertical menu. This will make the menu look better, or at least more symmetrical.

I recommend you downloading Menu.zip (4.46KB) to see these menus in action!

Advanced CSS – Part #6

Pseudo-elements are used to add special effects to some html elements.

Pseudo-elements are very much like pseudo-classes. The syntax for pseudo-elements is like pseudo-classes:

selector:pseudo-element {property:value;}

We can also use class selectors with our pseudo-elements:

selector.class:pseudo-element {property:value;}

Pseudo-elements

Since pseudo-elements exist to give some html elements some flair, we have a few special pseudo-elements that we need to be familiar with.

:first-line

As the name implies, this pseudo-element adds special styles to the first line of text. We can only use this pseudo-element with block-level elements (see Display Property for more details).

p:first-line {
     color:#ff9300;
     font-style:italic;
}

The above code would make the first line of text color turn to atomicpages orange and then change the first line of text to be italic. There are, however, certain properties that are not allowed when using the :first-line property. We can use the following:

  1. all font properties
  2. color
  3. all background properties
  4. text-decoration
  5. clear
  6. text-transform
  7. letter-spacing
  8. word-spacing
  9. vertical-align
  10. line-height

These are the only properties that are valid to use with the :first-line pseudo-element!

:first-letter

This pseudo-element applies unique styles to the first letter of text. Consider the following code:

p.a:first-letter {
     background:#fff url("images/fancy_a.gif") no-repeat;
     border:1px solid #ff9300;
     margin-left:25px;
}

This will load the first image of all p elements with the class “a” so:

<p class="a">very fancy image at the beginning of this makes this
sentence seem much more grandiloquent that it really is!</p>

This fancy_a.gif will also have a border of 1px which is solid and atomicpages orange and will have a left margin of 25px.

Like the :first-line pseudo-element, there are restrictions on which properties can be used with this pseudo-element.

  1. all font properties
  2. color
  3. all background properties
  4. text-decoration
  5. clear
  6. text-transform
  7. letter-spacing
  8. word-spacing
  9. vertical-align
  10. line-height
  11. margin properties
  12. padding properties
  13. float

:before

This pseudo-element is used to insert content before and element is rendered by the browser. Consider the following code:

span:before {background:#fff url("images/fancy_a.gif") no-repeat;}

This is saying that before every span element there will be that fancy “A” that we define earlier in this tutorial.

:after

This pseudo-element is used to insert content after and element is rendered by the browser. Consider the following code:

h4:after {background:#fff url("images/fancy_b.gif") no-repeat;}

This is saying that after every h4 element there will be that fancy “B”.

Now what does that accomplish? Nothing very significant in this case but you can download pseudo-elements.zip (7.76 KB) to see these pseudo-elements in action!

Advanced CSS – Part #5

Pseudo-classes are little snippets of code that will add additional effects to some selectors.

Pseudo-classes

The syntax for Pseudo-classes is very much like the syntax for css:

selector {property:value;} /*CSS Syntax*/
selector:pseudo-class { /*Pseudo-class Syntax*/
     property:value;
     property:value;
}

:link, :visited, :active, :hover

A very widely used example of Pseudo-classes is adding styles to links. Consider the following:

a:link, a:visited {
     text-decoration:underline;
     color:#ff9300;
}
a:hover {
     text-decoration:none;
     color:#ff9300;
}

These are called “global link styles”, these are styles that will apply to any and all links on a page where this code is. We have our pseudo-classes a:link, a:visited, a:hover and each is doing something different.

1. a:link

a:link sets the styles for an unvisited link:

a:link {
     color:red;
     font-weight:bold;
}

2. a:visited

a:visited sets the styles for a visited link:

a:visited {
     color:blue;
     font-weight:bold;
}

3. a:hover

a:hover sets the styles for links that you mouse over:

a:hover {
     color:aqua;
     font-weight:bold;
     text-decoration:unerline;
}

4. a:active

This sets the styles for a link that is currently active:

a:active {
     color:coral;
     font-weight:bold;
     text-decoration:unerline;
}

Order

Order is key when using these particular pseudo-classes. When you write these styles you must follow the correct order to make it all work.

a:link { /*This must be first*/
     color:red;
     font-weight:bold;
}
a:visited {  /*This must be second*/
     color:coral;
     font-weight:bold;
}
a:hover {  /*This must be third*/
     text-decoration:unerline;
     color:blue;
     font-weight:bold;
}
a:active { /*This must be last*/
     color:green;
     font-weight:bold;
}

We can also group the pseudo-classes together like this:

a:link, a:visited {
     text-decoraton:none;
     color:#ff9300;
}
a:hover, a:active {
     text-decoration:unerline;
     color:coral;
}

Like other CSS selectors, we can add a class selector to the Pseudo-classes, this would enable us to apply these styles any where on our web page if we desired it to be so.

selector.class:pseudo-class {
     property:value;
     property:value;
}
a.fancy:link, a.fancy:visited {
     text-decoration:underline;
     color:#ff9300;
}

This class fancy will make a link and you visit that link that we create, it will be underlined and be that atomicpages orange color.

:first-child

The first-child pseudo-class is as it sounds. This will apply any styles that you define to the first-child element of a parent element. Consider the following code:

body {
     margin:auto;
     width:1000px;
     background-color:#4c4c4c;
     font:normal 11px Verdana, "Times New Roman", sans serif;
     color:#fff;
}
p:first-child {
     color:blue;
     text-decoration:underline;
     font-weight:700;
}

We can use the first-child pseudo-class in some very helpful ways too! If, for instance, you wanted to apply a style that would make the first paragraph unique, we could use the first-child pseudo-class to achieve this goal. Consider the following:

.fancy p:first-child {
     color:red;
     font-style:italic;
}

This would set the first paragraph of the class “fancy” to be red and italic and all subsequent paragraphs to be whatever we set the styles to.

If you want to see pseudo classes in action feel free to check out pseudo-casses.zip (1.88KB) and see these examples in action!

CSS Reference

Quick Selection

Background Property

Properties Definitions Examples CSS
background Sets all background properties: background:#4c4c4c url("image/gloss.png") top center repeat-x; 1
background-attachment Sets whether the image scrolls or is fixed on a page: background-attachment:fixed; 1
background-color Sets the background color of an element: background:blue; 1
background-image Sets the background image on an element: background-image:url("image/header.jpg"); 1
background-position Sets the position of an element: background-position:center; 1
background-repeat Sets how the background will repeat: background-repeat:repeat; 1

Border and Outline Properties

Properties Definitions Examples CSS
border Sets all four definitions in one declaration border:1px inset blue; 1
border-style Sets the border style for all four sides border-style:groove; 2
border-width Sets the border width for all four sides border-width:thick; 1
border-color Sets the border color for all four sides border-color:#ff9300; 2
border-top Sets the top border in one declaration border-top:thin solid #fff; 1
border-top-width Sets the top border width border-top-width:medium; 1
border-top-color Sets the top border color border-top-color:purple; 2
border-top-style Sets the top border style border-top-style:ridge; 2
border-right Sets the right border in one declaration border-right:10px dotted orange; 1
border-right-color Sets the right border color border-right-color:#000; 2
border-right-style Sets the right border style border-right-width:dashed; 2
border-right-width Sets the right border width border-right-width:2px; 1
border-bottom Sets the bottom border in one declaration border-bottom:3px hidden transparent; 1
border-bottom-color Sets the bottom border color border-bottom-color:#470b0b; 2
border-bottom-style Sets the bottom border style border-bottom-style:none; 2
border-bottom-width Sets the bottom border width border-bottom-width:4px; 1
border-left Sets the left border in one declaration border-left:6px outset white; 1
border-left-color Sets the left border color border-left-color:#fff1d8; 2
border-left-style Sets the left border style border-left-style:inherit; 2
border-left-width Sets the left border width border-left-width:8px; 1
outline Sets the outline in one declaration outline:3px dotted green; 2
outline-color Sets the outline color outline-color:blue; 2
outline-style Sets the outline style outline-style:inset; 2
outline-width Sets the outline width outline-width:thin; 2

Dimensions

Properties Definitions Examples CSS
width Sets the width of an element width:1000px; 1
max-width Sets the maximum width of an element max-width:75% 2
min-width Sets the minimum width of an element min-width:20em; 2
height Sets the height of an element height:600px; 1
max-height Sets the maximum height of an element max-height:50%; 2
min-height Sets the minimum height of an element min-height:2em; 2

Font Properties

Properties Definitions Examples CSS
font Sets all the font properties in one declaration font:11px bold italic Verdana, Geneva, sans-serif 1
font-family Sets the font family font-family:Georgia, "Times New Roman", Times, serif; 1
font-size Sets the font size font-size:18px; 1
font-style Sets the font style font-style:oblique; 1
font-variant Sets the font variant font-variant:small-caps; 1
font-weight Sets the font weight (boldness) font-weight:700; 1

List Properties

Properties Definitions Examples CSS
list-style Sets all properties in one declaration list-style:circle outside url(“list.gif”); 1
list-style-image Sets list image list-style-image:url("blue.png"); 1
list-style-position Sets whether position is inside or outside the flow of the list list-style-position:inside; 1
list-style-type Sets list type list-style-type:none; 1

Margin Properties

Properties Definitions Examples CSS
margin Sets all four sides of the margin in one declaration margin:25px 3px 5px 15px; 1
margin-top Sets the top margin margin-top:10px; 1
margin-right Sets the right margin margin-right:15px; 1
margin-bottom Sets the bottom margin margin-bottom:50px; 1
margin-left Sets the left margin margin-left:25px; 1

Padding Properties

Properties Definitions Examples CSS
padding Sets all four sides of the padding in one declaration padding:15px 30px; 1
padding-top Sets the top padding padding-top:30px; 1
padding-right Sets the right padding padding:right:10px; 1
padding-bottom Sets the bottom padding padding-bottom:50px; 1
padding-left Sets the left padding padding-left:25px; 1

Positioning Properties

Properties Definitions Examples CSS
bottom Sets the bottom margin edge for a box that is positioned bottom:30px; 2
clear Sets which sides floated elements are not allowed clear:both; 1
clip Clips an element that is absolutely positioned clip:rect(30px 0px 60px 90px); 2
cursor Specifies the cursor to be displayed within a certain element cursor:crosshair; 2
display Specifies which type of box an element should have display:inline; 1
float Specifies whether or not a box should float left or right float:left; 1
left Sets the left margin edge for a box that is positioned left:45px; 2
overflow Sets what happens if content were to overflow its box overflow:scroll; 2
position Sets how an element is positioned position:relative; 2
right Sets the right margin edge for a box that is positioned right:15px; 2
top Sets the top margin edge for a box that is positioned top:20px; 2
visibility Specifies whether or not an element is visible visibility:hidden; 2
z-index Specifies the stacking order of elements z-index:-1; 2

Print Properties

Properties Definitions Examples CSS
page-break-before Sets the page break before an element page-break-before:always; 2
page-break-after Sets the page break after an element page-break-after:auto; 2
page-break-inside Sets the page break inside an element page-break-inside:avoid; 2
orphans Sets the minimum number of lines there must be and the bottom of an element after a page break orphans:2; 2
windows Sets the minimum number of lines there must be and the top of an element after a page break windows:10; 2

Table Properties

Properties Definitions Examples CSS
border-collapse Specifies whether or not the border should be collapsed border-collapse:collapse; 2
border-spacing Specifies distance between adjacent cell borders border-spacing:15pt; 2
caption-side Sets placement of table caption caption-side:center; 2
empty-cells Sets whether or not borders of empty cells are displayed empty-cells:hide; 2
table-layout Changes the algorithm used for the table layout table-layout:fixed; 2

Text Properties

Properties Definitions Examples CSS
color Sets the color of text color:#ff9300; 1
direction Sets the writing direction of text direction:right; 2
letter-spacing Specifies distance between letters of text letter-spacing:.75em; 1
line-height Specifies distance between lines of text line-height:15px; 1
text-align Sets the alignment of text text-align:center; 1
text-decoration Specifies the decoration added to text text-decoration:overline; 1
text-indent Sets indentation of first line in a text block text-indent:25px; 1
text-shadow Sets the shadow text may have text-shadow:;#ccc 15px -10px 8px; 2
text-transform Controls the capitalization of text text-transform:uppercase; 1
vertical-align Sets the vertical alignment of an element vertical-align:middle; 1
white-space Sets how the white space inside an element will be used white-space:pre-wrap; 1
word-spacing Specifies the distance between words in a text block word-spacing:30px; 1

Pseudo-classes and Pseudo-elements

Properties Definitions Examples CSS
:active Applies specified style to an active element a:active {<styles>} 1
:after Adds content after an element a:after {<styles>} 2
:before Adds content before an element a:before {<styles>} 2
:first-child Adds specified styles to the first child element body:first-child {<styles>} 2
:first-letter Adds specified styles to the first letter of an element p:first-letter {<styles>} 1
:first-line Adds specified styles to the first line of text p:first-line {<styles>} 1
:focus Adds a focus to keyboard input input:focus {<styles>} 2
:hover Applies styles to an element that you hover over a:hover {<styles>} 1
:lang Adds style to an element with a lang attribute q:lang {<styles>} 2
:link Applies styles to a unvisited link a:link {<styles>} 1
:visited Applies styles to a link that has been visited a:visited {<styles>} 1

Advanced CSS – Part #4

In this tutorial we’re going to talk about the float property. For this tutorial you will need to be familiar with the Basics of CSS and you should be familiar with the Advanced CSS tutorials until this point.

Float Property

The float property allows for an element to be pushed to the left or the right so other elements can wrap around it. Floated elements can only be moves horizontally (left or right). All floated elements will move as far right or as far left as it possibly can; this is dependent upon the nature of the container that the element is in.

Values

  1. left – floats element to the left
  2. right – floats element to the right
  3. none – specifies no floating

Consider the following code:

body {
     margin:auto;
     width:1000px;
     background-color:#4c4c4c;
     font:verdana, "Times New Roman", san-serif;
     font-size:11px;
     color:#fff;
}
#container {
     width:300px;
     background:transparent url('image/img.jpg') repeat-x;
     min-height:125px;
     float:right;
}

We have made the body element have a width of 1000px (the rest of the styles are irrelevant) and then we defined an ID selector that has a width of 300px and a min-height of 125px (it has to be at least 125px tall) and this element will be pushed to the edge of the right side of the container.

Since floated elements allow other elements to wrap around the floated element. All elements before the float property will not be affected, however, all element thereafter the float property will be affected. Consider the following code:

body { /*...*/ }
#container {
     width:300px;
     background:transparent url('image/img.jpg') repeat-x;
     min-height:125px;
     float:right;
}
.float-right {float:right;}
<html>
<body>
<div id="container">
     <p>This element will be on the right edge of the page that is
1000px across.</p>
</div>
<p>This is some text</p>
<img src="image/img.gif" height="100" width="100" alt="img"
class="float-right" />
<p>This is some text</p>
</body>
</html>

This example would allow for the text to wrap around “img.gif” no matter what size the image is (in the case of the example it is 100px by 100px).

Floating Elements

As we mentioned before, floating an element will push the element to the right or left side of the container if possible. Consider the following code:

.fancy {
     width:500px;
     float:left;
}
<h2>Title here</h2>
<div class="fancy">
<img src="img.gif" height="64" width="64" alt="img" />
<p>Description here</p>
</div>

This will put the image inside of an area of 500 px wide and infinitely tall (depending the amount of content in that class selector).

Clear Property

How do we “turn off” floated elements? The clear property specifies which sides of a floated element you want to keep clear of other floating elements.

Values

  1. left – clears the left side of the element
  2. right – clears the right side of the element
  3. both – clears both the left and right side of the element
  4. none – does not clear either side of the element

Consider the following code:

<html>
<head>
<style type="text/css">
.clearfix {clear:both;}
.fancy {
     width:500px;
     float:left;
     margin5px;
}
</style>
</head>
<body>
<div class="fancy">
<h2>Fancy Pictures</h2>
<img src="img.gif" height="64" width="64" alt="img" />
<img src="img2.gif" height="64" width="64" alt="img" />
<img src="img3.gif" height="64" width="64" alt="img" />
<img src="img4.gif" height="64" width="64" alt="img" />
<img src="img5.gif" height="64" width="64" alt="img" />
<p class="clearfix">Some explanatory text here</p>
<img src="img6.gif" height="64" width="64" alt="img" />
<img src="img7.gif" height="64" width="64" alt="img" />
</div>
</body>
</html>

In the code above, you would have all of the styles we defined in the fancy class and then a break of text where no floated element is allowed (neither left or right). This will clear a space for the explanatory text where no floated element will exist. This can be useful for adding more titles or text to the “fancy” class we created.

It is recommended that you download the float.zip (41.4KB) to see this property in action!

Advanced CSS – Part #3

Today we’re going to talk about advanced positioning techniques. This is a widely used property in CSS and it very useful! Positioning allows you to position an element, place one element behind another, and determine what happens when an element is too big.

The position of an element is controlled by top, right, bottom, and left properties. The stacking order of these elements is called the z-index property.

Position

As mentioned above, the position property allows us to specifically position an element on a web page, place one element behind or above another, and to determine what happens when an element is too big.

Properties to Know

  1. position
    • top
    • right
    • bottom
    • left

Note: the top, right, bottom, and left properties will NOT work unless a position property is defined first!

Values

  1. static
  2. fixed
  3. absolute
  4. relative

Static

All elements are positioned static by default. These are positioned by the normal flow of the web page and it is NOT changed by using the top, right, bottom, or left properties.

p.static {
     position:static;
     top:30px; /*This will not work*/
     right:25px; /*This will not work*/
}

Fixed

A fixed position is relative to the browser window. Its place is literally locked in place and if you scroll, the element will remain exactly where it is.

p.fixed {
     position:fixed;
     top:30px;
     right:25px;
}

The above code will place the element 30 px from the top of the browser window and 25 px to the right of the browser window while the position of the element is fixed. This means you can scroll the browser window and the element will literally be fixed on the page (it will not move). No matter where you are on the page, the element will always be visible.

If you use this fixed property, all other elements on the page will not know that it exists. The fixed element is removed from the normal flow of the web page so it does not impact any other elements. This also means that the fixed elements can overlap any other elements on the page.

Relative

A relative positioned element is relative to its “normal position” on the page. The relative positioned elements can be moved to overlap other elements; however, it is different in that the space for the normal flow of the page, is preserved. Even if two elements are over lapping the space for the elements is still preserved.

p.relative {
     position:relative;
     top:-50px;
     right:10px;
}

Note: this will be made clearer via example in a small downloadable zip archive at the end of this tutorial. I strongly recommend downloading this tutorial!!

Absolute

Elements that are absolutely positioned are removed from the normal flow of the web page. Like the fixed value, all other elements on the web page don’t know this element exists. This means elements can easily overlap one another.

p.absolute {
     position:absolute;
     top:50px;
     left:350px;
}

This element would be positioned in accordance to the html element of the web page. This would be exactly 50px from the top of the web page and 350px to the left of the web page as well.

If you wish to see these examples in action and read more about this material please download position.zip(57.0 KB)

Advanced CSS – Part #2

Today we’re going to talk about the display and visibility properties of CSS. This is the second advanced tutorial and is intended for those of us that already understand the Basics of CSS.

Visibility

The Visibility of an element can be adjusted by using this visibility property. Let us say that we want to make a certain element invisible on a web page or that we want a certain to definitely be visible, we would use this visibility property.

Values

We can have three possible values for this visibility property.

  1. hidden – hides the element (these elements still take up space)
  2. visible – makes the element visible (default)
  3. collapse – this removes a table row or table column without affecting the table layout. If this is used on non-table elements they will render as “hidden”.

Note: no version of IE 8 or less supports the collapse value for this property. Consider the following code:

.hidden {visibility:hidden;}

This class would make the element “hidden” but the space for it would still exist, but it would show nothing. This might be a little confusing at first, however, it is very easy! This is some visible text that we can see. This is some invisible text that we cannot see but still takes up space inside of the element. This is more text that we will write for the sake of this example. The more and more we write we see that there is this void in this paragraph and it feels as if something is missing, doesn’t it? You can actually look in the source code of this page and you will see this:

<span style="visibility:hidden;">This is some invisible text that
we cannot see but still takes up space inside
of the element.</span>

This was the code that was actually used to achieve this effect. We can see that that visibility property makes the desired element invisible (hidden) but it still takes up space.

We won’t go into great detail about the collapse property and the visible value is default so this is not needed in order for an element to show. For example:

p.fancy {
     color:#ff9300;
     text-decoration:underline;
     font-size:x-small;
     visibility:visible; /*This is default, therefore not needed*/
}

Display

The display property defines how an element is displayed on the web page. The display property is very useful and particularly powerful. If you’ve ever wondered how to make those fancy top navigation menus like on this blog, we would use a certain value for the display property.

Values

  1. none – hides element completely (no box)
  2. block – creates a box (line breaks above and below)
  3. inline – creates inline element (no line breaks at all)
  4. inline-block – creates block but it is laid out as an inline box
  5. compact – basically this box will nest against another box is there is enough space
  6. inline-table – element will create a box with no breaks before or after the <table>
  7. list-item – creates list (like a ul) in a block but the li are inline elements
  8. run-in – your browser will create a block or inline box depending on how it is used 1
  9. table – this will behave like a table (a CSS alternative to th, td, and tr elements!)
  10. table-caption – behaves like a table caption
  11. table-header-group – behaves like a table header group
  12. table-row – behaves like a table row
  13. table-row-group – behaves like a table row group
  14. table-cell – behaves like a table cell
  15. table-column – behaves like a table column
  16. table-column-group – behaves like table column (like <colgroup>)
  17. table-footer – behaves like a table footer
  18. table-footer-group – behaves like a footer row group

As we can see there are many many values we can have for the display property and each does something unique. These will take time to explain so I highly recommend you download display.zip (67.9KB)
or you check out Quirksmode for a very comprehensive overview of the display property.

1. This is only supported in the latest version of IE and Opera 9.2 and Opera 10. All otehr browsers don’t necessarily support this value. You can read all about the run-in value from W3C if you want to learn more about it.

Advanced CSS – Part #1

Now that we have all of the Basics of CSS down pat, we can move onto more advanced methods of writing and implementing style sheets!

Today we’re going to talk about grouping and nesting selectors and dimensions.

Grouping and Nesting

You might find yourself with selectors having the same values inside of your internal or external style sheet. Maybe it is about a specific link within a certain area of your web site, heading elements, or just about anything else you would want to group.

Grouping

Grouping allows us to apply multiple properties and values to multiple selectors but in a condensed fashion.

Let us assume we have some code:

h1 {color:#ff9300;}
h3 {color:#ff9300;}
span {color:#ff9300;}
p {color:#ff9300;}

So we have everything within the h1, h3, span, and p elements is the color #ff9300. Since all of these elements have a common property we can group these elements like this:

h1, h3, span, p {color:#ff9300;}

This space saver will have the same effect but on multiple selectors. The single color property will be applied to all of these html elements. Note: grouping elements will apply all properties and values to the grouped elements. Example:

html, body, p, span, a:link, {
     margin: 0;
     padding: 0;
     font:verdana, "Times New Roman", sans serif;
     color:#ff9300;
}

This means margin:0; padding:0; font:verdana, “Times New Roman”, sans serif; and color#ff9300; will be applied to all of those selectors.

Nesting

Nesting will allow you to apply styles to a selector within a selector. Consider the following:

#fancy p {
     background-color:#000;
     font-weight:700;
     text-decoration:none;
     color:#ff9300;
}

So we have our ID selector with an additional selector after it. So if we have something with the ID selector such as:

<div id="fancy">
     <p>Some fancy text here</p>
</div>

It is saying everything within the fancy ID that is inside of any p element will have all of those properties and values. We have used this type of nesting in previous CSS tutorials.

Dimensions

We covered the width and height properties in the box model tutorial. We going to introduce a few new properties that we can use to further control how tall/wide an element is.

The Height property will set the height of the element and the Width property will set the width of the element. Consider the following code:

h2, h3 {
     height:100px;
     width:300px;
}

This will set all h2 and/or h3 elements to w height of 100px by 300px regardless of how much space it is taking up on the page. These two elements will always be this height. What if we wanted those two elements to be within a certain area of space?

Properties

  1. width – defines width (will always be)
  2. height – defines height (will always be)
  3. max-width – defines max width (cannot be bigger than)
  4. min-width – defines min width (will not be smaller than)
  5. max-height – defines max height (cannot be taller than)
  6. min-height – defines min height (will not be shorter than)

The last four properties are new to us. So let us improve on the previous code!

h2, h3 {
     max-height:100px;
     max-width:300px;
}

Given the code above, this will set the h2 and h3 elements to be no larger than 100px by 300px but can also be 0px by 0px if the element is empty (has no content).

Although it seems like this property would be somewhat useless, there has been situation where something a web page wasn’t working the way I wanted it to. Perhaps the top navigation menu was wrapping when I resized the browser windows and I wanted the entire page to keep its consistency. I would use a max-width and min-width property to define the space, lo and behold it worked!

For a very comprehensive overview of the above tutorial, please download advanced-part1.zip (66.7KB)

Basics of CSS Part – #8

Today we’re going to continue our journey into the Box Model. If you aren’t familiar with this box model, you should read about it!

Padding

Padding — defines the space between the element border and element content and will be the same color as the background color of the element.
If we recall that in the box model we have margins > borders > padding > content so Padding is tertiary. Let’s assume you have an HTML paragraph element:

<!--No Padding-->
<p style="background-color:#fff;">Lorem ipsum dolor sit amet,
consectetur adipiscing elit.</p>
<!--With padding-->
<p style="background-color:#fff; padding:10px;">Lorem ipsum dolor
sit amet, consectetur adipiscing elit.</p>
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.

So what is different from the first line and the second line? The first line is without padding so the white background only fills to the end of the text instead the width of the box. The second line, however, spreads across the entire width of the box and it is a padding of 10px on all sides.

Padding works in very similar ways as the margin property except the margins are always invisible and the padding is based on the background of the element you are adding padding to.

Properties

Like margins, there are five possible properties we can use. They are the following:

  1. padding-bottom
  2. padding-left
  3. padding-top
  4. padding-right
  5. padding

The first four properties define individual sides for padding. For example, if you wanted to adjust the bottom and right padding for a certain element you could use the following:

p.element {
     padding-bottom:3px;
     padding-top:7px;
}

This will change the bottom bottom padding of the element by 3px and the top padding of the element by 7px. Example:

  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  • Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Remember that padding is basically an extension of the background of an element and you also need to take into account any other elements (whether above, below, left or right) that might get in the way of this padding. In theory, you could make an entire HTML and CSS website using only margins, borders, padding, and content.

Values

  1. em, px, pt – numeric values e.g. 1em, 15px, 2pt
  2. percent % – percent of the containing element e.g. 10% of 500px is 50px.
span.padding {
     padding-top:2em;
     padding-bottom:10px;
     padding-right:16%;
}

All of the above are valid ways to define padding of our span element and class of padding.

Padding – All Inclusive

The all inclusive padding property works the same exact way as the all inclusive margin property. This singular padding property will define padding how ever we need it to using one property instead of writing out each side.

padding:15px 30px 45px 60px;
  • top padding – 15px
  • right padding – 30px
  • bottom padding – 45px
  • left padding – 60px

This example has four values for the padding property. The values start with padding-top and then go clockwise to padding-left. So if you think of clock and a new day, which always starts at 12 or the top of the clock, and then rotate clockwise the next 90° angle is at the right, then the 180° is at the bottom, and then the 270° is at the left. So it’s like looking at the clock at every 90° angle you get top, right, bottom, left.

padding:15px 30px 45px;
  • top padding is 15px
  • right and left padding is 30px
  • bottom padding is 45px

This might come as a surprise to us because you would think they would group the Top and [bottom and left] segments separately, however; we can see in this example that the padding-top is 15px and the padding-bottom is 45px. The left and right padding is grouped together because they just belong together. If someone asks, “when I say a word you think of something that is opposite to that” and they ask, “up” you’ll probably say “down” off the bat.

padding:15px 30px;
  • top and bottom padding 15px
  • right and left padding 30px

This might be more evident since there are only two values here. Naturally, we’d think Top and [right and left] so the top and bottom padding will have a value of 15px whereas the right and left padding will have a value of 30px.

padding:15px;
  • top, right, bottom, and left padding is 15px

One single value for the padding property will set all padding to that value, in our case 15px.