CSS

Basics of CSS Part – #9

0

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

    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:

    1
    2
    3
    
    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 as 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,

    1
    2
    3
    4
    5
    
    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 click on the download button below!

    lists.zip
    lists.zip
    Size: 9.76 KB

Advanced CSS Part -#7

0

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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.

menu.zip
menu.zip
Size: 4.46 KB

Advanced CSS – Part #6

0

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:

1
selector:pseudo-element {property:value;}

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

1
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).

1
2
3
4
5
6
7
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:

1
2
3
4
5
6
7
8
9
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:

1
2
3
<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:

1
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:

1
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 click on the green download button to see these pseudo-elements in action!

pseudo-elements.zip
pseudo-elements.zip
Size: 7.76 KB

Advanced CSS – Part #5

1

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:

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:

1
2
3
4
5
6
7
a:link {
     color:red;
     font-weight:bold;
}

2. a:visited

a:visited sets the styles for a visited link:

1
2
3
4
5
6
7
a:visited {
     color:blue;
     font-weight:bold;
}

3. a:hover

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

1
2
3
4
5
6
7
8
9
a:hover {
     color:aqua;
     font-weight:bold;
     text-decoration:unerline;
}

4. a:active

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

1
2
3
4
5
6
7
8
9
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:

1
2
3
4
5
6
7
.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 click on the green download button below to see these examples in action!

pseudo-classes.zip
pseudo-classes.zip
Size: 1.88 KB

CSS Reference

0

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
Go to Top