CSS Margin

Margins are used to create space around elements, outside of any defined borders.

CSS Margins

The CSS margin properties are used to create space around elements, outside of any defined borders.

With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).

Margin – Individual Sides

CSS has properties for specifying the margin for each side of an element:

  • margin-top
  • margin-right
  • margin-bottom
  • margin-left

All the margin properties can have the following values:

  • auto – the browser calculates the margin
  • length – specifies a margin in px, pt, cm, etc.
  • % – specifies a margin in % of the width of the containing element
  • inherit – specifies that the margin should be inherited from the parent element

Tip: Negative values are allowed. Continue reading CSS Margin

CSS Border

The CSS border properties allow you to specify the style, width, and color of an element’s border.

CSS Border Style

The border-style property specifies what kind of border to display.

The following values are allowed:

  • dotted – Defines a dotted border
  • dashed – Defines a dashed border
  • solid – Defines a solid border
  • double – Defines a double border
  • groove – Defines a 3D grooved border. The effect depends on the border-color value
  • ridge – Defines a 3D ridged border. The effect depends on the border-color value
  • inset – Defines a 3D inset border. The effect depends on the border-color value
  • outset – Defines a 3D outset border. The effect depends on the border-color value
  • none – Defines no border
  • hidden – Defines a hidden border

The border-style property can have from one to four values (for the top border, right border, bottom border, and the left border). Continue reading CSS Border

CSS Background

The CSS background properties are used to add background effects for elements.

CSS background-color

The background-color property specifies the background color of an element.

The background color of a page is set like this:

body {
  background-color: lightblue;
}

Continue reading CSS Background

CSS Colors

Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values.

CSS Color Names

In CSS, a color can be specified by using a predefined color name:

Color Names Supported by All Browsers

All modern browsers support the following 140 color names (click on a color name, or a hex value, to view the color as the background-color along with different text colors):

Continue reading CSS Colors

CSS Comments

CSS comments are not displayed in the browser, but they can help document your source code.

CSS Comments

Comments are used to explain the code, and may help when you edit the source code at a later date.

Comments are ignored by browsers. Continue reading CSS Comments

How To Add CSS

When a browser reads a style sheet, it will format the HTML document according to the information in the style sheet.

Three Ways to Insert CSS

There are three ways of inserting a style sheet:

  • 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. Continue reading How To Add CSS

CSS Selectors

CSS selectors are used to “find” (or select) the HTML elements you want to style.

We can divide CSS selectors into five categories:

This page will explain the most basic CSS selectors. Continue reading CSS Selectors

CSS Attribute Selectors

Style HTML Elements With Specific Attributes

It is possible to style HTML elements that have specific attributes or attribute values.


CSS [attribute] Selector

The [attribute] selector is used to select elements with a specified attribute.

The following example selects all <a> elements with a target attribute:

Continue reading CSS Attribute Selectors

CSS Pseudo-elements

What are Pseudo-Elements?

A CSS pseudo-element is used to style specific parts of an element.

For example, it can be used to:

  • Style the first letter or line, of an element
  • Insert content before or after an element
  • Style the markers of list items
  • Style the viewbox behind a dialog box

Continue reading CSS Pseudo-elements

CSS Pseudo-classes

What are Pseudo-classes?

A pseudo-class is used to define a special state of an element.

For example, it can be used to:

  • Style an element when a user moves the mouse over it
  • Style visited and unvisited links differently
  • Style an element when it gets focus
  • Style valid/invalid/required/optional form elements

Syntax

The syntax of pseudo-classes:

selector:pseudo-class {
  property: value;
}

Continue reading CSS Pseudo-classes