This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

A Sprinkle of Style


So far we have explored the semantic markup provided by HTML. Our output has been widely accessible, but slightly bland blush

Lets take a look at Cascading Stylesheets (CSS), which will be used as a complement to HTML.

Cascading Stylesheets

Cascading Stylesheets (CSS) provide us with a means to apply stylistic rules to our HTML documents. These rules include font selection, alignment, color, layout, and even animations (with the new CSS3)!

CSS values typically live in the <head></head> section of a document. CSS styles are surrounded by <style></style> tags.

<head>
  <style type="text/css">
    ... style rules here ...
  </style>
</head>

Alternatively, you can create a seperate file containing CSS rules that will be included in HTML documents.

<link rel"stylesheet" href="style.css" type="text/css">

A CSS selector targets a specific page element or even a class of elements such as links or paragraph text and enables us to selectively style to our heart's content. The selectors that we will be covering are:

  • body
  • h1, h2, etc
  • p

Selectors are used in conjunction with properties and values. Once you select a page element, you can change its properties, such as color, to different values, such as green. Lets take a look.

selector {
  property-one: value;
  property-two: value;
}

In the example above, we have created a rule set describing how the browser (Mozilla Firefox, Chromium, etc.) should render the page elements that the selector targets. The rule set is enclosed in curly braces and is preceeded by a selcor. Also notice that each property declaration is separated by a semi-colon. These declarations could be written on the same line, but having them on separate lines makes it easier for us to read.

Body

Lets give our body a nice background. Pick your favorite color from the list of HTML Color Names and then style your background-color.

body
{
    background-color: MediumSpringGreen;
}

Headings

Headings usually contain large, bold text. The higher the heading level (e.g. <h1>), typically, the larger the font. Lets style our level one headers.

h1
{
    font-size: 120%;
    font-weight: bold;
}

For this example, the H1 font size is enlarged to 120%. This size is relative to the font-size for the containing document or page element. Using relative font sizes and page dimensions can save you a lot of headache compared to absolute values and can increase the accessibility of your content. For more on relative values, read the article Dao of Web Design.

Paragraphs

The font size, style, and color for our page paragraphs may need some adjusting to look good with our fresh background color and large headers. If you chose a dark background-color it will be important to pick a light font color so that visitors can enjoy your content cool

Lets set the font properties for our paragraphs.

p
{
    color: Tomato;
    font-family: sans-serif;
}

Learning Resources

W3Schools: CSS Backgrounds, CSS Text, CSS Fonts

Task Discussion


  • Karam   Oct. 10, 2013, 2:30 p.m.

    Very basic animations with CSS3: http://htmlpad.org/HTML5-Lesson9/

    I found an awesome step-by-step tutorial on how to do this right here:

    http://designshack.net/articles/css/circlegallery/

  • EcologicalHumanist   July 12, 2013, 2:59 p.m.

    Some CSS:

    http://jsfiddle.net/zmDKv/3/embedded/result/

  • bryanwoodsmall   July 3, 2013, 7:54 p.m.

    my first css.

    http://makefriendswithalgebra.com/Friendswithstyle.html

  • zoki   April 19, 2013, 5:34 a.m.

    http://jsfiddle.net/3eYUW/9/

  • Joris Pekel   Oct. 10, 2012, 10:28 a.m.
    Whoo, finally some colors!
  • lionel   Oct. 5, 2012, 10:39 a.m.

    Hi All,. 

    How would you use the link command in your index file?

    <link rel"stylesheet" href="style.css" type="text/css>

    Do you need to have the

     

    <html>
        <head>
        <style type="text/css">

    <link rel"stylesheet" href="style.css" type="text/css>

    </style>

    </head>

     

    and in the style sheet do you just have this code?

    body {
        background-color: BurlyWood;
        }
        h1 {
        font-size: 120%;
        font-weight: bold;
        }
        p {
        color: blue;
        font-family: Century Gothic;
        }

    Cause that doesnt work

    :~(

     

    What would I need to get the link thingy to work?
     

  • Adri   March 23, 2013, 11:40 p.m.
    In Reply To:   lionel   Oct. 5, 2012, 10:39 a.m.

    You need to create a separate CSS file (in this instance, style.css) with the following:

    body {
        background-color: BurlyWood;
        }
        h1 {
        font-size: 120%;
        font-weight: bold;
        }
        p {
        color: blue;
        font-family: Century Gothic;
        }

    And then, on your index file, you should have this on top:

    <!DOCTYPE html>
    <html>
      <head>
       <meta charset="UTF-8" />
        <link rel="stylesheet" href="style.css" />
     </head>

    You don't need the <style>...</style> tags if you already have an external stylesheet (<link rel="stylesheet" href="style.css" />).
     

  • saravanan   Aug. 20, 2012, 1:04 p.m.
  • saravanan   Aug. 26, 2012, 9:51 a.m.
    In Reply To:   saravanan   Aug. 20, 2012, 1:04 p.m.

    I posted the above link last week and when i check it today not found. Thanks to thimble.

    I would have used htmlpad. Only one page created using htmlpad exists other thimbles are not found........!!!!!!!!!!!

  • hamilton mena quejada   July 19, 2012, 2:47 p.m.

    Este es mi pequeño aporte sobre HTML 5 y CSS

     

    http://htmlpad.org/csshmq/

  • Giorgos Dimop   May 31, 2012, 1:35 p.m.
  • JoshM   May 14, 2012, 9:01 p.m.

    Really bad, but it's basic for you!

    http://htmlpad.org/htmlcssintrop2pu/

  • Pau Company   May 7, 2012, 12:01 p.m.
  • Nick   May 6, 2012, 8:57 a.m.

    Here's my god awful index page for the p2pu courses.

  • Vita   Dec. 5, 2011, 10:54 a.m.

    Well that was kinda fun... Played a bit with CSS and that's the result, check it out...

    http://dl.dropbox.com/u/18761433/HTML5Course/css.html

  • alcinoe   Dec. 5, 2011, 6:25 p.m.
    In Reply To:   Vita   Dec. 5, 2011, 10:54 a.m.

    Great job with the CSS, Vita! It put me into the holiday spirit, too. 

  • Vita   Dec. 6, 2011, 3:04 a.m.
    In Reply To:   alcinoe   Dec. 5, 2011, 6:25 p.m.

    Thank you Elizabeth, hope you'll have a great holiday..!!! smiley

  • alcinoe   Dec. 9, 2011, 2:31 a.m.
    In Reply To:   Vita   Dec. 6, 2011, 3:04 a.m.

    I hope that you have a wonderful holiday, too, Vita!

  • Pau Company   May 7, 2012, 12:06 p.m.
    In Reply To:   Vita   Dec. 5, 2011, 10:54 a.m.

    Hi Lisa:
    I pass the address of my exercise. I would like you to take a look. I looked at your code and from there, I've taught to use the CSS3 need to learn to work with rounded edges.Haberte recognize "stolen two of the colors of the boxes because I found excellent).Thank you. Sincerely,

    Pau Company

     

    http://paucompany.es/ejercicioshtml5/estilos.html

  • Stavros   Nov. 13, 2011, 1:47 p.m.

    CSS is a subject for a class of it's own. I found it dificult to follow. I haven't given up though still.

  • mosson   Nov. 10, 2011, 2:49 a.m.

     

    Hello everyone I made an analysis on the subject of Headings, and I share...
     
    The Font default is  16px in the document HTML
     
    and the result in the Headings: 
     
    H1 increases to 200% compared to the default amounted to 32px  (16px difference)
     
    H2 increases to 137.5% compared to the default amounted to 24px  (6px difference)
     
    H3 increases to 118.75% compared to the default amounted to 19px  (3px difference)
     
    H4 equal to 100% compared to the default is  16px  
     
    H5 decreases to 81.25% compared to the default now the value is  13px   (3px difference)
     
    H6 decreases to 68.75% compared to the default now the value is 11px   (5px difference)
  • Stavros   Nov. 13, 2011, 1:53 p.m.
    In Reply To:   mosson   Nov. 10, 2011, 2:49 a.m.

    Thanx, that was really helpful.

  • jenEliz   Oct. 18, 2011, 10:30 a.m.

    Alrighty... Here are my CSS updates!

    Added some updates to make the site more clean and legible... I know the background isn't the best but I'm working on it... Added some font colors and things!