Author: saqibkhan

  • Borders

    The border property creates a border around an HTML element, such as a div, image, or text. You can customize the border by changing the border stylewidthradius, and color for each side of the element. Borders help in improving the readability of the content as it provide separation of content and is also useful for highlighting important sections of the web page.

    CSS Borders Example

    You can try different ways to set the border in the below section and you can change the values as well.

    CSS Border Interactive Example

    Interactive example for CSS BordersBorder Width:  pxBorder Radius:  pxBorder Style:                      Solid                     Dashed                     Dotted                     Double                     Groove                     Ridge                     Inset                     Outset                 Border Color: 

    CSS Border Shorthand Property

    You can use the shorthand CSS border property to specify the border width, style, and color.

    Syntax

    The syntax for the border shorthand property is as follows:

    border: border-width border-style border-color | initial | inherit;

    Example

    In this example, we have used the CSS border property to set the border of a div and paragraph element.

    <html><head><style>
    
        p {
            border: solid 4px grey;
            padding: 10px;
        }
        div{
            border: darkred solid 5px;
            padding: 10px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt; Check the borders of paragraph !!!&lt;/p&gt;&lt;div&gt; Check the borders of div !!!&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Types of Border Properties

    In CSS, we can style the following border properties:

    • border-style: It specifies whether a border should be solid, dashed line, double line, or one of the other possible values.
    • border-width: It specifies the width of a border.
    • border-color: It specifies the color of a border.

    CSS border-style Property

    To specify the type of border style for an element, the border-style property is used. You can specify border style as solid, dashed, or dotted. Check out the output of the following example for all the types of border styles.

    Example

    In this example, we have used the border-style property to set the different border styles for each paragraph element.

    <!DOCTYPE html><html><head><style>
    
        .none {
            border-style: none;
        }
        .hidden {
            border-style: hidden;
        }
        .dotted {
            border-style: dotted;
        }
        .dashed {
            border-style: dashed;
        }
        .solid {
            border-style: solid;
        }
        .double {
            border-style: double;
        }
        .groove {
            border-style: groove;
        }
        .ridge {
            border-style: ridge;
        }
        .inset {
            border-style: inset;
        }
        .outset {
            border-style: outset;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Border Style Property&lt;/h1&gt;&lt;p class="none"&gt; No border.&lt;/p&gt;&lt;p class="hidden"&gt; Hidden border.&lt;/p&gt;&lt;p class="dotted"&gt; Dotted border.&lt;/p&gt;&lt;p class="dashed"&gt; Dashed border.&lt;/p&gt;&lt;p class="solid"&gt; Solid border.&lt;/p&gt;&lt;p class="double"&gt; Double border.&lt;/p&gt;&lt;p class="groove"&gt; Groove border.&lt;/p&gt;&lt;p class="ridge"&gt; Ridge border.&lt;/p&gt;&lt;p class="inset"&gt; Inset border.&lt;/p&gt;&lt;p class="outset"&gt; Outset border.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Border Style For Individual Sides

    You can specify different border styles for each side of the element. To set the border-style property for the individual sides of the element, we use the following CSS properties:

    • border-top-style: It sets the style of an element's top border.
    • border-bottom-style: It sets the style of an element's bottom border.
    • border-left-style: It sets the style of an element's left border.
    • border-right-style: It sets the style of an element's right border.

    Example

    In this example, we have used the CSS border-style property for individual sides to set the different border styles for each side of a paragraph element.

    <html><head><style>
    
        p {
            border-top-style: dotted; 
            border-right-style: solid; 
            border-bottom-style: dashed; 
            border-left-style: double;
            padding: 2em;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Border Style Individual Side&lt;/h2&gt;&lt;p&gt;Different border styles on all sides.&lt;/p&gt;&lt;/body&gt;&lt;html&gt;</pre>

    CSS border-width Property

    The border-width is used to specify the thickness of a border around an element. It can have values like thin, medium, thick, or any length (in px, or em ). Let us look at an example of this.

    Example

    In this example, we have used the CSS border-width property to set the different border widths for each paragraph element.

    <html><head><style>
    
        p.thin {
            border-style: solid; 
            border-width: thin;
            padding: 10px;
        }
        p.medium {
            border-style: solid; 
            border-width: medium;
            padding: 10px;
        }
        p.thick {
            border-style: solid; 
            border-width: thick;
            padding: 10px;
        }
        p.length {
            border-style: solid; 
            border-width: 10px;
            padding: 10px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="thin"&gt;Thin width.&lt;/p&gt;&lt;p class="medium"&gt;Medium width.&lt;/p&gt;&lt;p class="thick"&gt;Thick width.&lt;/p&gt;&lt;p class="length"&gt;Border Width: 10px.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Declare border-style property before declaring border-width property, else the border effect will not be seen.

    Border Width For Individual Sides

    You can specify different border widths for each side of an element. To set the border-width property for individual sides, use the following CSS properties:

    • border-top-width: Sets the width of the top border.
    • border-bottom-width: Sets the width of the bottom border.
    • border-left-width: Sets the width of the left border.
    • border-right-width: Sets the width of the right border.

    Example

    In this example, we have used the CSS border-width property for individual sides to set different border widths for each side of a paragraph element.

    <html><head><style>
    
        p {
            border-style: solid;
            border-top-width: thin; 
            border-right-width: thick; 
            border-bottom-width: medium; 
            border-left-width: 10px;
            padding: 2em;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Border Width Individual Sides&lt;/h2&gt;&lt;p&gt;Different border widths on all sides.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS border-color Property

    The border-color is used to set the color of the border. If no color is specified for the border, the default value is currentColor i.e. the foreground color.

    Example

    In this example, we have used the CSS border-color property to set the border colors of paragraph elements using the color name and hex value.

    <html><head><style>
    
        .name {
                border-style: dashed; 
                border-color: red;
                padding: 10px;
            }
        .hex {
                border-style: solid; 
                border-color: #00ff00;
                padding: 10px;
            }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="name"&gt;Border Color: red&lt;/p&gt;&lt;p class="hex"&gt;Border Color: #00ff00.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Declare border-style property before declaring border-color property, else the border effect will not be seen.

    Border Color For Individual Sides

    You can specify different border colors for each side of an element. To set the border-color property for individual sides, use the following CSS properties:

    • border-top-color: Sets the color of the top border.
    • border-bottom-color: Sets the color of the bottom border.
    • border-left-color: Sets the color of the left border.
    • border-right-color: Sets the color of the right border.

    Example

    In this example, we have used the CSS border-color property for individual sides to set different border colors for each side of a paragraph element.

    <html><head><style>
    
        p {
            border: solid 7px;
            border-top-color: red; 
            border-right-color: #0000ff; 
            border-bottom-color: rgb(100,123,111); 
            border-left-color: rgba(50,123,111,0.4);
            padding: 10px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Border Color Individual Sides&lt;/h2&gt;&lt;p&gt;Different border colors on all sides.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Borders Individual Side Shorthand Property

    To apply all the border properties, such as style, width, and color, to just one side of an element, we can make use of the shorthand border property for individual sides. The individual side border shorthand properties are as follows:

    • border-top property: It is a shorthand property for setting the top border properties.
    • border-bottom property: It is a shorthand property for setting the bottom border properties.
    • border-left property: It is a shorthand property for setting the left border properties.
    • border-right property: It is a shorthand property for setting the right border properties.

    Syntax

    The syntax for the individual side border shorthand properties is as follows:

    border-top: 4px solid red;border-bottom: 2px dashed blue;border-left: 6px dotted green;border-right: 8px double yellow;

    Example

    In this example, we have used the border shorthand property for individual sides to set different borders on each side.

    <html><head><style>
    
        p {
            border-top: red dashed thick;
            border-right: solid #0000ff medium;
            border-bottom: thin dotted rgb(100,123,111);
            border-left: rgba(50,123,111,0.4) 15px double;
            padding: 5px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt; Check out borders of paragraph !!!&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Override border Shorthand Property

    You can override the border shorthand property by using any individual border property. See the following sample code to clarify this point:

    div{border: 5px solid gray;border-bottom-width: 15px;}

    The above code will have a 5px thick, solid, and gray border on all the sides except for the bottom where the width will be 15px.

    Example

    Here is an example of overriding the border shorthand property:

    <html><head><style>
    
        div {
            padding: 10px;
            border: 5px solid gray;
            border-bottom-width: 15px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div&gt; Check the borders!!! &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Applying Borders to Inline Elements

    Borders can be given to any inline element in a similar manner. The border's thickness will not have any effect on the line height of the element. If left and right borders are specified in an inline element, it will displace the text around the border. Here is an example of applying a border to an inline element.

    Example

    In this example, we have used the border property on span (inline element).

    <html><head><style>
    
        span {
            border: 5px solid black;
            background-color: silver;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt; 
        Check the &lt;span&gt;inline elements&lt;/span&gt; with 
        borders and rest has no border.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS border-image Property

    To set images as borders for another element like div, span, body, paragraph, etc, you can use the border-image property. First you need to declare the border-style property before providing an image source, else no image will be displayed as the border.

    Example

    Here is an example of using the border-image property to set an image as the border of

    <html><head><style>
    
        div{
            background-color: #f0f0f0;
            border: 20px solid transparent;
            border-image: url(/css/images/border.png) 40;
            padding: 20px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div&gt;&lt;p&gt;
            This is an example of setting a 
            border image using CSS
        &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS border-radius Property

    CSS border-radius property is used to specify the roundness of border edges. The value for this property can be in length (px, em) or percentages. A border radius of 50% indicates a complete circle.

    Example

    In this example, we have created a rounded square and a circle using the border-radius property.

    <html><head><style>
    
         div{
            background-color: #00f9f9;
            height: 150px;
            width: 150px;
            text-align: center;
        }
        .round{
            border-radius: 20px;
        }
        .fullRound{
            border-radius: 50%;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="round"&gt;
        Round edged div
    &lt;/div&gt;&lt;div class="fullRound"&gt;
        Circular Div
    &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Borders All Properties

    All the properties related to the border are listed in the table below:

    PropertyDescriptionExample
    borderA shorthand property for setting all the border properties in one declarationTry It
    border-colorA shorthand property for setting border color of an element.Try It
    border-styleA shorthand property for setting style (solid / dashed) of border of an elementTry It
    border-widthA shorthand property for setting border width of an element.Try It
    border-bottomA shorthand property for setting bottom border of an element.Try It
    border-bottom-colorSets the color of bottom border of an element.Try It
    border-bottom-widthSets the width of bottom border of an element.Try It
    border-bottom-styleSets the style of bottom border of an element.Try It
    border-leftA shorthand property for setting left border of an element.Try It
    border-left-colorSets the color of left border of an element.Try It
    border-left-widthSets the width of left border of an element.Try It
    border-left-styleSets the style of left border of an element.Try It
    border-rightA shorthand property for setting right border of an element.Try It
    border-right-colorSets the color of right border of an element.Try It
    border-right-widthSets the width of right border of an element.Try It
    border-right-styleSets the style of right border of an element.Try It
    border-topA shorthand property for setting top border of an element.Try It
    border-top-colorSets the color of top border of an element.Try It
    border-top-widthSets the width of top border of an element.Try It
    border-top-styleSets the style of top border of an element.Try It
    border-imageA shorthand property for setting border image.Try It
    border-image-outsetSets the image outset i.e how much the border image area extends beyond the border box.Try It
    border-image-repeatThis property determines whether the border image should be repeated, rounded, spaced or stretched.Try It
    border-image-sourceSets the source/path of an image to be passed as a border to an element.Try It
    border-image-sliceThis property shows how to slice up an image in a border.Try It
    border-image-widthSets the width of the image to be set as a border.Try It

  • Styling Tables

    Styling tables in a webpage involves using CSS properties to customize the appearance of tables. CSS properties such as border-collapse, border-spacing, and caption-side can be applied to tables to control the borders, spacing, and alignment of the table and its cells.

    This chapter discusses how to set different properties of an HTML table using CSS.

    CSS Table Border Styling

    To style table borders, we use CSS properties like border and border-radius. You can set the border’s width, color, and style with border property on the table, rows, or individual cells.

    • border: CSS border property sets the width, style, and color of all four sides of the table border (e.g., border: 1px solid black;).
    • border-radius: CSS border-radius property rounds the corners of the table border (e.g., border-radius: 5px|50%).

    Example

    In this example, we have set the rounded table border using CSS border and border-radius property.

    <!DOCTYPE html><html><head><style>
    
        table {
            border-radius: 10px;
            border: 2px solid #031926;
            width: 100%;
        }
        td {
            border: 1px solid black;
            height: 50px;
            vertical-align: middle;
            text-align: center;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;th&gt;Header 4&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Table Border Collapse

    To merge the cell borders of the table or keep the cell borders separated, the border-collapse property is used. To merge the cell borders, collapse value is used while separate value keeps the border distinct.

    Example

    In the following example, we have used border-collapse property with collapse and separate values.

    <!DOCTYPE html><html><head><style>
    
        table {
            border: 3px solid purple;
        }
        th, td {
            border: 1px solid black;
            padding: 6px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt; border-collapse: separate &lt;/h2&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h2&gt; border-collapse: collapse &lt;/h2&gt;&lt;table style="border-collapse: collapse;"&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Table Border Spacing

    To set the distance separating adjacent cell borders in a table, the border-spacing property is used. This property may be specified as either one or two values.

    • border-spacing: 2px: It applies 2px spacing to both vertical and horizontal borders.
    • border-spacing: 1cm 2em: In this case, the first value defines the horizontal spacing between cells (i.e., the space between cells in adjacent columns), and the second value defines the vertical spacing between cells (i.e., the space between cells in adjacent rows).

    Example

    In this example, we have set the border-spacing property to 1em horizontally and 0.5em vertically.

    <!DOCTYPE html><html><head><style>
    
        table {
            border-collapse: separate;
            border-spacing: 1em 0.5em;
            border: 3px solid;
        }
        td {
            width: 1.5em;
            height: 1.5em;
            border: 1px solid black;
            text-align: center;
            vertical-align: middle;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Note: The border-spacing property only works when border-collapse is set to separate. If you set border-collapse to collapse, the border-spacing property will have no effect, and the borders will collapse into a single line.

    CSS Tables Caption Side

    To control the placement of the table caption, the caption-side property is used.

    Example

    In this example, we have used the caption-side property to place the table caption at the top and bottom of the table in the first and second table respectively.

    <!DOCTYPE html><html><head><style>
    
        .top caption {
            caption-side: top;
        }
        .bottom caption {
            caption-side: bottom;
        }
        table {
            border: 1px solid red;
        }
        td {
            border: 1px solid blue;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;table class="top"&gt;&lt;caption&gt;
            Caption ABOVE the table
        &lt;/caption&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;table class="bottom"&gt;&lt;caption&gt;
            Caption BELOW the table
        &lt;/caption&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Tables Empty Cells

    To render the empty cells of the table, the empty-cells property is used. The empty-cell property is applied only to tables and table cells. It has the following two values:

    • show: It is the default value that indicates that empty cells should be shown with borders and spacing as if they contained content.
    • hide: It indicates that empty cells should be hidden and not take up any space. Borders and spacing for empty cells will not be displayed.

    Example

    In this example, we have used the empty-cells property with show and hide value to show and hide the empty cells respectively.

    <!DOCTYPE html><html><head><style>
    
        table {
            width: 350px;
            border-collapse: separate;
            empty-cells: show;
        }
        td,th {
            padding: 5px;
            border-style: solid;
            border-width: 1px;
            border-color: #999999;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt; Empty Cells: Show &lt;/h2&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h2&gt; Empty Cells: Hide &lt;/h2&gt;&lt;table style="empty-cells: hide;"&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Table Layout

    CSS table-layout property is used to control how a browser should render a table. It defines the algorithm used to lay out table cells, rows, and columns. It has the following property values:

    • auto: It is the default value where the browser calculates the width of columns and cells based on the content.
    • fixed: It sets a fixed column width based on the specified table or column width. If width isn't specified, then the first row is used to determine column width and the rest of the rows follow the same column widths irrespective of the content.

    Example

    In this example, the table-layout property is used with auto and fixed values.

    <!DOCTYPE html><html><head><style>
    
        table {
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            border: 1px solid black;
            padding: 8px;
            text-align: center;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Table with fixed layout&lt;/h2&gt;&lt;table style="table-layout: fixed; "&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Row 1, Column 1&lt;/td&gt;&lt;td&gt;Row 1, Column 2&lt;/td&gt;&lt;td&gt;Row 1, Column 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Row 2, Column 1&lt;/td&gt;&lt;td&gt;Row 2, Column 2&lt;/td&gt;&lt;td&gt;Row 2, Column 3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h2&gt;Table with auto layout&lt;/h2&gt;&lt;table style="table-layout: auto; "&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;This is some longer content in Column 1&lt;/td&gt;&lt;td&gt;Short content&lt;/td&gt;&lt;td&gt;Even longer content that might wrap in Column 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Row 2, Column 1&lt;/td&gt;&lt;td&gt;Row 2, Column 2&lt;/td&gt;&lt;td&gt;Row 2, Column 3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Note: The fixed table layout is faster than auto, as the fixed value does not depend on the table's content but rather on the table's width.

    CSS Table Contents Alignment

    To align the content of table cells, CSS properties such as text-align and vertical-align properties are used.

    • text-align: It sets the horizontal alignment of the text content within table cells. It can have values like left, right, center, and justify.
    • CSS vertical-align: It sets the vertical alignment of the content within table cells. It can have values like top, bottom, and middle.

    Example

    In this example, we have used the text-align and vertical-align properties to align the text content horizontally and vertically.

    <!DOCTYPE html><html><head><style>
    
        table, td, th {
            border: 2px solid black;
        }
        table {
            border-collapse: collapse;
        }
        td {
            width: 100px;
            height: 70px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;th&gt;Header 4&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td style="text-align: center;"&gt;Data Center&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td style="vertical-align: bottom"&gt;Data Bottom&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td style="vertical-align: top"&gt;Data Top&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Note: The default horizontal alignment of <th> and <td> is center and left respectively. The default vertical alignment of both <th> and <td> is middle.

    CSS Tables Background color

    To set the background color of the cell, row, or entire table, the CSS background-color property is used.

    Syntax

    The syntax for setting the background color of the cell, row, and table is as follows:

    /* To set the background color of table */table{background-color: #f2f2f2;}/* To set the background color of a cell a row */td{background-color: #f2f2f2;}/* To set the background color of a row */tr{background-color: #ffffff;}

    Example

    In this example, we have set the background color of the table using the element selector. To set the background color of the row and a cell, we have used inline CSS.

    <!DOCTYPE html><html><head><style>
    
        table {
            border: 2px solid black;
            background-color: rgb(237, 181, 237);
            width: 100%;
            border-collapse: collapse;
        }
        td {
            height: 50px;
            vertical-align: middle;
            text-align: center;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Background color property&lt;/h2&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;th&gt;Header 4&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td style="background-color: #f0f0ff;"&gt; Data 2&lt;/td&gt;&lt;td&gt;Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr style="background-color: #04af2f;"&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Table Text Font Styles

    To style the text content of the table, we can use CSS font properties such as font-size, font-family, font-weight, etc. on the table elements.

    Example

    In this example, we have set the font-size and font-family of the th and td tags.

    <!DOCTYPE html><html><head><style>
    
        table.one {
            border-collapse: collapse;
            width: 400px;
        }
        th {
            font-size: large;
            font-family: 'Lucida Sans', sans-serif;
        }
        td {
            font-size: small;
            font-family: Verdana, Geneva, Tahoma, sans-serif;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;font styles&lt;/h2&gt;&lt;div&gt;&lt;table class = "one" border = "1"&gt;&lt;th&gt;Heading&lt;/th&gt;&lt;tr&gt;&lt;td&gt; Cell value&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Cell value&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Table Dividers

    Dividers in a table are used to separate the content of the table and make it more readable. To set a vertical divider, the border-right property is used whereas border-bottom property is used to set a horizontal divider.

    Example

    In this example, we have set the vertical divider using the border-right property and the horizontal divider using the border-bottom property.

    <!DOCTYPE html><html><head><style>
    
        table {
            border: 2px solid black;
            background-color: rgb(200, 240, 210);
            border-collapse: collapse;
            width: 100%;
        }
        th {
            border-bottom: 2px solid black;
            padding: 5px;
        }
        td {
            border-bottom: 1px solid grey;
            padding: 5px;
            text-align: center;
        }
        .vDiv {
            border-right: 2px solid black;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Horizontal Divider&lt;/h2&gt;&lt;table&gt;&lt;tr&gt;&lt;th class="vDiv"&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="vDiv"&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="vDiv"&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class="vDiv"&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Striped Table

    A striped table is a table that has alternating background colors for rows, making it easier to read and understand the data. To create a striped table, the nth-child selector is used to apply different background colors to odd and even rows.

    Example

    In this example, we have used the nth-child selector to set the green and light green background color of the cells creating a striped table.

    <!DOCTYPE html><html><head><style>
    
        table {
            border-collapse: collapse;
            width: 100%;
            color: white;
        }
        th, td {
            text-align: left;
            padding: 8px;
        }
        tr:nth-child(odd) {
            background-color: rgba(4, 175, 47, 1);
        }
        tr:nth-child(even) {
            background-color: rgba(4, 175, 47, 0.4);
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Striped table&lt;/h2&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;th&gt;Header 4&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt; Data 1&lt;/td&gt;&lt;td&gt; Data 2&lt;/td&gt;&lt;td&gt; Data 3&lt;/td&gt;&lt;td&gt; Data 4&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Responsive Table

    A responsive table refers to a table that adjusts and adapts its layout and formatting based on different screen sizes and resolutions. You can use the overflow-x property to add a horizontal scroll bar to the table in small screen sizes when the entire screen is not seen.

    Example

    In this example, we have used overflow-x property to add a horizontal scroll to adjust to smaller screens.

    <!DOCTYPE html><html lang="en"><head><meta name="viewport" content="width=device-width, 
    
          initial-scale=1.0"&gt;&lt;style&gt;
        .responsive-table {
            width: 100%;
            border-collapse: collapse;
            overflow-x: auto;
            display: block;
        }
        .responsive-table th, .responsive-table td {
            text-align: left;
            padding: 8px;
            border: 1px solid #ddd;
        }
        .responsive-table th {
            background-color: #f2f2f2;
        }
        .responsive-table tr:nth-child(odd) {
            background-color: #f9f9f9;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Responsive Table&lt;/h2&gt;&lt;table class="responsive-table"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Header 1&lt;/th&gt;&lt;th&gt;Header 2&lt;/th&gt;&lt;th&gt;Header 3&lt;/th&gt;&lt;th&gt;Header 4&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Data 1&lt;/td&gt;&lt;td&gt;Data 2&lt;/td&gt;&lt;td&gt;Data 3&lt;/td&gt;&lt;td&gt;Data 4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data 5&lt;/td&gt;&lt;td&gt;Data 6&lt;/td&gt;&lt;td&gt;Data 7&lt;/td&gt;&lt;td&gt;Data 8&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Data 9&lt;/td&gt;&lt;td&gt;Data 10&lt;/td&gt;&lt;td&gt;Data 11&lt;/td&gt;&lt;td&gt;Data 12&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Links

    Links are used to navigate from one webpage to other with a single click. We can use CSS properties to style links in various ways.

    CSS Links Example

    We can create a link in webpage using <a> tag. With CSS we can style this to built text link, button link and image link as shown below.

    CSSCSS Button

    Table of Contents

    • States of Link
    • Default Styles of Links
    • CSS Text Link
    • Styling States of Link
    • CSS Button Links
    • CSS Image Links

    A link in a webpage exist in various states, and these states of link can be styled using pseudo-classes in CSS. Following are common states of link.

    • Link: Represents unvisited links. This state of links can be styled with :link pseudo class. (This is default state for anchor tag).
    • Visited: Represents the links that are already visited (present in browser’s history). This state of links can be styled with :visited pseudo class.
    • Hover: Represents the state when user hover’s the mouse pointer over link. This state of links can be styled with :hover pseudo class.
    • Active: Represents the state when user clicks the link. This state of links can be styled with :active pseudo class.

    Following are default styles applied to a link in webpage. You can modify this style using CSS.

    • All the links will be underlined. You can remove this by setting text-decoration property to ‘none’.
    • All the unvisited links will be in blue color and visited links will be purple. You can use color property to modify this
    • The mouse pointer changes to a little hand icon when you hover over a link. You can use cursor property to modify this.
    • Hovered links will be underlined and active links are styled in red.

    A text content when clicked will navigate to a different webpage or a section of same page is called text link. Following example show how to make a text link.

    Example

    <html><head><style>
    
        body{
            padding: 10px;
        }
        a {
            color: blue; 
            text-decoration: none; 
        }
        a:hover {
            text-decoration: underline;
            transform: scale(1.1);   
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;a href="/index.htm"&gt; Click Me &lt;/a&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    As mentioned above in this example we have used pseudo-classes to style different states of a link.

    Example

    <html><head><style>
    
        body {
            padding: 10px;
            font-size: 1.2rem;
            font-family: sans-serif;
        }
        a {
            display: inline-block;
            transition: transform 0.2s ease;
        }
        a:link {
            color: green; 
            text-decoration: none;
        }
        a:visited {
            color: purple; 
        }
        a:hover {
            text-decoration: underline;
            transform: scale(1.1)
        }
        a:active {
            color: black;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt; Select course &lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="/html/index.htm" target="_blank"&gt; 
            HTML 
        &lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="/css/index.htm" target="_blank"&gt; 
            CSS 
        &lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="/python/index.htm" target="_blank"&gt; 
            Python 
        &lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    In CSS we can styles links in such a way that it looks like a clickable button. Following example shows this.

    Example

    <html><head><style>
    
        body {
            display: flex;
            justify-content: space-around;
            padding: 10px;
            height: 100px;
        }
        .button {
            display: inline-block;
            color: white;
            background-color: blue;
            height: 20%;
            padding: 10px 20px;
            text-align: center;
            text-decoration: none;
            border-radius: 5px;
            transition: all 0.3s ease;
        }
        .button:hover {
            background-color: darkblue;
            transform: scale(1.1);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;a class="button" href="/css/index.htm" target="_blank" &gt; 
        CSS
    &lt;/a&gt;&lt;a class="button" href="/html/index.htm" target="_blank" &gt; 
        HTML 
    &lt;/a&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    We can also attach links on images displayed in webpage. Following example shows this.

    Example

    <html><head><style>
    
        body {
            display: flex;
            justify-content: space-around;
            padding: 10px;
            height: 100px;
        }
        a img {
            transition: all 0.3s ease;
            border-radius: 5px;
        }
    
        a:hover img {
            transform: scale(1.05);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;a href="/css/index.htm" target="_blank" &gt;&lt;img src="/css/images/css.png" 
              alt="css-image-link" height="60px" &gt;&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Images

    In this article, we will learn about how to style an image to change its shape, size, and layout by using different CSS properties such as padding, borders, height, width, margins, etc.

    Table of Contents

    • Round Cornered Images
    • Circular And Elliptical Images
    • Bordered Images
    • Image Filters
    • Image as a Card
    • Center an Image
    • Text Inside Image
    • Image Fade In Overlay

    Round Cornered Images

    The following example demonstrates how to use border-radius property to create rounded border image.

    Example

    <!DOCTYPE html><html><head><style>
    
        img {
            width: 100px;
            height: 100px;
            border-radius: 20px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;img src="/css/images/pink-flower.jpg" alt="pink-flower"&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Circular And Elliptical Images

    The following example demonstrates how to use border-radius: 50% property to create images in circle and ellipse shape. When height and width of image are same we will get a circular image and when they are not same we will get elliptical images.

    Example

    <!DOCTYPE html><html><head><style>
    
        img {
            width: 100px;
            height: 100px;
            border-radius: 50%;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;img src="/css/images/pink-flower.jpg" alt="pink-flower"&gt;&lt;img src="/css/images/pink-flower.jpg" 
         style="height: 50px" alt="pink-flower"&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Bordered Images

    The following example demonstrates how to create a border around any images.

    Example

    <!DOCTYPE html><html><head><style>
    
        img {
            border: 2px solid red;
            border-radius: 10px;
            border: 7px solid black;
            width: 150px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;img src="/css/images/pink-flower.jpg" alt="pink-flower"&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Image Filters

    The following example demonstrates that different filter effects are applied to an image using filter property.

    Example

    <!DOCTYPE html><html><head><style>
    
        img {
            width: auto;
            height: 50px;
            margin: 10px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h4&gt;Blur Filter&lt;/h4&gt;&lt;img style="filter: blur(3px);" src="/css/images/pink-flower.jpg"&gt;&lt;h4&gt;Invert Filter&lt;/h4&gt;&lt;img style="filter: invert(110%);" src="/css/images/pink-flower.jpg"&gt;&lt;h4&gt;Saturate Filter&lt;/h4&gt;&lt;img style="filter: saturate(8);" src="/css/images/pink-flower.jpg"&gt;&lt;h4&gt;Sepia Filter&lt;/h4&gt;&lt;img style="filter: sepia(110%);" src="/css/images/pink-flower.jpg"&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Image as a Card

    The following example demonstrates a responsive polaroid-styled image with a shadow effect.

    <!DOCTYPE html><html><head><style>
    
        .polaroid-image {
            width: 60%;
            height: 200px;
            background-color: white;
            box-shadow: 0 3px 6px 0 grey, 0 8px 16px 0 black;
            margin-bottom: 25px;
            margin: 20px;
        }
        img {
            width: 100%;
            height: 150px;
        }
        .box {
            text-align: center;
            padding: 5px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="polaroid-image"&gt;&lt;img src="/css/images/red-flower.jpg" alt="Flower"&gt;&lt;div class="box"&gt;&lt;p&gt;Tree&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Center an Image

    There are several way to center an image inside a container, most popular way is to use flex layout with justify-content and align-items properties.

    • justify-content: center: This will center image horizontally
    • align-items: center: This will center image vertically

    Example

    In this example we used flex layout to center an image

    <!DOCTYPE html><html lang="en"><head><style>
    
        .container {
            display: flex;          
            justify-content: center; 
            align-items: center;    
            height: 300px;           
            border: 2px solid black; 
        }
        img {
            max-width: 100%;        
            height: auto;         
            border: 1px solid;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;img src="/css/images/logo.png"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Text Inside Image

    In CSS position property can be used to position text in different locations inside an image.

    First we need to set position property for image container as position: relative and position property of text as position: absolute. Now you can position of text elements using inset properties like top, bottom, right and left.

    Example

    <!DOCTYPE html><html><head><style>
    
        .container {
            position: relative;
            border: 2px solid #ef2c2c;
        }
        .center {
            position: absolute;
            top: 45%;
            width: 100%;
            text-align: center;
        }
        .top-left {
            position: absolute;
            top: 12px;
            left: 30px;
        }
        .top-right {
            position: absolute;
            top: 12px;
            right: 30px;
        }
        .bottom-left {
            position: absolute;
            bottom: 12px;
            left: 30px;
        }
        .bottom-right {
            position: absolute;
            bottom: 12px;
            right: 30px;
        }
        img {
            width: 100%;
            opacity: 0.7;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;img src="/css/images/red-flower.jpg" 
                width="1000px" height="350px"&gt;&lt;h3 class="center"&gt;
            Text at Center
        &lt;/h3&gt;&lt;h3 class="top-left"&gt;
            Text at Top Left
        &lt;/h3&gt;&lt;h3 class="top-right"&gt;
            Text at Top Right
        &lt;/h3&gt;&lt;h3 class="bottom-left"&gt;
            Text at Bottom Left&lt;/h3&gt;&lt;h3 class="bottom-right"&gt;
            Text at Bottom Right
        &lt;/h3&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Image Fade In Overlay

    Fade in overlay effect shows text when you hover over the image. There are several other overlay effects, for complete understanding checkout our tutorial on CSS overlay effects.

    Let's look at an example for image fade in overlay effect.

    Example

    <!DOCTYPE html><html><head><style>
    
        .img-container {
                position: relative;
                width: 250px; 
        }
        .img-overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7); 
            opacity: 0;
            transition: opacity 0.4s ease; 
        }
        .overlay-text {
            color: red;
            font-weight: bold;
            font-size: 25px;
            position: absolute;
            top: 40%;
            left: 20%;
        }
        .img-container:hover .img-overlay {
            opacity: 1;
        }
        img {
            width: 100%;
            height: 200px;
            display: block;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="img-container"&gt;&lt;div class="img-overlay"&gt;&lt;div class="overlay-text"&gt;TutorialsPoint&lt;/div&gt;&lt;/div&gt;&lt;img src="/css/images/see.jpg" alt="See Image"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Styling Text

    CSS Styling Text involves modifying the appearance of text content by setting proper color, alignment, letter-spacing, and indention to make it more visually appealing. This chapter demonstrates how to style texts on web pages using CSS properties.

    How to Style Text in CSS?

    We can style text in the following ways to style texts in CSS.

    • Change Color: The default color of text on a webpage will be black. You can change this according to your webpage theme, using the color property in CSS.
    • Set Alignment: You can use the text-align property in CSS to specify the alignment (center, left, right) of text inside a container.
    • Text Decoration: The text-decoration property in CSS can be used to add effects like underline, overline, or strike-through to texts.
    • Shadow Effect: If you want to create a shadow around text in your webpage you can use text-shadow property in CSS. This can create a 3D effect or a subtle glow around the text.
    • Change Font Style: The font-style property allows you to style the text as normal, italic, or oblique.

    Properties for Styling Text

    The following table lists CSS properties for styling text:

    PropertyDescription
    colorSets the color of the text.
    text-alignSets the alignment of the text.
    text-align-lastSets the alignment of the last line of a block of text.
    directionSets the direction of the text.
    text-indentSets the indentation of the first line of the text.
    letter-spacingSpecifies the space between the letters of a word.
    word-spacingSpecifies the space between the words in a block of text.
    white-spaceControls the white space flow inside the text in an element.
    text-decorationA shorthand property for setting the text decoration.
    text-decoration-lineIt specifies the type of line the text decoration will have.
    text-decoration-styleIt specifies the type of line drawn as a text decoration such as solid, wavy, dotted, dashed, or double.
    text-decoration-colorIt sets the color of the text-decoration line.
    text-decoration-thicknessIt sets the thickness of the text-decoration line.
    text-justifyIt controls how spaces are distributed between words and characters to align text.
    text-transformTransforms the text in either uppercase, lowercase, or capitalize.
    line-heightIt controls the amount of space between lines of text within an element.
    text-emphasisApplied emphasis marks to text.
    text-shadowAdds shadow to the text.
    line-breakControls how to set the rule for a line break.
    word-breakControls how to set the rule for a word break.
    text-combine-uprightCombines multiple typographic character units into the space of a single typographic character unit.
    text-orientationSets the orientation of the text characters in a line.
    text-underline-offsetAdds special visual effects to the text.
    text-overflowControls how hidden overflow content is displayed to users.

    Setting Text Color in CSS

    To set the text color, you can use the color property. The color can be specified with color name, Hexadecimal value, rgb/rgba value, or hsl/hsla value.

    Example

    In this example, we have set the text color of paragraphs using the color property.

    <!DOCTYPE html><html><head><style>
    
        .name {
            color: blueviolet;
        }
        .hex {
            color:#ff00ff;
        }
        .rgb {
            color: rgb(255,124,100);
        }
        .rgba {
            color: rgb(255,124,100,0.5);
        }
        .hsl {
            color: hsl(7deg, 98%, 50%);
        }
        .hsla {
            color: hsl(7deg, 98%, 50%, 0.5);
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="name"&gt;
        Color Name: blueviolet;
    &lt;/p&gt;&lt;p class="hex"&gt;
        Hexadecimal value: #ff00ff;
    &lt;/p&gt;&lt;p class="rgb"&gt;
        RGB value: rgb(255, 124, 100);
    &lt;/p&gt;&lt;p class="rgba"&gt;
        RGBA value: rgba(255, 124, 100, 0.5);
    &lt;/p&gt;&lt;p class="hsl"&gt;
        HSL value: hsla(7deg, 98%, 50%);
    &lt;/p&gt;&lt;p class="hsla"&gt;
        HSLA value: hsla(7deg, 98%, 50%, 0.5);
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Text Alignment

    The text in a webpage can be aligned horizontally and vertically inside a container. To set the alignment of the text, we use the following CSS properties.

    • The text-align property specifies the horizontal alignment of text in a container (left, right, center, justify).
    • The vertical-align property is used to align the text vertically at the top, bottom, baseline, and middle. This property is used with inline elements, inline-block elements, and table cells.

    Example

    In this example, we have used text-align and vertical-align properties to set the horizontal and vertical alignment of the text.

    <!DOCTYPE html><html><head><style>
    
        th{
            vertical-align: bottom;
            border: 2px solid; 
            width: 200px; 
            height: 150px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Text Alignment&lt;/h2&gt;&lt;p style="text-align: left;"&gt;Text Left Alignment.&lt;/p&gt;&lt;p style="text-align: right;"&gt;Text Right Alignment.&lt;/p&gt;&lt;p style="text-align: center;"&gt;Text Center Alignment.&lt;/p&gt;&lt;table&gt;&lt;th&gt;This is vertical alignment&lt;/th&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Aligning the Last Line in CSS

    To set the alignment for the last line of a block of text, you can use the text-align-last property. It is useful when text-align: justify; is used to align the last line properly.

    Example

    In this example, we have used the text-align-last property to align the last line differently.

    <!DOCTYPE html><html><head><style>
    
        p {
            width: 300px;
            text-align: justify;
            text-align-last: right;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;text-align-last property&lt;/h2&gt;&lt;p&gt;This is an example of text alignment. The last line of this paragraph will be aligned differently than the rest of the text.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Aligning Text with text-justify

    The text-justify property specifies the justification method of text when text-align: justify; is applied. The text remains unaffected in modern browsers. Try running the code in Firefox.

    Example

    In this example, we have used the text-justify property to control text justification.

    <!DOCTYPE html><html><head><style>
    
        p {
            width: 300px;
            text-align: justify;
            text-justify: inter-word;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;text-justify property&lt;/h2&gt;&lt;p&gt;This paragraph demonstrates how the text-justify property works. It adjusts the spacing between words.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Text Direction in CSS

    The text direction refers to the orientation of text characters in a document or element. You can use the direction property to set the text direction. This property accepts two values which are as follows:

    • ltr (Left-to-Right): It is the default value, used for languages that are written from left to right, like English.
    • rtl (Right-to-Left): It is used for languages that are written from right to left, such as Arabic or Hebrew. When using rtl, the text will be aligned right by default.

    Additionally, CSS provides a shorthand property, unicode-bidi, to control the bidi algorithm, which specifies how characters with different writing directions are displayed when they appear in the same paragraph.

    Example

    In this example, we have used the direction property to set the text direction of paragraphs.

    <!DOCTYPE html><html><body><p style = "direction: rtl;">
    
        Right to Left
    &lt;/p&gt;&lt;p style = "direction: ltr;"&gt;
        Left to Right
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Applying Text Decoration in CSS

    You can use the text-decoration property for adding extra decoration to the text, such as adding a line (underline, strikethrough, overline), color, style, and thickness to the line.

    It is a shorthand property for text-decoration-linetext-decoration-styletext-decoration-color, and text-decoration-thickness.

    Syntax

    The syntax for the text-decoration property is as follows:

    text-decoration: text-decoration-line text-decoration-color text-decoration-style text-decoration-thickness | initial | inherit;

    Example

    In this example, we have used the text-decoration property to apply overline, line-through, and underline styles with different colors, thicknesses, and line types.

    <!DOCTYPE html><html><body><h2>Text Decoration</h2><p style="text-decoration: overline solid red 5px;">
    
        Overline text decoration.
    &lt;/p&gt;&lt;p style="text-decoration: line-through solid green 1px;"&gt;
        Line-through text decoration.
    &lt;/p&gt;&lt;p style="text-decoration: underline dashed 2pt blue;"&gt;
        Underline text decoration.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Using CSS text-transform for Text Styling

    To change the appearance of text, the text-transform property is used. It transforms the text in various ways such as converting the text to uppercase, or lowercase, capitalizing the first letter of each word, or even capitalizing all letters.

    Example

    In this example, we have used the text-transform property to transform the text to uppercase, lowercase, and capitalize.

    <!DOCTYPE html><html><head><style>
    
        p{
            font-family: arial, sans-serif;
            font-size: 1em;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Text Transform&lt;/h2&gt;&lt;p style="text-transform: capitalize;"&gt;
        capitalizes the first character of each word.
    &lt;/p&gt;&lt;p style="text-transform: uppercase;"&gt;
        Transforms all text to uppercase.
    &lt;/p&gt;&lt;p style="text-transform: lowercase;"&gt;
        Transforms all text to Lowercase.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Adding Text Emphasis in CSS

    To apply emphasis marks on a block of text, you can use the text-emphasis property. These marks are typically used to highlight specific content or to indicate pronunciation or stress in certain languages.

    It is a shorthand property for text-emphasis-style and text-emphasis-color.

    Example

    In this example, we have used the text-emphasis property to apply emphasis marks to the text.

    <!DOCTYPE html><html><head><style>
    
        p{
            font-family: arial, sans-serif;
            font-size: 1em;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Text Emphasis&lt;/h2&gt;&lt;p style="text-emphasis: dot;"&gt; 
            The text is emphasized using dot.
        &lt;/p&gt;&lt;p style="text-emphasis: circle red;"&gt;
            The text is emphasized using red circle.
        &lt;/p&gt;&lt;p style="text-emphasis: triangle;"&gt; 
            The text is emphasized using triangle.
        &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Text Indentation Property

    To add space between the margin and the first line of text, you can use the text-indent property. A proper indentation improves the readability and clarity of text on a page.

    Example

    In this example, we have used the text-indent property to indent the first line of the text of the paragraphs.

    <html><body><h2>Text indentation</h2><p style="text-indent: 2cm;">Text indentation: 2 cm.</p><p style="text-indent: 2in;">Text indentation: 2 inches.</p><p style="text-indent: 20%;">Text indentation: 20%.</p></body></html>

    Letter Spacing in CSS

    To adjust the space between the letters of a text, you can use the letter-spacing property. The space can be increased or decreased between the letters.

    Example

    In this example, we have used the letter-spacing property to increase and decrease the space between the letters of the text.

    <!DOCTYPE html><html><body><h2>Letter spacing</h2><p style="letter-spacing: normal;">
    
        Letter spacing normal.
    &lt;/p&gt;&lt;p style="letter-spacing: 5px;"&gt;
        Letter spacing increased.
    &lt;/p&gt;&lt;p style="letter-spacing: -1px;"&gt;
        Letter spacing decreased.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Applying word-spacing in CSS

    The word-spacing property is used to adjust the space between the words of a text. The space can be increased or decreased between the words.

    Example

    In this example, we have used the word-spacing property to increase and decrease the space between the words of the text.

    <!DOCTYPE html><html><body><h2>Word spacing</h2><p style="word-spacing: normal;">
    
        Word spacing normal.
    &lt;/p&gt;&lt;p style="word-spacing: 100px;"&gt;
        Word spacing increased.
    &lt;/p&gt;&lt;p style="word-spacing: -2px;"&gt;
        Word spacing decreased.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Wrapping Text with white-space Property

    The white-space property controls how you can handle the white space inside an element. It allows you to manage the handling of spaces, tabs, and line breaks in the text.

    Example

    Here is an example of the white-space property using different values to control the white space inside the text.

    <!DOCTYPE html><html><head><style>
    
        p{
            border: 2px solid;
            padding: 5px;
            width: 50%;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;White-space property&lt;/h2&gt;&lt;p style="white-space: normal;"&gt;
        This is a paragraph with the white-space property set 
        to normal. The text will wrap when necessary, and 
        extra    spaces and line breaks are    ignored.
    &lt;/p&gt;&lt;p style="white-space: nowrap;"&gt;
        This is a paragraph with the white-space property set
        to nowrap. The text will not wrap to the next line, even 
        if it overflows the container.
    &lt;/p&gt;&lt;p style="white-space: pre;"&gt;
        This is a paragraph with white-space property set to pre.
        The text will respect all   spaces and line breaks. Means,
        the   text will be displayed as it is in     HTML code.
    &lt;/p&gt;&lt;p style="white-space: pre-wrap;"&gt;
        This is a paragraph with the white-space property set to
        pre-wrap. The text will respect all spaces and line breaks,
        but will wrap when necessary.
    &lt;/p&gt;&lt;p style="white-space: pre-line;"&gt;
        This is a paragraph with the white-space property set 
        to pre-line. The text will collapse spaces and wrap when 
        necessary, but will respect line breaks.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Line-Break Property

    To control how line breaks are handled in text, we use the CSS line-break property. This property is useful for handling line breaks in languages like Japanese, Chinese, or Korean. You can use values such as auto, loose, normal, strict, and anywhere with this property.

    Example

    In this example, we have used the line-break property with different values to control how the text breaks across lines based on language rules.

    <!DOCTYPE html><html><head><style>
    
        p{
            border: 2px solid;
            padding: 5px;
            width: 50%;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Line-break property&lt;/h2&gt;&lt;p style="line-break: auto;"&gt;
        This paragraph uses the line-break property set to auto.
        Line breaking is determined based on the default rules.
    &lt;/p&gt;&lt;p style="line-break: loose;"&gt;
        This paragraph uses the line-break property set to loose.
        Line breaking is done more frequently, typically used in 
        CJK (Chinese, Japanese, Korean) text.
    &lt;/p&gt;&lt;p style="line-break: normal;"&gt;
        This paragraph uses the line-break property set to normal.
        Line breaking follows the standard rules for the language 
        being used.
    &lt;/p&gt;&lt;p style="line-break: strict;"&gt;
        This paragraph uses the line-break property set to strict.
        Line breaking is restricted, typically preventing breaks 
        between characters that are normally kept together.
    &lt;/p&gt;&lt;p style="line-break: anywhere;"&gt;
        This paragraph uses the line-break property set to anywhere.
        Line breaks can happen at any point, even if it means 
        breaking words in unconventional places.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Word-Break Property

    The word-break property controls the behavior of word breaking and word wrapping in text. It handles the words when they are too long to fit within their container. You can use values such as normal, break-all, keep-all, and break-word with this property.

    Example

    In this example, we have used the word-break property with different values to control how the word breaks to fit their container.

    <!DOCTYPE html><html><body><h2>Word-break property</h2><p style="word-break: normal;">
    
        This paragraph uses the word-break property set to &lt;strong&gt;normal&lt;/strong&gt;. 
        Words will break only at normal word boundaries (such as 
        spaces or hyphens).
    &lt;/p&gt;&lt;p style="word-break: break-all;"&gt;
        This paragraph uses the word-break property set to &lt;strong&gt;break-all&lt;/strong&gt;.
        Words will break at any character to prevent overflow, 
        even in the middle of a word.
    &lt;/p&gt;&lt;p style="word-break: keep-all;"&gt;
        This paragraph uses the word-break property set to &lt;strong&gt;keep-all&lt;/strong&gt;.
        Words will only break at normal word boundaries, but CJK text 
        characters will not break unless necessary.
    &lt;/p&gt;&lt;p style="word-break: break-word;"&gt;
        This paragraph uses the word-break property set to &lt;strong&gt;break-word&lt;/strong&gt;.
        Words will break at normal boundaries or wherever necessary 
        to prevent overflow.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS line-height Property

    To specify the height of a line of a text, you can use the CSS line-height property . It controls the spacing between lines.

    Example

    In this example, we have used the line-height property to increase the space between lines.

    <!DOCTYPE html><html><head><style>
    
        p {
            width: 300px;
            line-height: 2;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;line-height property&lt;/h2&gt;&lt;p&gt;This paragraph has a line height of 2, making the text more readable and spaced out.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Creating Text Shadows with CSS

    The text-shadow property is used to add a shadow effect to the text. It allows you to create a shadow behind the text by specifying the shadow's horizontal and vertical offsets, blur radius, and color.

    You can apply multiple shadows by separating each shadow with a comma. The order of the shadows matters, with the first shadow being closest to the text.

    Example

    In this example, we have used the text-shadow property to apply different shadow effects to the paragraphs, including single and multiple shadows with different offsets, colors, and blur radii.

    <!DOCTYPE html><html><head><style>
    
        .txt1 {
            text-shadow: 2px 2px 5px grey;
        }
        .txt2 {
            text-shadow: -2px -2px 3px red;
        }
        .txt3 {
            text-shadow: 3px 3px 0px blue, -3px -3px 0px yellow;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Text-shadow property&lt;/h2&gt;&lt;p class="txt1"&gt;
        This text has a grey shadow with a blur radius of 5px.
    &lt;/p&gt;&lt;p class="txt2"&gt;
        This text has a red shadow with a blur radius of 3px 
        and offset to the top-left.
    &lt;/p&gt;&lt;p class="txt3"&gt;
        This text has two shadows: a blue shadow offset to the 
        bottom-right and a yellow shadow offset to the top-left.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS text-orientation Property

    To control the text orientation in vertical writing modes, we use the text-orientation property.

    Example

    In this example, we have used the text-orientation property to rotate the text vertically.

    <!DOCTYPE html><html><head><style>
    
        p {
            writing-mode: vertical-rl;
            text-orientation: upright;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;text-orientation property&lt;/h2&gt;&lt;p&gt;This text is displayed in a vertical layout using text-orientation.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Fonts

    CSS fonts allows to customize font style of webpages using various font related properties. In this tutorial we will understand the font styling in CSS.

    What is CSS Font?

    In CSS, the font property is used to style and adjust type of text used in webpage.

    You can define fonts and customize their appearance by setting properties like font-familyfont-sizefont-weight and font-style. You can also use the shorthand property font to manipulate all the font style.

    Types of CSS Fonts

    • Monospace Fonts: The font in which every letter have equal width.
    • Serif Fonts: Have small stroke at the edge of each letter.
    • San-Serif Fonts: Clean fonts with out any strokes.
    • Fantasy Fonts: Decorative fancy fonts.
    • Cursive Fonts: The font that resembles human handwriting.

    Popular CSS Fonts

    Here is a table with some popular font and their generic font-family.

    Generic Font FamilyExamples of Font Names
    SerifTimes New Roman
    Georgia
    Garamond
    Sans-serifArial
    Verdana
    Helvetica
    MonospaceCourier New
    Lucida Console
    Monaco
    CursiveBrush Script MT
    Lucida Handwriting
    FantasyCopperplate
    Papyrus

    Setting Text Fonts

    The font-family property is used to specify font name for a text content.

    Example

    In this example, we have used the font-family property to set the font of the paragraph element.

    <html><head><style>
    
        p {
            font-family: Lucida Handwriting, Cursive;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p &gt;
        This is a font that written in Lucida 
        Handwriting. THis is a font that looks 
        like human handwriting.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Text Font Size

    To set the font size of the text, we use font-size property. You can use its values such as small, medium, and, large to get a variety of font sizes of the text.

    Example

    In this example, we have used the font-size property with its different values to set the size of the text.

    <!DOCTYPE html><html lang="en"><head><title>Font Size Example</title><style>
    
        .small {
            font-size: small;
        }
        .medium {
            font-size: medium;
        }
        .larger {
            font-size: larger;
        }
        .px {
            font-size: 18px;
        }
        .percent {
            font-size: 120%;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;CSS Font-Size Examples&lt;/h2&gt;&lt;p class="small"&gt;This text has a 'small' font size.&lt;/p&gt;&lt;p class="medium"&gt;This text has a 'medium' font size.&lt;/p&gt;&lt;p class="larger"&gt;This text has a 'larger' font size.&lt;/p&gt;&lt;p class="px"&gt;This text has a font size of 18px.&lt;/p&gt;&lt;p class="percent"&gt;This text has a font size of 25%.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Font Weight

    You can use the font-weight property to set the boldness of the text.

    Example

    In this example, we have used the font-weight property to set the boldness of the text. The valid numeric value is from 100-900 for number class.

    <!DOCTYPE html><html lang="en"><head><title>CSS font-weight Property Example</title><style>
    
        .normal {
            font-weight: normal;
        }
        .bold {
            font-weight: bold;
        }
        .lighter {
            font-weight: lighter;
        }
        .number {
            font-weight: 600;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;CSS font-weight Property&lt;/h2&gt;&lt;p class="normal"&gt;This text has a 'normal' font-weight.&lt;/p&gt;&lt;p class="bold"&gt;This text has a 'bold' font-weight.&lt;/p&gt;&lt;p class="lighter"&gt;This text has a 'lighter' font-weight.&lt;/p&gt;&lt;p class="number"&gt;This text has a font-weight of 600.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Font Style

    To set the style of the written text, we use the font-style property. The text can be set to normal, italic or oblique.

    Example

    In this example, we have used font-style property to set the style of the written text.

    <!DOCTYPE html><html lang="en"><head><title>CSS font-style Property Example</title><style>
    
        .normal {
            font-style: normal;
        }
        .italic {
            font-style: italic;
        }
        .oblique {
            font-style: oblique;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;CSS font-style Property&lt;/h2&gt;&lt;p class="normal"&gt;It has 'normal' font-style.&lt;/p&gt;&lt;p class="italic"&gt;It has 'italic' font-style.&lt;/p&gt;&lt;p class="oblique"&gt;It has 'oblique' font-style.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Font Shorthand Property

    You can use the font shorthand property to modify size, weight, and style of texts.

    Example

    In this example, we have used the font shorthand property to set font style, weight, size and family.

    <html><head><style>
    
    .font-example {
        /* in-order: font style, weight, size, family */
        font: italic bold 20px Arial, sans-serif; 
    }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="font-example"&gt;
        This is an example of text with an adjusted 
        font style, weight, and size using the font 
        shorthand property.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Font Related Properties

    The following table lists all the CSS properties related to font:

    PropertyDescriptionExample
    font-familySpecifies the font for an element.Try It
    font-sizeSpecifies the size of the font.Try It
    font-weightSpecifies the boldness of the font.Try It
    font-styleSpecifies the style of the font (normal, italic, oblique).Try It
    font-variantSpecifies whether or not a text should be displayed in a small-caps font.Try It
    line-heightSpecifies the line height.Try It
  • Colors

    CSS uses color values to specify a color. Typically, these are used to set a color either for the foreground of an element (i.e. its text) or else for the background of the element. They can also be used to affect the color of borders and other decorative effects.

    You can specify your color values in various formats. Following table lists all the possible formats −

    FormatSyntaxDescriptionExample
    Keyword<property>: <colorname>CSS has a set of predefined color names that you can use directly.red, blue, green, yellow, black, white, etc.
    Hexadecimal Code#RRGGBBStarts with a hash (#) followed by six hexadecimal digits.#FF0000 – red
    Short Hexadecimal Code#RGBShorter version of hexadecimal format where each of the RGB components is represented by a single digit, and the value is duplicated.#F00 – red
    RGBrgb(red,green,blue)Colors can be defined using the rgb() function, which takes three parameters representing the red, green, and blue values.rgb(0, 0, 255) – blue
    RGBArgba()Similar to RGB, with an additional parameter for the alpha (transparency) value. 0 (fully transparent) and 1 (fully opaque)rgba(0,0,255,0.5) – translucent blue
    HSLhsl()Colors can be defined using the rgb() function which stands for Hue (0 to 360 degree), Saturation (%), and Lightness (%).hsl(120, 100%, 50%) – pure green
    HSLAhsla()Similar to HSL, with an additional parameter for the alpha (transparency) value.hsl(120, 100%, 50%, 0.5) – translucent green
    currentcolor KeywordcurrentcolorIt refers to the value of the color property of the element.color: red; /* Red text color */ border: 10px solid currentcolor; /* Red border color */
    System coloras per OS or browserCSS allows usage of system colors defined by the user’s OS or browser.ButtonText, Window, WindowText

    These formats are explained in more detail in the following sections −

    CSS Colors – Keyword

    CSS supports the color names to be directly passed to the property background-color and color. 140 standard color names are supported by CSS.

    Few of the examples are listed in the table below:

    ColorColor Name
     Black
     Red
     Blue
     Green
     Aquamarine

    Here is an example:

    <html><head><style>
       #colorkeyword{
    
      background-color: aqua;
      padding: 10px;
    } </style></head><body><h3>Color Keyword - example</h3><p>As the keyword passed is aqua, the background will appear as aqua colored..</p><div id="colorkeyword">
      This div element has a colored background based on the color keyword passed, i.e aqua.
    </div></body></html>

    CSS Colors – Hexadecimal Codes

    A hexadecimal is a 6 digit representation of a color. The first two digits(RR) represent a red value, the next two are a green value(GG), and the last are the blue value(BB).

    A hexadecimal value can be taken from any graphics software like Adobe Photoshop, Jasc Paintshop Pro, or even using Advanced Paint Brush.

    Each hexadecimal code will be preceded by a pound or hash sign ‘#’. Following are the examples of hexadecimal notation.

    Note: To specify the hexadecimal codes, you can use upper case or lower case letters.

    ColorColor Hexadecimal Code
     #000000
     #FF0000
     #00FF00
     #0000FF
     #FFFF00
     #00FFFF
     #FF00FF
     #C0C0C0
     #FFFFFF

    Here is an example:

    <html><head><style>
       #hexcode {
    
      background-color: #00ff00;
      padding: 10px;
    } </style></head><body><h3>Hexadecimal code - example</h3><p>As the hexadecimal code is #00ff00 the background will appear green.</p><div id="hexcode">
         This div element has a green background.
      &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Colors - Short Hexadecimal Codes

    This is a shorter form of the six-digit notation. In this format, each digit is replicated to arrive at an equivalent six-digit value. For example: #6A7 becomes #66AA77.

    A hexadecimal value can be taken from any graphics software like Adobe Photoshop, Jasc Paintshop Pro, or even using Advanced Paint Brush.

    Each short hexadecimal code will be preceded by a pound or hash sign '#'. Following are the examples of short hexadecimal notation.

    Note: To specify the hexadecimal codes, you can use upper case or lower case letters.

    ColorShort Hexadecimal Code
     #000
     #F00
     #0F0
     #0FF
     #FF0
     #0FF
     #F0F
     #FFF

    Here is an example:

    <html><head><style>
       #shorthex {
    
      background-color: #00f;
      padding: 10px;
    } </style></head><body><h3>Short Hexadecimal code - example</h3><p>As the short hexadecimal code is #00f the background will appear blue.</p><div id="shorthex">
         This div element has a blue background.
      &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Colors - RGB Values

    • This color value is specified using the rgb( ) property.
    • It takes three values, one each for red, green, and blue.
    • The value can be an integer between 0 and 255 or a percentage.

    NOTE: All the browsers does not support rgb() property of color so it is recommended not to use it.

    Following is the example to show few colors using RGB values.

    ColorColor RGB
     rgb(0,0,0)
     rgb(255,0,0)
     rgb(0,255,0)
     rgb(0,0,255)
     rgb(255,255,0)
     rgb(0,255,255)
     rgb(255,0,255)
     rgb(192,192,192)
     rgb(255,255,255)

    Here is an example:

    <html><head><style>
       #rgbvalue {
    
      background-color: rgb(255,0,255);
      padding: 10px;
    } </style></head><body><h3>RGB - example</h3><p>As the rgb(255,0,255) is set the background will appear accordingly.</p><div id="rgbvalue">
      This div element has a colored background based on the rgb values.
    </div></body></html>

    CSS Colors - RGBA Values

    • This color value is specified using the rgba( ) property.
    • It takes four values, one each for red, green, and blue and the last value as the alpha (transparency) value.
    • The alpha value can be any value between 0 and 1.

    NOTE: All the browsers do not support rgba() property of color so it is not recommended.

    Following is the example to show few colors using RGBA values.

    ColorColor RGBA
     rgba(0,0,0,0)
     rgba(255,0,0,0.2)
     rgba(0,255,0,0.3)
     rgba(0,0,255,0.5)
     rgba(255,255,0,0.7)
     rgba(0,255,255,0.1)
     rgba(255,0,255,1)
     rgba(192,192,192,0.4)
     rgba(255,255,255,1)

    Here is an example:

    <html><head><style>
       #rgbavalue {
    
      background-color: rgba(255,0,255,0.2);
      padding: 10px;
    } </style></head><body><h3>RGBA - example</h3><p>As the rgba(255,0,255,0.2) is set the background will appear with transparency value of 0.2.</p><div id="rgbavalue">
      This div element has a colored background based on the rgba values.
    </div></body></html>

    CSS Colors - HSL Values

    • This color value is specified using the hsl() function.
    • HSL stands for hue, saturation and lightness.
    • Hue is represented in degrees (0-360), saturation and lightness are represented as percentages (0% - 100%).

    Following is the example to show few colors using HSL property.

    ColorColor HSL
     hsl(0,0%,50%)
     hsl(255,80%,70%)
     hsl(290,100%,60%)
     hsl(360,70%,20%)
     hsl(89,80%,67%)

    Here is an example:

    <html><head><style>
       #hslvalue {
    
      background-color: hsl(355,70%,50%);
      padding: 10px;
    } </style></head><body><h3>HSL - example</h3><p>As the hsl(355,70%,50%) is set the background will appear based on the hsl values passed.</p><div id="hslvalue">
      This div element has a colored background based on the hsl values hsl(355,70%,50%).
    </div></body></html>

    CSS Colors - HSLA Values

    • This color value is specified using the hsl() function.
    • HSLA stands for hue, saturation, lightness and alpha.
    • It takes four values, first for hue, second for saturation, third for lightness and fourth is the alpha (transparency) value.
    • Hue is represented in degrees (0-360), saturation and lightness are represented as percentages (0% - 100%), and alpha value can be in between 0 and 1.

    Following is the example to show few colors using HSLA property.

    ColorColor HSLA
     hsla(0,0%,50%,0.5)
     hsla(255,80%,70%,1)
     hsla(290,100%,60%,0.2)
     hsla(360,70%,20%,0.4)
     hsla(89,80%,67%,0.9)

    Here is an example:

    <html><head><style>
       #hslavalue {
    
      background-color: hsla(355,70%,50%,0.4);
      padding: 10px;
    } </style></head><body><h3>HSLA - example</h3><p>As the hsla(355,70%,50%,0.4) is set the background will appear based on the hsla values passed, with high transparency.</p><div id="hslavalue">
      This div element has a colored background based on the hsl values hsla(355,70%,50%,0.4).
    </div></body></html>

    CSS Colors - currentcolor keyword

    The currentcolor keyword signifies the value of the color property of an element. It can be passed to any other styling property using the keyword currentcolor.

    Here is an example:

    <html><head><style>
       #currcolor {
    
      color: red;
      border: 5px solid currentcolor;
    } </style></head><body><h2>The currentcolor Keyword</h2><p>As the currentcolor keyword is used for border after color property is set as red, the border will also appear red.</p><div id="currcolor">
      This div element has a red text color and a red border.
    </div></body></html>

    CSS Colors - Building Color Codes

    You can build millions of color codes using our Color Code Builder. Check the HTML Color Code Builder.

    To use this tool, you would need a Java Enabled Browser.

    CSS Colors - Browser Safe Colors

    Here is the list of 216 colors which are supposed to be most safe and computer independent colors. These colors vary from hexa code 000000 to FFFFFF. These colors are safe to use because they ensure that all computers would display the colors correctly when running a 256 color palette −

    0000000000330000660000990000CC0000FF
    0033000033330033660033990033CC0033FF
    0066000066330066660066990066CC0066FF
    0099000099330099660099990099CC0099FF
    00CC0000CC3300CC6600CC9900CCCC00CCFF
    00FF0000FF3300FF6600FF9900FFCC00FFFF
    3300003300333300663300993300CC3300FF
    3333003333333333663333993333CC3333FF
    3366003366333366663366993366CC3366FF
    3399003399333399663399993399CC3399FF
    33CC0033CC3333CC6633CC9933CCCC33CCFF
    33FF0033FF3333FF6633FF9933FFCC33FFFF
    6600006600336600666600996600CC6600FF
    6633006633336633666633996633CC6633FF
    6666006666336666666666996666CC6666FF
    6699006699336699666699996699CC6699FF
    66CC0066CC3366CC6666CC9966CCCC66CCFF
    66FF0066FF3366FF6666FF9966FFCC66FFFF
    9900009900339900669900999900CC9900FF
    9933009933339933669933999933CC9933FF
    9966009966339966669966999966CC9966FF
    9999009999339999669999999999CC9999FF
    99CC0099CC3399CC6699CC9999CCCC99CCFF
    99FF0099FF3399FF6699FF9999FFCC99FFFF
    CC0000CC0033CC0066CC0099CC00CCCC00FF
    CC3300CC3333CC3366CC3399CC33CCCC33FF
    CC6600CC6633CC6666CC6699CC66CCCC66FF
    CC9900CC9933CC9966CC9999CC99CCCC99FF
    CCCC00CCCC33CCCC66CCCC99CCCCCCCCCCFF
    CCFF00CCFF33CCFF66CCFF99CCFFCCCCFFFF
    FF0000FF0033FF0066FF0099FF00CCFF00FF
    FF3300FF3333FF3366FF3399FF33CCFF33FF
    FF6600FF6633FF6666FF6699FF66CCFF66FF
    FF9900FF9933FF9966FF9999FF99CCFF99FF
    FFCC00FFCC33FFCC66FFCC99FFCCCCFFCCFF
    FFFF00FFFF33FFFF66FFFF99FFFFCCFFFFFF

    CSS Colors - Related Properties

    All the properties related to color are listed in the table below:

    PropertyDescription
    opacitySets the transparency level of an element.
    hueRepresents the hue angle of an element.
    colorSets the foreground of an element's text and text decoration.
    background-colorSets the color of the background.
    border-colorSets the color of the border of an element.
    box-shadowAdds a shadow effect around an element.
    outline-colorSets the color of the outline around an element.
    text-shadowAdds shadow to the text of an element.

  • Selectors

    CSS Selectors are used to select the HTML elements you want to style on a web page. They allow you to target specific elements or groups of elements to apply styles like colors, fonts, margins, and more.

    The element or elements that are selected by the selector are referred to as subject of the selector.

    CSS Universal Selector

    CSS universal selector is a special selector that selects all the elements in an HTML document. It is denoted by an asterisk mark (*).

    Syntax

    The syntax for the universal selector is as follows:

    *{margin: 0;padding: 0;}

    As per the above syntax, the universal selector is used to apply a margin and padding of 0 to all HTML elements.

    Example

    The following example demonstrates the use of a universal selector (*) to set the font size, background, and text color of the document:

    <html><head><style>
       * {
    
      background-color: peachpuff;
      color: darkgreen;
      font-size: 25px;
    } </style></head><body><h1>Universal selector (*)</h1><div>Parent element <p>Child paragraph 1</p><p>Child paragraph 2</p></div><p>Paragraph 3</p></body></html>

    CSS Element Selector

    CSS element selector selects and styles specific HTML elements. The element selector is defined by simply using the element’s name in the stylesheet.

    Syntax

    The syntax for the element selector is as follows:

    p{color: green;}h1{text-decoration-line: underline;}

    Example

    The following example demonstrates the use of a type selector to style the heading, paragraph and div element:

    <html><head><style>
       div {
    
      border: 5px inset gold;
      width: 300px;
      text-align: center;
    } p {
      color: green;
    } h1 {
      text-decoration-line: underline;
    } </style></head><body><div><h1>Type selector</h1><p>div with border and text-aligned to center</p><p>paragraph with green color</p><p>h1 with an underline</p></div></body></html>

    CSS Class Selector

    CSS class selector selects an element with a specific class attribute. The class selector is defined using a period (.) followed by the class name.

    Syntax

    The syntax for the class selector is as follows:

    .style-h1{text-decoration-line: underline;}.style-p{color: green;font-size: 25px;}

    Example

    The following example demonstrates the use of a class selector, where .style-p, .style-h1 and .style-div are class selectors:

    <html><head><style>
       .style-div {
    
      border: 5px inset gold;
      width: 300px;
      text-align: center;
    } .style-p {
      color: green;
      font-size: 25px;
    } .style-h1 {
      text-decoration-line: underline;
    } </style></head><body><div class="style-div"><h1 class="style-h1">class selector</h1><p class="style-p">class .style-p applied</p><p>No class applied on this p element</p></div></body></html>

    CSS ID Selector

    CSS ID selector selects an element with a specific value for its id attribute. It is denoted by the “#” (hash) symbol.

    Syntax

    The syntax for the ID selector is as follows:

    #style-p{color: green;font-size: 25px;}#style-h1{text-decoration-line: underline;color: red;}

    Example

    The following example demonstrates the use of an id selector, where #style-p, #style-h1 and #style-div are the id selectors applied on the elements:

    <html><head><style>
       #style-div {
    
      border: 5px inset purple;
      width: 300px;
      text-align: center;
      background-color: lightgoldenrodyellow;
    } #style-p {
      color: green;
      font-size: 25px;
    } #style-h1 {
      text-decoration-line: underline;
      color: red;
    } </style></head><body><div id="style-div"><h1 id="style-h1">ID selector</h1><p id="style-p">id #style-p applied</p><p>No id applied on this p element</p></div></body></html>

    CSS Attribute Selector

    CSS attribute selector selects an element based on a specific attribute or attribute values on an element.

    Syntax

    The syntax for the attribute selector is as follows:

    a[target]{background-color: peachpuff;}

    You can also specify the element with an attribute having a specific value.

    a[href="https://www.tutorialspoint.com"]{background-color: peachpuff;}

    Example

    The following example demonstrates the use of an attribute selector. Here we have changed the style of elements with target attribute:

    <html><head><style>
       a[target] {
    
      background-color: #04af2f;
      color: white;
      font-size: 2em;
    } </style></head><body><h2>Attribute selector</h2><p>Styling applied to anchor element with target attribute:</p><a href="#">Tutorialspoint</a><a href="#" target="_blank">google</a><a href="#" target="_self">wikipedia</a></body></html>

    CSS Group Selector

    CSS group selector allow us to apply same style to multiple elements at a time. Name of elements are comma-separated. The group selector keep CSS concise and avoid redundancy.

    Syntax

    The syntax for the group selector is as follows:

    /* Apply same background color for h1 and h2 */h1, h2{background-color: grey;}

    Example

    The following example shows how to use group selectors in CSS. We have set the background color for the headings and other HTML elements with reduced code.

    <html><head><style>
    
      /* This applies to both &lt;h1&gt; and &lt;h2&gt; elements */
      h1, h2 {
            background-color: grey;           
            padding: 4px;
      }
      /*Applies to all paragraphs, elements with class*/
      /*'highlight', and element with ID 'hightlightSpan'*/
      p, .highlight, #hightlightSpan {
            background-color: yellow;
            padding: 10px;
      }
    </style></head><body><h1>CSS Selectors</h1><h2>Group Selectors</h2><p>This is a paragraph.</p><div class="highlight">
         This is div
      &lt;/div&gt;&lt;br&gt;&lt;span id="hightlightSpan"&gt;
         This is span
      &lt;/span&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Pseudo-class Selector

    CSS pseudo-class selector styles a specific state of an element, such as :hover is used to style an element when hovered.

    Syntax

    The syntax for the pseudo-class selector is as follows:

    a :hover{background-color: peachpuff;color: green;font-size: 2em;}

    Example

    The following example demonstrates the use of a pseudo-class selector. The font size, text and background color of the link changes on hovering over it.

    <html><head><style>
       a:hover {
    
      background-color: peachpuff;
      color: green;
      font-size: 2em;
    } </style></head><body><h2>Pseudo-class selector</h2><p>Styling applied to anchor element with a pseudo-class:</p><a href="#">Tutorialspoint</a></body></html>

    CSS Pseudo-element Selector

    CSS pseudo-element selector is used to style a specific part of an element rather than the element itself.

    Syntax

    The syntax for the pseudo-element selector is as follows:

    a::before{content:url();}

    Example

    The following example demonstrates the use of a pseudo-element selector (::before) and (::after):

    <html><head><style>
    
      /* Add and style contents before paragraph */
      p::before {
            content: "Note: ";
            font-weight: bold;
            color: red;
      }
      /* Add and style contents after paragraph */
      p::after {
            content: " [Read more]";
            font-style: italic;
            color: blue;
      }
    </style></head><body><h2>Pseudo-element selector</h2><p>This is a paragraph.</p></body></html>

    CSS Descendant Selector

    CSS descendant selector styles all the tags which are child of a particular specified tag. To mention as descendant, a single space between parent and child element is used.

    Syntax

    The syntax for the descendant selector is as follows:

    div p{color: blue;}

    The above code set text color of paragraph tags that are inside div element to blue.

    Example

    The following example show how to use descendant selector in css. We have changed the text color of the paragraph element that is immediate child of the div element:

    <!DOCTYPE html><html lang="en"><head><style>
    
      div{
         border: 2px solid;
      }
      div p {
         color: blue;
      }
    </style></head><body><div><p>
         This paragraph is inside a div 
         and will be blue.
      &lt;/p&gt;&lt;section&gt;&lt;p&gt;
            This paragraph is inside a 
            section which is inside a 
            div and will also be blue.
         &lt;/p&gt;&lt;/section&gt;&lt;/div&gt;&lt;p&gt;
      This paragraph is outside the div 
      and will not be blue.
    </p></body></html>

    CSS Child Selector

    CSS child selector selects all the direct child of a particular element. This is denoted by '>' (Greater than) symbol.

    Syntax

    The syntax for the child selector is as follows:

    div > p{color: blue;}

    The above code set text color of paragraph tags that are directly inside div element to blue.

    Example

    The following example shows how to use child selector in css:

    <!DOCTYPE html><html lang="en"><head><style>
    
      div{
         border: 2px solid;
      }
      div &gt; p {
         color: blue;
      }
    </style></head><body><div><p>
            This paragraph is inside a div and 
            will be blue.
         &lt;/p&gt;&lt;section&gt;&lt;p&gt;
               This paragraph is inside a  
               section which is inside a div
               and will not be blue as this 
               is not direct child
            &lt;/p&gt;&lt;/section&gt;&lt;/div&gt;&lt;p&gt;
         This paragraph is outside the div
         and will not be blue.
      &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Adjacent Sibling Selectors

    CSS adjacent sibling selector selects an element that is immediately preceded by a specified element. A plus symbol ( "+" ) is used to denote adjacent sibling.

    Syntax

    The syntax of adjacent sibling selector is as follows:

    h1 + p{margin-top: 0;}

    The above code sets top margin of paragraph tag just after h1 tag to 0.

    Example

    The following example shows how to use adjacent sibling selector in css.

    <!DOCTYPE html><html lang="en"><head><style>
    
      div{
         border: 4px solid;
      }
      div + p {
         color: blue;
      }
    </style></head><body><p>
         This paragraph is above the div 
         and will not be blue
      &lt;/p&gt;&lt;div&gt;&lt;p&gt;
            This paragraph is inside a div 
            and will not be blue.
         &lt;/p&gt;&lt;/div&gt;&lt;p&gt;
         This paragraph 1 after the div 
         and will be blue.
      &lt;/p&gt;&lt;p&gt;This paragraph 2 after the 
         div and will not be blue.
      &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS General Sibling Selector

    general sibling selector targets all the element that is preceded by a specified element. The general sibling selector is denoted by tilde symbol ( "~" ).

    Syntax

    The syntax of general sibling selector is as follows:

    h1 ~ p{color: gray;}

    The above code sets text color of all the paragraph after h1 tag to gray.

    Example

    The following example shows how to use general sibling selector in css:

    <!DOCTYPE html><html lang="en"><head><style>
    
      div{
         border: 4px solid;
      }
      div ~ p {
         color: blue;
      }
    </style></head><body><p>
         This paragraph is above the div 
         and will not be blue
      &lt;/p&gt;&lt;div&gt;&lt;p&gt;
            This paragraph is inside a div 
            and will not be blue.
         &lt;/p&gt;&lt;/div&gt;&lt;p&gt;
         This paragraph 1 after the div 
         and will be blue.
      &lt;/p&gt;&lt;p&gt;This paragraph 2 after the 
         div and will be blue.
      &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Nested Selectors

    CSS nesting allows to nest one style rule inside another rule, with the selector of the child rule relative to the selector of the parent rule.

    Characteristics

    The nesting selector shows the relationship between the parent and child rules.

    • When the nested selectors are parsed by the browser, it automatically adds a whitespace between the selectors, thus creating a new CSS selector rule.
    • In situations where the nested rule needs to be attached to the parent rule (without any whitespace), like while using the pseudo-class or compound selectors, the & nesting selector must be prepended immediately to achieve the desired result.
    • In order to reverse the context of rules, the & nesting selector can be appended.
    • There can be multiple instances of & nesting selector.

    Syntax

    The syntax for the nesting selector is as follows:

    nav{& ul{list-style: none;& li{display: inline-block;& a{text-decoration: none;color: blue;&:hover{color: red;}}}}}

    Example

    The following example demonstrates the use of a nesting selector (&):

    <html><head><style>
    
      #sample {
         font-family: Verdana, Geneva, Tahoma, sans-serif;
         font-size: 1.5rem;
         &amp; a {
            color: crimson;
            &amp;:hover,
            &amp;:focus {
               color: green;
               background-color: yellow;
            }
         }
      }
    </style></head><body><h1>& nesting selector</h1><p id="sample">
      Hover &lt;a href="#"&gt;over the link&lt;/a&gt;.
    </p></body></html>

  • Units

    CSS Units define the measurement system used to specify the values. CSS offers a number of different units for expressing length and measurement. CSS unit is used to specify the property size for a page element or its content.

    There are a number of ways to specify and measure length in CSS. It is used to specify margins, padding, font size, width, height, border, etc.

    For example – font-size: 50px, here number 50 has a suffix px i.e., pixel, it is a CSS measurement unit.

    There should be no whitespace between the number and the unit. The unit can be left out when the value is 0.

    Length Units

    Length units can be categorized into two types:

    • Absolute units: Fixed unit lengths that does not depend on screen width.
    • Relative units: Responsive unit lengths that changes according to screen width.

    Absolute Length Units

    These units are categorized as fixed-length units, which means that lengths specified with absolute units maintain an exact, unchanged size on the screen.

    These units prove to be very effective when the browser has comprehensive information about the properties of the screen, the printer being used, or other appropriate user agents.Height: 2pxHeight: 2cmHeight: 2inHeight: 2pt

    The following table contains all the types of absolute units:

    UnitDescriptionEquivalent valueExample
    mmRefers to millimeter, it is used to specify the measurements in millimeters.1mm = 1/10th of 1cmfont-size: 10mm;
    cmRefers to centimeter, it is used to specify the measurements in centimeters.1cm = 37.8px = 25.2/64infont-size: 5cm;
    QRefers to Quarter-millimeters, it is used to specify the measurements in centimeters.1Q = 1/40th of 1cmfont-size: 5Q;
    inRefers to inches, it is used to specify the measurement in inches.1in = 2.54cm = 96pxfont-size: 1in;
    ptRefers to point, it is used to specify the measurements in points.1pt = 1/72 of 1infont-size: 20pt;
    pcRefers to picas, it is used to specify the measurement in picas.1pc = 1/6th of 1inwidth: 6pc;
    pxRefers to pixels, it is used to specify the measurement in pixels.1px = 1/96th of 1infont-size: 15px;

    Absolute units prove valuable for projects where responsiveness is not a priority. However, they are less beneficial for responsive websites because they do not adjust when screen dimensions change.

    Example

    Here is an example of absolute units (mm, cm, in, Q) used for font sizes:

    <html><head><style>
    
        .unit-mm {
            font-size: 5mm;
        }
        .unit-cm {
            font-size: 1cm;
        }
        .unit-inch {
            font-size: 0.5in;
        }
        .unit-quarter {
            font-size: 40Q;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1 class="unit-mm"&gt; Font size 5mm &lt;/h1&gt;&lt;h1 class="unit-cm"&gt; Font size 1cm &lt;/h1&gt;&lt;h1 class="unit-inch"&gt; Font size 0.5inch &lt;/h1&gt;&lt;h1 class="unit-quarter"&gt; Font size 40Q &lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Relative Length Units

    Relative length units are measured in relation to other elements or viewport of the screen.

    Relative units are great for styling responsive websites because they can be adjusted proportionally based on window size or parent elements. These units define lengths relative to other length properties.Height: 3emHeight: 3exHeight: 3lhHeight: 3vh

    The following table contains all the types of relative units:

    UnitDescriptionExample
    emRelative to the font-size of the element.font-size: 4em;
    exRelative to the x-height of the current font.font-size: 4ex;
    chRelative to width of the "0".font-size: 4ch;
    remRelative to font-size of the root element.font-size: 2rem;
    lhIt is relative to the line height of the element.font-size: 4lh;
    rlhIt is relative to the line height of the root element.font-size: 4rlh;
    vhIt is relative to the height of the viewport. 1vh = 1% or 1/100 of the height of the viewport.font-size: 4vh;
    vwIt is relative to the width of the viewport. 1vw = 1% or 1/100 of the width of viewport.width: 4vw;
    vminIt is relative to the smaller dimension of the viewport. 1vmin = 1% or 1/100 of the viewport's smaller dimension.width: 4vmin;
    vmaxIt is relative to the larger dimension of the viewport. 1vmax = 1% or 1/100 of the viewport's larger dimension.width: 4vmax;
    vbIt is relative to the size of the initial containing block in the direction of the root element's block axis. 1vb = 1% of containing block's size (block axis).font-size: 4vb;
    viIt is relative to the size of the initial containing block in the direction of the root element's inline axis. 1vb = 1% of containing block's size (inline axis).font-size: 4vi;
    svw, svhIt is relative to the width and height of the smaller viewport. 1svw = 1% or 1/100 of the smaller viewport's width and 1svh = 1% or 1/100 of the smaller viewport's height.width: 40svw; height: 40svh;
    lvw, lvhIt is relative to the width and height of the larger viewport. 1lvw = 1% or 1/100 of the larger viewport's width and 1lvh = 1% or 1/100 of the larger viewport's height.width: 40lvw; height: 40lvh;
    dvw, dvhIt is relative to the width and height of the dynamic viewport. 1dvw = 1% or 1/100 of the dynamic viewport's width and 1dvh = 1% or 1/100 of the dynamic viewport's height.width: 40dvw; height: 40dvh;

    Example

    Here is an example of relative units (em, rem, vw, vh, %) used for font sizes:

    <html><head><style>
    
        .unit-em {
            font-size: 2em;
        }
        .unit-rem {
            font-size: 1.5rem;
        }
        .unit-vw {
            font-size: 5vw;
        }
        .unit-vh {
            font-size: 5vh;
        }
        .unit-percent {
            font-size: 150%;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1 class="unit-em"&gt;Font size 2em &lt;/h1&gt;&lt;h1 class="unit-rem"&gt;Font size 1.5rem &lt;/h1&gt;&lt;h1 class="unit-vw"&gt;Font size 5vw &lt;/h1&gt;&lt;h1 class="unit-vh"&gt;Font size 5vh &lt;/h1&gt;&lt;h1 class="unit-percent"&gt;Font size 150% &lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Units px (Pixel) and em (indicates size relative to the size of the font) are two of the measurement units of length. In order to convert px to em, try our free CSS - PX to EM Converter.

  • Types of CSS

    CSS stands for “Cascading Style Sheet”. It is a style sheet language used to control the layout and other visual aspects of the web pages. CSS plays an important role in modern web development by providing the tools necessary to create visually appealing, accessible, and responsive websites.

    There are three types of CSS which are mentioned below.

    • Inline CSS
    • Internal CSS
    • External CSS

    1. Inline CSS

    Inline CSS is applied directly to an HTML element using the style attribute. It has the highest priority among the three methods.

    Example 1

    In this example we have changed the text color using color property and font of the written text using inline CSS properties.

    <p style="color: #04af2f; font-family: Verdana, sans-serif;">
    
    This line is an inline-styled paragraph.
    </p>

    Example 2

    In this example we have changed the background color, height, width and text color of the div element using inline CSS properties.

    <div style="background-color: #04af2f;color: white;height: 100px;width: 250px">
    
    This line is an inline-styled paragraph.
    </p>

    2. Internal CSS

    Internal CSS is defined within the <style> tag inside the <head> section of an HTML document.

    Example 1

    In this example we have changed the text color and font of the written text using internal CSS properties.

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
    
    &lt;style&gt;
        p{color: #04af2f;font-family: Verdana, sans-serif;}
    &lt;/style&gt;
    </head> <body>
    &lt;p&gt;This line is styled using internal CSS.&lt;/p&gt;
    </body> </html>

    Example 2

    In this example we have changed the background color, height, width and text color of the div element using internal CSS properties.

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
    
    &lt;style&gt;
        p{background-color: #04af2f;color: white;height: 100px;width: 250px;}
    &lt;/style&gt;
    </head> <body>
    &lt;p&gt;This line is styled using internal CSS.&lt;/p&gt;
    </body> </html>

    3. External CSS

    External CSS is written in a separate .css file and linked to the HTML document using the <link> tag. This is the recommended method for large projects as it improves maintainability.

    Example

    Here is an example of External CSS having separate files for HTML and CSS.

    HTML File

    <!-- HTML file --><head><link rel="stylesheet" href="styles.css"></head><body><p>This line is styled using external CSS.</p></body>

    CSS File

    /* styles.css */p{color: #04af2f;font-family: Verdana, sans-serif;}

    Note: The specificity order for above three types of CSS is as follows: Inline CSS > Internal CSS > External CSS.