03 The Mechanics of CSS


CSS, or cascading style sheets, describes the presentation of web pages, including colors, layout, and fonts. It does this through properties that style HTML elements.

Think form and content. CSS is the form, HTML the content.

CSS is separate from the content and is able to adapt the presentation of the content to different types of devices such as large screens, small screens, or printers.

The separation of CSS from HTML makes it easier to maintain sites, share style sheets across pages, and tailor pages to different environments.

CSS developed along side of HTML. Read its history here (by the creators of CSS).

CSS is deceptively easy to apply when it is done inline. That means you write the code right into the HTMl tags using the style attribute. You write these locally applied rules like this: style="inline CSS rules go here". The addition of the code style="color: red" to the paragraph tag <p > looks like <p style="color: red">, and colors the following paragraph red.

Changes are seen as soon as you update the browser. This immediacy and the simplicity of the syntax makes CSS appear easy, which it is when there is no cascade or style sheets to worry about.

It is possible to write all of your presentation in the tags themselves but you would find that to be highly inefficient. It is generally frowned upon to write CSS locally and defeats the purpose of the cascade.

What is CSS?

CSS gets its power from inheritance, the style sheets and the cascade. It is called CSS, or cascading style sheets. With that power comes the responsibility of keeping track of all the selectors that target the html element, and when there are many overlapping rules it can become difficult.

Not all properties are inheritable but those that are can be set in the <html> or <body> element, and every child element that is inside of these containing or parent elements, will inherit these properties.

The cascade describes how the CSS rules in the style sheet are applied globally. The CSS styles are a list of rules prefaced by one or more selectors. These rules can be placed in the header of the page or can be in an external style sheet, a blank text file saved with the .css extensiont, in which case, a link to that style sheet is placed in the header of the page.

Selectors provide the link between the rules in the style sheet and the HTML elements that receive the rule. The browser parses the document, figures out every element in the document tree, heals any errors it may find, and tracks all of the selectors used by the style sheet to target the different tags so that it can assign the rules of properties and their values to each element, so that the page ends up looking the way it does.

Inheritance

Every element is a child of the <html> element. If you target the <html> , or the <body> element, many properties will be inherited but not all.

For example, a child element inherits the font property of its parent but it will not inherit the background property. Other automatically inherited properties are: color, font (and related properties), letter-spacing, line-height, list-style (and related properties), text-align, text-indent, text-transform, visibility, white-space, and word-spacing.

Properties not automatically inherited are: background (and related properties), border (and related properties), display, float and clear, height and width, margin (and related properties), min- and max-height and -width, outline, overflow, padding (and related properties), position (and related properties), text-decoration, vertical-align, z-index. There are more but this gives you an idea.

Inheritance prevents certain properties from having to be declared over and over again in a style sheet allowing the developers to write less CSS and the users to view faster-loading pages.

Inheritance plays a large roll in CSS but it’s not overtly visible. Be aware that it’s constantly determining what the elements on the page look like. In the cascade, inherited properties lose out to any properties provided by a selector.

CSS Selectors

Central to this process is the selectors that connect the CSS properties to the element when the properties are no longer written in the element itself.

There are many kinds of selectors, and W3C has a whole list of them, I have a reduced set of them below. It is best to keep things simple and start with the type selector.

You can select all paragraphs in a document by using the type selector, like this: p {css property: value;}. It is possible to limit the scope and target only the paragraphs in the header for example. This is done by placing header before the p like this header p {css property: value;}.

An interactive demonstration can be of great help when conveying this information. What follows is the first of many such interactive demos.

Interactive CSS Selectors Demo

The following demonstration selects all of the text contained by <p> in this document. You can change the font, the type color and the type size (or anything else if you write in the rule).

Words inside of /* */ are comments. They are there to help you come up with values that match the properties. For example, /* green, #abc, hsla(35,100%,50%,1) */ are suggestions to replace black with green, blue grey or orange in color: black. You will see the content surrounded by <p> tags go to the default color black while typing in the new value. When CSS does not recognize a value it ignores it and applies the default value.

Your changes could render the document unreadable. To reset the document, refresh the page. The selector <p> selects all paragraph elements.

If you find something you like (this concerns other demos more than this one), copy the declaration and use it in your project.

CSS Code View

If you colored the text green, you will notice that the paragraph above that was originally colored red is still colored red. Why did the color not change? The same is true of the font-family.

We will explore this in much greater detail but know that the cascade determines which rule wins out over all the other rules that target the element.

You can overpower the cascade order by using !important, which is inserted right after the value and before the semicolon. Try inserting green !important in the demo, and you will see that the global CSS rules now override the local inline rules in the cascade. The red type in the introduction will be green.

The !important quickly destroys the cascade and quickly makes the CSS unusable, so it is not recommended unless you have no other options. I use it mostly for testing purposes, to check on whether I’m actually targeting an element or not in WordPress template that I did not create.

Specificity and Grouping Selectors

It’s not always desirable to select all the paragraphs in the document. We usually want to select very specific paragraphs, or elements, in the document. This paragraph or that paragraph but not all of them. This is achieved including one or more containing or parent elements, each separated by a space.

Selectors move up the DOM tree to achieve greater specificity. In the following demonstration, each line is targeted individually by nesting the selectors. Use a space “ ” between selectors to specifically select the child element inside of the parent element:

Selectors can also be grouped to share the same rule. Use a comma “ ,” between the selectors to include all of the selectors to which the rule will be applied. This is the case with the last two selector in this demonstration:

CSS Code View

Header in div

Paragraph in div

  1. Item 1 of list in div
  2. Item 2 of list in div

The blue div has the 120% applied twice, and is larger than all other text in this demonstration. Another example of the cascade at work. Play around with the demo to see how specificity is able to narrow the targeted section to exactly the element that you want to style.

The HTML code for this example is:

<div id="example0">
	<h2>Header in <span>div</span></h2>
	<p>Paragraph in <span>div</span>
	<ol>
		<li>Item 1 of list in <span>div</span>
		<li>Item 2 of list in <span>div</span>
	</ol>
</div>


CSS Rule Placement

There are three places where to put style definitions. CSS styles can be written in the tag itself, called inline styles, in the head of the document, called embedded styles and in an external style sheet.

Inline Style definitions

You can write the style directly in the tag itself. The attribute style has as its value whatever properties are to effect the HTML element style="color: red".

 <p style="color: red">

Embedded Style definitions

The CSS can be put in the <head> of the document between <style> and </style> tags. Using a p selector it’s possible to target all the paragraphs in the document with the embedded style.

<style type="text/css"> 
p { color: red}
</style>

The declarations are now enclosed in brackets and is called the declaration box, and in front of the bracket is the selector that targets which element is to be affected by the rule. The rule can be applied to many elements. Many rules can target the same elements and that increases the complexity. Different selectors with conflicting rules can target the same element. Which rule will win out to style the element? That is the question.

This is were it can get difficult as several rules can target the same element. When that happens, the browser has to select which rule wins out in what’s called the cascade.

Embedding the styles is more efficient than writing them in each paragraph element but it’s still not ideal, since the rules can only targets the HTML elements in the page. Websites almost always consist of multiple pages.

External Style Sheets

The most efficient place for styles is in an external style sheet. The external style sheet is a simple text file saved with a .css extension with nothing but CSS style rules on it. They can be written in any way, as spaces, returns and tabs are ignored by the browser. Start out by separating all of the properties in their own line, as in the following example, so that they are easy to read:

p {
	color: red; 
	background: white; 
	width: 400px; 
	padding: 20px; 
	border: 2px solid green; 
	margin: 20px auto;
}

It’s also possible to write the declarations in one line:

p {color: red; background: white; width: 400px; padding: 20px; border: 2px solid green; margin: 20px auto;}

There is no difference, as all the white space is collapsed. I tend to write all of my declarations in one line as that reduces scrolling in long style sheets. Once I finish I sometimes clean the styles so each declaration is on its own line.

The file is saved with a .css extension, often in its own folder called CSS, in case there are more than one external or imported style sheets. Note that it is required to exit the folder when linking to an external resource like a picture: use ../ in front of the resource to get out of the css folder.

Each HTML page in the site refers to that style sheet by linking to it by including the following code in the header of each document:

<link rel="stylesheet" href="css/styles.css" media="all">

You can copy this into your pages, though it is already included it in the HTML5 template.

It’s possible to link to or import several style sheets and some think it’s a good organizing principle to break the styles into several style sheets like the CSS reset, typography, navigation, layout or alternate styles for depreciated browsers.

The downside to all this organization is that each stylesheet requires a server request. That is, your browser has to ask the server for one more file to complete the page. The lag time involved in asking the server for each style sheet can take more time than the actual loading of the stylesheet itself. Because of this a more common approach is to put all the styles into a single style sheet.


Browser Style Sheets

There are three style sheets.

User Agent styles are the default style sheet the browser uses, which are modifiable by an end user.

Author Styles are the styles that you create when you design the page.

User Styles style information specified by the user apart from the default style sheet.


Browser’s Default Style Sheet

Even if you do not write any CSS, the HTML has formatting applied, so that an <h1> tag looks different from an <h2> or a <p> tag. Where do these styles come from?

Every browser has a default style sheet, called the user agent style sheet, like this one from Safari that formats the markup.

If you look at Safari’s default style sheet, you will see that it determines how the p element is to be displayed:

p {
    display: block;
    -webkit-margin-before: 1__qem;
    -webkit-margin-after: 1__qem;
    -webkit-margin-start: 0;
    -webkit-margin-end: 0;
}

A paragraph not yet styled looks as specified by the browser style sheet. If you start styling the elements with your own styles in an author style sheet, they are built on top of the styles specified in the user agent style sheet. That may lead to problems when the page is looked at in a different browser. To solve that problem a CSS reset is used.

Resetting Browser Default Style

To counteract the possibility that your author styles change with different browsers, you need to neutralize the default user agent style sheet the browser uses.

To counter this we implement a CSS reset. The CSS reset was initiated by Eric Meyer, and he recently released a second version. Take the following code and place it at the beginning of your style sheet, and it will neutralize the default styles of the browser style sheet.

/* Begin CSS Reset */
/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}
/* End CSS Reset */

A more nuanced CSS reset is accomplished by normalize.

Last Style Definition Wins

There is a pattern here. The styles in the author style sheet override the styles in the user agent style sheet. In the order in which CSS rules are compiled, subsequent declarations override previous ones.

If you repeat the same property twice, the second one will style the element. {color: green; color: red} The type will be colored red, not green.

This is also true of the three places where you can place the rules. If the same rule is found in the user agent style sheet, the author’s style sheet and any other imported external style sheets, the embedded or internal style sheet and repeated once again inline, that is, in the element itself, guess which rule wins out?

The inline rule wins out. The rule of thumb here is that the closer the style definition is to the element being styled, the more likely it is to triumph over the other rules and style the element. The insight we are given is that the order in which the browser concatenates the various style sheets is as given above, with the default style sheet first, then external style sheets, then the internal style sheet and finally it arrives at the elements themselves.

That the last style definition wins is part of the cascade:


The Cascade

CSS stands for cascading style sheets. The cascade is the ordering of selectors that determines which selector wins out over to style the element. In writing CSS you intentionally determine the cascade by how each selector targets their element(s).

CSS rules can be written in a number of places. In each place it is possible to have many declarations targeting the same HTML element. It is, therefore likely that element are targeted by several rules at once — from inline rules to embedded rules to external rules, to user stylesheets. When there are several rules targeting the same property with different values, only one of these rules wins out to determine the presentation of the HTML element. Figuring out which rule ends up styling the element is important.That is why it is necessary to figure out how the cascade works.

For example, <span style="color: red">red</span> this text’s inherited default color of black is overridden by the inline style that colors the type red. That tells you that the inline style trumps the inherited style.

The browser needs to figure out which rule to use when there are multiple conflicting values for a property. The cascade refers to the selection process in which these styles are evaluated so that only one value ends up styling the element.

Generally speaking, the closer a style definition is to the tag, the more likely it wins out. The inline style wins out over the embedded style, the embedded style trumps a linked or imported style sheet which in turn wins over the browser’s default style sheet, and they all trump inherited styles.

The cascade takes the selector’s specificity into its calculation. One selector ends trumping all the other selectors. There is a formula that determines the cascade. It prioritizes ids over classes, for example. Classes win out over structural tags and structural tags trump typed tags.

The new CSS transition and animation take top spots.

You can pull out the big gun and place !important after a rule if you absolutely want it to be the case no matter what.

Be judicious in your use of classes, ids, and especially !important, as you can easily make a document unmanageable. The rule of thumb is to use as low a cascade value as possible to select an element.

A fun interactive explanation of how the CSS cascade works It explains the cascade hierarchy in importance, origin, specificity, and position, and quizzes you on each of these points. If in doubt, take the quiz.


Cascade Layers

Cascade Layers are designed to ease CSS development on large projects. Cascade Layers provides a powerful way to organize styles into layers, where specificity is calculated independently inside each layer.

picture describes cascade layers

/* establish the layer order, so the "override" layer takes precedence */
@layer framework, override;

@layer override {
  @keyframes slide-left {
    from { translate: 0; }
    to { translate: -100% 0; }
  }
}

@layer framework {
  @keyframes slide-left {
    from { margin-left: 0; }
    to { margin-left: -100%; }
  }
}

.sidebar { animation: slide-left 300ms; }

In this case the override layer has a higher cascade priority than the framework layer, so slide-left will animate using the translate property rather than margin-left.

Cascade Layers are for advanced websites and are outside the scope of this class.


The Elements of a CSS Rule

CSS has a simple syntax and uses a number of English keywords to specify the names of various style properties and values. That makes it easy to use. A style sheet consists of a list of rules. Each rule or rule-set consists of one or more selectors and a declaration block. A declaration-block consists of a list of declarations in braces. Each declaration itself consists of a property, a colon :, and a value. If there are multiple declarations in a block, a semi-colon ; must be inserted to separate each declaration. The rule-set is applied to the target element by a selector. The combination of a selector with a rule-set completes the CSS statement.

The CSS Rule

The CSS rule contains both the selectors and the declarations.

Example: h1 {font-size:1.5em; font-weight: 700; color: #333; }

The Declaration

The declaration is a property-value pair and is part of the rule that describes how the selector will style the element. When the style sheet is embedded in the document, convention is to follow each declaration inline:
Example: h1 { font-size:1.5em; font-weight: 700; color: #333;}

While in an external style sheet, convention is the put each delectation on a new line with a tab in front:
Example: h1{
    font-size:1.5em;
    font-weight: 700;
    color: #333;
}

With the advent of @media queries in responsive design my preference is to write all CSS rules inline to minimize scrolling.

The Declaration Box

Declarations determines how the selector will style the element. it’s always bookended by curly braces { }.

Example: h1 { font-size:1.5em; font-weight: 700; color: #333;}

Properties

A property describes an aspect of an elements’s presentation such as color, font-size or border-width. The property is always followed by a value and the two are always separated by a colon:. There are a lot of properties, so it’s best to have a place where you can look them up, though repetition will acquaint you with the ones you need.

Example: h1 {color: #356590; padding: 3px 16px;}

Values

A value describes a specific appearance, such as a specific width, the name of a color, or some set value, such as for text-decoration: none; which removes the underline from text, often used to remove the underline that the browser’s default stylesheet gives a link. The value commences the declaration statement, and you signify that to the computer by using a semicolon ;. While it’s not necessary to tell the browser that there are no more declarations in the declaration box, it’s a good practice to close the last declaration as well.

Example: h1 {color:#356590; padding: 3px 16px;}

CSS Selectors

Selectors are patterns that match the elements of a document that will then have the declarations applied to them.

The easiest selector to understand is the type selector, in which p selects the paragraph element: <p>.

Selector Hierarchy

There are six different types of selectors,

  1. type
  2. universal
  3. attribute
  4. pseudo class
  5. class
  6. ID

The type selector selects the element by the tag itself:

Type: p { }

Use the type selector whenever possible. Use other selectors when the type selector cannot target an element.

The universal selector selects all elements:

Universal: * { }

The scope of the universal selector can be restricted through specificity.

Universal: nav * { }

The universal selector only selects all elements inside the nav element.

The attribute selector selects elements on the basis of their attributes. Example of attribute selector selecting all image tags with an attribute of “alt”:

Attribute: img[alt] { }

Example of attribute selector selecting all links to pdf files.

Attribute: a[href$=”.pdf”] {color: #45a2f4;}

The pseudo class selects different states of an element, most often used to target the different states of a link like hover.

Pseudo Class: a:hover { }

The class selector selects all elements belonging to the same class on a page. Example of a class selector that selects any element with a class=”example”:

Class: .example { }

Example of a pseudo selector that selects any link and creates a hover class:

The ID selector selects an ID attribute. The value of each ID should be unique, assigned to only one element per page. Example of a ID selector that selects any element with an id=”container”:

ID: #container { }

Selecting, Specificity and Grouping Selectors

Use a space   to separate nested elements. body header nav ul li a would target the link in the nav of the header. Easier to use only the required elements header nav a. That would get all links in the nav contained in the header. If there were no other navs in the document, you could get alway with nav a

To make selecting elements more precise:

  • selectors can be combined by using a comma ,
  • a greater than sign > for limiting that combination to a child of the selected parent
  • a plus sign + for selecting the following sibling, meaning the directly adjacent element
  • and the tilde ~ for selecting indirectly adjacent selectors. For example, #french~p selects all the ps that are adjacent selectors of the element that has id="french".

Space: article p { } selects all <p> elements in <article>.

Greater Than: article>p { } selects only the <p> elements that are children of the <article>.

Adjacent: article+p { } selects only the <p> elements adjacent to the <article>.

Tilde: article~p { } selects all indirectly adjacent <p> elements to the <article>.

Grouping Selectors:

When several selectors share the same declarations, they can be grouped into a comma-separated , list:

example: h1, p, img, nav { }

Pseudo Class Selectors

Pseudo classes are based on the structure of the document rather than attributes or things found in the document.

A pseudo-class selector acts as if you have added a class to an element in your markup. It defines a special state of an element. It can be used to style an element when a user mouses over it, style visited and unvisited links and style an element when it gets focus.

Pseudo-class selectors make it possible to select the first, last or alternate lines to alternate the color in a set of links, for example:
First Child: tr:first-child { }
Last Child: tr:last-child { }
Nth Child: p:nth-child(2) { }
Nth Child: p:nth-child(odd) { }
Nth Child: p:nth-child(even) { }

Pseudo Classes

Pseudo classes create a class of an element that already exists but where there is no predetermined class. You can select a link, the anchor tag but there is no class to select the hover state of that link. This pseudo-class selector creates a pseudo class that can be styled as the link’s hover state. Many of these pseudo classes are structural, meaning that they use information that lies in the document tree to select different elements.

here is a list of pseudo classes:

:nth-child(N)

Matches elements on the basis of their positions within a parent element’s list of child elements (N here stands for the number of the element but it can also support odd and even, as in li:nth-child(odd)).

:nth-last-child(N)

Matches elements on the basis of their positions within a parent element’s list of child elements.

:nth-of-type(N)

Matches elements on the basis of their positions within a parent element’s list of child elements of the same type. CodePen

:nth-last-of-type(N)

Matches elements on the basis of their positions within a parent element’s list of child elements of the same type.

:last-child

Matches an element that’s the last child element of its parent element.

:first-of-type

Matches the first child element of the specified element type.

:last-of-type

Matches the last child element of the specified element type. CodePen

:only-child

Matches an element if it’s the only child element of its parent.

:only-of-type

Matches an element that’s the only child element of its type.CodePen

:empty

Matches elements that have no children. CodePen

:target

Matches an element that’s the target of a fragment identifier in the document’s URI.

:checked Pseudo-class

Matches elements like checkboxes or radio buttons that are checked.

:not(S)

Matches elements that aren’t matched by the specified selector. It is useful to exclude elements from a particular selection.


Pseudo Classes: not

Sophisticated example of the pseudo class not.A collapsable guide to the layout modes. All of the work is being done by the last CSS rule using the pseudo class not, as in not target or hover li:not(:target):not(:hover) .

CSS Code View

Live Demo

Here you will find links to the layout modes article, including the document flow, the positioning of the elements within the document flow and display, z-index and overflow properties.


Ids and Classes

HTML uses the Id and Class attributes for general naming of the elements. This is used by Javascript as well as CSS to select these elements.

In the markup the id uses a hash mark # before the name #this in the style sheet and the HTML code looks like <div id="this">, while the class uses a period . before the name .that in the style sheet and the HTML code looks like <div class="that">.

An id is to be assigned only once per page, while classes can be assigned multiple times per page. That difference gives ids more weight than classes when it comes to the CSS cascade.

Ids and Classes so obviously connect the rule with the element that they were over-used in the past when other selectors could have worked just as well. If it were a screaming contest, it was an outright cacophony. That particular affliction, along with the overuse of the generic div element, is called divitis and classitis. It is best to be as quiet as possible, and not jump to using classes and ids unless necessary.


Pseudo Elements

CSS can create markup where there is none. These are called Pseudo Elements, and you can style them as if they existed.

For Example, if you wanted to style the first letter of a paragraph, you can target it even though there is no markup. There are four pseudo elements all together: ::first-letter, ::first-line, ::before and ::after. All of them are written using another selector, two colons :: and the pseudo element, as in p::first-letter {} (The double colon is new, so a single colon is also accepted). Some use cases for before and after.

03 Applying CSS


There are many CSS properties. You do not have to memorize them all. You will remember the ones you use most often, and the other ones you can always look up. I’ve separated many of the properties into a number of interactive demos so that you can try them out. Spend some time in this sandbox, change the values of the properties or add new properties. This is where you can play and learn while it is still fun.

Color

Color is always RGB. That is how computers work. These are additive colors, you can call them by name or RGB value, both by percentages, decimal number or hexadecimal shorthand (Hexadecimal is base 16, or 1,2,3,4,5,6,7,8,9,a,b,c,d,e,f ), which is the most common form, introduced by a # hash mark, and allows each value (red, green and blue) to be represented by two Hexadecimal values, like #000000 is black or #ff0000 is red, #00ff00 is green and #0000ff is blue. Can you guess what the value of white is? If you guessed #000000, you are correct!

Here are the names of the colors, with their Hexadecimal and decimal numbers as an example:

Color Color name Hex rgb Decimal

    aliceblue #f0f8ff 240,248,255

    antiquewhite #faebd7 250,235,215

    aqua #00ffff 0,255,255

    aquamarine #7fffd4 127,255,212

    azure #f0ffff 240,255,255

    beige #f5f5dc 245,245,220

    bisque #ffe4c4 255,228,196

    black #000000 0,0,0

    blanchedalmond #ffebcd 255,235,205

    blue #0000ff 0,0,255

    blueviolet #8a2be2 138,43,226

    brown #a52a2a 165,42,42

    burlywood #deb887 222,184,135

    cadetblue #5f9ea0 95,158,160

    chartreuse #7fff00 127,255,0

    chocolate #d2691e 210,105,30

    coral #ff7f50 255,127,80

    cornflowerblue #6495ed 100,149,237

    cornsilk #fff8dc 255,248,220

    crimson #dc143c 220,20,60

    cyan #00ffff 0,255,255

    darkblue #00008b 0,0,139

    darkcyan #008b8b 0,139,139

    darkgoldenrod #b8860b 184,134,11

    darkgray #a9a9a9 169,169,169

    darkgreen #006400 0,100,0

    darkgrey #a9a9a9 169,169,169

    darkkhaki #bdb76b 189,183,107

    darkmagenta #8b008b 139,0,139

    darkolivegreen #556b2f 85,107,47

    darkorange #ff8c00 255,140,0

    darkorchid #9932cc 153,50,204

    darkred #8b0000 139,0,0

    darksalmon #e9967a 233,150,122

    darkseagreen #8fbc8f 143,188,143

    darkslateblue #483d8b 72,61,139

    darkslategray #2f4f4f 47,79,79

    darkslategrey #2f4f4f 47,79,79

    darkturquoise #00ced1 0,206,209

    darkviolet #9400d3 148,0,211

    deeppink #ff1493 255,20,147

    deepskyblue #00bfff 0,191,255

    dimgray #696969 105,105,105

    dimgrey #696969 105,105,105

    dodgerblue #1e90ff 30,144,255

    firebrick #b22222 178,34,34

    floralwhite #fffaf0 255,250,240

    forestgreen #228b22 34,139,34

    fuchsia #ff00ff 255,0,255

    gainsboro #dcdcdc 220,220,220

    ghostwhite #f8f8ff 248,248,255

    gold #ffd700 255,215,0

    goldenrod #daa520 218,165,32

    gray #808080 128,128,128

    green #008000 0,128,0

    greenyellow #adff2f 173,255,47

    grey #808080 128,128,128

    honeydew #f0fff0 240,255,240

    hotpink #ff69b4 255,105,180

    indianred #cd5c5c 205,92,92

    indigo #4b0082 75,0,130

    ivory #fffff0 255,255,240

    khaki #f0e68c 240,230,140

    lavender #e6e6fa 230,230,250

    lavenderblush #fff0f5 255,240,245

    lawngreen #7cfc00 124,252,0

    lemonchiffon #fffacd 255,250,205

    lightblue #add8e6 173,216,230

    lightcoral #f08080 240,128,128

    lightcyan #e0ffff 224,255,255

    lightgoldenrodyellow #fafad2 250,250,210

    lightgray #d3d3d3 211,211,211

    lightgreen #90ee90 144,238,144

    lightgrey #d3d3d3 211,211,211

    lightpink #ffb6c1 255,182,193

    lightsalmon #ffa07a 255,160,122

    lightseagreen #20b2aa 32,178,170

    lightskyblue #87cefa 135,206,250

    lightslategray #778899 119,136,153

    lightslategrey #778899 119,136,153

    lightsteelblue #b0c4de 176,196,222

    lightyellow #ffffe0 255,255,224

    lime #00ff00 0,255,0

    limegreen #32cd32 50,205,50

    linen #faf0e6 250,240,230

    magenta #ff00ff 255,0,255

    maroon #800000 128,0,0

    mediumaquamarine #66cdaa 102,205,170

    mediumblue #0000cd 0,0,205

    mediumorchid #ba55d3 186,85,211

    mediumpurple #9370db 147,112,219

    mediumseagreen #3cb371 60,179,113

    mediumslateblue #7b68ee 123,104,238

    mediumspringgreen #00fa9a 0,250,154

    mediumturquoise #48d1cc 72,209,204

    mediumvioletred #c71585 199,21,133

    midnightblue #191970 25,25,112

    mintcream #f5fffa 245,255,250

    mistyrose #ffe4e1 255,228,225

    moccasin #ffe4b5 255,228,181

    navajowhite #ffdead 255,222,173

    navy #000080 0,0,128

    oldlace #fdf5e6 253,245,230

    olive #808000 128,128,0

    olivedrab #6b8e23 107,142,35

    orange #ffa500 255,165,0

    orangered #ff4500 255,69,0

    orchid #da70d6 218,112,214

    palegoldenrod #eee8aa 238,232,170

    palegreen #98fb98 152,251,152

    paleturquoise #afeeee 175,238,238

    palevioletred #db7093 219,112,147

    papayawhip #ffefd5 255,239,213

    peachpuff #ffdab9 255,218,185

    peru #cd853f 205,133,63

    pink #ffc0cb 255,192,203

    plum #dda0dd 221,160,221

    powderblue #b0e0e6 176,224,230

    purple #800080 128,0,128

    red #ff0000 255,0,0

    rosybrown #bc8f8f 188,143,143

    royalblue #4169e1 65,105,225

    saddlebrown #8b4513 139,69,19

    salmon #fa8072 250,128,114

    sandybrown #f4a460 244,164,96

    seagreen #2e8b57 46,139,87

    seashell #fff5ee 255,245,238

    sienna #a0522d 160,82,45

    silver #c0c0c0 192,192,192

    skyblue #87ceeb 135,206,235

    slateblue #6a5acd 106,90,205

    slategray #708090 112,128,144

    slategrey #708090 112,128,144

    snow #fffafa 255,250,250

    springgreen #00ff7f 0,255,127

    steelblue #4682b4 70,130,180

    tan #d2b48c 210,180,140

    teal #008080 0,128,128

    thistle #d8bfd8 216,191,216

    tomato #ff6347 255,99,71

    turquoise #40e0d0 64,224,208

    violet #ee82ee 238,130,238

    wheat #f5deb3 245,222,179

    white #ffffff 255,255,255

    whitesmoke #f5f5f5 245,245,245

    yellow #ffff00 255,255,0

    yellowgreen #9acd32 154,205,50

Change the Color of the Background

CSS Code View

Live Demo


Font Properties

Basic properties that style the font. Children inherit these properties from parents.

CSS Code View

Live Demo

Font Style Properties

More stylistic properties to change the appearance of fonts.

CSS Code View

Live Demo

Font Style Properties


Text Layout Properties

These properties have to do with the inline flow of the characters. The children inherit all of these properties from their parents. I have created three selectors, one for the header h3., one for the body text p and one for the superscript inside the body text.

CSS Code View

Live Demo

Text Layout Properties

Change the text by applying different styles. Notice that when you make a mistake, the style reverts to the default style. The 1st selector is an h3. The 2nd selector is a p. The 3rd selector is a span acting like a superior (sup) tag and the 4th selector selects an actual sup tag and colors it red.


Box Properties

box model

Each HTML element is a box. The size of this box is determined by its inheritance, the content, or by a CSS rule. By default, the width of the box is inherited from the parent and the height is determined by the content. That makes the box is as wide as its parent, or the width of the window when the parent element is the body tag.

The box remains invisible unless it has a border or a background.

The box has four sides. You can target each side’s margin, border and padding at once padding: 10px; the vertical and horizontal sides separately padding: 0 auto or each side independently, starting at the top and moving clockwise: top, right, bottom and left padding: 0px 30px 60px 90px;.

CSS Code View

Each element is a box. The size of the box is determined by the parent element, or if CSS is used, the box can have a width, height, padding, border and margins. Padding and the border is added to the size of the content.


Border Properties

The border usually uses a shorthand where the width, color and style are given in this order: border: 10px solid #f68;. Leaving any one of these out breaks the border. Here is a list of the styles:

none: No border. Color and width are ignored (i.e., the border has width 0 unless the border is an image.
hidden: Same as none but has different behavior in the border conflict resolution rules for border-collapsed tables.
dotted A series of round (square?) dots.
dashed: A series of square-ended dashes.
solid: A single line segment.
double: Two parallel solid lines with some space between them.
groove: Looks as if it were carved in the canvas.
ridge: Looks as if it were coming out of the canvas.
inset: Looks as if the content on the inside of the border is sunken into the canvas.
outset: Looks as if the content on the inside of the border is coming out of the canvas.

CSS Code View

Live Demo

You can change the border attributes including the style yourself.


Thematic Break

The Thematic Break, also knows as the horizontal rule in previous version of HTML has the following CSS for the default style. You can change that.

CSS Code View

Live Demo



Margin and Padding Properties

These properties belong to the box model, and are similar, in that they create or subtract space from the sides of the box. I created two elements to show how border collapse works when two margins overlap and only the largest margin is used.

CSS Code View

Live Demo

BOX 1: The margin is set for 20px auto, meaning that the box has 20 pixels of margin on top and bottom, and the left and right margins are automatically adjusted to center the box, possible only because the box has a declared width

BOX 2: Notice that even though both boxes have a 20px margin, that margin collapses between the boxes. If you play around with the numbers, you will see that it collapses the smaller margin into the larger margin.


List Properties

Lists are used for any element that acts like a list. Many elements are styled as lists, and when that happens, its important to take control over the list element, so that the items styled take on the look that does not necessarily look like a list. Menus are a great example of this but many other items are also considered candidates for being styled as lists. If the list-item-style is turned to one of the automatically incrementing counters, the un-ordered list is automatically turned into an ordered list.

CSS Code View

Live Demo

  • List Item
  • List Item
    • List Item
    • List Item
    • List Item

The HTML for the unordered list

<ul>
<li> List Item </li>
<li> List Item </li>
	<ul>
		<li> List Item </li>
		<li> List Item </li>
		<li> List Item </li>
	</ul>
</li>
</ul>

Live Demo

Have numbers span ordered lists by using the attribute start="3" or 55 or 77

  1. List Item
  2. List Item
  1. List Item
  2. List Item
  1. List Item
  2. List Item
  1. List Item
  2. List Item

You can style the bullets of the list with gradients As Eric Meyer demonstrates.

CSS Code View

Live Demo

  • This is a list item.
  • So is this.
  • The list is unordered.
  1. This is a list item.
  2. So is this.
  3. The list is ordered.
  1. This is a list item.
  2. So is this.
  3. The list is ordered.


Tables

Tables used as tables are part of the HTML/CSS landscape. Tables used for layout purposes are to be banished. That said, if you want to style a table, its best to create as many hooks in it as possible. This means that a table can have table header, a table footer and multiple table bodies. It is also possible to style columns. The code and an explanation of how the table works is reproduced below:

CSS Code View

The Caption Holds the Title of the Table
Head 1 Head 2 Head 3
table cell item table cell item table cell item
table cell item table cell item table cell item
table cell item table cell item table cell item
table cell item table cell item table cell item
table cell item table cell item table cell item
table cell item table cell item table cell item
table cell item table cell item table cell item
table cell item table cell item table cell item
The Footer is a Place for Information About the Table.
<table id="table">
 <caption>The Caption Holds the Title of the Table
 </caption>
 <col><col><col>
 <thead> 
  <tr><th>Head 1</th><th>Head 2</th><th>Head 3</th></tr>
 </thead>
 <tbody>
  <tr> <td> table cell item </td>  <td> table cell item </td>  <td> table cell item </td></tr>
  <tr> <td> table cell item </td>  <td> table cell item </td>  <td> table cell item </td></tr>
  <tr> <td> table cell item </td>  <td> table cell item </td>  <td> table cell item </td></tr>
  <tr> <td> table cell item </td> <td> table cell item </td> <td> table cell item </td></tr>
 </tbody>
 <tbody>
  <tr> <td> table cell item </td>  <td> table cell item </td>  <td> table cell item </td></tr>
  <tr> <td> table cell item </td>  <td> table cell item </td>  <td> table cell item </td></tr>
  <tr> <td> table cell item </td>  <td> table cell item </td>  <td> table cell item </td></tr>
  <tr> <td> table cell item </td> <td> table cell item </td> <td> table cell item </td></tr>
 </tbody>
 <tfoot>
 <tr><td colspan="3">
	The Footer is a Place for Information About the Table.
  </td></tr>
 </tfoot>
</table>

The HTML code for the table can be divided into parts, and each can be styled separately. There are table headings, footers, data cells and captions and rows, all of which can be styled. The even and odd table rows are styled using pseudo selectors. If you look at the code, you can see that the table caption comes first, then the table header, two table bodies that contain the table rows and the table data cells and finally a table footer. The row tags allow the three rows to be styled individually, though that too could be handled by pseudo selectors.


Pseudo Classes

Pseudo classes are additional classes that belong to the same element. They are most frequently applied to the link element for navigation, where hover and click or visited states can be applied to that link, though any element can have hover states applied:

CSS Code View


Pseudo Classes: not

A collapsable guide to the layout modes. All of the work is being done by the last CSS rule using the pseudo class not, as in not target or hover li:not(:target):not(:hover) .

CSS Code View

Live Demo

Here you will find links to the layout modes article, including the document flow, the positioning of the elements within the document flow and display, z-index and overflow properties.

HTML

  <nav>
    <ul>
      <li id="nav-about"><a href="#nav-about">Layout Modes</a>
        <ul>
          <li><a href="#layoutModes" target="_blank">Layout Modes</a>
          <li><a href="#block" target="_blank">Block Display</a>
          <li><a href="#inline" target="_blank">Inline Display</a>
        </ul>
      <li id="nav-projects"><a href="#nav-projects">Positioning</a>
        <ul>
          <li><a href="#positioning" target="_blank">Normal</a>
          <li><a href="#relative" target="_blank">Relative</a>
          <li><a href="#absolute" target="_blank">Absolute</a>
          <li><a href="#fixed" target="_blank">Fixed</a>
          <li><a href="#floats" target="_blank">Floats</a> 
          <li><a href="#tables" target="_blank">Tables</a>
        </ul>
      <li id="nav-interact"><a href="#nav-interact">Other</a>
        <ul>
          <li><a href="#display" target="_blank">Display</a>
          <li><a href="#zindex" target="_blank">Z Index</a>
          <li><a href="#over-flow" target="_blank">Overflow</a>
        </ul>
    </ul>
  </nav>
  <div>
   Here you will find links to the <a href="http://b.parsons.edu/~dejongo/04-the-layout-modes" target="_blank">layout modes</a> article, including the document flow, the positioning of the elements within the document flow and display, z-index and overflow properties. 
  </div>
</div>


Pseudo Elements

Pseudo classes add elements to existing elements, in CSS rather than in the document. They can be first letter and line in an inline element and the before and after a block element.

CSS Code View

Live Demo

Pseudo Elements

A pseudo element I use often is the first-line pseudo element.

But other pseudo elements, particularly the before and after, are powerful, as you can see in this posted notes look complete with a page curl above right, created by the before element on the div above, without additional elements in the HTML. Quotes as in <q>quotation marks</q>can be given quotation marks using the before and after pseudo elements.

See more such wizardry in the creation of 84 icons, using nothing but such CSS tricks.

The trick with the folded corner is a really thick border that surrounds the empty content between the quotes of the css content property. The corners of the border meet at a 45° angle, and when the content is nothing, all four corners meet at the middle in a sharp point. Put two such corners together and you get the triangle used to shape the page curl. All you have to do is fill those two sides with the color of the page curl, and fill the other two sides with the background color to block out the corner of the note, which would otherwise stick out.

Cursor

Give the user feedback. Changing the cursor is an instruction away. Roll over the square to see the change.

CSS Code View

Live Demo

Roll the cursor over this square to see it change to a little hand with a pointing finger cursor. Change the cursor in the CSS view to check out all of the cursors.

So much more

30 Seconds if CSS. Ready made CSS snippets to help you build your website.

03 Why CSS?

CSS or Cascading Style Sheets makes life easy. There are two parts, the rules and the cascade. The rules give form to the content. The cascade connects the rules to targeted HTML elements. All the rules for the website are located in one external style sheet making it easy to change the site as a whole.

It’s possible to avoid the cascade by writing all the CSS rules inline. That would be painful. It becomes even more painful if the site is to be revised. Unlike Print, websites are in a continual state of revision. It is possible to embed styles in the head of an HTML document. This is great for writing style definitions once and applying them many times throughout the page. If there are more pages, however, it’s better to separate the styles out of the document into an external style sheet. A link to that stylesheet in the head of the HTML page keeps it in synch with the external style sheet.

Imagine updating your whole site by changing only one rule. That is the power of CSS!

The Bright Side

  • Using a single stylesheet is a boon for consistency.
  • A reduction in repetition and file size.
  • By separating form from content, the content can be repurposed to work on watches, phones, tablets and computer screens. This is called responsive web design.
  • Build fantastically complex web pages with relative ease.
  • Ease of revisions to the layout.
  • CSS allows for better Search Engine Optimization. You can put the most important information first no matter where it ends up being on the page. Content remains readable compared to using tables for layout purposes.

The Darker Side

  • The cascade can become confusing as the number of style definitions grow.
  • Leave out one character and the whole thing can break down. More usually, only the declaration breaks down but watch out for errors by validating often.

Quick Peek

The browser allows you to apply any inline CSS style to any object just for the page you are looking at. Open up the inspector in Chrome — right-click on the object you wish to investigate and select inspect. Once the new window opens up, navigate to the top of the left sidebar and right below the Filter box you will see: a javascript function that will insert whatever inline CSS style you want on the selected element.

element.style {
}

Here you can add any style to the selected element. This style will disappear as soon as the page is refreshed but it allows you to see what a style would look like. The left column, when looking at elements, provides a detailed description of all the css used to paint this object. You will be exploring this feature the more time you spend coding. The same feature is available in Safari and Firefox.

CSS is Awesome

by Brandon Smith

CSS is hard because its properties interact, often in unexpected ways. Because when you set one of them, you’re never just setting that one thing. That one thing combines and bounces off of and contradicts with a dozen other things, including default things that you never actually set yourself.

One rule of thumb for mitigating this is, never be more explicit than you need to be. Web pages are responsive by default. Writing good CSS means leveraging that fact instead of overriding it. Use percentages or viewport units instead of a media query if possible. Use min-width instead of width where you can. Think in terms of rules, in terms of what you really mean to say, instead of just adding properties until things look right. Try to get a feel for how the browser resolves layout and sizing, and make your changes and additions on top of that judiciously. Work with CSS, instead of against it.

Another rule of thumb is to let either width or height be determined by content. In this case, that wasn’t enough, but in most cases, it will be. Give things an avenue for expansion. When you’re setting rules for how your elements get sized, especially if those elements will contain text content, think through the edge cases. “What if this content was pared down to a single character? What if this content expanded to be three paragraphs? It might not look great, but would my layout be totally broken?”

CSS is weird. It’s unlike any other code, and that makes a lot of programmers uncomfortable. But used wisely it can, in fact, be awesome.

01 Visual Literacy

The web is a design language, and as some of you are not primarily Communication Design students, it becomes clear that there needs to be a reference that covers what Visual Design students take for granted.

Parsons students can use these links as resources for researching their projects. Visual literacy goes beyond how something looks. It makes communication more effective.

Visual literacy is used to solve problems in communication. This is a lot like structuring an argument in the academic essay, where reason supplants opinion. Instead of finding causes and reasons why, or a genealogy, bringing visually literate structuring to the communication makes the message more engaging, apparent and clear.

To that end, I’ve assembled a number of links to help develop visual literacy.

A Modernist design manual

Designer Massimo Vignelli’s life’s work through his design philosophy with a wealth of examples in The Vignelli Canon. He distributed it free on his website for everyone to become better visually educated. That site no longer exists, so I put the book on my server. You can purchase it. The Vignelli Canon represents the status quo that David Carson opposed that we will cover in Week 6)

Design Inspiration

Use the following resources to explore your site design. Be inspired — copy, borrow, steal — but make sure that whatever you take, you make your own. The top priority is to effectively communicate the content.

  1. Design Inspiration
  2. Inspirational Sites
  3. Design History
  4. The Story of Art
  5. Rijks Studio
  6. Visual Literacy
  7. Adobe Behance

Design Sandbox

Canva is an online graphic design platform that lets nondesigners put together and play with designs till something clicks. You can use it instead of photoshop to prototype your design. Download it as a PDF and open it in Illustrator, and all of the elements are editable.

  1. Canva

Web Design Sandbox

Invision is a web and mobile device application design platform that allows you to create and user test your designs before you commit to them with code.

  1. InVision

Typography

Typography remains elusive for many, so I gleaned useful definitions, descriptions and links to explain the basics.

  1. typographic resource
  2. Fonts in Use

Books on Design and code

  1. Casey Reas, Chandler McWilliams, and LUST, Form+Code in Design, Art, and Architecture

  2. Kimberly Elam, Geometry of Design
  3. Armin Hofmann, Graphic Design Manual
  4. Robert Bringhurst, The Elements of Typographic Style
  5. Frank Chimero, The Shape of Design
  6. Leah Buley, The User Experience Team of One
  7. Compiled by Laurel Schwulst, Very Interactive Library
  8. Paul Ford, What is Code?
  9. Emil Ruder, Typographie