CSS - tutorial - 01 - styling

revision:


Content

CSS colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA values. CSS backgrounds properties are as follows: CSS border properties allow you to specify the style, width, and color of an element's border. CSS margin properties are used to create space around elements, outside of any defined borders. CSS padding properties are used to generate space around an element's content The CSS height and width properties are used to set the height and width of an element. An outline is a line that is drawn around elements, OUTSIDE the borders.


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

top

CSS color names : predefined color names, such as tomato, blue, red, gray, etc.

CSS background color : - for HTML elements.

examples

Hello World

code:
                    <h3 style="background-color:blue;margin-left: 3vw;">Hello World</h3>
                

CSS text color : - for text.

examples

Hello World

code:
                    <h3 style="color:blue; margin-left: 3vw;">Hello World</h3>
                

CSS border color : - for borders.

examples

Hello World

code:
                    <h3 style="border:2px solid tomato; margin-left: 3vw;">Hello World</h3>
                

CSS color values: colors can also be specified using RGB values, HEX values, HSL values, RGBA values, and HSLA values.


CSS backgrounds properties are as follows:

top

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

A color is most often specified by a valid color name (like "red"), a HEX value (like "#ff0000"), an RGB value (like "rgb(255,0,0)"). The background color can be set for any HTML element.

"background-image" property specifies an image to use as the background of an element.

By default, the image is repeated so it covers the entire element. The background image can also be set for specific elements, like the <p> element.

"background-repeat" property

By default, the background-image property repeats an image both horizontally and vertically.

To repeat an image only horizontally use "background-repeat: repeat-x;". To repeat an image vertically, set "background-repeat: repeat-y".

Showing the background image only once is specified by "background-repeat: no-repeat".

"background-clip" property specifies the painting area of the background

"background-position" property is used to specify the position of the background image.

"background-attachment" property specifies whether the background image should scroll or be fixed (will not scroll with the rest of the page).

"background-origin" property specifies where the background image(s) is/are positioned

"background-size" property specifies the size of the background image(s)

"background" shorthand property

To shorten the code, it is possible to specify all the background properties in one single property, i.e. a shorthand property.

When using the shorthand property the order of the property values is: background-color, background-image, background-repeat, background-attachment, background-position.

It does not matter if one of the property values is missing, as long as the other ones are in this order.

Ohter properties impacting the background

The "opacity property" specifies the opacity/transparency of an element. It can take a value from 0.0 - 1.0. The lower the value, the more transparent.
When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency.

Transparency using RGBA

If you do not want to apply opacity to child elements, use RGBA color values.
In addition to RGB, you can use an RGB color value with an alpha channel (RGBA), which specifies the opacity for a color.
An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).


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

top

border - sets all the border properties in one declaration

border-bottom - sets all the bottom border properties in one declaration

border-bottom-color - sets the color of the bottom border

border-bottom-style - sets the style of the bottom border

border-bottom-width - sets the width of the bottom border

border-color - sets the color of the four borders

border-left - sets all the left border properties in one declaration

border-left-color - sets the color of the left border

border-left-style - sets the style of the left border

border-left-width - sets the width of the left border

border-radius - sets all the four border-*-radius properties for rounded corners

border-right - sets all the right border properties in one declaration

border-right-color - sets the color of the right border

border-right-style - sets the style of the right border

border-right-width - sets the width of the right border

border-style - sets the style of the four borders

border-top - sets all the top border properties in one declaration

border-top-color - sets the color of the top border

border-top-style - sets the style of the top border

border-top-width - sets the width of the top border

border-width - sets the width of the four borders

"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).

"border-width" property specifies the width of the four borders.

The width can be set as a specific size (in px, pt, cm, em, etc.) or by using one of the three pre-defined values: thin, medium, or thick.

The border-width property can have from one to four values (for the top border, right border, bottom border, and the left border).

"border-color" property is used to set the color of the four borders.

The color can be set by: name (i.e. a color name, like "red"), HEX ( i.e. a HEX value, like "#ff0000"), RGB (i.e. a RGB value, like "rgb(255,0,0)"), HSL (i.e. a HSL value, like "hsl(0, 100%, 50%)"), transparent. Note: if border-color is not set, it inherits the color of the element.

The border-color property can have from one to four values (for the top border, right border, bottom border, and the left border).

In CSS, there are properties for specifying each of the borders (top, right, bottom, and left):

If the border-style property has "four values": (e.g. border-style: dotted solid double dashed) top border is dotted, right border is solid, bottom border is double, left border is dashed.

If the border-style property has "three values": (e.g. border-style: dotted solid double) top border is dotted, right and left borders are solid, bottom border is double.

if the border-style property has "two values":(e.g. border-style: dotted solid) top and bottom borders are dotted, right and left borders are solid.

If the border-style property has "one value": (e.g. border-style: dotted) all four borders are dotted./P>

CSS border - shorthand property:

To shorten the code, it is also possible to specify all the individual border properties in one property. The border property is a shorthand property for the following individual border properties: border-width, border-style (required), border-color. All the individual border properties can be specified for just one side.

CSS rounded borders:

The "border-radius property" is used to add rounded borders to an element.


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

top

CSS margin properties are as follows

margin - a shorthand property for setting the margin properties in one declaration

margin-bottom - sets the bottom margin of an element

margin-left - sets the left margin of an element

margin-right - sets the right margin of an element

margin-top - sets the top margin of an element

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.

The margin property to be set to "auto" to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins.

Tip: negative values are allowed.

Margin - shorthand property: 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

If the margin property has "four values": (e.g. margin: 25px 50px 75px 100px) top margin is 25px, right margin is 50px, bottom margin is 75px. left margin is 100px.

If the margin property has "three values": (e.g. margin: 25px 50px 75px) top margin is 25px, right and left margins are 50px, bottom margin is 75px.

If the margin property has "two values": (e.g. margin: 25px 50px) top and bottom margins are 25px, right and left margins are 50px.

If the margin property has "one value": (e.g. margin: 25px) all four margins are 25px.

Margin collapse: top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins.

This does not happen on left and right margins! Only top and bottom margins!

Example: assume a 'h1' element has a bottom margin of 50px and a 'h2' element has a top margin set to 20px. Common sense would seem to suggest that the vertical margin between the 'h1' and the 'h2' would be a total of 70px (50px + 20px). But due to margin collapse, the actual margin ends up being 50px.


CSS padding properties are used to generate space around an element's content

top

The space is generated inside of any defined borders.

CSS padding properties are as follows:

padding - a shorthand property for setting all the padding properties in one declaration

padding-bottom - sets the bottom padding of an element

padding-left - sets the left padding of an element

padding-right - sets the right padding of an element

padding-top - sets the top padding of an element

All the padding properties can have the following values:

length - specifies a padding in px, pt, cm, etc.,
% - specifies a padding in % of the width of the containing element,
inherit - specifies that the padding should be inherited from the parent element.
Note: Negative values are not allowed.

Padding - shorthand property: to shorten the code, it is possible to specify all the padding properties in one property.

The padding property is a shorthand property for the following individual padding properties: padding-top, padding-right, padding-bottom, padding-left

If the padding property has "four values": (e.g. padding: 25px 50px 75px 100px) top padding is 25px, right padding is 50px, bottom padding is 75px, left padding is 100px.

If the padding property has "three values": (e.g. padding: 25px 50px 75px) top padding is 25px, right and left padding are 50px, bottom padding is 75px.

If the padding property has "two values": (e.g. padding: 25px 50px) top and bottom paddings are 25px, right and left paddings are 50px.

If the padding property has "one values": (e.g. padding: 25px) all four paddings are 25px.

Padding and element "width"

The CSS width property specifies the width of the element's content area. The content area is the portion inside the padding, border, and margin of an element (the box model).

If an element has a specified width, the padding added to that element will be added to the total width of the element. This is often an undesirable result.
To keep the width at the predefined value, no matter the amount of padding, you can use the box-sizing property. This causes the element to maintain its width; if you increase the padding, the available content space will decrease.


The CSS height and width properties are used to set the height and width of an element.

top

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.

CSS dimension properties are as follows:

height - sets the height of an element

max-height - sets the maximum height of an element

max-width - sets the maximum width of an element

min-height - sets the minimum height of an element

min-width - sets the minimum width of an element

width - sets the width of an element

The height and width properties may have the following values:

auto - 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.

The "max-width property" is used to set the maximum width of an element.

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 and means that there is no maximum width).

When the browser window is smaller than the width of the element, 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.

Note: The value of the max-width property overrides width.


An outline is a line that is drawn around elements, OUTSIDE the borders.

top

An outline makes the element to "stand out".

CSS has the following outline properties:

outline - a shorthand property for setting outline-width, outline-style, and outline-color in one declaration

outline-color - sets the color of an outline

outline-offset - specifies the space between an outline and the edge or border of an element

outline-style - sets the style of an outline

outline-width - sets the width of an outline

"outline" differs from "borders"!

Unlike border, the outline is drawn outside the element's border, and may overlap other content.

Also, the outline is NOT a part of the element's dimensions; the element's total width and height is not affected by the width of the outline.

The "outline-style" property specifies the style of the outline, and can have one of the following values:

dotted - defines a dotted outline,
dashed - defines a dashed outline,
solid - defines a solid outline,
double - defines a double outline,
groove - defines a 3D grooved outline,
ridge - defines a 3D ridged outline,
inset - defines a 3D inset outline,
outset - defines a 3D outset outline,
none - defines no outline,
hidden - defines a hidden outline.

The "outline-width" property specifies the width of the outline, and can have one of the following values:

thin (typically 1px),
medium (typically 3px),
thick (typically 5px),
a specific size (in px, pt, cm, em, etc).

The "outline-color" property is used to set the color of the outline. The color can be set by:

name - specify a color name, like "red",
HEX - specify a hex value, like "#ff0000",
RGB - specify a RGB value, like "rgb(255,0,0)",
HSL - specify a HSL value, like "hsl(0, 100%, 50%)",
invert - performs a color inversion (which ensures that the outline is visible, regardless of color background)

CSS outline - shorthand property:

The "outline property" is a shorthand property for setting the following individual outline properties: outline-width, outline-style (required), outline-color.

The "outline property" is specified as one, two, or three values from the list above. The order of the values does not matter.

CSS outline offset:

The "outline-offset property" adds space between an outline and the edge/border of an element. The space between an element and its outline is transparent.