Category: CSS

  • Combinators

    CSS Combinators used to specify a particular relationship between selectors so that styling can be done based on relationship between elements in HTML structure. In other words combinators help you target elements based on their position and hierarchy within the HTML document.

    For example, with combinators you can style paragraph element that palaced immediately after div element.

    Table of Contents

    • Types of Combinators
    • CSS Descendant Combinator
    • CSS Child Combinator
    • CSS Adjacent Sibling Combinator
    • CSS General Sibling Combinator
    • Combining Multiple Combinators

    Types of Combinators

    There are four main types of CSS combinators:

    • Descendant Combinator (whitespace)
    • Child Combinator (>)
    • Adjacent sibling Combinator (+)
    • General sibling Combinator (~)

    Now we will look forward to example usage of each of this combinators, before that make sure that you are well versed with CSS selectors.

    CSS Descendant Combinator

    The descendant combinators are used to select elements that are nested within another element, regardless of how deeply they are nested. These are often represented by a single space (” “).

    For example you can target all <p> elements inside the <div> like this:

    div p{/* CSS styles for all p elements inside div */}

    Similarly we can combine any selectors like classes, id etc. In the following example we combined a element selector with class selector.

    Example

    <!DOCTYPE html><html><head><style>
    
        .parent {
            background-color: lightblue;
            padding: 20px;
        }
        .parent p {
            color: red;
            font-weight: bold;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="parent"&gt;&lt;h3&gt; This is h3 tag inside parent element &lt;/h3&gt;&lt;p&gt; This is a p tag inside parent element. &lt;/p&gt;&lt;p&gt; This is a p tag inside parent element. &lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt; This is a p tag outside parent element. &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Child Combinator

    The child combinator (>) is used to select elements that are direct children of a specified element. It will not select elements that are further nested inside the child elements.

    For example, you can target all direct <p> child elements inside the <div> like this:

    div > p{/* CSS styles for all direct p children inside div */}

    Similarly, we can combine any selectors like classes, ids, etc. In the following example, we style all the p tags inside class parent.

    Example

    <!DOCTYPE html><html><head><style>
    
        .parent {
            background-color: lightgreen;
            padding: 20px;
        }
        .parent &gt; p {
            color: blue;
            font-weight: bold;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="parent"&gt;&lt;h3&gt; This is an h3 tag inside parent. &lt;/h3&gt;&lt;p&gt; This is a direct p tag inside  parent. &lt;/p&gt;&lt;p&gt; This is another direct p tag inside parent. &lt;/p&gt;&lt;div&gt;&lt;p&gt; 
                This is a p tag nested inside a div in parent element. 
            &lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;p&gt; This is a p tag outside parent element. &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Adjacent Sibling Combinator

    The adjacent sibling combinator (+) is used to select an element that is immediately preceded by a specified element. It only affects the element that directly follows the specified element.

    For example, you can target a <p> element that immediately follows an <h3> element like this:

    h3 + p{/* CSS styles for the p element immediately following an h3 */}

    In the following example, we target the <p> element that comes right after an <h3> element.

    Example

    <!DOCTYPE html><html><head><style>
    
        .container {
            padding: 20px;
            background-color: lightyellow;
        }
        h3 + p {
            color: purple;
            font-style: italic;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;h3&gt;This is an h3 tag&lt;/h3&gt;&lt;p&gt; 
            This is a p tag that immediately follows the h3 tag.
        &lt;/p&gt;&lt;p&gt;
            This is another p tag, but it is not immediately 
            after the h3 tag.
        &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS General Sibling Combinator

    The general sibling combinator (~) is used to select all elements that are siblings of a specified element and appear after it in the HTML structure. It will select all matching siblings, not just the one immediately following.

    For example, you can target all <p> elements that follow an <h3> element like this:

    h3 ~ p{/* CSS styles for all p elements following an h3 element */}

    In the following example, we target all <p> elements that are siblings of an <h3> element and appear after it.

    Example

    <!DOCTYPE html><html><head><style>
    
        .container {
            padding: 20px;
            background-color: #f0f0f0;
        }
        h3 ~ p {
            color: darkorange;
            text-decoration: underline;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;p&gt; This is a p tag before h3 tag &lt;/p&gt;&lt;h3&gt; This is an h3 tag &lt;/h3&gt;&lt;p&gt; This is a p tag that follows the h3 tag. &lt;/p&gt;&lt;p&gt; This is another p tag that also follows the h3 tag. &lt;/p&gt;&lt;div&gt; This is a div tag. &lt;/div&gt;&lt;p&gt; This is a p tag after div tag &lt;/p&gt;&lt;/div&gt;&lt;p&gt; This is a p tag after h3 tag outside container. &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Combining Multiple Combinators

    In CSS, you can combine multiple combinators to create more complex and specific selectors. By combining different combinators, you can target elements based on more complex relationships within the HTML structure.

    For example, you can target a <p> element that is a direct child of a <div> and immediately follows an <h3> element like this:

    div > h3 + p{/* CSS styles for p elements that are direct children of a div and 
    
      immediately follow an h3 element */}</pre>

    In the following example, we combine the child combinator with the adjacent sibling combinator to target anchor elements inside navigation menu.

    Example

    <!DOCTYPE html><html><head><style>
    
        nav {
            padding: 10px;
            background-color: #f0f0f0;
        }
        ul {
            list-style-type: none;
            padding: 0;
        }
        ul &gt; li {
            display: inline-block;
            margin-right: 15px;
        }
        ul &gt; li + a {
            color: red;
            text-decoration: underline;
        }
        ul &gt; li &gt; a {
            color: blue;
            text-decoration: none;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;nav&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;About&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Services&lt;/a&gt;&lt;/li&gt;&lt;a href="#"&gt;Contact&lt;/a&gt;&lt;/ul&gt;&lt;/nav&gt;&lt;/body&gt;&lt;/html&gt;</pre>
  • Atrribute Selector Property

    Description

    CSS attribute selectors allow you to select HTML elements based on the presence or value of one or more attributes. They are a powerful way to target specific elements in your HTML markup. Attribute selectors are enclosed in square brackets [] and can take various forms.

    The following sections discusses some common ways to use attribute selectors:

    CSS [attribute] Selector

    This selector selects elements that have specified attribute, regardless of its value.

    Syntax

    [attribute]
    

    Example

    Following is an example to select all the HTML elements with a “data-toggle” attribute

    <html><head><style>
       [data-toggle] {
    
      background: yellow;
      color: red;
    } </style></head><body><h2>CSS [attribute] Selector</h2><div data-toggle="yes">The div with data-toggle attribute</div><div>The div without data-toggle attribute</div><p data-toggle="no">A paragraph with data-toggle attribute</p><p>A paragraph without data-toggle attribute</p></body></html>

    CSS [attribute=”value”] Selector

    This selector selects elements that have a specific attribute with a specific value.

    Syntax

    [attribute="value"]
    

    Example

    This selector selects all elements with a ‘data-toggle’ attribute whose value is set to ‘yes’.

    <html><head><style>
       [data-toggle="yes"] {
    
      background: yellow;
      color: red;
    } </style></head><body><h2>CSS [attribute="value"] Selector</h2><div data-toggle="yes">The div with data-toggle attribute</div><div>The div without data-toggle attribute</div><p data-toggle="no">A paragraph with data-toggle attribute</p><p>A paragraph without data-toggle attribute</p></body></html>

    CSS [attribute*=”value”] Selector

    This selector selects elements that have a specific attribute with a value containing a specific substring.

    Syntax

    [attribute*="value"]
    

    Example

    This selector selects all the elements with a “src” attribute which contain a “images” in the path:

    <html><head><style>
       [src*="images"] {
    
      border: 2px dashed red;
    } </style></head><body><h2>CSS [attribute*="value"] Selector</h2><img alt="Logo" src = "/css/images/logo.png" /></body></html>

    CSS [attribute^=”value”] Selector

    This selector selects elements that have a specific attribute with a value that starts with a specific string.

    Syntax

    [attribute^="value"]  
    

    Example

    This selector selects all elements with a “href” attribute starting with “https://”

    <html><head><style>
       [href^="https"] {
    
      background: yellow;
      text-decoration:none;
      color:red;
    } </style></head><body><h2>CSS [attribute^="value"] Selector</h2><p><a href="https://www.tutorialspoint.com">HTTPS Link</a></p><p><a href="http://www.tutorialspoint.com">HTTP Link</a></p></body></html>

    CSS [attribute$=”value”] Selector

    This selector selects elements that have a specific attribute with a value that ends with a specific string.

    Syntax

    [attribute$="value"]  
    

    Example

    This selector selects all elements with a “src” attribute which ends with “.png”

    <html><head><style>
       [src$=".png"] {
    
      border: 2px dashed red;
    } </style></head><body><h2>CSS [attribute$="value"] Selector</h2><img alt="Logo" src = "/css/images/logo.png" /></body></html>

    CSS [attribute|=”value”] Selector

    This selector select elements that have a specific attribute with a value that begins with a specified substring followed by a hyphen (-). This selector is often used for selecting elements with language-specific attributes, such as lang attributes, which often use hyphens to denote language subcodes.

    Syntax

    [attribute|="value"]
    

    Example

    This selector selects all the elements with a “lang” attribute that starts with “en” followed by a hyphen:

    <html><head><style>
       [lang|="en"] {
    
      border: 2px dashed red;
    } </style></head><body><h2>CSS [attribute|="value"] Selector</h2><div lang="en">Hello World in English!</div><div lang="fr">Bonjour tout le monde en franais!</div><div lang="es">Hola Mundo en espaol!</div></body></html>

    CSS [attribute~=”value”] Selector

    This selector is used to select elements that have a specific attribute with a value containing a specified word. The word should be a standalone word, surrounded by spaces or at the beginning or end of the attribute value.

    Syntax

    [attribute~="value"]   
    

    Example

    This selector select all elements with a “class” attribute containing the word “beautifyme”

    <html><head><style>
       [class~="beautifyme"] {
       background-color:lightblue;
       border:2px solid red;
    
      }
    </style></head><body><h2>CSS [attribute|="value"] Selector</h2><div class="beautifyme">div with <b>beautifyme</b> class</div><div class="beautifyme1">div with <b>beautifyme1</b> class</div><div class="beautifyme-2">div with <b>beautifyme-2</b> class</div></body></html>

    Attribute Selectors For href Links

    You can style elements based on their href attribute is a common attribute used on <a> elements to specify the URL that the link points to.

    Here is an example −

    <html><head><style>
    
      ul {
      list-style: none;
    } a[href] {
      color: rgb(11, 11, 231);
    } a[href="css_backgrounds.htm"] {
      color: rgb(224, 152, 18);
    } a[href~="css_colors.htm"] {
      color: rgb(51, 255, 0);
    } a[href|="css_padding.htm"] {
      color: rgb(0, 255, 255);
    } a[href^="css_margins.htm"] {
      color: rgb(255, 0, 55);
    } a[href$="css_lists.htm"] {
      color: rgb(255, 230, 0);
    } a[href*="css_borders.htm"] {
      color: rgb(112, 108, 108);
    } </style></head><body><ul><li><a href="css_text.htm">CSS Text</a></li><li><a href=".htm">CSS Background</a></li><li><a href="css_colors.htm">CSS Color</a></li><li><a href="css_padding.htm">CSS Padding</a></li><li><a href="css_margins.htm">CSS Margin</a></li><li><a href="css_lists.htm">CSS List</a></li><li><a href="css_borders.htm">CSS Borders</a></li></ul></body></html>

    Attribute Selectors For Inputs

    Attribute selectors can be used to select input elements based on specific criteria, such as their type, name, value, or other attributes.

    Here is an example −

    <html><head><style>
       input {
    
      display: block;
      margin: 10px;
    } input[type] {
      border: 1px solid #e0dd29;
    } input[placeholder="Password"] {
      border: 1px solid #f00;
    } input[name|="emailid"] {
      background-color: rgb(236, 178, 233);
    } input[type~="submit"] {
      background-color: rgb(88, 241, 88);
      padding: 10px;
    } input[value^="But"] {
      background-color: rgb(236, 149, 68);
      padding: 10px;
    } input[type$="box"] {
      border-radius: 5px; 
      height: 50px;
      width: 20px;
    } input[type*="adi"] {
      height: 50px;
      width: 20px;
    } </style></head><body><input type="text" placeholder="Username"><input type="password" placeholder="Password"><input type="email" placeholder="Email" name="emailid"><input type="submit" placeholder="Submit"><input type="button" value="Button"><input type="checkbox" placeholder="Checkbox"><input type="radio" placeholder="Radio"></body></html>

    Attribute Selectors For Title

    To style elements based on the title attribute, you can use the CSS attribute selector title to style elements that have a title attribute containing a specific value.

    Here is an example −

    <html><head><style>
       span {
    
      display: block;
      margin: 10px;
      padding: 5px;
    } span[title] {
      border: 2px solid rgb(231, 40, 40);
      background-color: rgb(109, 177, 236);
    } span[title="CSS Background"] {
      border: 2px solid rgb(231, 40, 40);
      background-color: rgb(250, 163, 192);
    } span[title|="CSS Border"] {
      border: 2px solid rgb(231, 40, 40);
      background-color: rgb(245, 248, 79);
    } span[title^="Mar"] {
      border: 2px solid rgb(231, 40, 40);
      background-color: rgb(255, 147, 23);
    } span[title$="ght"] {
      border: 2px solid rgb(231, 40, 40);
      background-color: rgb(102, 201, 240);
    } span[title*="CSS Width"] {
      border: 2px solid rgb(231, 40, 40);
      background-color: rgb(191, 14, 235);
    } </style></head><body><span title="CSS Text">A text refers to a piece of written or printed information in the form of words or characters.</span><span title="CSS Background">You can set backgrounds of various HTML elements.</span><span title="CSS Border"> border property is used to create a border around an element.</span><span title="Margin">Setting up a margin around an HTML element.</span><span title="Height">The height property sets the height of an element's content area.</span><span title="CSS Width">The width sets the width of an element's content area.</span></body></html>

    Attribute Selectors For Language

    You can use the lang attribute to select elements based on their language. The lang attribute specifies the language of the text contained within an element.

    Here is an example −

    <html><head><style>
       div[lang] {
    
      color: red;
    } div[lang="fr"] {
      color: blue;
    } div[lang~="es"] {
      color: green;
    } div[lang|="de"] {
      color: orange;
    } div[lang^="it"] {
      color: purple;
    } div[lang$="ja"] {
      color: brown;
    } div[lang*="zh"] {
      color: teal;
    } </style></head><body><div lang="en">Hello World in English!</div><div lang="fr">Bonjour tout le monde en franais!</div><div lang="es">Hola Mundo en espaol!</div><div lang="ja"></div><div lang="de">Hallo Welt auf Deutsch!</div><div lang="it">Ciao Mondo in italiano!</div><div lang="zh"></div></body></html>

    CSS Multiple Attribute Selectors

    CSS multiple attribute selectors allow you to select elements based on multiple attribute values. It is use to target specific element that meet multiple criteria.

    Here is an example −

    <html><head><style>
       ul {
    
      list-style: none;
    } a[href] {
      color: rgb(231, 11, 194);
    } a[href="css_backgrounds.htm"][href$=".htm"] {
      color: rgb(224, 152, 18);
    } a[href="css_colors.htm"] {
      color: rgb(51, 255, 0);
    } </style></head><body><ul><li><a href="css_text.htm">CSS Text</a></li><li><a href="css_backgrounds.htm">CSS Background</a></li><li><a href="css_colors.htm">CSS Color</a></li></ul></body></html>

    CSS Attribute Selectors With Sibling Combinator

    CSS attribute selectors and sibling combinators can be used together to select specific elements based on their attributes and their relationship to other elements.

    • General sibling combinator (~) − This combinator selects all sibling elements that follow a specified element, but are not directly adjacent to it.Syntaxselector1 ~ selector2 { /* CSS styles */ }
    • Adjacent sibling combinator (+) − This combinator selects the element that is directly after the specified element.Syntaxselector1 + selector2 { /* CSS styles */ }

    The following example demonstrates that the adjacent sibling combinator (+) selects the paragraph immediately after the heading, while the general sibling combinator (~) selects <div> after the heading −

    <html><head><style>
       h2 + p { 
    
      font-weight: bold;
      color:blue;
    } h2 ~ div {
      color: red;
    } </style></head><body><h2>CSS Background</h2><p>You can set backgrounds of various HTML elements.</p><div><p>Contrary to popular belief, Lorem Ipsum is not simply random text.</p></div><p>There are many variations of passages of Lorem Ipsum available.</p></body></html>

  •  Loaders

    CSS loaders are animation effects that are used to indicate the loading process of a webpage. They are implemented using CSS and can be applied to various elements on a webpage, such as a spinner or a loading progress. CSS loaders are commonly used to improve user experience by visually indicating that content is being loaded or processed.

    Sample of CSS Loaders

    Here you can see what is a CSS loader, you may have seen these kind of loading animation on different websites.

    Table of Contents

    • How to Create a CSS Loader?
    • Basic CSS Loaders Example
    • Create Pulsing Dots Loader
    • Different Types of Spinning Loaders
    • CSS Loaders Using Gradient

    How to Create a CSS Loader?

    To create a loader in CSS, follow the below mentioned steps steps.

    • Create the HTML Structure: Define a outer div container element to hold loader content and a inner div container for loader.
    • Style the Loader Container: Set up the width and height for loader container. Use flex layout to center loader elements properly inside the container.
    • Style the Loader Element: Set up height and width for loader element also. Then set up border-top property as per width and color of loader you want. Also use border-radius value as 50% to ensure a circular loader.
    • Add Animation to the Loader: Use CSS animation to create the loading effect. With this you can add rotation effect, scaling, or any other transformations.
    • Define Keyframes for Animation: Specify the @keyframes rules for your animation, detailing how the loader should move or change over time.

    You may use various color combinations, shape, patterns and animation tricks for the loader. Play around with CSS properties to create your loader.

    You need to add the -webkit- prefix in your code for the browsers that do not support the animation and transform properties.

    Basic CSS Loaders Example

    Following example demonstrates creating a loader using CSS as discussed in the previous section:

    Example

    <!DOCTYPE html><html><head><style>
    
        .container {
            display: flex;
            justify-content: space-around;
            align-items: center;
            height: 150px;
            background-color: #e0f7e9;
            margin: 0;
        }
    
        .loader {
            border: 8px solid #e0f7e9; 
            border-top: 8px solid #34a853; 
            border-radius: 50%;
            width: 60px;
            height: 60px;
            animation: innerFrame-spin 2s ease infinite;
        }
        
        @keyframes innerFrame-spin {
            0% {
                transform: rotate(0deg);
            }
            100% {
                transform: rotate(360deg);
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="loader"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Create Pulsing Dots Loader

    The pulsing dots loader is commonly used in windows operating system while booting the system. We made this using six div elements inside loader each set with different animation delay using pseudo-class :nth-child.

    Example

    <!DOCTYPE html><html><head><style>
    
        .container {
            display: flex;
            justify-content: space-around;
            align-items: center;
            height: 150px;
            background-color: #e0f7e9;
            margin: 0;
        }
        
        .PulsingDotsLoader {
            display: flex;
            justify-content: space-around;
            align-items: center;
            width: 60px;
            height: 60px;
        }
        
        .PulsingDotsLoader div {
            width: 12px;
            height: 12px;
            background-color: #34a853;
            border-radius: 50%;
            animation: PulsingDotsLoader-animation 1.2s ease infinite;
        }
        
        .PulsingDotsLoader div:nth-child(1) {
            animation-delay: -0.6s;
        }
        .PulsingDotsLoader div:nth-child(2) {
            animation-delay: -0.5s;
        }
        .PulsingDotsLoader div:nth-child(3) {
            animation-delay: -0.4s;
        }
        .PulsingDotsLoader div:nth-child(4) {
            animation-delay: -0.3s;
        }
        .PulsingDotsLoader div:nth-child(5) {
            animation-delay: -0.2s;
        }
        .PulsingDotsLoader div:nth-child(6) {
            animation-delay: -0.1s;
        }
        
        @keyframes PulsingDotsLoader-animation {
            0%, 100% {
                transform: scale(1);
                opacity: 1;
            }
            50% {
                transform: scale(0.5);
                opacity: 0.3;
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="PulsingDotsLoader"&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Different Types of Spinning Loaders

    This example shows loaders with multiple spinners.

    • First loader have two spinners, because we defined properties border-top and border-bottom.
    • For second loader we defined border-right along with previously defined top and bottom values, this make three spinner loader.
    • And for third loader we defined four different values for border property, which makes a four spinner loader.

    Example

    <!DOCTYPE html><html><head><style>
    
        body {
            display: flex;
            justify-content: space-around;
            align-items: center;
            height: 200px;
            background-color: #f0f0f0; 
        }
        .loader {
            border: 16px solid #f3f3f3; /* Light grey */
            border-top: 16px solid blue;
            border-bottom: 16px solid blue;
            border-radius: 50%;
            width: 70px;
            height: 70px;
            animation: spin 2s linear infinite;
        }
        .second{
            border-right: 16px solid blue;
        }
        .third{
            border-top: 16px solid #ADD8E6;   /* Light Blue */
            border-right: 16px solid #87CEEB; /* Sky Blue */
            border-bottom: 16px solid #1E90FF; /* Dodger Blue */
            border-left: 16px solid #4169E1;  /* Royal Blue */
        }
        @keyframes spin {
            0% { 
                transform: rotate(0deg); 
            }
            100% { 
                transform: rotate(360deg); 
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="loader"&gt;&lt;/div&gt;&lt;div class="loader second"&gt;&lt;/div&gt;&lt;div class="loader third"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Loaders Using Gradient

    CSS gradients can be used to design custom colors for loader elements by creating a smooth transition between two or more colors.

    Example

    <html><head><style>
    
        .loader-test {
            width: 50px;
            height: 50px;
            padding: 10px;
            aspect-ratio: 1;
            border-radius: 50%;
            margin: 20px;       
            background: linear-gradient(10deg,#ccc,red);
            mask: conic-gradient(#0000,#000), 
                  linear-gradient(#000 0 0) 
                  content-box;
            -webkit-mask-composite: source-out;
            mask-composite: subtract;
            animation: load 1s linear infinite;
        }
        @keyframes load {
            to{
                transform: rotate(1turn)
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="loader-test"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Comments

    In CSS, comments are useful in adding explanatory notes or annotations within your stylesheet that are not interpreted as styling instructions by the web browser.

    Syntax

    /* This is a comment */p{color: red;/* Set text color to red */}

    CSS comments are intended for the benefit of developers and are ignored by the browser when rendering a web page. They are useful in documentation, debugging, etc.

    Types of CSS Comments

    In CSS, there are two main ways to create comments:

    • Single-line Comments: Single-line comments are created using /* to start the comment and */ to end it.
    • Multi-line Comments: Multi-line comments allow you to add comments that span multiple lines. They are also enclosed within /* and */.
    /* Single line Comment *//*
    Comment
    which stretches
    over multiple
    lines
    */

    HTML And CSS comments

    In HTML tutorial we learned that, a command in HTML is defined between <!– and –> symbols.

    Syntax

    <html><head><style>
    
         /* This is a CSS Comment */
      &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;!-- This is an html comment format --&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Example

    Here is an example showing html comment and CSS comment format:

    <html><head><style>
    
      /* Target all div elements */
      div {
         background-color: red; /* Set background color */
         height: 50px; /* Set the height  */
         width: 200px; /* Set the width  */
         padding: 5px; /* Set the padding  */
         border: 5px solid black; /* Set the border  */
      }
    </style></head><body><!-- This is an html comment format --><div>
      Styles Applied
    </div></body></html>
  • Image Gallery

    CSS Image gallery is used to organize and display images in responsive and visually appealing format. CSS properties can be used to control the layout of images, size, shape, spacing, spanning and lot of other visual effects. In this tutorial we will learn all of those.

    CSS image galleries are commonly used on websites to display products, portfolios, or other visual content in a visually appealing way.

    Following is a simple example of image gallery of courses.

    Gallery Image 2
    Gallery Image 3
    Gallery Image 4
    Gallery Image 5

    Table of Contents

    • Create a Image Gallery In CSS
    • Example of CSS Image Gallery
    • Design Simple Image Gallery
    • Image Gallery With Hover Effect
    • Horizontal Scroll Image Gallery
    • Vertical Scroll Image Gallery
    • CSS Grid Layout Image Gallery
    • Responsive Image Gallery
    • CSS Tabbed Image Gallery
    • CSS Image Gallery Slideshow

    To create a simple image gallery, you can follow these steps:

    • Set Up the HTML Structure: Create a container (e.g., a <div>) to hold all your images. And use <img> tag to render images one by one.
    • Prepare Images: Ensure all images have consistent dimension. If images are of different dimension, fix a common height (or in some case width) for all images in layout so that width can be adjusted according to height.
    • Style the Layout: Use CSS grid layout system to define layout of container. You can then define number of columns needed for gallery or spanning of large images using various properties of grid layout system.
    • Make It Responsive: Use CSS media queries to adjust the number of columns or the size of the images based on the screen size.
    • Add Hover Effects: Use CSS :hover pseudo-class add effects like scaling, border changes, or shadow effects when user hovers mouse over images.

    To display a simple image gallery we can use flexbox layout system. Flexbox is one dimensional layout system, that can be used to display images or any html elements either horizontally or vertically. Even though we have flex wrap property to multiple rows as shown in example, we does not have complete control of two dimensional display as there in grid layout.

    Example

    <!DOCTYPE html><html><head><style>
    
        .image-gallery {
            display: flex;
            gap: 20px;
            flex-flow: row wrap;
            justify-content: space-around;
            align-items: center;
        }
        .image-gallery img {
            width: auto;
            height: 70px;
            border: 3px solid #555;
            border-radius: 10px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="image-gallery"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    We can use pseudo-class :hover to style mouse over image state. Effect like increased size, border color change will make image gallery looks interactive to user.

    Example

    <!DOCTYPE html><html><head><style>
    
        .image-gallery {
            display: flex;
            gap: 20px;
            flex-flow: row wrap;
            justify-content: space-around;
            align-items: center;
        }
        .image-gallery img {
            width: auto;
            height: 70px;
            border: 3px solid #555;
            border-radius: 10px;
        }
        img:hover{
            transform: scale(1.1);
            border: 3px solid #555;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="image-gallery"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The example shows how to create an image gallery with a horizontal scrollable layout using the overflow property

    Example

    <!DOCTYPE html><html><head><style>
    
    
        .image-gallery {
            display: flex;
            flex-direction: row; /* Arrange images horizontally */
            gap: 20px;
            height: 200px;
            width: 900px; /* Set a fixed width for the gallery */
            overflow-x: auto; /* Enable vertical scrolling */
            padding: 10px;
        }
    
        .image-gallery img {
            width: auto;
            height: 70px;
            border: 3px solid #555;
            border-radius: 10px;
        }
    
        img:hover {
            transform: scale(1.1);
            border: 3px solid #555;
        } 
    
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="image-gallery"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The following example shows how to create an image gallery with a vertical scrollable layout using the overflow property.

    Example

    <!DOCTYPE html><html><head><style>
    
    
        .image-gallery {
            display: flex;
            flex-direction: column; /* Arrange images vertically */
            gap: 20px;
            justify-content: flex-start;
            align-items: center;
            height: 300px; /* Set a fixed height for the gallery */
            overflow-y: auto; /* Enable vertical scrolling */
            padding-right: 10px;
        }
    
        .image-gallery img {
            width: auto;
            height: 70px;
            border: 3px solid #555;
            border-radius: 10px;
        }
    
        img:hover {
            transform: scale(1.1);
            border: 3px solid #555;
        }
    
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="image-gallery"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;img src="/css/images/css.png" alt="css"&gt;&lt;img src="/css/images/html.png" alt="html"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS grid layout is most commonly used layout system for designing image gallery, with this we can align images in 2 dimensional manner. Let us see an example for this.

    Example

    <!DOCTYPE html><html><head><style>
    
        /* Gallery container */
        .gallery {
            display: grid;
            gap: 10px;
            padding: 10px;
            font-family: Arial, sans-serif;
        }
        /* style image items */
        .gallery img {
            width: 100%;
            height: 100px; /* Set a same height for all images */
            object-fit: fit;
            display: block;
            border-radius: 8px;
            border: 3px solid #ccc;
            transition: all 0.3s ease;
        }
        /* Spanning the first image across two rows */
        .gallery img:first-child {
            grid-row: span 2;
            height: 210px; /* Double the height of regular images */
        }
        /* Spanning the sixth image across two columns */
        .gallery img:nth-child(6) {
            grid-column: span 2;
        }
        /* Hover effect */
        .gallery img:hover {
            transform: scale(1.02);
            border-color: #555 ; 
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="gallery"&gt;&lt;img src="/w3css/images/w3css_pdfcover.jpg" alt="Gallery Image 1"&gt;&lt;img src="/css/images/html.png" alt="Gallery Image 2"&gt;&lt;img src="/css/images/css.png" alt="Gallery Image 3"&gt;&lt;img src="/css/images/html.png" alt="Gallery Image 4"&gt;&lt;img src="/css/images/css.png" alt="Gallery Image 5"&gt;&lt;img src="/html/images/logo.png" alt="Gallery Image 6"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    We can use CSS media queries to create a responsive image gallery that scales and rearranges its content based on the screen width, providing an optimal viewing experience on different devices and screen sizes. On smaller screens, the images will be wider and more spaced apart.

    @media [screen width condition] {
    
    /* CSS rules to apply if the media query matches */
    }

    With media queries we can also define style for specific orientation (landscape or portrait) of user device. The default value for this is portrait.

    Example

    <!DOCTYPE html><html><head><style>
    
        /* Gallery container */
        .gallery {
            display: grid;
            gap: 10px;
            padding: 10px;
            font-family: Arial, sans-serif;
        }
        /* 4 columns in case of large screen */
        @media (min-width: 600px) {
            .gallery {
                grid-template-columns: repeat(4, 1fr);
            }
        }
        /* 1 column in case of small screen */
        @media (max-width: 599px) {
            .gallery {
                grid-template-columns: 1fr;
            }
        }
        /* Individual image items */
        .gallery img {
            width: 100%;
            height: 100px; /* Set a same height for all images */
            object-fit: fit; /* Ensure images fits the area */
            display: block;
            border-radius: 8px;
            border: 3px solid #ccc; /* Default border color */
            transition: all 0.3s ease;
        }
        /* Spanning the first image across two rows */
        .gallery img:first-child {
            grid-row: span 2;
            height: 210px; /* Double the height of regular images */
        }
        /* Spanning the sixth image across two columns */
        .gallery img:nth-child(6) {
            grid-column: span 2;
        }
        /* Hover effect */
        .gallery img:hover {
            transform: scale(1.02);
            border-color: #555 ; 
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="gallery"&gt;&lt;img src="/w3css/images/w3css_pdfcover.jpg" alt="Gallery Image 1"&gt;&lt;img src="/css/images/html.png" alt="Gallery Image 2"&gt;&lt;img src="/css/images/css.png" alt="Gallery Image 3"&gt;&lt;img src="/css/images/html.png" alt="Gallery Image 4"&gt;&lt;img src="/css/images/css.png" alt="Gallery Image 5"&gt;&lt;img src="/html/images/logo.png" alt="Gallery Image 6"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    A tabbed image gallery means images are arranged in such a way that initially smaller version of images will be displayed and when you click on that small image, a larger version of the same image will open. Let's see how to design it.

    Example

    <!DOCTYPE html><html><head><style>
    
        .image-gallery {
            display: flex;
            justify-content: space-around;
            gap: 20px;
            height: 200px;
        }
        .image-container { 
            display: flex;
            justify-content: center;
            align-items: center;
            width: 25%;
            height: 200px;
        }
        .image-container img {
            width: auto;
            height: 50px;
            cursor: pointer; 
        }
        .gallery-tab {
            display: none;
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.8);
            justify-content: center;
            align-items: center;
        }
        .tab-content {
            display: flex;
            justify-content: space-around;
            width: 80%;
            max-height: 80%;
        }
        .tab-content img {
            width: 150px;
            height: auto;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Click on the images:&lt;/h1&gt;&lt;div class="image-gallery"&gt;&lt;div class="image-container" onclick="opentab('img2')"&gt;&lt;img src="/css/images/html.png" alt="Gallery Image 2" id="img2"&gt;&lt;/div&gt;&lt;div class="image-container" onclick="opentab('img3')"&gt;&lt;img src="/css/images/css.png" alt="Gallery Image 3" id="img3"&gt;&lt;/div&gt;&lt;div class="image-container" onclick="opentab('img4')"&gt;&lt;img src="/css/images/html.png" alt="Gallery Image 4" id="img4"&gt;&lt;/div&gt;&lt;div class="image-container" onclick="opentab('img5')"&gt;&lt;img src="/css/images/css.png" alt="Gallery Image 5" id="img5"&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="tab" class="gallery-tab" onclick="closetab()"&gt;&lt;div class="tab-content"&gt;&lt;img id="tabImg"&gt;&lt;/div&gt;&lt;/div&gt;&lt;script&gt;
        function opentab(imageId) {
            var tab = document.getElementById("tab");
            var tabImg = document.getElementById("tabImg");
            tab.style.display = "flex";
            tabImg.src = document.getElementById(imageId).src;
        }
        function closetab() {
            document.getElementById("tab").style.display = "none";
        }
    &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    By using CSS and JavaScript we can create a simple slideshow gallery. The gallery has several images, and only one image is shown at a time. You can click the left and right arrows to move through the images. To understand code, you have to be well versed with JavaScript.

    Example

    <!DOCTYPE html><html><head><style>
    
        .container{
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            width: 60%;
        }
        .slideshow-container {
            position: relative;
            width: 100%;
            overflow: hidden;
        }
        .image-gallery {
            display: flex;
            transition: transform 0.5s ease-in-out;
        }
        .image-container {
            width: 100%;
            height: 100%;
        }
        .image-container img {
            width: 100%;
            height: 100%;
        }
        .prev-img, .next-img {
            cursor: pointer;
            position: absolute;
            top: 50%;
            width: auto;
            padding: 16px;
            margin-top: -22px;
            background-color: rgba(0, 0, 0, 0.5);
            color: white;
            font-weight: bold;
            font-size: 18px;
            transition: 0.6s ease;
        }
        .next-img {
            right: 0;
        }
        .prev-img:hover, .next-img:hover {
            background-color: rgba(0, 0, 0, 0.8);
        }
        .bottom-img-container {
            text-align: center;
            margin-top: 20px;
        }
        .bottom-img {
            height: 40px; 
            width: 50px; 
            margin: 0 10px; 
            cursor: pointer;
            opacity: 0.5; 
        }
        .bottom-img.current-bottom-img {
            opacity: 1; 
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="slideshow-container"&gt;&lt;div class="image-gallery"&gt;&lt;div class="image-container"&gt;&lt;img src="/css/images/scenery.jpg" &gt;&lt;/div&gt;&lt;div class="image-container"&gt;&lt;img src="/css/images/scenery3.jpg" &gt;&lt;/div&gt;&lt;div class="image-container"&gt;&lt;img src="/css/images/scenery4.jpg"&gt;&lt;/div&gt;&lt;/div&gt;&lt;a class="prev-img" onclick="slides(-1)"&gt;&lt;/a&gt;&lt;a class="next-img" onclick="slides(1)"&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="bottom-img-container"&gt;&lt;img class="bottom-img current-bottom-img" 
             src="/css/images/scenery.jpg" onclick="activeSlides(1)"&gt;&lt;img class="bottom-img" 
             src="/css/images/scenery3.jpg" onclick="activeSlides(2)"&gt;&lt;img class="bottom-img" 
             src="/css/images/scenery4.jpg" onclick="activeSlides(3)"&gt;&lt;/div&gt;&lt;/div&gt;&lt;script&gt;
        var index = 1;
        displaySlides(index);
        function slides(n) {
            displaySlides(index += n);
        }
        function activeSlides(n) {
            displaySlides(index = n);
        }
        function displaySlides(n) {
            var i;
            var allSlides = document.getElementsByClassName("image-container");
            var allThumbnails = document.getElementsByClassName("bottom-img");
            if (n &gt; allSlides.length) {
                index = 1;
            }
            if (n &lt; 1) {
                index = allSlides.length;
            }
            for (i = 0; i &lt; allSlides.length; i++) {
                allSlides[i].style.display = "none";
            }
            for (i = 0; i &lt; allThumbnails.length; i++) {
                allThumbnails[i].className = 
                        allThumbnails[i].className.replace(" current-bottom-img", "");
                allThumbnails[i].style.filter = "brightness(50%)";
            }
            allSlides[index - 1].style.display = "block";
            allThumbnails[index - 1].className += " current-bottom-img";
            allThumbnails[index - 1].style.filter = "brightness(100%)";
        }
    &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>
  • Icons

    CSS icons are used to add graphical representations, symbols, or small images to web elements. They serve several purposes in web development, such as:

    Table of Contents

    • Importance of Icon
    • How to add Icons to a Webpage?
    • Icons using SVG
    • Icons using icon fonts
    • Icons using images
    • Icons using Pseudo-Elements
    • Icons using Google Icons
    • Bootstrap Icons

    Importance of Icon

    • Enhanced user experience: Provides visual cues and context to various elements on a webpage, like instead of adding the text save, delete, etc. you may add an icon to represent them.
    • Reduced load time: CSS icons are often lightweight compared to traditional image icons, which means they can load quickly, reducing the overall page load time.
    • Scalability: CSS icons can be easily scaled up or down without loss of quality. It is important for responsive web designing.
    • Customization: CSS icons can be customized by changing their size, color, and other visual properties using CSS rules. This flexibility allows you to match the icons to your website’s overall design and branding.
    • Accessibility: CSS icons can be styled to meet accessibility standards, such as providing alternative text for screen readers.
    • Reduced HTTP Requests: Using CSS icons can reduce the number of HTTP requests made by a webpage since they are often part of the stylesheet.

    How to Add Icons to a Webpage?

    • Inline SVGs: Involves embedding SVG (Scalable Vector Graphics) directly into your HTML code. You can create or obtain SVG icons and insert them as inline elements.
    • Icon fonts: Icon fonts are custom fonts that contain icons as glyphs. You can use these fonts to display icons by setting the appropriate font-family and specifying the icon’s Unicode character.
    • CSS background images: You can use CSS background images to display icons by setting the background-image property on an element.
    • Pseudo-elements: Involves using the ::before and ::after pseudo-elements to insert content before or after an HTML element and then styling that content to display an icon.
    • CSS Libraries and Frameworks: Many CSS libraries and frameworks, like Font Awesome, Material Icons, and Google Icons, provide pre-designed sets of icons that you can easily include in your projects. They often come with ready-to-use classes or utility classes.

    Icons Using SVG

    In HTML <svg> tag can be used to create custom icons using d attribute of <path>.

    The d attribute of the <path> tag in SVG defines the shape of the path using a series of commands and parameters in a predefined syntax, representing lines, curves, and other geometric shapes. These commands include moveto (M), lineto (L), curveto (C), and others, each followed by coordinates or control points that specify the path’s structure.

    Example

    <!DOCTYPE html><html><head><style>
    
        .icon {
            width: 24px; 
            height: 24px; 
            fill: #000; 
            margin-left: 15px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;SVG Icons&lt;/h1&gt;&lt;!-- Search Icon --&gt;&lt;svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"&gt;&lt;path d="M23.707 22.293l-6.364-6.364C18.454 14.68 19 13.418
                 19 12c0-3.866-3.134-7-7-7s-7 3.134-7 7 3.134 7 7 7c1.418 
                 0 2.68-.546 3.929-1.071l6.364 6.364a1 1 0 0 0 1.414-1.414zM5 
                 12c0-3.309 2.691-6 6-6s6 2.691 6 6-2.691 6-6 6-6-2.691-6-6z"/&gt;&lt;/svg&gt;&lt;!-- Download Icon --&gt;&lt;svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"&gt;&lt;path d="M19 14v7H5v-7H3v7c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2v-7h-2zm-7-9h2v7h3l-4 4-4-4h3V5zm-1-2h4V0h-4v3z"/&gt;&lt;/svg&gt;&lt;!-- Cloud Icon --&gt;&lt;svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"&gt;&lt;path d="M19.35 10.04c.63-.34 1.22-.79 1.68-1.35C21.47 6.39 19.76 4 
                 17 4c-2.33 0-4.31 1.45-5.13 3.5H11.5C8.42 7.5 6 9.92 6 13s2.42
                 5.5 5.5 5.5h7c3.04 0 5.5-2.46 5.5-5.5-.01-2.52-1.65-4.67-4-5.46l.35.5z"/&gt;&lt;/svg&gt;&lt;!-- Home Icon --&gt;&lt;svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"&gt;&lt;path d="M12 2l10 9h-3v11h-6V14H9v8H3V11H0l12-9z"/&gt;&lt;/svg&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Icons Using Icon Fonts

    To use icon fonts in your web project, you need to follow this steps:

    • Include the icon font library, Popular choices include Font Awesome, Material Icons, or custom icon fonts.
    • Use <i> tag in HTML and apply the icon font class to it. Then, specify the Unicode character for the desired icon.

    Example

    <!DOCTYPE html><html><head><!-- Include Font Awesome --><link rel="stylesheet" 
    
          href=
    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"><style>
        .icon {
            /* Specify Font Awesome family */
            font-family: "Font Awesome 5 Free"; 
            /* Minimum Font weight for Font Awesome */
            font-weight: 600;
            font-size: 24px; 
            color: #000; 
            margin-right: 15px; 
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;CSS font Awesome Icons&lt;/h1&gt;&lt;span class="icon"&gt; &amp;amp#xf002; &lt;/span&gt;&lt;span class="icon"&gt; &amp;amp#xf019; &lt;/span&gt;&lt;span class="icon"&gt; &amp;amp#xf0c2; &lt;/span&gt;&lt;span class="icon"&gt; &amp;amp#xf015; &lt;/span&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Icons Using Images

    The background-image property in CSS can also used to display icons that are stored at system storage.

    Example

    Following example demonstrates using background image as an icon:

    <DOCTYPE html><html><head><style>
    
        .icon-img {
            width: 30px;
            height: 30px;
            background-image: url('/css/images/logo.png');
            background-size: cover;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="icon-img"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Icons Using Pseudo-Elements

    Pseudo-elements like ::before and ::after can be used to insert an icon before or after an element as demonstrated in the following example.

    To know more about pseudo-elements check out tutorial on CSS Pseudo-Elements.

    Example

    Here in this example we use pseudo element to render icons.

    <DOCTYPE html><html><head><style>
    
        li {
            list-style: none;
        }
        li::before {
            content: url(/css/images/smiley.png);
            margin-right: 15px;
            font-size: 20px;
        }
        p::after {
            content: url(/css/images/smiley.png);
            margin-left: 15px;
            font-size: 5px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul&gt;&lt;li&gt;Butterscotch&lt;/li&gt;&lt;li&gt;Chocolate&lt;/li&gt;&lt;li&gt;Coconut&lt;/li&gt;&lt;/ul&gt;&lt;p&gt; 
        In the above list we made custom label using 
        before pseudo-element, we added icon at the end 
        of this paragraph using ::after pseudo-element. 
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Icons Using Google Icons

    We can also use icons provided by Google Icons in our webpage. This way you can call any icon by just mentioning the name of icon.

    You just need to add the following link in the head section of your html code:

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

    Note: There is no need to install or download Google icons.

    Example

    Following example demonstrates using Google Icons.

    <DOCTYPE html><html><head><link rel="stylesheet" 
    
          href=
    "https://fonts.googleapis.com/icon?family=Material+Icons"></head><body><h1> Google Fonts Icon </h1><span class="material-icons" style="font-size:40px;">
        pets 
    &lt;/span&gt;&lt;span class="material-icons" style="font-size:40px;"&gt;
        settings
    &lt;/span&gt;&lt;span class="material-icons" style="font-size:40px;"&gt;
        attachment
    &lt;/i&gt;&lt;span class="material-icons" style="font-size:40px;"&gt;
        person
    &lt;/span&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Bootstrap Icons

    To use Bootstrap icon add following code in head section of your webpage.

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

    Example

    Following example demonstrates using Bootstrap Icons:

    <!DOCTYPE html><html><head><link rel="stylesheet" 
    
          href=
    "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></head><body><h1> Bootstrap Icons </h3><span class="glyphicon glyphicon-cloud"></span><span class="glyphicon glyphicon-remove"></span><span class="glyphicon glyphicon-user"></span><span class="glyphicon glyphicon-envelope"></span><span class="glyphicon glyphicon-thumbs-up"></span></body></html>
  • Align

    The term alignment, in context of web design and CSS, refers to the positioning and arrangement of elements or content within a layout, typically with respect to specific guidelines or reference points. Alignment is used to create visually pleasing and organized designs by ensuring that elements are positioned relative to each other or to the layout structure in a consistent and harmonious way.

    Alignment can be applied to various types of elements, including text, images, buttons, and more, to create a cohesive and polished design. CSS provides various properties that can be used to align the elements.

    There are two main aspects of alignment:

    • Horizontal alignment: This refers to the positioning of elements along the horizontal axis, which typically runs from left to right. Common horizontal alignment options include:
      • Left alignment: Elements are aligned to the left side of a container or layout.
      • Center alignment: Elements are positioned in the center of a container or layout horizontally.
      • Right alignment: Elements are aligned to the right side of a container or layout.
    • Vertical alignment: This refers to the positioning of elements along the vertical axis, which typically runs from top to bottom. Common vertical alignment options include:
      • Top alignment: Elements are aligned to the top of a container or layout.
      • Middle or Center alignment: Elements are centered vertically within a container or layout.
      • Bottom alignment: Elements are aligned to the bottom of a container or layout.

    CSS Align – position Property

    There are several ways to align an element to left. Let us see few ways to achieve this.

    With the use of the CSS property position, the alignment of elements can be adjusted.

    Here is an example of setting alignment using position:

    <html><head><style>
       .right-alignment {
    
      position: absolute;
      right: 0px;
      width: 100px;
      border: 3px solid #0d1601;
      background-color: rgb(244, 244, 135);
      padding: 10px;
    } .left-alignment {
      position: relative;
      left: 0px;
      width: 100px;
      border: 3px solid #0c1401;
      background-color: blanchedalmond;
      padding: 10px;
    } .center-alignment {
      margin: auto;
      border: 3px solid black;
      padding: 5px;
      background-color: rgb(241, 97, 126);
      width: 120px;
      position: relative;
    } </style></head><body><div class="right-alignment"><h3>Right align</h3><strong>Right align with position:absolute</strong></div><div class="left-alignment"><h3>Left align</h3><strong>Left align with position:relative</strong></div><div class="center-alignment"><h3>Center align</h3><strong>Vertically & horizontally centered using position:relative and margin:auto.</strong></div></body></html>

    Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

    CSS Align – float Property

    With the use of the CSS property float, the alignment of elements can be adjusted.

    Here is an example of setting alignment using float:

    <html><head><style>
       .right-alignment {
    
      float: right;
      width: 100px;
      border: 3px solid #0d1601;
      background-color: rgb(244, 244, 135);
      padding: 10px;
    } .left-alignment {
      float: left;
      left: 0px;
      width: 100px;
      border: 3px solid #0c1401;
      background-color: blanchedalmond;
      padding: 10px;
    } </style></head><body><div class="right-alignment"><h3>Right align</h3><strong>Right align with float:right</strong></div><div class="left-alignment"><h3>Left align</h3><strong>Left align with float:left</strong></div></body></html>

    CSS Align – text-align Property

    To align the text inside an element, use the property text-align.

    Here is an example to align the text inside a <div> element:

    <html><head><style>
       div {
    
      width: 300px;
      border: 3px solid #0d1601;
      padding: 10px;
      margin-bottom: 1cm;
    } .right-alignment {
      text-align: right; 
      color:red;
    } .left-alignment {
      text-align:left;
      color:green;
    } .center-alignment {
      text-align: center;
      color:blue;
    } </style></head><body><div class="right-alignment"><h3>Right align</h3><strong>Text right aligned</strong></div><div class="left-alignment"><h3>Left align</h3><strong>Text left aligned</strong></div><div class="center-alignment"><h3>Center align</h3><strong>Text center aligned</strong></div></body></html>

    CSS Align – padding Property

    A piece of text can be centered vertically using padding CSS property.

    <html><head><style>
       .center-alignment { 
    
      padding: 100px 0;
      border: 3px solid black; 
      margin: 5px;
      background-color: lightblue;
    } </style></head><body><div class="center-alignment"><p>Vertically centerd using padding.</p></div></body></html>

    CSS Align – Center Text

    Here is an example if you want to center the text vertically as well as horizontally, you need to use text-align:center along with padding:

    <html><head><style>
       .center-alignment { 
    
      padding: 100px 0;
      text-align: center;
      border: 3px solid black; 
      margin: 5px;
      background-color: lightblue;
    } </style></head><body><div class="center-alignment"><p>Vertically & horizontally centerd using padding and text-align properties.</p></div></body></html>

    CSS Align – justify-content Property

    Here is an example if you want to center the text vertically as well as horizontally, using flex and justify-content properties:

    <html><head><style>
       .center-alignment {
    
      display: flex;
      justify-content: center;
      align-items: center;
      height: 300px;
      border: 3px solid black; 
      font-size: larger;
      background-color: lightblue;
    } </style></head><body><div class="center-alignment"><p>Vertically & horizontally centered using flex and justify-content.</p></div></body></html>

    CSS Align – Related Properties

    Following table lists all the properties related to alignment:

    PropertyDescription
    align-contentAligns the content of a flex container along the cross-axis or a grid’s block axis.
    align-itemsControls the alignment of items of a flex container along the cross-axis.
    align-selfControls the alignment of an individual item within a container.
    vertical-alignDetermines the vertical alignment of inline, inline-block or a table cell text.
    line-heightSets the distance between lines of text.
    text-alignSets the horizontal alignment of inline, inline-block or a table cell text.
    marginShorthand for margin values that can modify the alignment.
  • Forms

    Forms are required, when you want to collect some data from the site visitor. They have input fields for users to enter information, labels to identify the input fields, and buttons to submit the form or perform an action.

    We can style HTML forms by changing the appearance of form elements, such as text fields, checkboxes, radio buttons, select menus, and submit buttons.

    HTML Form Sample

    This is a simple form that accept First and Last name with the desired tutorial you wants to learn. Submit button will auto redirect you to our courses page.First Name:Last Name:Tutorial:         Select Tutorial         HTML         CSS         Python         JavaScript     

  • Overlay

    An overlay is a transparent layer of content that is placed on top of another element. It can be used to create different effects, such as a modal window, a tooltip, or a popover.

    The overlay element should be positioned absolutely and have a higher z-index than the content element. This will ensure that the overlay is displayed on top of the content.

    Example of Overlay Effect

    Here you can see overlay effect when you hover mouse on top of TutorialsPoint logo. This types of styling give a dynamic experience for users that are visiting to your webpage.

    Learn CSS

    Table of Contents

    • How to Create Overlay Effect?
    • CSS Overlay Basic Example
    • Overlay Effect Using Javascript
    • CSS Overlay Top to Bottom Sliding
    • CSS Overlay Bottom to Top Sliding
    • CSS Overlay Left to Right Sliding
    • CSS Overlay Right to Left Sliding
    • CSS Overlay Fade Effect
    • CSS Overlay Image Title
    • CSS Image Icon Overlay

    How to Create Overlay Effect?

    To create an overlay using CSS, follow these steps:

    • Create the Container: Use a container element to hold the content you want to overlay. A container can be div element, span element or even an image.
    • Set Up the Positioning: Make the container position: relative so that any absolutely positioned elements inside it will be positioned relative to this container.
    • Add the Overlay Element: Inside the container, add another element (the overlay) and set position: absolute to ensure it covers the entire container. Also make sure the overlay’s top, left properties are set to 0 and width, height properties are set to 100%, so it fully covers the container.
    • Style the Overlay: Set the background color of the overlay using rgba() function to give a semi-transparent effect. And initially, set the opacity of the overlay to 0 to make it invisible by default.
    • Add Hover Effect: Use hover pseudo-class to the overlay container to change the overlay’s opacity from 0 to 1 when user moves mouse over container.

    CSS Overlay Basic Example

    The following example shows how to create a simple overlay effect in CSS using above steps.

    Example

    <!DOCTYPE html><html lang="en"><head><style>
    
        .container {
            height: 150px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }
        .overlay-container {
            position: relative;
            width: auto; 
            height: auto; 
            display: inline-block;
        }
        img {
            display: block;
            width: 100%;
            height: auto;
        }
        .overlay {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 128, 0, 0.5); 
            opacity: 0;
            transition: opacity 0.5s ease;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .overlay-container:hover .overlay {
            opacity: 1;
        }
        .login-button {
            padding: 10px 20px;
            background-color: #ffffff;
            color: #228B22;
            border: 2px solid #228B22;
            border-radius: 5px;
            text-decoration: none;
            font-size: 16px;
            font-weight: bold;
            transition: background-color 0.3s, color 0.3s;
        }
        .login-button:hover {
            background-color: #228B22;
            color: #ffffff;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="overlay-container"&gt;&lt;img src="/html/images/test.png" alt="Image"&gt;&lt;div class="overlay"&gt;&lt;a href="/css/index.htm" 
               target="_blank" class="login-button"&gt;
                    Learn CSS
            &lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Overlay Effect Using JavaScript

    The following example uses javascript to create an overlay effect that appears when you click a button and disappears when you click anywhere on the page.

    JavaScript can be used to show and hide the overlay div element by using the 'querySelector()' method to get the overlay element. When the button is clicked, a function is executed that toggles the display property of the overlay container between block (visible) and none (hidden).

    Example

    <!DOCTYPE html><html lang="en"><head><style>
    
        .container {
            position: fixed;
            display: none;
            width: 100%;
            height: 100%;
            text-align: center;
            background-color: rgba(213, 86, 207, 0.3);
            z-index: 999; 
            cursor: pointer;
        }
        .content {
            padding: 20px;
        }
        .button {
            background-color: #38dc29; 
            color: white; 
            border: none;
            padding: 20px;
            cursor: pointer;
            font-size: 20px;
            text-align: center; 
            display: block; 
            margin: 50px auto;  
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container" onclick="overlay()"&gt;&lt;h1&gt;TutorialsPoint CSS Overlay&lt;/h1&gt;&lt;/div&gt;&lt;div style="padding:20px"&gt;&lt;button class="button" onclick="overlay()"&gt;
            Click on Button
        &lt;/button&gt;&lt;/div&gt;&lt;script&gt;
        let Visible = false;
        function overlay() {
            const overlay = document.querySelector(".container");
            overlay.style.display = Visible ? "none" : "block";
            Visible = !Visible;
        }
    &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Overlay Top to Bottom Sliding

    The following example shows an image with an overlay that slides down from the top of the image to the bottom when the user hovers over it

    Example

    <!DOCTYPE html><html lang="en"><head><style> 
    
        img { 
            width: 200px; 
            height: 200px; 
            margin-left: 40%;
        } 
        .overlay-container { 
            position: relative; 
            width: 25%; 
            height: auto;
        }  
        .overlay-container:hover .top-bottom { 
            opacity: 1; 
            height: 200px; 
        } 
        .top-bottom{ 
            position: absolute; 
            transition: 0.9s ease; 
            opacity: 0.3; 
            width: 200px; 
            border-radius: 5px;
            height: 0; 
            top: 0; 
            left: 40%;  
            background-color: #d346e6; 
            text-align: center;
            color: #ffffff;
        }
        h2 {
            text-align: center;
        }   
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt; Hover your cursor over the image.&lt;/h2&gt;&lt;div class="overlay-container"&gt;&lt;img src= "/css/images/tutimg.png"&gt;&lt;div class="top-bottom"&gt;&lt;h2&gt; Top to Bottom Image Overlay &lt;/h2&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Overlay Bottom to Top Sliding

    The following example shows an image with an overlay effect that slides up from the bottom of the image to the top when the user hovers over it

    Example

    <!DOCTYPE html><html lang="en"><head><style> 
    
        img { 
            width: 200px; 
            height: 200px; 
        } 
        .overlay-container { 
            position: relative; 
            width: 25%; 
            height: auto;
        }  
        .overlay-container:hover .bottom-top { 
            opacity: 1; 
            height: 200px; 
        } 
        .bottom-top { 
            position: absolute; 
            transition: 0.9s ease; 
            opacity: 0.3; 
            width: 200px; 
            border-radius: 5px;
            height: 0; 
            bottom: 0; 
            background-color: #d346e6; 
            text-align: center;
            color: #ffffff;
        }
        h2 {
            text-align: center;
        }   
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Hover your cursor over the image.&lt;/h2&gt;&lt;div class="overlay-container"&gt;&lt;img src= "/css/images/tutimg.png"&gt;&lt;div class="bottom-top"&gt;&lt;h2&gt;Bottom to Top Image Overlay&lt;/h2&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Overlay Left to Right Sliding

    The following example shows an image with an overlay effect that slides from left to right when you hover over it

    Example

    <!DOCTYPE html><html lang="en"><head><style>
    
        img {
            width: 200px;
            height: 200px;             
        }
        .overlay-container {
            position: relative;
            width: 25%;
            height: auto;
        }
        .overlay-container:hover .left-right { 
            opacity: 1;
            width: 200px;
        }
        .left-right { 
            position: absolute;
            transition: 0.9s ease;
            opacity: 0.3;
            width: 0; 
            border-radius: 5px;
            height: 200px; 
            top: 0;
            left: 0; 
            background-color: #d346e6;
            text-align: center;
            color: #ffffff;
        }
        h2 {
            text-align: center;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Hover your cursor over the image.&lt;/h2&gt;&lt;div class="overlay-container"&gt;&lt;img src="/css/images/tutimg.png"&gt;&lt;div class="left-right"&gt;&lt;h2&gt;Left to Right Image Overlay&lt;/h2&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Overlay Right to Left Sliding

    The following example shows an image with an overlay effect that slides from right to left when you hover over it

    <!DOCTYPE html><html lang="en"><head><style>
    
        img {
            width: 200px;
            height: 200px;           
        }
        .overlay-container {
            position: relative;
            width: 67%;
            height: auto;
        }
        .overlay-container:hover .right-left { 
            opacity: 1;
            width: 200px; 
        }
        .right-left { 
            position: absolute;
            transition: 0.9s ease;
            opacity: 0.3;
            width: 0; 
            border-radius: 5px;
            height: 200px; 
            top: 0;
            background-color: #d346e6;
            text-align: center;
            color: #ffffff;
        }
        h2 {
            text-align: center;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Hover your cursor over the image.&lt;/h2&gt;&lt;div class="overlay-container"&gt;&lt;img src="/css/images/tutimg.png"&gt;&lt;div class="right-left"&gt;&lt;h2&gt;Right to Left Image Overlay&lt;/h2&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Overlay Fade Effect

    The following example shows how to create an overlay that appears on top of an image when the user hovers over it

    Example

    <!DOCTYPE html><html lang="en"><head><style> 
    
        img { 
            width: 200px; 
            height: 200px;       
        } 
        .overlay-container { 
            position: relative; 
            width: 25%; 
        }  
        .overlay-container:hover .fade-effect { 
            opacity: 0.9; 
            height: 200px; 
        } 
        .fade-effect { 
            position: absolute; 
            transition: 0.9s ease; 
            opacity: 0; 
            width: 200px; 
            height: 200px; 
            border-radius: 5px;
            top: 0; 
            background-color: #d346e6; 
            text-align: center;
            color: #ffffff;
        }
        h2 {
            text-align: center;
        }   
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Hover your cursor over the image.&lt;/h2&gt;&lt;div class="overlay-container"&gt;&lt;img src= "/css/images/tutimg.png"&gt;&lt;div class="fade-effect"&gt;&lt;h2&gt;Fade Overlay Effect&lt;/h2&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Overlay Image Title

    Here is an example of an overlay that displays the title of an image when the user hovers over it

    <!DOCTYPE html><html lang="en"><head><style> 
    
        img { 
            width: 200px; 
            height: 200px;            
        } 
        .overlay-container { 
            position: relative; 
            width: 25%; 
        }  
        .overlay-container:hover .title-overlay { 
            opacity: 0.9; 
            height: 80px; 
        } 
        .title-overlay { 
            position: absolute; 
            transition: 0.9s ease; 
            opacity: 0; 
            width: 200px; 
            height: 80px; 
            border-radius: 5px;
            bottom: 0;  
            background-color: #f0f0f0; 
            text-align: center;
        }
        h1 {
            text-align: center;
            color: black;
        }   
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Hover your cursor over the image.&lt;/h2&gt;&lt;div class="overlay-container"&gt;&lt;img src= "/css/images/tutimg.png"&gt;&lt;div class="title-overlay"&gt;&lt;h1&gt;TutorialsPoint&lt;/h1&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Image Icon Overlay

    Here is an example of an overlay that displays the icons over an image when the user hovers over it

    Example

    <!DOCTYPE html><html lang="en"><head><link rel="stylesheet" 
    
    href=
    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><style>
        .overlay-container {
            position: relative;
            width: 200px; 
            height: 200px; 
        }  
        img { 
            width: 100%; 
            height: 100%;
        }
        .icon-overlay {
            position: absolute; 
            transition: 0.9s ease; 
            opacity: 0; 
            width: 100%;
            height: 100%; 
            top: 0;
            background-color: rgba(211, 70, 230, 0.9); 
            text-align: center;
            color: #ffffff;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .overlay-container:hover .icon-overlay {
            opacity: 1;
        }
        .display-icon {
            color: rgb(60, 235, 50);
            font-size: 100px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        } 
        h2 {
            text-align: center;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;Hover your cursor over the image.&lt;/h2&gt;&lt;div class="overlay-container"&gt;&lt;img src="/css/images/tutimg.png"&gt;&lt;div class="icon-overlay"&gt;&lt;a href="#" class="display-icon"&gt;&lt;i class="fa fa-star"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>
  • Navigation Bar

    Navigation bar is a section of a graphical user interface (GUI) that helps users navigate through a website, app, or other software. It is essential for users to quickly and easily navigate to the content they are looking for.

    The navigation bar can be horizontal or vertical, that contains links to important pages or features.

    Table of Contents

    • How to Design a Responsive Navbar?
    • CSS Horizontal Navbar
    • CSS Vertical Navbar
    • CSS Fixed Navbar
    • Divider Lines for Navbar
    • Fixed Vertical Navbar
    • CSS Dropdown Navbar

    How to Design a Responsive Navbar?

    Following are common steps followed to design a navbar in CSS:

    • HTML Structure: Use anchor tag ( <a> ) inside unordered list ( <ul> ) to obtain structure of navbar.
    • Remove Default Styles: Use the property ‘list-style-type: none’ to remove default styling and label of unordered list.
    • Flex Layout: Use the property display: flex for ul element and set flex-direction as row or column depending upon horizontal or vertical flexbox needed.
    • Responsive Design: Use CSS media queries to change between horizontal and vertical layout of navbar depending upon user screen width.
    • Hamburger Menu: Create a hamburger menu icon that toggles the visibility of the list on smaller screens.

    Navbars can also contain other elements, such as the logo of the website or app, search bar, or social media icons. Navbars can be styled using CSS to change their appearance.

    CSS Horizontal Navbar

    Following example shows a horizontal navigation bar, which is the most common type of navigation bar displayed across the top of a web page as shown below

    Example

    <!DOCTYPE html><html><head><style>
    
        body{
            color: #4CAF50;
        }
        ul {
            background-color: #333;
            overflow: hidden;
            list-style-type: none;
            margin: 0;
            padding: 0;
            width: 100%; 
            display: flex;
            flex-direction: row;
        }
        li a {
            display: block;
            color: #f2f2f2;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
            font-size: 18px;
            transition: background-color 0.3s, color 0.3s;
        }
        li a:hover {
            background-color: #575757;
            color: #ffffff;
        }
        .active-link {
            background-color: #4CAF50;
            color: white;
         }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul&gt;&lt;li class="active-link"&gt;&lt;a href="#" &gt;Tutorialspoint&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Articles&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Courses&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt; Welcome to TutorialsPoint &lt;/h1&gt;&lt;h3&gt; Simple Easy Learning at your fingertips &lt;/h3&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Vertical Navbar

    A vertical navigation bar is also known as a sidebar menu. It is typically positioned on the left or right side of the screen.

    Example

    <!DOCTYPE html><html><head><style>
    
        body{
            color: #4CAF50;
            display: flex;
            flex-direction: row;
            gap: 10px;
        }
        ul {
            background-color: #333;
            overflow: hidden;
            list-style-type: none;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
        }
        li a {
            display: block;
            color: #f2f2f2;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
            font-size: 18px;
            transition: background-color 0.3s, color 0.3s;
        }
        li a:hover {
            background-color: #575757;
            color: #ffffff;
        }
        .active-link {
            background-color: #4CAF50;
            color: white;
         }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul&gt;&lt;li class="active-link"&gt;&lt;a href="#" &gt;Tutorialspoint&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Articles&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Courses&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;h1&gt; Welcome to TutorialsPoint &lt;/h1&gt;&lt;h3&gt; Simple Easy Learning at your fingertips &lt;/h3&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Dropdown Navbar

    A dropdown navbar is a navigation bar with a drop-down menu that appears when a user hovers over a link. Dropdown menus are a way to show a list of related items in a small space.

    Example

    <!DOCTYPE html><html><head><link rel="stylesheet" 
    
         href=
    "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><style>
        .navbar {
            background-color: #2c3e50;
            overflow: hidden;
        }
        .navbar a {
            display: block;
            float: left;
            color: #ecf0f1;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
            font-size: 16px;
            transition: background-color 0.3s, color 0.3s;
        }
        .navbar a:hover {
            background-color: #34495e;
            color: #ecf0f1;
        }
        .active-link {
            background-color: #4CAF50;
            color: white;
        }
        .dropdown {
            float: left;
        }
        .dropdown .dropbtn {
            border: none;
            color: #ecf0f1;
            padding: 14px 20px;
            background-color: #2c3e50;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s, color 0.3s;
        }
        .dropdown .dropbtn:hover {
            background-color: #4CAF50;
            color: #ecf0f1;
        }
        .dropdown-content {
            display: none;
            position: absolute;
            background-color: #34495e;
            min-width: 160px;
            box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
            z-index: 1;
        }
        .dropdown-content a {
            float: none;
            color: #ecf0f1;
            padding: 12px 16px;
            text-decoration: none;
            display: block;
            text-align: left;
            transition: background-color 0.3s, color 0.3s;
        }
        .dropdown-content a:hover {
            background-color: #4CAF50;
            color: white;
        }
        .dropdown:hover .dropdown-content {
            display: block;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;nav class="navbar"&gt;&lt;a href="#" class="active-link"&gt;Tutorialspoint&lt;/a&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;div class="dropdown"&gt;&lt;button class="dropbtn"&gt;Articles
                &lt;i class="fa fa-caret-down"&gt;&lt;/i&gt;&lt;/button&gt;&lt;div class="dropdown-content"&gt;&lt;a href="#"&gt;HTML&lt;/a&gt;&lt;a href="#"&gt;CSS&lt;/a&gt;&lt;a href="#"&gt;Bootstrap&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;a href="#"&gt;Courses&lt;/a&gt;&lt;/nav&gt;&lt;h1&gt; Welcome to TutorialsPoint &lt;/h1&gt;&lt;h3&gt; Simple Easy Learning at your fingertips &lt;/h3&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Fixed Navbar

    A fixed navbar is a navigation bar that sticks to the top of the screen when the user scrolls down the page. To make a navbar fixed, you can use the position: fixed property.

    Example

    <!DOCTYPE html><html><head><style>
    
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            height: 2000px;
            background-color: #e6e451;
        }
        .navbar {
            position: fixed;
            top: 0;
            width: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #2c3e50;
        }
        .navbar a {
            display: block;
            float: left;
            color: #ecf0f1;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
            font-size: 17px;
            transition: background-color 0.3s, color 0.3s;
        }
        .navbar a:hover {
            background-color: #34495e;
            color: #ecf0f1;
        }
        .active-link {
            background-color: #4CAF50;
            color: white;
        }
        .heading {
            padding-top: 70px;
            text-align: center;
            background-color: #e6e451;
            padding-bottom: 300px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;nav class="navbar"&gt;&lt;a href="#" class="active-link"&gt;
            Tutorialspoint
        &lt;/a&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;a href="#"&gt;Articles&lt;/a&gt;&lt;a href="#"&gt;Courses&lt;/a&gt;&lt;/nav&gt;&lt;div class="heading"&gt;&lt;h1&gt; Welcome to TutorialsPoint &lt;/h1&gt;&lt;h2&gt; Tutorialspoint CSS Fixed Navbar &lt;/h2&gt;&lt;h3&gt;Scrolldown....&lt;/h3&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Divider Lines for Navbar

    You can also add a divider line between the links in the navbar using the border-right property as shown below

    Example

    <!DOCTYPE html><html><head><style>
    
        body{
            color: #4CAF50;
        }
        ul {
            background-color: #333;
            overflow: hidden;
            list-style-type: none;
            margin: 0;
            padding: 0;
            width: 100%; 
            display: flex;
            flex-direction: row;
        }
        li a {
            display: block;
            color: #f2f2f2;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
            font-size: 18px;
            border-right: 5px solid green;
            transition: background-color 0.3s, color 0.3s;
        }
        li a:hover {
            background-color: #575757;
            color: #ffffff;
        }
        .active-link {
            background-color: #4CAF50;
            color: white;
         }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul&gt;&lt;li class="active-link"&gt;&lt;a href="#" &gt;Tutorialspoint&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Articles&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Courses&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt; Welcome to TutorialsPoint &lt;/h1&gt;&lt;h3&gt; Simple Easy Learning at your fingertips &lt;/h3&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Fixed Vertical Navbar

    The following example demonstrates how the position: fixed property can be used to create a fixed vertical navbar, which ensures that the navbar stays on the left side of the screen, even when the user scrolls down the page.

    Example

    <!DOCTYPE html><html><head><style>
    
        div{
            height: 1000px;
            margin-left: 35%;
        }
        ul {
            background-color: #333;
            overflow: hidden;
            list-style-type: none;
            position: fixed;
            width: 30%;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
        }
        li a {
            display: block;
            color: #f2f2f2;
            text-align: center;
            padding: 14px 20px;
            text-decoration: none;
            font-size: 18px;
            transition: background-color 0.3s, color 0.3s;
        }
        li a:hover {
            background-color: #575757;
            color: #ffffff;
        }
        .active-link {
            background-color: #4CAF50;
            color: white;
         }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul&gt;&lt;li class="active-link"&gt;&lt;a href="#" &gt;Tutorialspoint&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Home&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Articles&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;Courses&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&lt;h1&gt; Welcome to TutorialsPoint &lt;/h1&gt;&lt;h3&gt; Simple Easy Learning at your fingertips &lt;/h3&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>