CSS Vocabulary and Grammar Basics
CSS Vocabulary and Grammar Basics
CSS is a language system that allows you to define the presentation of elements in HTML
Formatting the way an element appears. Examples of this would be the font a piece a
text is displayed in or a border placed around an image.
Positioning the way an element fits into a document. For example how a sidebar is
positioned in a document
There are three types of styles that can be used in HTML documents:
o
Tag styles this type of style controls the presentation of HTML elements formatted
with a particular tag. For example a p tag style controls the formatting of a paragraph.
Class styles A class is a style that can be applied to any HTML element. It is applied by
adding the class attribute to an HTML element. For example you could create a class
style called blue-text that formats the element it is applied to with a blue font color
regardless of the tag it is applied to <h1 class= blue-text> and
<p class=blue-text> will format both elements as blue text.
Inline this is when a style is defined within the element it affects. You can only define
class styles in this way not tag or id styles. Inline styles are almost never used because of
various limitations and problem associated with them.
Internal this is when a style is defined in the <head> section of the HTML document
and then applied to elements in the <body> section. You can define tag, id and class
styles as internal styles. Internal styles avoid some of the issues related to inline styles
but still have significant limitations are not considered best practice when working with
styles.
External External styles are styles defined in a separate document and then HTML
pages are linked to this separate files. External style sheets allow for the full benefit of
styles and are the best way to work with styles.
When you define a style you name them with the following rules:
o Class styles prefix the style with a . period. So the class style bluetext would be
named as .bluetext
o ID styles are prefixed with the # number symbol. So the id style sidebar would be
defined as #sidebar
o Tag styles do not have a prefix and are written with the plan tag name such as p or h1
CSS styles are written in a specific format:
stylename {
property:value;
property:value;
property:value;
}
o
o
o
o
o
o
o
This would make the tags <h1>, <h2> and <h3> all blue and bold.
You can specify a a parent child relationship to select particular tags and format them with
a style. For example if you wanted to make <li> blue and bold but only list items contained
in <ol> ordered lists not <ul> unordered list you would use the following format:
ol li {
color:blue;
font-weight:bold;
}
This targets any <li> tags that are contained or are children of <ol> tags.
Properties are characteristics of an element. For example text can have a font-family property, a
font-size property and a color property. Every HTML element has many properties.
The value is the setting for the property. For example the text property font-size could have
the value 24px and the text property color could have the value blue.
The CSS Box model is a form that represents how spacing is added around objects. The model
has three different values: Padding, Border, and Margin.
The padding is the area between the object and any border around the object. The margin is
the space between the border around the object and the objects surrounding the object.
font-family
The is the font the text will be displayed in. Can have multiple values.
Subsequent values will be used if the preceding font is not available on the
visitors computer
font-size
font-weight
font-style
color
line-height
text-align
text-decoration
width
height
padding
The spacing between an object and the border (if any) around the object. You
can specify the location of the padding for example you can use the property -
margin
The spacing between the border around an object. Follows the same rules as
the padding property.
border
Applies a border around an object. Requires three values: width, color and
style. For example border:1px #000000 solid; would apply a 1px black solid
border around the object.
If you wish to specify a particular side of an object to apply the border to you
can use border-top:1px #000000 solid;, etc just as you can
specify a side of an object for padding and margin.
Other Properties
list-style-type
background-image
background-repeat
background-color
display
Controls the way a browser displays an element. The two standard values are
display:block;, display:inline; and
display:none
The value none will cause an element not to be displayed.
The value block forces an element to display as a block level element and
allows for CSS box model properties to be applied.
The value inline causes an element to be displayed as an inline level element.
Inline element do not have breaks after them and cannot have box model
properties applied to them.
float
clear
Returns an element to the normal flow of the document after a floated element