Difference between revisions of "CSS/Style basics"

From virtualMV2015wiki
Jump to: navigation, search
(Media attribute)
(No difference)

Revision as of 01:27, 5 March 2013

 Home  CSS <


Styles are an important technique used to separate content and formatting (style). Styles can be added directly to a tag, inserted in a section at the top of the web page or included in a linked file.

Icon Objectives.png

Objectives :Using styles in a web page

By the end of this page you will be able to:

  • Describe how CSS can be implemented in a web page:
    • Embedded into a tag (and the use of the span tag)
    • Embedded into a style section in the head tag
    • Included in a Linked Style sheet

Embedded into a tag

A style can also be embedded into the actual tag, for example:

 <p style="color: #00FF00">Paragraph one in green</p>

Will display the paragraph in green

Paragraph one in green

Span tag

Span is a generic inline container, and allows a style to be attached to specific words in text . Attributes allowed are id (replaces name from html 3.x), class, title, style, lang.

In this example the style changes the colour of the word world to red. The title tag gives a tool tip when the mouse hovers over the word world.

 <p>Hello <span style="{color:red}" title = "global thing" >world</span></p>

Hello world

This is better handled by using an embeded style separated from the tag.

   <title>**’s first page</title>
     .tooltip {color:red}
   <h1>**’s web</h1> 
   <p>Hello <span class="tooltip" title = "global thing" >world</span></p>
   <h2>Favourite quotes</h2> 
   <p>Live long and prosper

Note that the .tooltip can be assigned to all tags

Embedded style sheet

The style sheet is placed between the <style> and </style> tags placed after the <head> tag.

 <title>Style sheet example</title>
   body {background:darkblue; color:yellow;}
   h1{background:black; border 3px red;}

This style sheet defines the pages background and colour (body), as well as bold (b) and heading 1 (h1).

Linked Style sheet

 <title>Style sheet example</title>
 <link rel="stylesheet" type="text/css" href="css/page.css">

This example assumes that page.css is kept in the css directory.


The CSS syntax is made up of three parts: a selector, a property and a value:

 selector {property: value}
  • The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon and surrounded by curly braces:
 body {color: black}
  • If the value is multiple words, put quotes around the value:
 p {font-family: "sans serif"}
  • Note: If you wish to specify more than one property, each property is separated with a semi-colon. The example below shows how to define a center aligned paragraph, with a red text color:
 p {text-align:center;color:red}


  • You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. Each header element will be green:
 h1,h2,h3,h4,h5,h6 {color: green}

Contextual selectors

Contextual selectors are strings of two or more simple selectors separated by a space. These selectors can be assigned normal properties and, due to the rules of cascading order, they will take precedence over simple selectors. For example, the contextual selector in:

  p strong { background: yellow }

is p strong. This rule says that strongly emphasized text within a paragraph should have a yellow background; strong text in a heading would be unaffected.


  • With the class selector you can define different styles for the same type of HTML element.
 p.right {text-align: right}
 p.center {text-align: center}
  • You have to use the class attribute in your HTML document:
 <p class="right">This paragraph will be right-aligned.</p>
 <p class="center">This paragraph will be center-aligned.</p>
  • Note: Only one class attribute can be specified per HTML element! The example below is wrong:
 <p class="right" class="center">This is a paragraph.</p>
  • You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class.
  • In the example below, all HTML elements with class="center" will be center-aligned:
 .center {text-align: center}
  • In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector:
 <h1 class="center">This heading will be center-aligned</h1>
 <p class="center">This paragraph will also be center-aligned.</p>

FAQ Example

If you want to simplify the coding on an FAQ page and change the formatting depending on whether you are displaying a question or answer you could use a style definition as follows:

  p.question {color: green; font-style: italic}
   .answer  {color: red}
And your FAQ using the defined classes would be as follows:

<p class="question">What is HTML?</p>
<p class="answer">HTML is a markup language for Web pages.</p>
<p class="question">How do we build a Web Site?</p>
<p class="answer">Looks like we have some things to do!!</p>

ID selector #

The ID selector only applies to ONE element on a page.

Icon Note.png


It is recommended that the id selector is NOT used in CSS but is necessary in JavaScript to identify a particular object in a Web Document.

For example

div#green {color: red}

Will match

<div id="green">Some text</div>

But not

<h2 id="green">Some text</h2>


*#green {color: red}

will match the first element with the id value of "green"

Class and ID selector differences

  • # is an id selector, used to target a single specific element with a unique id, but
  • . is a class selector used to target multiple elements with a particular class.
  • #foo {} will style the single element declared with an attribute id="foo"
  • .foo {} will style all elements with an attribute class="foo" (you can have multiple classes assigned to an element too, just separate them with spaces, e.g. class="foo bar")

In CSS what is the difference between “.” and “#” when declaring a set of styles? (2011)[1]

Internal Priorities

  1. element
  2. .class
  3. #id

Comments in CSS

  • You can insert comments in CSS to explain your code, which can help you when you edit the source code at a later date. A comment will be ignored by the browser. A CSS comment begins with "/*", and ends with "*/", like this:
 /* This is a comment */
 p{text-align: center;
 /* This is another comment */
 color: black;font-family: arial}

Sample css file linked to an html page

This is an example called neon.css (from Frontpage)

 a:link    { color: rgb(204,255,0); }
 a:visited { color: rgb(102,204,0); } 
 a:active  { color: rgb(255,255,0);}
 body   {font-family: Verdana, Arial, Helvetica;
         background-color: rgb(0,0,0);
         color: rgb(102,255,51);
 table  {table-border-color-light: rgb(255,255,153);
         table-border-color-dark: rgb(255,255,51); 
 h1, h2, h3, h4, h5, h6
        { font-family: Verdana, Arial, Helvetica;
 h1     {color: rgb(204,255,0);}
 h2     {color: rgb(153,504,255);}
 h3     {color: rgb(255,51,153);}
 h4     {color: rgb(255,51,153);}
 h5     {color: rgb(153,504,255);}
 h6     {color: rgb(204,255,0);}

To include this in an html file

<title>HTML/CSS example</title>
 <link rel="stylesheet" type="text/css" href="neon.css" />
<h1>Heading one</h1>
<p>If you are creating this html file<br />
try to create a page that tests out all of the styles in neon.css</p>


Add comments here (use -~~~~~)

Icon References.png References

  1. In CSS what is the difference between “.” and “#” when declaring a set of styles? (2011) In stack overflow internet services wiki. Retrieved March 1, 2011 from http://stackoverflow.com/questions/602168/in-css-what-is-the-difference-between-and-when-declaring-a-set-of-styles

CSS/Style basics. (2020). In virtualMV's ( Michael Verhaart ) wiki. Retrieved August 7, 2020, from http://www.virtualmv.com/wiki/index.php?title=CSS/Style_basics    (zotero)