CSS Syntax
CSS Syntax
CSS Syntax
CSS Syntax
A CSS rule consists of a selector and a declaration block.
CSS Syntax
2. CSS Selectors
A CSS selector selects the HTML element(s) you want to style.
CSS Selectors
CSS selectors are used to "find" (or select) the HTML elements you want to
style.
The element selector selects HTML elements based on the element name.
To select an element with a specific id, write a hash (#) character, followed by
the id of the element.
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed
by the class name.
You can also specify that only specific HTML elements should be affected by a
class.
The universal selector (*) selects all HTML elements on the page.
The grouping selector selects all the HTML elements with the same style
definitions.
Look at the following CSS code (the h1, h2, and p elements have the same
style definitions).
External CSS
Internal CSS
Inline CSS
External CSS
With an external style sheet, you can change the look of an entire website by
changing just one file!
Each HTML page must include a reference to the external style sheet file inside
the <link> element, inside the head section.
An external style sheet can be written in any text editor, and must be saved
with a .css extension.
The external .css file should not contain any HTML tags.
Internal CSS
An internal style sheet may be used if one single HTML page has a unique
style.
The internal style is defined inside the <style> element, inside the head section.
Inline CSS
An inline style may be used to apply a unique style for a single element.
To use inline styles, add the style attribute to the relevant element. The style
attribute can contain any CSS property.
If some properties have been defined for the same selector (element) in
different style sheets, the value from the last read style sheet will be used.
Cascading Order
What style will be used when there is more than one style specified for an
HTML element?
All the styles in a page will "cascade" into a new "virtual" style sheet by the
following rules, where number one has the highest priority:
So, an inline style has the highest priority, and will override external and
internal styles and browser defaults.
4. CSS Colors
Colors are specified using predefined color names, or RGB, HEX, HSL,
RGBA, HSLA values.
5. CSS Borders
The border-style property specifies what kind of border to display.
The border-style property can have from one to four values (for the top border,
right border, bottom border, and the left border).
6. CSS Margins
Margin - Individual Sides
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
To shorten the code, it is possible to specify all the margin properties in one
property.
The margin property is a shorthand property for the following individual
margin properties:
margin-top
margin-right
margin-bottom
margin-left
CSS Padding
Padding - Individual Sides
CSS has properties for specifying the padding for each side of an element:
padding-top
padding-right
padding-bottom
padding-left
To shorten the code, it is possible to specify all the padding properties in one
property.
padding-top
padding-right
padding-bottom
padding-left
The height and width properties are used to set the height and width of an
element.
The height and width properties do not include padding, borders, or margins. It
sets the height/width of the area inside the padding, border, and margin of the
element.
The height and width properties may have the following values:
auto - This is default. The browser calculates the height and width
length - Defines the height/width in px, cm, etc.
% - Defines the height/width in percent of the containing block
initial - Sets the height/width to its default value
inherit - The height/width will be inherited from its parent value
Setting max-width
The max-width can be specified in length values, like px, cm, etc., or in percent
(%) of the containing block, or set to none (this is default. Means that there is
no maximum width).
The problem with the <div> above occurs when the browser window is smaller
than the width of the element (500px). The browser then adds a horizontal
scrollbar to the page.
Using max-width instead, in this situation, will improve the browser's handling
of small windows.
8. CSS Outline
CSS has the following outline properties:
outline-style
outline-color
outline-width
outline-offset
outline
The outline-style property specifies the style of the outline, and can have one of
the following values:
9. CSS Text
The color property is used to set the color of the text. The color is specified by:
1. Serif fonts have a small stroke at the edges of each letter. They create a
sense of formality and elegance.
2. Sans-serif fonts have clean lines (no small strokes attached). They
create a modern and minimalistic look.
3. Monospace fonts - here all the letters have the same fixed width. They
create a mechanical look.
4. Cursive fonts imitate human handwriting.
5. Fantasy fonts are decorative/playful fonts.
All the different font names belong to one of the generic font families.
The position property specifies the type of positioning method used for an
element.
static
relative
fixed
absolute
sticky
Elements are then positioned using the top, bottom, left, and right properties.
However, these properties will not work unless the position property is set first.
They also work differently depending on the position value.
The z-index property specifies the stack order of an element (which element
should be placed in front of, or behind, the others).
The float property is used for positioning and formatting content e.g. let an
image float left to the text in a container.
In its simplest use, the float property can be used to wrap text around images.
The clear property specifies what should happen with the element that is next
to a floating element.
none - The element is not pushed below left or right floated elements.
This is default
left - The element is pushed below left floated elements
right - The element is pushed below right floated elements
both - The element is pushed below both left and right floated elements
inherit - The element inherits the clear value from its parent
When clearing floats, you should match the clear to the float: If an element is
floated to the left, then you should clear to the left. Your floated element will
continue to float, but the cleared element will appear below it on the web page.
Also, with display: inline-block, the top and bottom margins/paddings are
respected, but with display: inline they are not.
The ::first-line pseudo-element is used to add a special style to the first line of a
text.
font properties
color properties
background properties
word-spacing
letter-spacing
text-decoration
vertical-align
text-transform
line-height
Clear
The ::first-letter pseudo-element is used to add a special style to the first letter
of a text.
The ::before pseudo-element can be used to insert some content before the
content of an element.
The ::after pseudo-element can be used to insert some content after the content
of an element.