Intermediate CSS



index
Disabled back button Next Section
printable version

Section 1: More Selector Forms

The universal, element, and class selectors were previously discussed.


There are other selector forms that allow greater specificity.

Combined Class and Element Selector

If you want to be more specific, you can use class and element selectors together.

For example,

p.narrow
{
   letter-spacing: -1px;
}
p.wide
{
   letter-spacing: 10px;
}

As with class selectors, a combined class is associated with a particular occurrence of an HTML tag by using the class attribute of the tag.

For example,

<p class = "narrow">Put narrow stuff here.</p>
<p class = "wide">Now for some wide stuff.</p>

Here is the output:

Put narrow stuff here.

Now for some wide stuff.

The above rules select only paragraphs (<p> tags) belonging to the "narrow" class or "wide" class.


id Selector

An id selector allows the application of a style to one specific element.

General form:

#specific-id {property-value list}

Example:

#section14
{
   font-size: 20px;
}

Your html tag (only one) must be linked to that id using the id attribute, like this:

<div id="section14"</p>
</div>

Difference between class and id:

/*
This is another id selector, which will apply only to
a special paragraph. It should be used ONE TIME ONLY
in your html! In the html simply add id="odd-paragraph",
as in <p id="odd-paragraph">Halloween</p>
*/
p#odd-paragraph
{
color: orange;
font-size: 12pt;
font-weight: bold;
background-color: black;
display:inline; /* makes background only the width of the text */
}


Contextual Selector

These are also referred to as descendant selectors.

The context of a selector is determined by what its parent element is.

  • In other words, what the element is nested (or contained) within in the document.

For instance, if you wanted special formatting for an anchor tag inside a list item, you could write:

li a
{
   font-family:Calibri;
   color:red;
}


Pseudo Classes

Pseudo classes are styles that apply when something happens, rather than because the target element simply exists.

  • Names begin with colons.
  • "hover" classes apply when the mouse cursor is over the element.
  • "focus" classes apply when an element has focus.
  • a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective.
  • a:active MUST come after a:hover in the CSS definition in order to be effective

<!-- pseudo.html -->
<!DOCTYPE html>
<html>
<head>
<title>Pseudo Classes</title>
<style>
a:link {color:red} /* unvisited link */
a:visited {color:green} /* visited link */
a:hover {color:fuchsia} /* mouse over link */
a:active {color:blue} /* selected link */
</style>
</head>
<body>
<p>
<a href="http://www.isu.edu" target="_blank">ISU</a>
</p>
<body>
<html>

Pseudo classes only work reliably with anchor tags, although some browsers allow pseudo classes to be used with form fields, etc., as in the example below.

<!-- pseudo.html -->
<!DOCTYPEhtml>
<html>
<head>
<title>Pseudo Classes 2</title>
<style>
input:hover { color: #ffffff; background-color: #ff0000; }
</style>
</head>
<body>
<form method="get" action="http://www.google.com/">
<input type="submit" value="click me" />
</form>
<body>
<html>

Read more at tizag's CSS Tutorial - Pseudo Class & Mouseover


There are even more complex selector forms to be seen in The 30 CSS Selectors You Must Memorize.

Section 2: Order Applied

Browser styles:


User styles:


Author styles:


We explained the levels of style sheets earlier.


The cascade comes into play when rules conflict (like between Author styles and User styles or between an external style sheet and an inline style sheet), the more specific style takes precedence – inline styles will win over document-level and external styles; document-level styles will win over external styles.

In effect, the browser searches for a style property specification starting with inline, then document-level (embedded), then external until it finds that property (or there isn't a property).

If you want a rule in your external style sheet to win over document-level or inline style sheets, you can use the !important statement in the rule. For example, if you include the following in your external style sheet:

p { background-color: white !important; }

...and an inline style in your document, as below:

<p style="background-color:yellow">

Then the inline style would NOT override the paragraph style in the external style, because it included the !important statement. Thus, the paragraph will have a white background. (More in !important rules.)

If multiple external style sheets contain styles for the same tag, the sheet linked last will take precedence.

In other words rules cascade down from the top to the bottom, but browsers try to match rules from the bottom to the top:

Cascading effect
Section 3: More on Fonts

text-shadow

text-shadow is a font property that adds a shadow to each letter

@font-face

Downloaded fonts eliminate the need to depend on the limited number of fonts users have installed on their computers, and instead allows the browser viewing the web page to download a particular font from a server to render the page if the user has not got that font installed.

The @font-face rule allows a page to be linked to fonts that are automatically fetched and activated when needed. This allows authors to select a font that closely matches the design goals for a given page rather than limiting the font choice to a set of fonts available on a given platform.

To use web fonts, each form of the font family must be declared using the @font-face rule.

For more on @font-face, visit Google web fonts.

Section 4: Display Styles

Block-level vs. Inline

In previous versions of HTML, most elements were divided into two broad categories: block-level and inline.

The display style can now be specified by using the display property in CSS, which determines an element's formatting on the rendered page.

Graphical web browsers have their own built-in style sheets that dictate how various HTML elements display by default, including whether they should be treated as block-level or inline.

Understanding HTML5 Content Models

Video explanation.

Section 5: Float and Alignment

Float Property

Left Align and Right Align Text on the Same Line

You can apply float:left and float:right to headings or paragraphs to position them on opposite sides of the page.

Section 6: Box Model

Each element in an HTML page defines a rectangular region that the browser uses to place the element in the browser window.

CSS has a collection of properties that describe various aspects of the formatting for the rectangle around each element that together make up what is referred to as the W3C's CSS box model.

The following diagram shows the properties that make up the box model and how they relate to each other:

CSS Box Model.

Here is a good explanation of Managing the CSS Box Model.

Turble!

When specifying borders, margins, or padding four values can be declared at once by either specifying two or four values.

Borders


Margins


Padding


Background

The background properties include color, image, repeat, position, and attachment and apply to various elements (body, paragraph, table, etc.) See CSS Backgrounds at the W3Schools.

Section 7: Hiding Elements

There are two main approaches for hiding an element in CSS:

The seconde approach leaves a gap in your page, so the first approach is more common.


display: none;

If you don’t want an element to display at all, you can set the value of the display property to none.

For example, to hide an entire portion of a web page, enclose it in a <div> tag and set the display property to none, as in the following example:

<!-- Hide it -->
<div style="display:none;">
<p>Stuff we want to hide.</p>
<p>More stuff we want to hide.</p>
</div>

To display the hidden portion, set the display property to block (or inline if the hidden element is an inline element like span):

<!-- Show it -->
<div style="display:block;">
<p>Stuff we want to hide and then show.</p>
<p>More stuff we want to hide and then show.</p>
</div>


visibility: visible;

The visibility property specifies whether or not an element is visible, but those hidden elements take up space on the page.

<!-- Hide it -->
<div style="visibility: hidden;">
<p>Stuff we want to hide and then show.</p>
<p>More stuff we want to hide and then show.</p>
</div>

<!-- Show it -->
<div style="visibility: visible;">
<p>Stuff we want to hide.</p>
<p>More stuff we want to hide.</p>
</div>

Section 8: Let's Play

Click the "Edit and Click Me >>" to see the results of the html code in the left panel in the panel on the right.

Then, play around with the html code, making changes and seeing the effect of those changes in the right panel.