FANDOM


Examples in Each Chapter Edit

This CSS tutorial contains hundreds of CSS examples.

CSS Example Edit

body {     background-color: #d0e4fe; }

h1 {     color: orange;     text-align: center; }

p {     font-family: "Times New Roman";     font-size: 20px; }

CSS Comments Edit

Comments are used to explain your code, and may help you when you edit the source code at a later date. Comments are ignored by browsers.

A CSS comment starts with /* and ends with */. Comments can also span multiple lines:

Example Edit

p {     color: red;     /* This is a single-line comment */     text-align: center; }

/* This is a multi-line comment */

CSS Selectors Edit

CSS selectors allow you to select and manipulate HTML elements.

CSS selectors are used to "find" (or select) HTML elements based on their id, class, type, attribute, and more.

The element Selector Edit

The element selector selects elements based on the element name.

You can select all <p> elements on a page like this: (all <p> elements will be center-aligned, with a red text color)

Example Edit

p {     text-align: center;     color: red; }

The id Selector Edit

The id selector uses the id attribute of an HTML element to select a specific element.

An id should be unique within a page, so the id selector is used if you want to select a single, unique element.

To select an element with a specific id, write a hash character, followed by the id of the element.

The style rule below will be applied to the HTML element with id="para1":

Example Edit

#para1 {     text-align: center;     color: red; }

The class Selector Edit

The class selector selects elements with a specific class attribute.

To select elements with a specific class, write a period character, followed by the name of the class:

In the example below, all HTML elements with class="center" will be center-aligned:

Example Edit

.center {     text-align: center;     color: red; }

You can also specify that only specific HTML elements should be affected by a class.

In the example below, all <p> elements with class="center" will be center-aligned:

Example Edit

p.center {     text-align: center;     color: red; }

Grouping Selectors Edit

If you have elements with the same style definitions, like this:

h1 {     text-align: center;     color: red; }

h2 {     text-align: center;     color: red; }

p {     text-align: center;     color: red; }

you can group the selectors, to minimize the code.

To group selectors, separate each selector with a comma.

In the example below we have grouped the selectors from the code above:

Example Edit

h1, h2, p {     text-align: center;     color: red; }

CSS Background Edit

CSS background properties are used to define the background effects of an element.

CSS properties used for background effects:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position

Background Color Edit

The background-color property specifies the background color of an element.

The background color of a page is set like this:

more css examples

Example Edit

body {     background-color: #b0c4de; }

With CSS, a color is most often specified by: Edit

  • a HEX value - like "#ff0000"
  • an RGB value - like "rgb(255,0,0)"
  • a color name - like "red"

Example Edit

h1 {     background-color: #6495ed; }

p {     background-color: #e0ffff; }

div {     background-color: #b0c4de; }

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.