Terms and Definitions in CSS
Terms and Definitions in CSS
HTML (the Hypertext Markup Language) and CSS (Cascading Style Sheets) are two of
the core technologies for building Web pages. HTML provides the structure of the
page, CSS the (visual and aural) layout, for a variety of devices. Along
with graphics and scripting, HTML and CSS are the basis of building Web pages
and Web Applications.
CSS is a style language that defines layout of HTML documents. For example,
CSS covers fonts, colours, margins, lines, height, width, background images
and advanced positions.
Comment
A comment has no effect on the display of the page that’s styled; it’s for the CSS author to read
and better understand the code. Comments are universal to (as far as I know) all
programming and related languages. In the example below, the line that says “this is for IE6”
is a comment:
* html #box {
width: 200px;
}
Rule Set
A rule set is a single section of CSS including the selector, the curly braces, and the different
lines with properties and values. The code in the example below comprises one rule set:
Declaration Block
A declaration block is the section of CSS where the property/value pairs appear. In the
example below, everything found between the curly braces (not including the comments) is a
declaration block:
body {
font-family: Arial, sans-serif; /* starts with this line */
color: #555;
font-size: 14px;
line-height: 20px; /* ends here, before the closing curly brace */
}
Declaration
A declaration is generally any single line of CSS that appears between the curly braces,
whether shorthand or longhand. In the example below, everything after the first curly brace,
and before the last curly brace (not including the comment) is a declaration:
body {
font-family: Arial, sans-serif; /* this line is a declaration */
}
In this next example, there are two declarations between the curly braces:
body {
font-family: Arial, sans-serif; /* one declaration */
color: #555; /* another declaration */
}
Property
A property is what appears before the colon in any line of CSS.
#box {
width: 200px; /* the property is "width" (without the colon) */
}
Value
A value is what appears immediately after the colon in any line of CSS.
#box {
width: 200px; /* after the colon, without the semi-colon */
}
If you use shorthand, a single declaration could have multiple values.
Selector
A selector is the part of the CSS line that selects what element to target with the
property/value pair. In the example below “#container #box” is the selector:
/* the selector is everything on the first line */
/* excluding the opening curly brace */
#container #box {
width: 200px;
}
Class Selector
The selector in the example below targets an element by its class name. So every element with
a class of “navigation” will receive the styles in question:
ID Selector
The selector in the example below targets an element by its ID. So every element with an ID of
“navigation” will receive the styles in question:
Universal Selector
The universal selector matches any element within the context in which it’s placed in a
selector. In the example below, the * character is the universal selector:
Attribute Selector
An attribute selector selects an element to style based on an attribute and/or attribute value.
The example below targets certain paragraph elements based on the existence of a “style”
attribute:
Pseudo-Class
A pseudo-class works similarly to a regular CSS class, except it’s not explicitly declared in the
HTML. In the example below, the pseudo-class is added to the anchor element:
/* the word "hover" along with the preceding colon is the pseudo-
class */
a:hover {
text-decoration: none;
}
A pseudo-class always has a single colon followed by a keyword of some sort, with no space
before or after the colon. Other pseudo-classes include :visited, :focus, and :first-
child.
Pseudo-Element
A pseudo-element is not the same as a pseudo-class. While a pseudo-class matches elements
that actually exist, pseudo-elements target “virtual” elements that can change depending on
the actual HTML. CSS2 pseudo-elements use a single colon and CSS3 pseudo-elements use a
double colon. In the example below, the first rule set uses a CSS2 pseudo-element, and the
second rule set uses a CSS3 pseudo-element:
/* "first-letter" including the preceding colon is the pseudo-element
*/
p:first-letter {
display: block;
float: left;
margin: 0 5px 5px 0;
}
Combinator
A combinator is the character in a selector that connects two selectors together. There are four
types of combinators. These four combinators help create descendant selectors (with a space
character), child selectors (with the “>” character), adjacent sibling selectors (with the “+”
character), and general sibling selectors (with the “~” character). To dispel any confusion,
here are those four combinators in use:
/* In all 4 examples */
/* whatever appears between "div" and "p" is a combinator */
/* in the first example, the combinator is a space character */
div p {
color: #222;
}
div>p {
color: #333;
}
div+p {
color: #444;
}
div~p {
color: #555;
}
At-Rule
An at-rule has nothing to do with Star Wars. An at-rule is an instruction given in a CSS
document using the @ character. An at-rule could have a declaration block or a simple string
of text. The example below has two different at-rules:
@import url(secondary.css);
@media print {
#container {
width: 500px;
}
}
The at-rule is not just the “@media” or “@import” part at the beginning; the entire instruction
comprises the complete at-rule.
Statement
A statement in CSS is any at-rule or rule set. In the example below, there are two statements;
one is an at-rule, and the other is a rule set:
Identifier
An identifier can be anything that appears as a property, id, class, keyword value, and at-rule.
In the example, below there are four identifiers:
Keyword
A keyword is a value for a property and is somewhat like a reserved word for a particular
property. Different properties have different keywords, and all properties allow the keyword
“inherit”. In the example below, the value “auto” is a keyword.
#container {
height: auto; /* "auto" is a keyword */
}