DW Faisalabad New Version

DW Faisalabad New Version
Please Jump to New Version
Showing posts with label CSS Tutorial. Show all posts
Showing posts with label CSS Tutorial. Show all posts

Wednesday, 2 August 2017

CSS Fonts

The CSS font properties define the font family, boldness, size, and the style of a text.

Difference Between Serif and Sans-serif Fonts
CSS Font Families
In CSS, there are two types of font family names:

  • generic family - a group of font families with a similar look (like "Serif" or "Monospace")
  • font family - a specific font family (like "Times New Roman" or "Arial")





Font Family
The font family of a text is set with the font-family property.

The font-family property should hold several font names as a "fallback" system. If the browser does not support the first font, it tries the next font, and so on.

Start with the font you want, and end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available.

Note: If the name of a font family is more than one word, it must be in quotation marks, like: "Times New Roman".

More than one font family is specified in a comma-separated list:

Example
p {
    font-family: "Times New Roman", Times, serif;
}
Try it Yourself »
For commonly used font combinations, look at our Web Safe Font Combinations.

Font Style
The font-style property is mostly used to specify italic text.

This property has three values:

  • normal - The text is shown normally
  • italic - The text is shown in italics
  • oblique - The text is "leaning" (oblique is very similar to italic, but less supported)

Example
p.normal {
    font-style: normal;
}
p.italic {
    font-style: italic;
}
p.oblique {
    font-style: oblique;
}
Try it Yourself »

Font Size
The font-size property sets the size of the text.

Being able to manage the text size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.

Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for paragraphs.

The font-size value can be an absolute, or relative size.

Absolute size:

  • Sets the text to a specified size
  • Does not allow a user to change the text size in all browsers (bad for accessibility reasons)
  • Absolute size is useful when the physical size of the output is known

Relative size:

  • Sets the size relative to surrounding elements
  • Allows a user to change the text size in browsers


Set Font Size With Pixels
Setting the text size with pixels gives you full control over the text size:

Example
h1 {
    font-size: 40px;
}
h2 {
    font-size: 30px;
}
p {
    font-size: 14px;
}
Try it Yourself »

Tip: If you use pixels, you can still use the zoom tool to resize the entire page.

Set Font Size With Em
To allow users to resize the text (in the browser menu), many developers use em instead of pixels.

The em size unit is recommended by the DWF.

1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is 16px.

The size can be calculated from pixels to em using this formula: pixels/16=em

Example
h1 {
    font-size: 2.5em; /* 40px/16=2.5em */
}
h2 {
    font-size: 1.875em; /* 30px/16=1.875em */
}
p {
    font-size: 0.875em; /* 14px/16=0.875em */
}
Try itYourself »

In the example above, the text size in em is the same as the previous example in pixels. However, with the em size, it is possible to adjust the text size in all browsers.

Unfortunately, there is still a problem with older versions of IE. The text becomes larger than it should when made larger, and smaller than it should when made smaller.

Use a Combination of Percent and Em
The solution that works in all browsers, is to set a default font-size in percent for the <body> element:

Example
body {
    font-size: 100%;
}
h1 {
    font-size: 2.5em;
}
h2 {
    font-size: 1.875em;
}
p {
    font-size: 0.875em;
}
Try it Yourself »

Our code now works great! It shows the same text size in all browsers, and allows all browsers to zoom or resize the text!

Font Weight
The font-weight property specifies the weight of a font:

Example
p.normal {
    font-weight: normal;
}
p.thick {
    font-weight: bold;
}
Try it Yourself »

Font Variant
The font-variant property specifies whether or not a text should be displayed in a small-caps font.

In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted uppercase letters appears in a smaller font size than the original uppercase letters in the text.

Example
p.normal {
    font-variant: normal;
}
p.small {
    font-variant: small-caps;
}
Try it Yourself »

All CSS Font Properties
Property
Description
font Sets all the font properties in one declaration
font-family Specifies the font family for text
font-size Specifies the font size of text
font-style Specifies the font style for text
font-variant Specifies whether or not a text should be displayed in a small-caps font
font-weight Specifies the weight of a font
Read More »

CSS Text



Text Color
The color property is used to set the color of the text.

With CSS, a color is most often specified by:

  • a color name - like "red"
  • a HEX value - like "#ff0000"
  • an RGB value - like "rgb(255,0,0)"

Look at CSS Color Values for a complete list of possible color values.

The default text color for a page is defined in the body selector.

Example
body {
    color: blue;
}
h1 {
    color: green;
}
Try it Yourself »



Text Alignment
The text-align property is used to set the horizontal alignment of a text.

A text can be left or right aligned, centered, or justified.

The following example shows center aligned, and left and right aligned text (left alignment is default if text direction is left-to-right, and right alignment is default if text direction is right-to-left):

Example
h1 {
    text-align: center;
}
h2 {
    text-align: left;
}
h3 {
    text-align: right;
}
Try it Yourself »
When the text-align property is set to "justify", each line is stretched so that every line has equal width, and the left and right margins are straight (like in magazines and newspapers):

Example
div {
    text-align: justify;
}
Try it Yourself »

Text Decoration
The text-decoration property is used to set or remove decorations from text.

The value text-decoration: none; is often used to remove underlines from links:

Example
a {
    text-decoration: none;
}
Try it Yourself »
The other text-decoration values are used to decorate text:

Example
h1 {
    text-decoration: overline;
}
h2 {
    text-decoration: line-through;
}
h3 {
    text-decoration: underline;
}
Try it Yourself »
Note: It is not recommended to underline text that is not a link, as this often confuses the reader.

Text Transformation
The text-transform property is used to specify uppercase and lowercase letters in a text.

It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of each word:

Example
p.uppercase {
    text-transform: uppercase;
}
p.lowercase {
    text-transform: lowercase;
}
p.capitalize {
    text-transform: capitalize;
}
Try it Yourself »

Text Indentation
The text-indent property is used to specify the indentation of the first line of a text:

Example
p {
    text-indent: 50px;
}
Try it Yourself »

Letter Spacing
The letter-spacing property is used to specify the space between the characters in a text.

The following example demonstrates how to increase or decrease the space between characters:

Example
h1 {
    letter-spacing: 3px;
}
h2 {
    letter-spacing: -3px;
}
Try it Yourself »

Line Height
The line-height property is used to specify the space between lines:

Example
p.small {
    line-height: 0.8;
}
p.big {
    line-height: 1.8;
}
Try it Yourself »

Text Direction
The direction property is used to change the text direction of an element:

Example
div {
    direction: rtl;
}
Try it Yourself »

Word Spacing
The word-spacing property is used to specify the space between the words in a text.

The following example demonstrates how to increase or decrease the space between words:

Example
h1 {
    word-spacing: 10px;
}
h2 {
    word-spacing: -5px;
}
Try it Yourself »

Text Shadow
The text-shadow property adds shadow to text.

The following example specifies the position of the horizontal shadow (3px), the position of the vertical shadow (2px) and the color of the shadow (red):

Example
h1 {
    text-shadow: 3px 2px red;
}
Try it Yourself »

All CSS Text Properties

Property
Description
color Sets the color of text
direction Specifies the text direction/writing direction
letter-spacing Increases or decreases the space between characters in a text
line-height Sets the line height
text-align Specifies the horizontal alignment of text
text-decoration Specifies the decoration added to text
text-indent Specifies the indentation of the first line in a text-block
text-shadow Specifies the shadow effect added to text
text-transform Controls the capitalization of text
text-overflow Specifies how overflowed content that is not displayed should be signaled to the user
unicode-bidi Used together with the direction property to set or return whether the text should be overridden to support multiple languages in the same document
vertical-align Sets the vertical alignment of an element
white-space Specifies how white-space inside an element is handled
word-spacing Increases or decreases the space between words in a text

Read More »

CSS Outline

The CSS outline properties specify the style, color, and width of an outline.

An outline is a line that is drawn around elements (outside the borders) to make the element "stand out".

However, the outline property is different from the border property - The outline is NOT a part of an element's dimensions; the element's total width and height is not affected by the width of the outline.



Outline Style
The outline-style property specifies the style of the outline.

The outline-style property 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. The effect depends on the outline-color value
  • ridge - Defines a 3D ridged outline. The effect depends on the outline-color value
  • inset - Defines a 3D inset outline. The effect depends on the outline-color value
  • outset - Defines a 3D outset outline. The effect depends on the outline-color value
  • none - Defines no outline
  • hidden - Defines a hidden outline

The following example first sets a thin black border around each <p> element, then it shows the different outline-style values:

Example
p {
    border: 1px solid black;
    outline-color: red;
}
p.dotted {outline-style: dotted;}
p.dashed {outline-style: dashed;}
p.solid {outline-style: solid;}
p.double {outline-style: double;}
p.groove {outline-style: groove;}
p.ridge {outline-style: ridge;}
p.inset {outline-style: inset;}
p.outset {outline-style: outset;}
Result:



Try it Yourself »



Outline Color
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"
  • RGB - specify a RGB value, like "rgb(255,0,0)"
  • Hex - specify a hex value, like "#ff0000"
  • invert - performs a color inversion (which ensures that the outline is visible, regardless of color background)

Example
p {
    border: 1px solid black;
    outline-style: double;
    outline-color: red;
}
Result:



Try it Yourself »

Outline Width
The outline-width property specifies the width of the outline.

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.

Example
p {border: 1px solid black;}
p.one {
    outline-style: double;
    outline-color: red;
    outline-width: thick;
}
p.two {
    outline-style: double;
    outline-color: green;
    outline-width: 3px;
}
Result:



Try it Yourself »

Outline - Shorthand property
To shorten the code, it is also possible to specify all the individual outline properties in one property.

The outline property is a shorthand property for the following individual outline properties:

  • outline-width
  • outline-style (required)
  • outline-color

Example
p {
    border: 1px solid black;
    outline: 5px dotted red;
}
Result:



Try it Yourself »

All CSS Outline Properties

Property
Description
outline Sets all the outline properties 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

Read More »

CSS Box Model

All HTML elements can be considered as boxes. In CSS, the term "box model" is used when talking about design and layout.

The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins, borders, padding, and the actual content. The image below illustrates the box model:



Explanation of the different parts:

  • Content - The content of the box, where text and images appear
  • Padding - Clears an area around the content. The padding is transparent
  • Border - A border that goes around the padding and content
  • Margin - Clears an area outside the border. The margin is transparent

The box model allows us to add a border around elements, and to define space between elements.

Example
div {
    width: 300px;
    border: 25px solid green;
    padding: 25px;
    margin: 25px;
}
Try it Yourself »

Width and Height of an Element
In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.



Assume we want to style a <div> element to have a total width of 350px:

Example
div {
    width: 320px;
    padding: 10px;
    border: 5px solid gray;
    margin: 0;
}
Try it Yourself »

Here is the math:



The total width of an element should be calculated like this:

Total element width = width + left padding + right padding + left border + right border + left margin + right margin

The total height of an element should be calculated like this:

Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin

Note for old IE: Internet Explorer 8 and earlier versions, include padding and border in the width property. To fix this problem, add a <!DOCTYPE html> to the HTML page.

Read More »

CSS Height and Width



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

The height and width can be set to auto (this is default. Means that the browser calculates the height and width), or be specified in length values, like px, cm, etc., or in percent (%) of the containing block.



Example
div {
    height: 200px;
    width: 50%;
    background-color: powderblue;
}
Try it Yourself »



Example
div {
    height: 100px;
    width: 500px;
    background-color: powderblue;
}
Try it Yourself »
Note: The height and width properties do not include padding, borders, or margins; they set the height/width of the area inside the padding, border, and margin of the element!

Setting max-width
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. 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.

Tip: Drag the browser window to smaller than 500px wide, to see the difference between the two divs!



Note: The value of the max-width property overrides width.
The following example shows a <div> element with a height of 100 pixels and a max-width of 500 pixels:

Example
div {
    max-width: 500px;
    height: 100px;
    background-color: powderblue;
}
Try it Yourself »
All CSS Dimension Properties

Property
Description
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

Read More »

CSS Padding



CSS Padding
The CSS padding properties are used to generate space around content.

The padding clears an area around the content (inside the border) of an element.

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

Padding - Individual Sides
CSS has properties for specifying the padding for each side of an element:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

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

The following example sets different padding for all four sides of a <p> element:

Example
p {
    padding-top: 50px;
    padding-right: 30px;
    padding-bottom: 50px;
    padding-left: 80px;
}
Try it Yourself »

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

Example
p {
    padding: 50px 30px 50px 80px;
}
Try it Yourself »
So, here is how it works:

If the padding property has four values:

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:

padding: 25px 50px 75px;

  • top padding is 25px
  • right and left paddings are 50px
  • bottom padding is 75px

If the padding property has two values:

padding: 25px 50px;

  • top and bottom paddings are 25px
  • right and left paddings are 50px

If the padding property has one value:

padding: 25px;

  • all four paddings are 25px

Example
div.ex1 {
    padding: 25px 50px 75px 100px;
}
div.ex2 {
    padding: 25px 50px 75px;
}
div.ex3 {
    padding: 25px 50px;
}
div.ex4 {
    padding: 25px;
}
Try it Yourself »

All CSS Padding Properties

Property
Description
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

Read More »

CSS Margins



CSS Margins
The CSS margin properties are used to generate space around elements.

The margin properties set the size of the white space outside the border.

With CSS, you have full control over the margins. There are CSS 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.

The following example sets different margins for all four sides of a <p> element:

Example
p {
    margin-top: 100px;
    margin-bottom: 100px;
    margin-right: 150px;
    margin-left: 80px;
}
Try it Yourself »

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
Example
p {
    margin: 100px 150px 100px 80px;
}
Try it Yourself »

So, here is how it works:

If the margin property has four values:

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:

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:

margin: 25px 50px;
  • top and bottom margins are 25px
  • right and left margins are 50px
If the margin property has one value:

margin: 25px;
  • all four margins are 25px

The auto Value
You can set the margin property 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:

Example
div {
    width: 300px;
    margin: auto;
    border: 1px solid red;
}
Try it Yourself »

The inherit Value
This example lets the left margin be inherited from the parent element:

Example
div.container {
    border: 1px solid red;
    margin-left: 100px;
}
p.one {
    margin-left: inherit;
}
Try it Yourself »

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!

Look at the following example:

Example
h1 {
    margin: 0 0 50px 0;
}
h2 {
    margin: 20px 0 0 0;
}
Try it Yourself »

All CSS Margin Properties


Property
Description
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

Read More »

Tuesday, 1 August 2017

CSS Borders

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



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

Example
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
p.solid {border-style: solid;}
p.double {border-style: double;}
p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
p.inset {border-style: inset;}
p.outset {border-style: outset;}
p.none {border-style: none;}
p.hidden {border-style: hidden;}
p.mix {border-style: dotted dashed solid double;}


Try it Yourself »


Border Width
The 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).



Example
p.one {
    border-style: solid;
    border-width: 5px;
}
p.two {
    border-style: solid;
    border-width: medium;
}
p.three {
    border-style: solid;
    border-width: 2px 10px 4px 20px;
}
Try it Yourself »

Border Color
The border-color property is used to set the color of the four borders.

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)"
  • transparent

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

If border-color is not set, it inherits the color of the element.



Example
p.one {
    border-style: solid;
    border-color: red;
}
p.two {
    border-style: solid;
    border-color: green;
}
p.three {
    border-style: solid;
    border-color: red green blue yellow;
}
Try it Yourself »

Border - Individual Sides
From the examples above you have seen that it is possible to specify a different border for each side.

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



Example
p {
    border-top-style: dotted;
    border-right-style: solid;
    border-bottom-style: dotted;
    border-left-style: solid;
}
Try it Yourself »
The example above gives the same result as this:

Example
p {
    border-style: dotted solid;
}
Try it Yourself »
So, here is how it works:

If the border-style property has four values:

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:

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:

border-style: dotted solid;

  • top and bottom borders are dotted
  • right and left borders are solid

If the border-style property has one value:

border-style: dotted;

  • all four borders are dotted

The border-style property is used in the example above. However, it also works with border-width and border-color.

Border - Shorthand Property
As you can see from the examples above, there are many properties to consider when dealing with borders.

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

Example
p {
    border: 5px solid red;
}
Result:


Try it Yourself »

You can also specify all the individual border properties for just one side:

Left Border
p {
    border-left: 6px solid red;
    background-color: lightgrey;
}
Result:


Try it Yourself »

Bottom Border
p {
    border-bottom: 6px solid red;
    background-color: lightgrey;
}
Result:



Try it Yourself »
Rounded Borders
The border-radius property is used to add rounded borders to an element:



Example
p {
    border: 2px solid red;
    border-radius: 5px;
}
Try it Yourself »
Note: The border-radius property is not supported in IE8 and earlier versions.

All CSS Border Properties

Property
Description
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
Read More »

CSS Backgrounds



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

The background color of a page is set like this:

Example
body {
    background-color: lightblue;
}

Try it Yourself »

With CSS, 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)"

Look at CSS Color Values for a complete list of possible color values.

In the example below, the <h1>, <p>, and <div> elements have different background colors:

Example
h1 {
    background-color: green;
}
div {
    background-color: lightblue;
}
p {
    background-color: yellow;
}

Try it Yourself »

Background Image
The 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 for a page can be set like this:

Example
body {
    background-image: url("paper.gif");
}

Try it Yourself »

Below is an example of a bad combination of text and background image. The text is hardly readable:

Example
body {
    background-image: url("bgdesert.jpg");
}

Try it Yourself »



Background Image - Repeat Horizontally or Vertically
By default, the background-image property repeats an image both horizontally and vertically.

Some images should be repeated only horizontally or vertically, or they will look strange, like this:

Example
body {
    background-image: url("gradient_bg.png");
}

Try it Yourself »

If the image above is repeated only horizontally (background-repeat: repeat-x;), the background will look better:

Example
body {
    background-image: url("gradient_bg.png");
    background-repeat: repeat-x;
}

Try it Yourself »



Background Image - Set position and no-repeat
Showing the background image only once is also specified by the background-repeat property:

Example
body {
    background-image: url("img_tree.png");
    background-repeat: no-repeat;
}

Try it Yourself »

In the example above, the background image is shown in the same place as the text. We want to change the position of the image, so that it does not disturb the text too much.

The position of the image is specified by the background-position property:

Example
body {
    background-image: url("img_tree.png");
    background-repeat: no-repeat;
    background-position: right top;
}

Try it Yourself »

Background Image - Fixed position
To specify that the background image should be fixed (will not scroll with the rest of the page), use the background-attachment property:

Example
body {
    background-image: url("img_tree.png");
    background-repeat: no-repeat;
    background-position: right top;
    background-attachment: fixed;
}

Try it Yourself »

Background - Shorthand property
To shorten the code, it is also possible to specify all the background properties in one single property. This is called a shorthand property.

The shorthand property for background is background:

Example
body {
    background: #ffffff url("img_tree.png") no-repeat right top;
}

Try it Yourself »

When using the shorthand property the order of the property values is:

  1. background-color
  2. background-image
  3. background-repeat
  4. background-attachment
  5. background-position

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

Property
Description
background Sets all the background properties in one declaration
background-attachment Sets whether a background image is fixed or scrolls with the rest of the page
background-color Sets the background color of an element
background-image Sets the background image for an element
background-position Sets the starting position of a background image
background-repeat Sets how a background image will be repeated
Read More »

CSS Icons



How To Add Icons
The simplest way to add an icon to your HTML page, is with an icon library, such as Font Awesome.

Add the name of the specified icon class to any inline HTML element (like <i> or <span>).

All the icons in the icon libraries below, are scalable vectors that can be customized with CSS (size, color, shadow, etc.)

Font Awesome Icons
To use the Font Awesome icons, add the following line inside the <head> section of your HTML page:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

Note: No downloading or installation is required!

Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<i class="fa fa-cloud"></i>
<i class="fa fa-heart"></i>
<i class="fa fa-car"></i>
<i class="fa fa-file"></i>
<i class="fa fa-bars"></i>
</body>
</html>
Result:



Try It Yourself »

Bootstrap Icons
To use the Bootstrap glyphicons, add the following line inside the <head> section of your HTML page:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

Note: No downloading or installation is required!

Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<i class="glyphicon glyphicon-cloud"></i>
<i class="glyphicon glyphicon-remove"></i>
<i class="glyphicon glyphicon-user"></i>
<i class="glyphicon glyphicon-envelope"></i>
<i class="glyphicon glyphicon-thumbs-up"></i>
</body>
</html>
Result:



Try It Yourself »

Google Icons
To use the Google icons, add the following line inside the <head> section of your HTML page:

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

Note: No downloading or installation is required!

Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<body>
<i class="material-icons">cloud</i>
<i class="material-icons">favorite</i>
<i class="material-icons">attachment</i>
<i class="material-icons">computer</i>
<i class="material-icons">traffic</i>
</body>
</html>
Result:



Try It Yourself »


Read More »

CSS Links

With CSS, links can be styled in different ways.


Styling Links
Links can be styled with any CSS property (e.g. color, font-family, background, etc.).

Example
a {
    color: hotpink;
}

Try it Yourself »

In addition, links can be styled differently depending on what state they are in.

The four links states are:
  • a:link - a normal, unvisited link
  • a:visited - a link the user has visited
  • a:hover - a link when the user mouses over it
  • a:active - a link the moment it is clicked
Example
/* unvisited link */
a:link {
    color: red;
}
/* visited link */
a:visited {
    color: green;
}
/* mouse over link */
a:hover {
    color: hotpink;
}
/* selected link */
a:active {
    color: blue;
}

Try it Yourself »

When setting the style for several link states, there are some order rules:
  • a:hover MUST come after a:link and a:visited
  • a:active MUST come after a:hover

Text Decoration
The text-decoration property is mostly used to remove underlines from links:

Example
a:link {
    text-decoration: none;
}
a:visited {
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}
a:active {
    text-decoration: underline;
}

Try it Yourself »

Background Color
The background-color property can be used to specify a background color for links:

Example
a:link {
    background-color: yellow;
}
a:visited {
    background-color: cyan;
}
a:hover {
    background-color: lightgreen;
}
a:active {
    background-color: hotpink;

Try it Yourself »

Advanced - Link Buttons
This example demonstrates a more advanced example where we combine several CSS properties to display links as boxes/buttons:

Example
a:link, a:visited {
    background-color: #f44336;
    color: white;
    padding: 14px 25px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
}
a:hover, a:active {
    background-color: red;
}

Try it Yourself »

Read More »

CSS Lists



HTML Lists and CSS List Properties
In HTML, there are two main types of lists:

  • unordered lists (<ul>) - the list items are marked with bullets
  • ordered lists (<ol>) - the list items are marked with numbers or letters

The CSS list properties allow you to:

  • Set different list item markers for ordered lists
  • Set different list item markers for unordered lists
  • Set an image as the list item marker
  • Add background colors to lists and list items

Different List Item Markers
The list-style-type property specifies the type of list item marker.

The following example shows some of the available list item markers:

Example
ul.a {
    list-style-type: circle;
}
ul.b {
    list-style-type: square;
}
ol.c {
    list-style-type: upper-roman;
}
ol.d {
    list-style-type: lower-alpha;
}

Try it Yourself »

Note: Some of the values are for unordered lists, and some for ordered lists.

An Image as The List Item Marker
The list-style-image property specifies an image as the list item marker:

Example
ul {
    list-style-image: url('sqpurple.gif');
}

Try it Yourself »

Position The List Item Markers
The list-style-position property specifies whether the list-item markers should appear inside or outside the content flow:

Example
ul {
    list-style-position: inside;
}

Try it Yourself »

Remove Default Settings
The list-style-type:none property can also be used to remove the markers/bullets. Note that the list also has default margin and padding. To remove this, add margin:0 and padding:0 to <ul> or <ol>:

Example
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
}

Try it Yourself »

List - Shorthand property
The list-style property is a shorthand property. It is used to set all the list properties in one declaration:

Example
ul {
    list-style: square inside url("sqpurple.gif");
}

Try it Yourself »

When using the shorthand property, the order of the property values are:


  • list-style-type (if a list-style-image is specified, the value of this property will be displayed if the image for some reason cannot be displayed)
  • list-style-position (specifies whether the list-item markers should appear inside or outside the content flow)
  • list-style-image (specifies an image as the list item marker)

If one of the property values above are missing, the default value for the missing property will be inserted, if any.

Styling List With Colors
We can also style lists with colors, to make them look a little more interesting.

Anything added to the <ol> or <ul> tag, affects the entire list, while properties added to the <li> tag will affect the individual list items:

Example
ol {
    background: #ff9999;
    padding: 20px;
}
ul {
    background: #3399ff;
    padding: 20px;
}
ol li {
    background: #ffe5e5;
    padding: 5px;
    margin-left: 35px;
}
ul li {
    background: #cce5ff;
    margin: 5px;
}
Result:

Try it Yourself »

All CSS List Properties

Property
Description
list-style Sets all the properties for a list in one declaration
list-style-image Specifies an image as the list-item marker
list-style-position Specifies if the list-item markers should appear inside or outside the content flow
list-style-type Specifies the type of list-item marker

Read More »

CSS Tables

The look of an HTML table can be greatly improved with CSS:



Table Borders
To specify table borders in CSS, use the border property.

The example below specifies a black border for <table>, <th>, and <td> elements:



Example
table, th, td {
   border: 1px solid black;
}

Try it Yourself »

Notice that the table in the example above has double borders. This is because both the table and the <th> and <td> elements have separate borders.

Collapse Table Borders
The border-collapse property sets whether the table borders should be collapsed into a single border:



Example
table {
    border-collapse: collapse;
}
table, th, td {
    border: 1px solid black;
}

Try it Yourself »

If you only want a border around the table, only specify the border property for <table>:

Example
table {
    border: 1px solid black;
}

Try it Yourself »

Table Width and Height
Width and height of a table are defined by the width and height properties.

The example below sets the width of the table to 100%, and the height of the <th> elements to 50px:



Example
table {
    width: 100%;
}
th {
    height: 50px;
}

Try it Yourself »

Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or center) of the content in <th> or <td>.

By default, the content of <th> elements are center-aligned and the content of <td> elements are left-aligned.

The following example left-aligns the text in <th> elements:



Example
th {
    text-align: left;
}

Try it Yourself »

Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or middle) of the content in <th> or <td>.

By default, the vertical alignment of the content in a table is middle (for both <th> and <td> elements).

The following example sets the vertical text alignment to bottom for <td> elements:



Example
td {
    height: 50px;
    vertical-align: bottom;
}

Try it Yourself »

Table Padding
To control the space between the border and the content in a table, use the padding property on <td> and <th> elements:



Example
th, td {
    padding: 15px;
    text-align: left;
}

Try it Yourself »

Horizontal Dividers



Add the border-bottom property to <th> and <td> for horizontal dividers:

Example
th, td {
    border-bottom: 1px solid #ddd;
}

Try it Yourself »

Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:



Example
tr:hover {background-color: #f5f5f5}

Try it Yourself »

Striped Tables



For zebra-striped tables, use the nth-child() selector and add a background-color to all even (or odd) table rows:

Example
tr:nth-child(even) {background-color: #f2f2f2}

Try it Yourself »

Table Color
The example below specifies the background color and text color of <th> elements:



Example
th {
    background-color: #4CAF50;
    color: white;
}

Try it Yourself »

Responsive Table
A responsive table will display a horizontal scroll bar if the screen is too small to display the full content:



Add a container element (like <div>) with overflow-x:auto around the <table> element to make it responsive:

Example
<div style="overflow-x:auto;">
<table>
... table content ...
</table>
</div>

Try it Yourself »

CSS Table Properties

Property
Description
border Sets all the border properties in one declaration
border-collapse Specifies whether or not table borders should be collapsed
border-spacing Specifies the distance between the borders of adjacent cells
caption-side Specifies the placement of a table caption
empty-cells Specifies whether or not to display borders and background on empty cells in a table
table-layout Sets the layout algorithm to be used for a table

Read More »