Author: saqibkhan

  • Media Queries

    Media queries in CSS are used to apply different CSS styles based on the screen size, resolution, and other characteristics of the user device. Media queries uses @media rule to include a extra block of CSS properties when a certain conditions are met. Media queries can also be used to style printable version of page separately.

    Syntax

    @media not|only mediatype and (media feature) and (media feature){
    
    CSS-Code;}</pre>

    Here, media-type can be things like screen, print, speech, etc., and media-feature can be characteristics such as width, height, aspect ratio, orientation, and more.

    You might have noticed that the same website looks different on mobile and desktop devices. This type of screen depended styling is achieved using CSS media queries. In this tutorial, we will learn about media queries and the properties associated with them.

    Media Types

    Media types are used in CSS media queries to apply different styles based on device. The most common media types are allprint, and screen. If you don't specify a media type, it matches all devices.

    • all: Default Value. This value applies to all media types.
    • print: This value is only valid when printing the document.
    • screen: This value is only valid for screens, such as computers, tablets, and smartphones.

    JavaScript provides a CSS object model interface called CSSMediaRule, which can be used to access the rules created using the @media CSS at-rule.

    CSS Media Type Print

    Sometimes the print version generated for user doesn't require all the styling shown in screen. The print version generally generated in grayscale style or with simple light colors. This type of designing is recommended, as dark backgrounds will consume more ink from printer.

    Example

    Following example demonstrates use of a CSS media query with the media type print.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        body{
            background-color: black;
            color: white;
            padding: 10px;
        }
        @media print {
            body { 
                background-color: white;
                color: black;
                padding: 10px;
            }
        }
        button {
            background-color: aqua;
            padding: 5px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h3&gt; Tutorialspoint &lt;/h3&gt;&lt;p&gt;CSS Media Type - Print&lt;/p&gt;&lt;p&gt;
        Background is white for printable version of this page.
        Check out...
    &lt;/p&gt;&lt;button onclick="printPage()"&gt;Print Page&lt;/button&gt;&lt;script&gt;
    function printPage() {
        window.print();
    }
    &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Media Type All

    This is used to specify common styling that used in all screen sizes, printable version and other versions.

    Example

    The following example demonstrates the use of a CSS media query with the media type all

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        @media all {
            body{
                background-color: black;
                color: white;
                padding: 10px;
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h3&gt;Tutorialspoint &lt;/h3&gt;&lt;p&gt; CSS Media Type - All &lt;/p&gt;&lt;p&gt; 
        In printable version or any screen size, the 
        styles of this page is same.
    &lt;/p&gt;&lt;button onclick="printPage()"&gt;
        Print Page
    &lt;/button&gt;&lt;script&gt;
        function printPage() {
        window.print();
        }
    &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Media Type Screen

    This value is only valid for screens, such as computers, tablets, and smartphones.

    Example

    The following example demonstrates that if the screen size is greater than 500px, the background color changes to pink and text color changes to blue

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .container {
            display: flex;
            flex-direction: row;
            padding: 10px;
            gap: 10px;
        }
        .child{
            padding: 10px;
            background-color: #f0f0f0;
            border: 2px solid;
        }
        @media screen and (max-width: 500px) {
            .container {
                flex-direction: column;
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;
        Orientation of child elements depend on screen size,
        for screen size less than 500px, children align in 
        two different columns. 
    &lt;/p&gt;&lt;div class="container"&gt;&lt;div class="child" &gt; HTML &lt;/div&gt;&lt;div class="child"&gt; CSS &lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Width Ranges for Media Queries

    In media Queries, you can also specify screen width range like this.

    @media (width > 700px){/* Styles for screens that are at least 700px wide */}

    Example

    The following example demonstrates that when the screen width is between 600px and 800px, the background color changes to yellow and text color changes to red.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
    p {
        color: blue;
    }
    @media (600px &lt; width &lt; 800px) {
        p {
            background-color: yellow;
            color: red;
        }
    }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Tutorialspoint&lt;/h1&gt;&lt;p&gt;CSS Media Type - Screen&lt;/p&gt;&lt;p&gt;Resize the browser size to see the effect.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Comma Separated Media Types

    A comma in media query is similar to logical 'OR' operator.

    The following declaration will applying to screen width less than 500px or for printable versions. You can also use OR operator instead of comma.

    @media (min-width: 500px), print{/* Styles */}

    Example

    The following example demonstrates using media types with comma separation. The background color changes when in print mode and also for screen size is > 500px

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        body {
            background-color: white;
            color: black;
        }
        @media (min-width: 500px), print {
            body {
                background-color: black;
                color: white;
            }
        }
    button {
        background-color: violet;
        padding: 5px;
    }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;CSS Media Type - Screen and Print&lt;/h1&gt;&lt;p&gt;
        Resize the window to less than 500px to see the 
        background and font color changes to default.
    &lt;/p&gt;&lt;p&gt;
        Click on below button to see the effect when you 
        print the page. ( Enable background graphics options 
        if any at print section )
    &lt;/p&gt;&lt;button onclick="printPage()"&gt;Print Page&lt;/button&gt;&lt;script&gt;
        function printPage() {
            window.print();
        }
    &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Media Type With Only

    Media Type With Only applies the style only if the entire media query matches. This can be helpful to prevent older browsers from applying styles.

    Example

    The following example demonstrates that when the screen width is between 500px and 700px, the background color changes to pink and text color changes to blue

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
    body {
        color: red;
    }
    @media only screen and (min-width: 500px) and (max-width: 700px) {
        body {
            color: blue;
            background-color: pink;
        }
    }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Tutorialspoint&lt;/h1&gt;&lt;p&gt;CSS Media Type - Screen&lt;/p&gt;&lt;p&gt;
    Resize the browser window to see the effect.
    </p></body></html>

    CSS Media Queries AND Operator

    The AND operator is used to combine multiple conditions in a media query. Both conditions must be true for the styles to apply.

    Example

    This example demonstrates when screen width is between 500px and 700px, style will be added.

    Open Compiler

    <!DOCTYPE html><!DOCTYPE html><html lang="en"><head><style>
    
        body {
            background-color: lightgray;
            color: black;
        }
        @media (min-width: 500px) and (max-width: 700px) {
            body {
                background-color: lightgreen;
                color: blue;
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt; Media Query with AND Operator&lt;/h1&gt;&lt;p&gt; 
        Resize the browser window between 500px and 700px 
        to see the effect.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Media Queries NOT Operator

    The NOT operator negates a media query, applying the styles only if the media query does not match.

    The NOT operator is evaluated last in a media query, so the above media query would be evaluated as follows:

    @media not (all and (monochrome)){/*  */}/* It's not evaluated like this:*/@media (not all) and (monochrome){/*  */}

    Example

    In this example NOT operator negate the condition width < 700.

    Open Compiler

    <!DOCTYPE html><!DOCTYPE html><html lang="en"><head><style>
    
        body {
            background-color: lightgray;
            color: black;
        }
        @media not screen and (width &lt; 700px) {
            body {
                background-color: orange;
                color: white;
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt; Media Query with NOT Operator &lt;/h1&gt;&lt;p&gt; 
        Resize the browser window to less than 700px 
        to see the effect.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Creating Complex Media Query

    You can create complex media queries by combining multiple conditions using the andnot, and only operators. You can also combine multiple media queries into a comma-separated list.

    Example

    In this example we combined multiple media queries, try changing browser width to see multiple effects.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        body {
            background-color: lightgray;
            color: black;
        }
        @media only screen and (min-width: 500px) and (max-width: 700px) {
            body {
                background-color: pink;
                color: blue;
            }
        }
        @media not screen and (max-width: 700px) {
            body {
                background-color: orange;
                color: white;
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt; 
        Media Query with AND, NOT, and ONLY Operators 
    &lt;/h1&gt;&lt;p&gt; 
        Resize the browser window to see the effects.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Media Queries For Screen Orientation

    We can define style for specific orientation (landscape or portrait) of user device. The default value for this is portrait.

    @media (orientation: portrait){/* Styles */}

    Example

    The following example demonstrates that when the screen width is greater than 500px and the device is in landscape orientation, the text color changes to blue

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        body {
            color: red;
        }
        @media (min-width: 500px) and (orientation: landscape) {
            body {
                color: blue;
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h3&gt;Resize the browser window to see the effect.&lt;/h3&gt;&lt;p&gt;
        The text color changed to blue when the viewport width is 
        at least 500px and the device is in landscape orientation.
    &lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Variables

    CSS Variables are custom properties that allows you to store and reuse values throughout your CSS program. CSS variables are similar to variables in other programming languages

    How to declare a Variable in CSS?

    CSS variables are typically defined using :root selector. Following is syntax to declare a CSS variable:

    :root{--variable-name: value;}

    To use a CSS variable, follow the syntax:

    selector{var(--variable-name, fallback-value);}

    The selector can be a classid or tag. Learn what is selectors here.

    We can use the var function to replace values for CSS properties on any elements.

    CSS does not allow variables to be used in media queries or container queries, Also you cannot use them to set the name of a CSS property or selector.

    The Traditional Way

    In this example we will see how the colors and styling done without using variable. Here you can notice that we are repetitively specifying property values.

    Example

    Open Compiler

    <html lang="en"><head><style>
    
        body {
            background-color: #f0f0f0;
            color: #333;
            font-family: 'Arial', sans-serif;
            padding: 10px;
        }
        header {
            background-color: #0044cc;
            color: #fff;
            padding: 10px;
        }
        .container {
            background-color: #fff;
            padding: 10px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;header&gt;&lt;h1&gt;Welcome to My Website&lt;/h1&gt;&lt;/header&gt;&lt;div class="container"&gt;&lt;p&gt;
            This is a container with a background color 
            defined traditionally. Here we need to specify 
            repeating color values multiple times. 
        &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Using CSS Variables

    The following code shows how to use variables to avoid redundancy in CSS. This become more relevant when working on large code bases in real world application.

    Here you can also see that we defined the color '#0044cc' as blue, So developer can repetitively use this color by using variable blue.

    Example

    Open Compiler

    <html lang="en"><head><style>
    
        :root {
            --main-bg-color: #f0f0f0;
            --main-text-color: #333;
            --primary-font: 'Arial', sans-serif;
            --padding: 10px;
            --blue: #0044cc;
            --header-text: #fff;
            --container-bg: #fff;
        }
        
        body {
            background-color: var(--main-bg-color);
            color: var(--main-text-color);
            font-family: var(--primary-font);
            padding: var(--padding);
        }
        
        header {
            background-color: var(--blue);
            color: var(--header-text);
            padding: var(--padding);
        }
        
        .container {
            background-color: var(--container-bg);
            padding: var(--padding);
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;header&gt;&lt;h1&gt;Welcome to My Website&lt;/h1&gt;&lt;/header&gt;&lt;div class="container"&gt;&lt;p&gt;
            This is a container with a background color 
            defined using variables.
        &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    The Root Pseudo-Class

    CSS variables are declared at the top of the stylesheet using the :root pseudo-class, which matches the root element of the document. This means that CSS variables declared using the :root selector can be used by any element in the document.

    CSS variable names are case-sensitive, which means that --pink-color and --Pink-color are two different variables.

    Step-1: Define the custom properties

    To declare variables using the :root pseudo-class, you simply add the '--' prefix to the variable name and then set its value.

    :root{--pink-color: pink;--blue-color: blue;}

    Step-2: Use the custom properties in the CSS

    These variables can then be used throughout your CSS code using the var() function.

    .box{width: 400px;height: 200px;background-color:var(--pink-color);color:var(--blue-color);}.box1, .box2{display: inline-block;background-color:var(--pink-color);width: 100px;height: 50px;color:var(--blue-color);}

    Example

    The following example shows that how to define our own shade of color variation in hex and rgb value, store in a variable and reuse later.

    Open Compiler

    <html><head><style>
    
        :root {
            --white-color: #f0f0f0;
            --black-color: rgb(0, 0, 21);
        }
        .box {
            text-align: center;
            padding: 20px;
            background-color: var(--white-color);
            color: var(--black-color);
        }
        .box1, .box2 {
            display: inline-block;
            background-color: var(--black-color);
            color: var(--white-color);
            width: 100px;
            height: 50px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="box"&gt;&lt;h2&gt;Tutorialspoint&lt;/h2&gt;&lt;p&gt; How to code a website using HTML and CSS &lt;/p&gt;&lt;div class="box1"&gt; HTML &lt;/div&gt;&lt;div class="box2"&gt; CSS &lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Inheritance of Custom Properties

    You can use CSS variables to define reusable CSS values that can be inherited by child elements.

    Step - 1: Declare the custom property in the :root selector.

    This makes the variable global and accessible to all elements in the document.

    :root{--pink-color: pink;}

    Step - 2: Use the var() function to access the custom property in the CSS.

    This allows you to reuse the custom property throughout all the children of box.

    .box{--box-background:var(--pink-color);background-color:var(--box-background);}

    Step - 3: Use the color inside child.

    This allows you to customize the value of the custom property for specific elements.

    .box1, .box2{background-color:var(--box-background);}

    Example

    The following example demonstrates that the use of CSS custom properties with inheritance.

    Open Compiler

    <html><head><style>
    
        :root {
            --white-color: #f0f0f0;
            --black-color: rgb(0, 0, 21);
        }
        .box {
            --box-background: var(--white-color);
            background-color: var(--box-background);
            text-align: center;
            padding: 20px;
        }
        .box1, .box2 {
            display: inline-block;
            background-color: var(--black-color);
            /* Box Background is defined at parent box */
            color: var(--box-background);
            width: 100px;
            height: 50px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="box"&gt;&lt;h2&gt;Tutorialspoint&lt;/h2&gt;&lt;p&gt; How to code a website using HTML and CSS &lt;/p&gt;&lt;div class="box1"&gt; HTML &lt;/div&gt;&lt;div class="box2"&gt; CSS &lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Variables Fallback Value

    You can use CSS variables with fallback values to ensure that your CSS code is still valid and works even if the variable is not defined.

    CSS fallback values are not used to make CSS custom properties work in older browsers. They are only used as a backup in browsers that support CSS custom properties, in case the variable is not defined or has an invalid value.

    Example

    In below example, we have only defined color shade for white, But are also using black variable. Since we are defining fallback-value for black color variable, there will not be any error.

    Open Compiler

    <html><head><style>
    
        :root {
            --white-color: #f0f0f0;/* Shade for white */
            /* variable for black not defined */
        }
        .box {
            text-align: center;
            padding: 20px;
            background-color: var(--white-color, white);
            color: var(--black-color, black);
        }
        .box1, .box2 {
            display: inline-block;
            background-color: var(--black-color, black);
            color: var(--white-color, white);
            width: 100px;
            height: 50px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="box"&gt;&lt;h2&gt;Tutorialspoint&lt;/h2&gt;&lt;p&gt; How to code a website using HTML and CSS &lt;/p&gt;&lt;div class="box1"&gt; HTML &lt;/div&gt;&lt;div class="box2"&gt; CSS &lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Variables Invalid Value

    In below example, the --red-color custom property is defined with a value of 15px. This is an invalid value because the red color property only accepts color values.

    However, the browser will not be able to resolve the value of the custom property because it is invalid. As a result, it will simply ignore the CSS rule and the text in the second h2 element will remain the same color.

    Example

    In this example even though we are making color of h2 as red using color property, due to error caused by invalid color the default color black is used.

    Open Compiler

    <html><head><style>
    
    :root {
        /* define a invalid value for c0lor */
        --red-color: 15px;
    }
    h2 {
        /* Make color of h2 as red */
        color: red;
    }
    h2 {
        /* Use invalid color for h2, this will cause error
          and default color value (black) is used */
        color: var(--red-color);
    }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;
        Tutorialspoint CSS Variables.
    &lt;/h2&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Variables in Javascript

    The following example demonstrates that how to use JavaScript to set CSS variables globally and locally.

    Example

    Open Compiler

    <html><head><style>
    
        .box {
            text-align: center;
            padding: var(--padding);
            background-color: var(--white-color);
            color: var(--black-color);
        }
        .box1, .box2 {
            display: inline-block;
            background-color: var(--black-color);
            color: var(--white-color);
            width: 100px;
            height: 50px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="box"&gt;&lt;h2&gt;Tutorialspoint&lt;/h2&gt;&lt;p&gt;How to code a website using HTML and CSS&lt;/p&gt;&lt;div class="box1"&gt;HTML&lt;/div&gt;&lt;div class="box2"&gt;CSS&lt;/div&gt;&lt;/div&gt;&lt;script&gt;
        const root = document.documentElement;
        const boxElement = document.querySelector('.box');
        // Define a global variable
        root.style.setProperty('--padding', '20px');
        // Define variables specific to the box element
        boxElement.style.setProperty('--white-color', '#f0f0f0');
        boxElement.style.setProperty('--black-color', 'rgb(0, 0, 21)');
    &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

  • Pagination

    CSS pagination is a technique of creating page numbers for a website. This help users to easily navigate between large amounts of content. In this chapter, we will learn how to setup and style pagination using CSS.

    CSS Pagination Example

    Here is a example of pagination styled using CSS.

    12345

    First Page

    Table of Contents

    • Setup Pagination For a Website
    • Simple Pagination Example
    • Styling Pagination Using CSS
    • Rounded Pagination Buttons
    • Hoverable Transition Effect
    • Bordered Pagination
    • Space Between Links
    • Pagination Size
    • Centered Pagination
    • Next and Previous Buttons
    • Breadcrumb Pagination
    • Pagination With List

    How to Setup Pagination For a Website?

    To set up pagination for a website, you need to break your content into small pages and provide navigation to move between them in every page. Let’s see step by step procedure for setting up pagination.

    Setup HTML Structure

    First we need to set HTML structure for pagination setup. We can use anchor tags enclosed in div elements for this. Following is code for HTML structure of a pagination setup.

    <div class="pagination"><a href="#">«</a><!-- Previous Page --><a href="#" class="active">1</a><a href="#">2</a><a href="#">3</a><a href="#">»</a><!-- Next Page --></div><div id="page1" class="page active">Page 1 Shows....</div><div id="page2" class="page">Page 2 Shows....</div>

    Control Visibility With CSS

    Initially all the pages should be invisible, expect the first page. For this, we can use display property as none for all pages. As class “active” is added to first page, it will be visible initially.

    .pagination{display: flex;justify-content: center;}.page{display: none;}.active{display: block;}

    Pagination Logic With JavaScript

    Now, we need to add some JavaScript to handle pagination logic. We can capture click event on pagination links using JavaScript addEventListener() method and change visibility of pages based on that.

    Simple Pagination Example

    The example belows shows simple pagination setup developed using the above steps.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        body{
            height: 150px;
        }
        .pagination {
            display: flex;            
            justify-content: center;    
            margin: 20px 0;            
        }
        .pagination a {
            color: green;               
            border: 5px solid #ddd;     
            padding: 5px 10px;         
            margin: 0 5px;           
        }
        .page {
            display: none;         
        }
        .active {
            display: block;      
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#page1"&gt;1&lt;/a&gt;&lt;a href="#page2"&gt;2&lt;/a&gt;&lt;a href="#page3"&gt;3&lt;/a&gt;&lt;a href="#page4"&gt;4&lt;/a&gt;&lt;a href="#page5"&gt;5&lt;/a&gt;&lt;/div&gt;&lt;div id="page1" class="page active"&gt;Page 1 Shows....&lt;/div&gt;&lt;div id="page2" class="page"&gt;Page 2 Shows....&lt;/div&gt;&lt;div id="page3" class="page"&gt;Page 3 Shows....&lt;/div&gt;&lt;div id="page4" class="page"&gt;Page 4 Shows....&lt;/div&gt;&lt;div id="page5" class="page"&gt;Page 5 Shows....&lt;/div&gt;&lt;script&gt;
        document.addEventListener('DOMContentLoaded', function() {
            const pages = document.querySelectorAll('.pagination a');
            const contentPages = document.querySelectorAll('.page');
            pages.forEach(page =&gt; {
                page.addEventListener('click', function(event) {
                    event.preventDefault();
                    // Remove 'active' class from all content pages
                    contentPages.forEach(p =&gt; p.classList.remove('active'));
                    // Find the target page and display it
                    const targetPage = document.querySelector(this.getAttribute('href'));
                    if (targetPage) {
                        targetPage.classList.add('active');
                    }
                    // Add 'active' class to the clicked link
                    this.classList.add('active');
                });
            });
        });
    &lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Styling Pagination Using CSS

    To style pagination links, we can use pseudo-class :active and :hover.

    • Pseudo-class :active Can be used to highlight current page in pagination links.
    • Pseudo-class :hover Can be used to style mouse hover states of pagination link.

    The following example shows how these properties can enhance appearance of pagination links.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            list-style: none;
            padding: 0;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
        }
        .pagination a.active-link {
            background-color: violet;
            color: white;
        }
        .pagination a:hover:not(.active-link) {
            background-color: pink;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Rounded Pagination Buttons

    We can create a pagination links with a rounded button by using border-radius CSS property. Let see an example:

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            list-style: none;
            padding: 0;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
            border-radius: 8px;
        }
        .pagination a.active-link {
            background-color: violet;
            color: white;
        }
        .pagination a:hover:not(.active-link) {
            background-color: pink;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Hoverable Transition Effect

    We can make pagination link smooth with transition effects when hovering over the each page links using transition property. Let see an example:

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            list-style: none;
            padding: 0;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
            transition: background-color 0.4s;
        }
        .pagination a.active-link {
            background-color: violet;
            color: white;
        }
        .pagination a:hover:not(.active-link) {
            background-color: pink;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Bordered Pagination

    To add border to pagination links, we can use CSS border property. Here is an example:

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            list-style: none;
            padding: 0;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
            transition: background-color 0.4s;
            border: 2px solid black;
        }
        .pagination a.active-link {
            background-color: violet;
            color: white;
        }
        .pagination a:hover:not(.active-link) {
            background-color: pink;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    We can use CSS margin property to create a space around each link in the pagination component. Let see an example for this.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            list-style: none;
            padding: 0;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
            transition: background-color 0.4s;
            border: 2px solid black;
            margin: 2px;
        }
        .pagination a.active-link {
            background-color: violet;
            color: white;
        }
        .pagination a:hover:not(.active-link) {
            background-color: pink;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Pagination Size

    To change the size of pagination links, we can use font-size property. Let see an example:

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            list-style: none;
            padding: 0;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
            border: 2px solid black;
            font-size: 20px;
            margin: 2px;
        }
        .pagination a.active-link {
            background-color: violet;
            color: white;
        }
        .pagination a:hover:not(.active-link) {
            background-color: pink;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Centered Pagination

    We can use the justify-content CSS property to center pagination links. Here is an example.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            list-style: none;
            padding: 0;
            justify-content: center;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
            transition: background-color 0.7s;
            border: 2px solid black;
            margin: 2px;
        }
        .pagination a.active-link {
            background-color: violet;
            color: white;
        }
        .pagination a:hover:not(.active-link) {
            background-color: pink;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Pagination With Next and Previous Buttons

    We can add previous and next buttons across pagination links for quicker navigation. Let see an example for this.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            list-style: none;
            padding: 0;
            margin: 10px;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
            transition: background-color 0.4s;
            border: 2px solid black;
            margin: 2px;
        }
        .prev-next{
            background-color: grey;
        }
        .pagination a.active-link {
            background-color: violet;
            color: white;
        }
        .pagination a:hover:not(.active-link) {
            background-color: pink;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#" class="prev-next"&gt;&lt; Previous&lt;/a&gt;&lt;a href="#" class="active-link"&gt;Page 1&lt;/a&gt;&lt;a href="#"&gt;Page 2&lt;/a&gt;&lt;a href="#"&gt;Page 3&lt;/a&gt;&lt;a href="#"&gt;Page 4&lt;/a&gt;&lt;a href="#" class="prev-next"&gt;Next &gt;&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Breadcrumb pagination is a navigation method that shows users the path theyve taken to reach their current page. Let's see an example to design breadcrumb pagination.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        ul.breadcrumb-pagination {
            padding: 10px;
            list-style: none;
            background-color: pink;
        }
        ul.breadcrumb-pagination li {
            display: inline-block;
        }
        ul.breadcrumb-pagination li a {
            color: blue;
        }
        ul.breadcrumb-pagination li+li:before {
            padding: 10px;
            content: "/\00a0";
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul class="breadcrumb-pagination"&gt;&lt;li&gt;&lt;a href="#"&gt;Tutorialspoint&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;CSS Pagination&lt;/a&gt;&lt;/li&gt;&lt;li class="active-link"&gt;CSS Pagnation With Breadcrumb&lt;/li&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Pagination With List

    We can also use unordered list (<ul>) and list items (<li>) for creating the pagination. Let see an example.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .pagination {
            display: flex;
            padding: 0;
            list-style: none;
        }
        .pagination li {
            margin: 5px;
        }
        .pagination a {
            text-decoration: none;
            padding: 8px 12px;
            color: #333;
            border: 2px solid black;
        }
        .pagination a:hover {
            background-color: pink;
        }
        .pagination .active-link {
            background-color: violet;
            color: white;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul class="pagination"&gt;&lt;li&gt;&lt;a href="#"&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Print Page

  • Buttons

    Buttons are interactive elements that allow users to perform actions on your website or application. Buttons are commonly either rectangular or circular in shape and have a text label that says what will happen when you click on them.

    While browsing web pages, you may have come across various interactive elements, such as submit buttons. In this article we will cover how to style buttons using CSS, explore different types of buttons, and discuss related properties.

    Table of Contents

    • How to Style a Button in CSS?
    • CSS Buttons Colors
    • CSS Button Borders
    • CSS Rounded Buttons
    • CSS Button Hover Effect
    • CSS Button Focus and Active
    • CSS Button Shadow Effect
    • CSS Disabled Buttons
    • CSS Buttons Widths
    • CSS Buttons Groups
    • CSS Vertical Buttons Groups
    • CSS Buttons on Image
    • CSS Animated Buttons

    How to Style a Button in CSS?

    • Setting the Dimensions: In CSS, height and width properties can be used to adjust size of buttons in a webpage.
    • Define Color and Border: A UI compatible color and border with the right thickness make buttons stand out. The background-color and border property can be used to set color and border for any buttons in css.
    • Shadow Effect: Adding a shadow effect around button using box-shadow property enhances button style.
    • Hover Effect: Interactive styling like hover effect change the style of button when user hover the mouse over it. This can include changes in opacity, size (using transforms), etc.
    • Animated Button: CSS animation can be used to create dynamic interactive buttons.

    Setting Buttons Colors

    As mentioned above we can use background-color property in CSS to give right color for buttons. Check out the example

    Example

    In this example we use style buttons with different colors that matches with UI of background color.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        body{
            background: black;
            padding: 40px;
        }
        button {
            padding: 20px;
            margin-bottom: 10px;
            cursor: pointer;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;button style="background: #FFD700"&gt;
        Yellow Button
    &lt;/button&gt;&lt;button style="background: #00FFFF"&gt;
        Blue Button
    &lt;/button&gt;&lt;button style="background: #FFFFFF"&gt;
        White Button
    &lt;/button&gt;&lt;button style="background: #FF4500"&gt;
        Orange Button
    &lt;/button&gt;&lt;button style="background: #32CD32"&gt;
        Lime Button
    &lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Button Borders

    The border is space around the edge of a button. We can style a button border using border property.

    The border property take three values thickness, type and color of border.

    Example

    Here is an example of how to create buttons with different border colors using

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        button {
            font-size: 12px;
            padding: 10px;
            margin: 5px;
            background-color: #f0f0f0;
        }
        .style1 {
            border: 3px solid darkred;
        }
        .style2 {
            border: 3px solid gold;
        }
        .style3 {
            border: 3px solid black;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;button class="style1"&gt;
        darkred border
    &lt;/button&gt;&lt;button class="style2"&gt;
        gold border
    &lt;/button&gt;&lt;button class="style3"&gt;
        black border
    &lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Creating Rounded Buttons

    We can create round cornered button or completely circular button using border-radius property.

    /* Round cornered button */border-radius: 10px;/* Circular button */border-radius: 50%;

    Example

    Here is an example of how to create rounded corner buttons.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        button {
            font-size: 12px;
            padding: 10px;
            margin: 5px;
        }
        .style1 {
            border-radius: 10px;
            background-color: violet;
        }
        .style2 {
            border-radius: 20px;
            background-color: pink;
        }
        .style3 {
            border-radius: 50%;
            background-color: violet;
        }
    </style></head><body><button class="style1">
        Border-radius: 10px;
    &lt;/button&gt;&lt;button class="style2"&gt;
        Border-radius: 20px;
    &lt;/button&gt;&lt;button class="style3"&gt;
        Circle
    &lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Button Hover Effect

    Moving the mouse pointer above button without clicking on it is called hovering. We can style hover state of a button using :hover pseudo-class.

    button:hover{property: value;}

    Example

    Here is an example of creating button hover effect.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        button {
            font-size: 12px;
            padding: 10px;
            margin: 5px;
            background-color: #1156b3; /* Darker blue */
        }
        .style1:hover {
            background-color: #0069d9; /* Slightly darker blue */
        }
        .style2:hover {
            transform: scale(1.2); /* Size increase effect */
        }
        .style3:hover {
            background-color: lightblue; 
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;button class="style1"&gt;
        Button 1
    &lt;/button&gt;&lt;button class="style2"&gt;
        Button 2
    &lt;/button&gt;&lt;button class="style3"&gt;
        Button 3
    &lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Button Focus and Active Styling

    The focus state of a button is the state when the button is focused, typically after being clicked or tabbed into. The active state of a button is the state when the button is being clicked. We can style these states using the pseudo-class :focus. and :active

    button:focus{property: value;}button:active{property: value;}

    Example

    Here is an example focus state and active state of a button.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
    .style-button {
        display: inline-block;
        padding: 15px;
        background-color: pink;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        transition: background-color 0.3s ease, transform 0.1s ease;
    }
    .style-button:hover {
        background-color: violet;
    }
    .style-button:focus {
        outline: none;
        box-shadow: 0 0 5px 2px blue;
    }
    .style-button:active {
            transform: translateY(2px);
            background-color: yellow;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;button class="style-button"&gt;Press Me&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Shadow Effect on Button

    In CSS, the box-shadow property used to add shadow effect around buttons.

    A box shadow is described by horizontal and vertical offsets relative to the element, blur and spread radius, and color. Following is the syntax of box-shadow:

    button{box-shadow: inset horizontal vertical
    
                blur-radius spread-color;}</pre>

    Example

    Here is an example of how to create buttons with drop shadows.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        button {
            font-size: 12px;
            padding: 10px;
            margin: 5px;
        }
       .style1 {
          background-color: pink;
          box-shadow: 0 5px 10px 0 red; 
       }
       .style2 {
          background-color: yellow;
       }
       .style3:hover {
          background-color: yellow;
          box-shadow: 0 5px 10px 0 orange;
       }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;button class="style1"&gt;
        Button 1
    &lt;/button&gt;&lt;button class="style2"&gt;
        Button 2
    &lt;/button&gt;&lt;button class="style3"&gt;
        Button 3
    &lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Disabled Buttons

    A disabled button is a button that is not clickable. This button can be enabled using JavaScript when certain conditions are met.

    We can create a disabled button by setting the cursor property to not-allowed.

    Example

    Here is an example.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        button {
            font-size: 12px;
            padding: 10px;
            margin: 5px;
        }
        .style1 {
            background-color: pink;
            cursor: pointer;
        }
        .style2 {
            background-color: yellow;
            opacity: 0.5;
            cursor: not-allowed;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;button class="style1"&gt;
        Normal Button
    &lt;/button&gt;&lt;button class="style2"&gt;
        Disabled Button
    &lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Buttons Width

    We can define horizontal width for button using width property.

    Example

    Here is an example of how to create buttons with different widths.

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        button {
            font-size: 12px;
            padding: 10px;
            margin: 5px;
        }
       .style1 {
          background-color: pink;
          width: 200px;
       }
       .style2 {
          background-color: violet;
          width: 50%;
       }
       .style3 {
          background-color: yellow;
          width: 100%;
       }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;button class="style1"&gt;
        width 100px
    &lt;/button&gt;&lt;br&gt;&lt;button class="style2"&gt;
        width 50%
    &lt;/button&gt;&lt;br&gt;&lt;button class="style3"&gt;
        width 100%
    &lt;/button&gt;&lt;br&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Buttons Groups

    Here is an example of how to create a horizontal button group by removing the margins and adding the float: left property to each button.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
       .button-group {
          display: flex; 
          justify-content: center;
          float: left; 
       }
       .style-button {
          background-color: yellow;
          border: none;
          padding: 10px 20px;
          float: left;
          text-align: center;
          text-decoration: none;
          font-size: 16px;
       }
       .style-button:hover {
          background-color: orange;
       }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="button-group"&gt;&lt;button class="style-button"&gt;Submit&lt;/button&gt;&lt;button class="style-button"&gt;Cancel&lt;/button&gt;&lt;button class="style-button"&gt;Reset&lt;/button&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Vertical Buttons Groups

    Here is an example of how to create a vertical button group by setting display: block and float: left property

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
    .button-group { 
        justify-content: center;
        float: left; 
    }
    .style-button {
        background-color: yellow;
        border: 2px solid orange;
        padding: 10px 20px;
        text-align: center;
        display: block;
        text-decoration: none;
        font-size: 16px;
        width: 100px; 
    }
    .style-button:hover {
        background-color: violet;
    }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="button-group"&gt;&lt;button class="style-button"&gt;Home&lt;/button&gt;&lt;button class="style-button"&gt;Blog&lt;/button&gt;&lt;button class="style-button"&gt;Setting&lt;/button&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Buttons on Image

    Here is an example of how to overlay a button on top of an image using relative positioning

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .image-container {
            position: relative;
            display: inline-block;
        }
        img {
            width: 300px;
            height: 200px;
        }
        button {
            position: absolute;
            top: 40%;
            left: 30%;
            background-color: yellow;
            border: none;
            padding: 15px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 15px; 
        }
        button:hover {
            background-color: orange;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="image-container"&gt;&lt;img src="/css/images/tree.jpg" alt="Your Image"&gt;&lt;button&gt;Click Me&lt;/button&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Animated Buttons

    In, CSS we can use pseudo-elements to animate a button clicking effect.

    Example

    Here is an example of how to create animated effect on a button

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        button {
            display: inline-block;
            padding: 15px;
            background-color: violet;
            border: none;
            text-align: center;
            text-decoration: none;
            font-size: 20px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        button::before {
            content:"";
            position: absolute;
            width: 0;
            height: 100%;
            top: 0;
            left: 0;
            background-color: pink;
            transition: width 0.3s ease-in-out;
        }
        button:hover::before {
            width: 100%;
        }
        .icon::after {
            content: '\2192'; 
            margin-left: 10px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;button&gt;
        Hover Me &lt;span class="icon"&gt;&lt;/span&gt;&lt;/button&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Print Page

  • Tooltips

    CSS Tooltips are small pop-up boxes that appears when user hovers over an element, providing additional information or more context. Tooltips are styled and positioned using CSS properties inset, translateX, translateY etc. In this tutorial we will learn how create, position and style tooltips using CSS.

    Demo Tooltip Examples

    The following section shows examples of demo tooltips. The tooltip will be visible when you hover over the texts below.TopRightLeftBottom

    Table of Contents

    • How to Create Tooltips in CSS?
    • Basic Tooltip
    • Positioning Tooltips
    • Tooltip Arrows
    • Fade In Tooltips
    • Advantages of Tooltips

    How to Create Tooltips in CSS?

    We will be following below mentioned steps to create tooltips using CSS and HTML.

    • Setting Up the Element: Create the element you want to hover over to trigger the tooltip such as, a button, an image, or any other HTML element.
    • Create the Tooltip Text: Use <span> element to create tooltip container and include tooltip texts there. We will hide this initially using visibility: hidden property in CSS.
    • Position the Tooltip: Using CSS positioning properties, we will place the tooltip element at right location around container. We have set ‘position: absolute’ on the tooltip and ‘position: relative’ on the container.
    • Styling the Tooltip: Modify the appearance of the tooltip by setting the background-color, text-color, padding, etc.
    • Display the Tooltip on Hover: For displaying the tooltip when user hovers over the target element, we have used CSS hover effect. This displays the tooltip.

    Basic Tooltip

    The following example demonstrates how to create a basic tooltips using CSS. The tooltip is displayed when the user hovers over the text.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .tooltip {
            position: relative;
            display: inline-block;
            cursor: pointer;
        }
        .tooltip .tooltiptext {
            visibility: hidden;
            width: 120px;
            background-color: #000;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            margin-left: -60px;
            opacity: 0;
            transition: opacity 0.3s;
        }
        .tooltip:hover .tooltiptext {
            visibility: visible;
            opacity: 1;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h3&gt;Hover over the text below to see the tooltip&lt;/h3&gt;&lt;div class="tooltip"&gt;
        Hover over this text
        &lt;span class="tooltiptext"&gt; This is a tooltip &lt;/span&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Positioning Tooltips

    Using CSS positioning rules we can position tooltips anywhere around main container such as top, bottom, left or right.

    To position a tooltip, first we need to set 'position: relative;' property to element container of tooltip. It allows child elements with 'position: absolute' to be positioned relative to the element container. The position of absolutely positioned element can be easily adjusted by using inset properties like top, bottom, right and left.

    .element{position: relative;}.tooltip{position: absolute;top: 50px;left: 50px;}

    The tooltip element will be placed 50px from top border and 50px from left border of container element.

    Now let us look at an example of displaying tooltip in all different direction.

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        .container{
            display: flex;
            justify-content: space-around;
            margin-top: 10%;
        }
        .tooltip-container {
            position: relative;
        }
        
        .button{
            font-family: san-serif;
            font-weight: bold;
            padding: 2px;
            border-radius: 5px;
            background-color: white;
        }
        .tooltip-container .tooltip-text {
            visibility: hidden;
            width: 120px;
            background-color: black;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px 0;
            position: absolute;
            z-index: 1;
            opacity: 0;
            transition: opacity 0.3s;
        }
        .tooltip-container:hover .tooltip-text {
            visibility: visible;
            opacity: 1;
        }
        /* Tooltip on top */
        .tooltip-top .tooltip-text {
            bottom: 125%;
            left: 50%;
            transform: translateX(-50%);
        }
        /* Tooltip on bottom */
        .tooltip-bottom .tooltip-text {
            top: 125%;
            left: 50%;
            transform: translateX(-50%); /* To center on top side */
        }
        /* Tooltip on left */
        .tooltip-left .tooltip-text {
            top: 50%;
            right: 125%;
            transform: translateY(-50%);
        }
        /* Tooltip on right */
        .tooltip-right .tooltip-text {
            top: 50%;
            left: 125%;
            transform: translateY(-50%);
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="tooltip-container tooltip-top"&gt;&lt;button class="button"&gt;Top &lt;/button&gt;&lt;span class="tooltip-text"&gt;Tooltip on top&lt;/span&gt;&lt;/div&gt;&lt;div class="tooltip-container tooltip-right"&gt;&lt;button class="button"&gt;Right &lt;/button&gt;&lt;span class="tooltip-text"&gt;Tooltip on right&lt;/span&gt;&lt;/div&gt;&lt;div class="tooltip-container tooltip-left"&gt;&lt;button class="button"&gt;Left &lt;/button&gt;&lt;span class="tooltip-text"&gt;Tooltip on left&lt;/span&gt;&lt;/div&gt;&lt;div class="tooltip-container tooltip-bottom"&gt;&lt;button class="button"&gt;Bottom&lt;/button&gt;&lt;span class="tooltip-text"&gt;Tooltip on bottom&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Tooltip Arrows

    To create an arrow for tooltip that appears from a specific side of the tooltip, add "empty" content after tooltip, using the pseudo-element ::after and content property. The arrow then can be shaped and colored using border property.

    /* Arrow styles */.tooltip-text::after{content:"";position: absolute;border-width: 5px;border-style: solid;border-color: black transparent transparent transparent;}

    We set color only for one side of border, this will result in a triangular shaped border on top side as the content is empty. To learn more about how to create arrow in CSS, visit our free tutorial on CSS arrows.

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        .container{
            display: flex;
            justify-content: space-around;
            margin: 10%;
        }
        .tooltip-container {
            position: relative;
        }
        
        .button{
            font-family: san-serif;
            font-weight: bold;
            padding: 2px;
            border-radius: 5px;
            background-color: white;
        }
        .tooltip-container .tooltip-text {
            visibility: hidden;
            width: 120px;
            background-color: black;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px 0;
            position: absolute;
            z-index: 1;
            opacity: 0;
            transition: opacity 0.3s;
        }
        .tooltip-container:hover .tooltip-text {
            visibility: visible;
            opacity: 1;
        }
        /* Tooltip on top */
        .tooltip-top .tooltip-text {
            bottom: 125%;
            left: 50%;
            transform: translateX(-50%);
        }
        /* Tooltip on right */
        .tooltip-right .tooltip-text {
            top: 50%;
            left: 125%;
            transform: translateY(-50%);
        }
        /* Arrow styles */
        .tooltip-text::after {
            content: "";
            position: absolute;
            border-width: 5px;
            border-style: solid;
        }
        /* Arrow pointing up for top tooltip */
        .tooltip-top .tooltip-text::after {
            top: 100%;
            left: 50%;
            transform: translateX(-50%);
            border-color: black transparent transparent transparent;
        }
        /* Arrow pointing left for right tooltip */
        .tooltip-right .tooltip-text::after {
            top: 50%;
            left: -10px;
            transform: translateY(-50%);
            border-color: transparent black transparent transparent;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="tooltip-container tooltip-top"&gt;&lt;button class="button"&gt;Top &lt;/button&gt;&lt;span class="tooltip-text"&gt;Tooltip on top&lt;/span&gt;&lt;/div&gt;&lt;div class="tooltip-container tooltip-right"&gt;&lt;button class="button"&gt;Right &lt;/button&gt;&lt;span class="tooltip-text"&gt;Tooltip on right&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Fade In Tooltips

    The CSS fade in tooltip or tool tip animation is a tooltip that appears gradually with a fading effect, creating a smooth and visually appealing transition.

    To create a fade in tooltip, first you need to set opacity of tooltip element as 0, then for hovered state of tooltip element set opacity as 1. Now use transition to smoothly change opacity from 0 to 1.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style>
    
        .tooltip {
            position: relative;
            display: inline-block;
            cursor: pointer;
            margin: 10%;
        }
        .tooltip .tooltiptext {
            visibility: hidden;
            width: 120px;
            background-color: #555;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            margin-left: -60px;
            opacity: 0;
            transition: opacity 2s;
        }
        .tooltip:hover .tooltiptext {
            visibility: visible;
            opacity: 1;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body style="text-align:center;"&gt;&lt;div class="tooltip"&gt;
        Hover over this text
        &lt;span class="tooltiptext"&gt;
            This is a fade-in tooltip
        &lt;/span&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Advantages of Tooltips

    • Tooltips give extra info without making the webpage messy. They help users understand different parts of the webpage better.
    • CSS tooltips can be customized and put in different positions for different screens and devices. This makes them useful for all types of websites, even those that change sizes on different screens.
    • CSS tooltips are highly customizable, it allows developers to style them to match the website's design theme, including colors, fonts, and animations.
    • Implementing CSS tooltips is relatively simple and doesn't require complex JavaScript or additional libraries.
  •  Box Sizing

    The box-sizing property in CSS is used to define how the total width and height of an element is calculated. By default, the width and height of an element includes its content, padding, and border. However, with the box-sizing property, you can alter this behavior to include or exclude the padding and border from the width and height calculation.

    In older version of CSS (CSS2), the default way in which the box model worked made the element look bigger than the dimensions that were passed to it (width and height).

    width + padding + border = actual visible/rendered width of an element’s box

    height + padding + border = actual visible/rendered height of an element’s box

    CSS box sizing property

    The CSS box-sizing property is useful in adjusting the behaviour of the layout of the elements.

    Possible Values

    The box-sizing CSS property can have a value, that is a keyword, either of one of the following:

    • content-box: This is the default value. When this value is passed to box-sizing property, it returns the default behavior, where padding or/and border are added in the total width and height of the element.
    • border-box: When this value is passed to box-sizing property, it tells the browser to adjust the padding or margin values, that are passed to an element. This results in shrinking the content area and absorbing the border or/and padding specified for the element. It is the default styling used for <table>, <select>, <button>, and <input> elements.

    padding-box value is not supported by any browser and hence can not be passed to the box-sizing property.

    Applies to

    All the HTML elements that accept width or height.

    DOM Syntax

    object.style.boxSizing = "content-box | border-box";
    

    Example

    Here is an example of CSS property box-sizing: border-box:

    Open Compiler

    <html><head><style>
       .with-content-box {
    
      width: 200px;
      height: 100px;
      padding: 10px;
      border: 3px solid green;
      background-color: rgb(241, 234, 85);
      box-sizing: content-box;
    } .with-border-box {
      width: 200px;
      height: 100px;  
      padding: 10px;  
      border: 3px solid rgb(64, 10, 215);
      background-color: lightpink;
      box-sizing: border-box;
    } </style></head><body><div class="with-content-box">CONTENT BOX</div><br /><div class="with-border-box">BORDER BOX</div></body></html>

    The example given above shows a clear difference between the box-sizing: border-box and box-sizing: content-box property values. Here the box-sizing: border-box property ignores the padding value in calculation of total width and height. Whereas the box-sizing: content-box property value includes the padding value in the calculation.

    For a smooth, fluid and intuitive responsive design, the box-sizing: border-box value can be set in the following manner:

    * {
       box-sizing: border-box;
    }
    

    The above syntax may not give desired results, as it ignores the pseudo-elements. So there is another syntax which includes the pseudo-elements for the reset.

    *, *:before, *:after {
       box-sizing: border-box;
    }
    

    This universal box sizing method will make the use of box-sizing: content-box | padding-box difficult. Hence, there is one more syntax that may be more useful and apt in such situations.

    html {
       box-sizing: border-box;
    }
    *, *:before, *:after {
       box-sizing: inherit;
    }
    

    This universal box-sizing reset syntax is more useful and gives more flexibility to the users.

    Though every current browser supports the box-sizing: border-box, unprefixed; but some older versions of the browsers need support. In order to provide that support you may need to use the prefixes -webkit and -moz in the following manner:

    html {
       -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
       box-sizing: border-box;
    }
    *, *:before, *:after {
       -webkit-box-sizing: inherit;
       -moz-box-sizing: inherit;
       box-sizing: inherit;
    }
    

  • Multiple Column Layout

    CSS provide several properties to design multiple column layout for webpages. The multiple column layout is used generally for layout designing in newspapers, online blogs, books, etc. In this chapter we will discuss how to create and style multiple column layout using HTML and CSS.

    Table of Contents

    • CSS Create Multiple Columns
    • Setting Column Width
    • Spanning Columns in Multiple Column Layout
    • Specify Gap Between Columns
    • CSS Column Rules
    • Multiple Column Layout Related properties

    CSS Create Multiple Columns

    In CSS, we can use the column-count property to specify number of columns for displaying texts inside any element. Let us see an examples:

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style> 
    
        .multicol-col-count {
            column-count: 3;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt; Three Column Layout &lt;/h2&gt;&lt;div class="multicol-col-count"&gt;
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit, 
        sed diam nonummy nibh euismod tincidunt ut laoreet dolore 
        magna aliquam erat volutpat. Ut wisi enim ad minim veniam, 
        quis nostrud exerci tation ullamcorper suscipit lobortis 
        nisl ut aliquip ex ea commodo consequat. Duis autem vel 
        eum iriure dolor in hendrerit in vulputate velit esse mol
    &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Setting Column Width

    To make column layout, it is not necessary to use column-count property, we can also use column-width property. Number of columns will be determined automatically based on width of column specified. Let see an example.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style> 
    
        .multicol-col-width {
            column-width: 100px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;&lt;strong&gt; Column Width 100px &lt;/strong&gt;&lt;/p&gt;&lt;div class="multicol-col-width"&gt;
        Lorem ipsum dolor sit amet, con sec tetuer ad ipis cing el 
        sed diam nonummy nibh eui smod tincidunt ut laoreet dolo 
        magna aliquam erat volutpat. Ut wisi enim ad minim veni, 
        quis nostr ud exerci tation ulla mc orper suscipit lob ort 
        nisl ut aliq uip ex ea comm odo cons equat. Duis au tem 
        eum iriure dolor in hen drerit in vul put ate ve lit esse mol
        estie con se quat, vel ill
    &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Specify the Gap Between Columns

    To specify gap between columns, we can use column-gap property. Let see an example.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style> 
    
        .multicol-col-width {
            column-width: 100px;
            column-gap: 40px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;&lt;strong&gt; Column Gap 40px &lt;/strong&gt;&lt;/p&gt;&lt;div class="multicol-col-width"&gt;
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit, 
        sed diam nonummy nibh euismod tincidunt ut laoreet dolore 
        magna aliquam erat volutpat. Ut wisi enim ad minim veniam, 
        quis nostrud exerci tation ullamcorper suscipit lobortis 
        nisl ut aliquip ex ea commodo consequat. Duis autem vel 
        eum iriure dolor in hendrerit in vulputate velit esse mol
        estie consequat, vel illum dolore eu feugiat nulla facilisis 
    &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Column Rules

    In CSS multiple column layout, we can add rules (or lines) between columns using column rule properties. Following are column-rule properties in CSS:

    • column-rule-style: Defines the style of the line between columns (e.g., solid, dashed).
    • column-rule-color: Sets the color of the line between columns.
    • column-rule-width: Specifies the width (thickness) of the line between columns.
    • column-rule: A shorthand property to set the style, color, and width of the line between columns.

    Let see an example for these properties.

    Example

    Open Compiler

    <!DOCTYPE html><html><head><style> 
    
        .multicol-col-width {
            column-count: 3;
            column-rule-style: dashed;
            column-rule-color: red;
            column-rule-width: 3px;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="multicol-col-width"&gt;
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit, 
        sed diam nonummy nibh euismod tincidunt ut laoreet dolore 
        magna aliquam erat volutpat. Ut wisi enim ad minim veniam, 
        quis nostrud exerci tation ullamcorper suscipit lobortis 
        nisl ut aliquip ex ea commodo consequat. Duis autem vel 
        eum iriure dolor in hendrerit in vulputate velit esse mol
        estie consequat, vel illum dolore eu feugiat nulla facilisis 
        averunt lectores legere me lius quod ii legunt saepius.
    &lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS Spanning Columns in Multiple Column Layout

    If you want to add a heading, or any other elements that spans across all the columns in layout, You can use column-span property. The value of this property following:

    • column-span: all - The heading will span over every other column in layout.
    • column-span: none - The heading will be placed as a seperate column.

    Open Compiler

    <!DOCTYPE html><html><head><style> 
    
        .multicol-col-rule {
            column-count: 3;
            column-rule: 3px solid;
        }
        .colspan-none {
            column-span: none;
            background-color: lightskyblue;
        }
        .colspan-all{
            column-span: all;
            background-color: lightskyblue;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="multicol-col-rule"&gt;&lt;h1 class="colspan-none" &gt;Heading on First Columns&lt;/h1&gt;&lt;p&gt;
            Lorem ipsum dolor sit amet, consectetuer adipi
            scing elit, sed diam nonummy nibh euismod tincidunt ut 
            laoreet dolore magna aliquam erat volutpat. Lorem ipsum dolor 
            sit amet, conse ctetuer adip iscing elit, sed diam nonu 
            mmy nibh euis mod tincidunt ut laoreet dolore magna aliq
            am erat volutpat.
        &lt;/p&gt;&lt;/div&gt;&lt;div class="multicol-col-rule"&gt;&lt;h1 class="colspan-all" &gt;Heading spanning across all columns&lt;/h1&gt;&lt;p&gt;
            Ut wisi enim ad minim veniam, quis nostrud exerci ta
            tion ullamcorper suscipit lobortis nisl ut aliquip ex 
            ea commodo consequat. Duis autem vel eum iriure dolor 
            in hendrerit in vulputate velit esse molestie consequat, 
            vel illum dolore eu feugiat nulla facilisis at vero eros
        &lt;/p&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Following table shows all the properties that are supported in CSS for multiple column layout.

    PropertyDescriptionExample
    column-countSpecifies the number of columns an element is divided into when displayed in a multi-column layout.Try It
    column-fillSpecifies how to fill columns.Try It
    column-gapSets the size of the gap between an element's columns.Try It
    column-widthSets the column width in a multi-column layout.Try It
    column-ruleShorthand property that sets the color, style, and width of the line drawn between columns in a multi-column layout.Try It
    column-rule-colorSets the color of the line drawn between columns in a multi-column layout.Try It
    column-rule-styleSets the style of the line drawn between columns in a multi-column layout.Try It
    column-rule-widthSets the width of the line drawn between columns in a multi-column layout.Try It
    column-spanDefines whether an element should span across one column or all columns, in a multi-column layout.Try It
  • Animations

    CSS animations allows to create smooth transitions between different styles without using JavaScript. For example,

    What is CSS Animation?

    In CSS we can dynamically change styles of elements based on time duration, user interaction or state changes called CSS animations. It is implemented using the @keyframes rule to create the animation and the animation property to apply it to an element.

    Table of Contents

    • CSS @keyframes Rule
    • Animation Delay Property
    • Set Animation Iteration Count
    • Animation Direction Property
    • Animation Timing Function
    • Set Animation Fill Modes
    • Animation Shorthand Property
    • List of CSS Animation Properties

    The @keyframes Rule

    The @keyframes rule is used to define keyframes for animation specifying how the animated element look at various stage of animation. Consider following code that defines a keyframe rule.

    .box{animation: colorChange 5s infinite;}@keyframes colorChange{0%{background-color: red;}50%{background-color: green;}100%{background-color: blue;}}

    This code will define animation for element with class .box, the name of animation is colorChange, run for 5 seconds and it repeats infinite number of times.

    The keyframe rule is defined for animation named colorChange.

    • At 0% of total duration of animation( ie, 0 seconds) the background color will be red.
    • At 50% of total time( ie, 2.5 seconds) the background color changes to green.
    • At 100% of total duration( ie, 5 seconds) color changes to blue.

    Animation Delay Property

    We can set delay for starting an animation using animation-delay property. Check out following example

    You can also set negative value for animation-delay properties. If you are using a negative value -n, then the animation will start as if it had already been playing for n seconds.

    Example

    In this example ball will start to move left after 2 seconds.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        .ball {
            width: 50px;
            height: 50px;
            background-color: #3498db;
            border-radius: 50%;
            position: absolute;
            left: 0; 
            animation-name: moveRight;
            /* Set duration */
            animation-duration: 2s;
            /* Set delay for animation */
            animation-delay: 2s; 
        }
        @keyframes moveRight {
            to {
                left: calc(100% - 50px);
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="ball"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Set Animation Iteration Count

    We can set number of times a animation should repeats itself using animation-iteration-count property.

    The CSS specification does not support negative values for this property. It can take value as a positive integer (e.g., 1, 2, 3, etc.) or keyword 'infinite'

    Example

    In this example we set ball iteration count to infinite.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        .ball {
            width: 50px;
            height: 50px;
            background-color: #3498db;
            border-radius: 50%;
            position: absolute; 
            left: 0; 
            animation-name: moveRight;
            /* Set duration */
            animation-duration: 2s;
            /* Set number of time animation repeats */
            animation-iteration-count: infinite;
        }
        @keyframes moveRight {
            to {
                left: calc(100% - 50px);
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="ball"&gt;&lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Animation Direction Property

    We can specify the direction in which animation should run using animation-direction property.

    Following are the valid values for animation-direction property

    • normal: The animation is played as normal (forwards). This is default.
    • reverse: The animation is played in reverse direction (backwards).
    • alternate: The animation is played forwards first, then backwards.
    • alternate-reverse: The animation is played backwards first, then forwards.

    Example

    In this example we used inline css to set animation direction property.

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        .ball {
            width: 50px;
            height: 50px;
            background-color: #3498db;
            border-radius: 50%;
            position: relative;
            left:0;
            
            animation-name: moveRight ;
            animation-duration: 2s;
            animation-iteration-count: infinite;
        }
        @keyframes moveRight {
            to {
                left: calc(100% - 50px);
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;animation-direction: normal&lt;/h2&gt;&lt;div class="ball" 
         style="animation-direction: normal; "&gt;&lt;/div&gt;&lt;h2&gt;animation-direction: reverse&lt;/h2&gt;&lt;div class="ball" 
         style="animation-direction: reverse;"&gt;&lt;/div&gt;&lt;h2&gt;animation-direction: alternate&lt;/h2&gt;&lt;div class="ball" 
         style="animation-direction: alternate;"&gt;&lt;/div&gt;&lt;h2&gt;animation-direction: alternate-reverse&lt;/h2&gt;&lt;div class="ball" 
         style="animation-direction: alternate-reverse;"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Animation Timing Function

    In CSS, the animation-timing-function property is used to define speed curve of animation. It can take following values.

    • ease: The animation will start slow, then fast, then end slowly (The default value).
    • linear: Animation with the same speed from start to end.
    • ease-in: Animation with a slow start.
    • ease-out: Animation with a slow end.
    • ease-in-out: Animation with a slow start and end.
    • cubic-bezier(n,n,n,n): This lets us to define our own values for speed. To know more check out Cubic Bezier Function article.

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        .ball {
            width: 50px;
            height: 50px;
            background-color: #3498db;
            border-radius: 50%;
            position: relative;
            left:0;
            
            animation-name: moveRight ;
            animation-duration: 2s;
            animation-iteration-count: infinite;
        }
        @keyframes moveRight {
            to {
                left: calc(100% - 50px);
            }
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;h2&gt;linear&lt;/h2&gt;&lt;div class="ball" 
         style="animation-timing-function: linear;"&gt;&lt;/div&gt;&lt;h2&gt;ease&lt;/h2&gt;&lt;div class="ball" 
         style="animation-timing-function: ease;"&gt;&lt;/div&gt;&lt;h2&gt;ease-in&lt;/h2&gt;&lt;div class="ball" 
         style="animation-timing-function: ease-in;"&gt;&lt;/div&gt;&lt;h2&gt;ease-out&lt;/h2&gt;&lt;div class="ball" 
         style="animation-timing-function: ease-out;"&gt;&lt;/div&gt;&lt;h2&gt;ease-in-out&lt;/h2&gt;&lt;div class="ball" 
         style="animation-timing-function: ease-in-out;"&gt;&lt;/div&gt;&lt;h2&gt;cubic-bezier(0.25, 0.1, 0.25, 1)&lt;/h2&gt;&lt;div class="ball" 
         style="animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);"&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Set Animation Fill Modes

    The animation-fill-mode property specifies a style for the target element when the animation is not playing (before it starts, after it ends, or both).

    The animation-fill-mode property can have the following values:

    • none: The animation will not apply any style before or after it starts. This is default.
    • forwards: At end of animation element will keep the style set by the last keyframe rule.
    • backwards: At end of animation element will keep the style set by the first keyframe rule.
    • both: The animation will follow the rules for both forwards and backwards.

    Check out output of following code for more understanding:

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        .box {
            padding: 10px;
            background-color: green;
            color: white;
            font-size: 16px;
            margin: 20px;
            animation-duration: 2s;
            animation-name: changeColor;
        }
        /* Animation Definition */
        @keyframes changeColor {
            0% {
                background-color: blue;
            }
            100% {
                background-color: red ;
            }
        }
        /* Animation Fill Modes */
        .none {
            animation-fill-mode: none;
        }
        .forwards {
            animation-fill-mode: forwards;
        }
        .backwards {
            animation-fill-mode: backwards;
            animation-delay: 2s;
        }
        .both {
            animation-fill-mode: both;
            animation-delay: 2s;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="box none"&gt;None&lt;/div&gt;&lt;div class="box forwards"&gt;Forwards&lt;/div&gt;&lt;div class="box backwards"&gt;Backwards&lt;/div&gt;&lt;div class="box both"&gt;Both&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Animation Shorthand Property

    In CSS, the animation property is shorthand for following properties

    • animation-name: Sets name for animation.
    • animation-duration: Sets duration of animation.
    • animation-timing-function: Define speed curve of animation.
    • animation-delay: Sets delay before which animation starts.
    • animation-iteration-count: Sets number of time animation repeats.
    • animation-direction: Defines direction of execution of animation.
    • animation-fill-mode: Describes the pre-run and post-run styling.
    • animation-play-state: Describes play/pause nature of animation.

    Example

    Open Compiler

    <html lang="en"><head><style>
    
    .box {
        padding: 20px;
        background-color: #3498db;
        color: white;
        font-size: 16px;
        /* Name, duration, timing function, delay, repeat, direction, fill mode */
        animation: changeColor 2s ease-in-out 1s infinite alternate both;
    }
    /* Animation Definition */
    @keyframes changeColor {
        0% {
            background-color: #3498db;
        }
        100% {
            background-color: #e74c3c;
        }
    }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="box"&gt;Animate Me!&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    List of CSS Animation Properties

    The following are the animation property's sub-properties:

    PropertyDescriptionExample
    animation-compositionIndicates the composite operation to apply when many animations are having simultaneous effects on the same property.Try It
    animation-delayIndicates whether the animation should begin at the beginning of the animation or somewhere along the way, as well as the amount of time that should pass between an element loading and the start of an animation sequence.Try It
    animation-directionIndicates if the initial iteration of animation should be forward or backward and if iterations after that should continue in the same direction or change direction each time the sequence is executed.Try It
    animation-durationIndicates how long it takes for an animation to finish one cycle.Try It
    animation-fill-modeDescribes the pre-run and post-run styling that an animation applies to its target.Try It
    animation-iteration-countIndicates how many times an animation should recur.Try It
    animation-nameIt gives the name of the @keyframes at-rule that describes the keyframes of an animation.Try It
    animation-play-stateIndicates whether an animation sequence should be played or paused.Try It
    animation-timing-functionDescribes the acceleration curves that are used to specify the keyframe transitions in an animation.Try It
  • transition Property

    CSS transition property allows you to animate changes in an element’s style properties over a specified duration. They provide a simple and efficient way to add animations to web elements without the need for complex JavaScript code or external libraries.

    CSS transition property is a shorthand property for

    • transition-property
    • transition-duration
    • transition-timing-function
    • transition-delay
    • transition-behavior (This property is on an experimental basis and may not be supported).

    Possible Values

    • <length> − A specific length value such as pixels (px), centimeters (cm), inches (in), etc.

    Applies to

    All elements, ::before and ::after pseudo-elements.

    Syntax

    transition: margin-right 4s;
    transition: margin-right 4s 1s;
    transition: margin-right 4s ease-in-out;
    transition: margin-right 4s ease-in-out 1s;
    transition: display 4s allow-discrete;
    transition: margin-right 4s, color 1s;
    

    The value for the transition property is defined as one of the following:

    • The none value indicates that there will be no transitions on this element. This is the default value.
    • Commas are used to separate one or more single-property transitions.

    A single-property transition specifies the transition for one specific property or all properties. This includes:

    • A zero or one value indicating the property or properties for which the transition should be applied. This can be specified as:
      • <custom-ident> representing a single property.
      • The all value in transitions indicates that the transition will be applied to all attributes that change when the element changes its state
      • If no value is specified, all value will be assumed, and the transition will apply to all changing properties.
    • Specify zero or one <easing-function> value indicating the easing function to be used.
    • Specify zero, one, or two <time> values for transition properties. The first parsed-time value is applied to transition-duration, and the second is assigned to transition-delay.
    • If a property has discrete animation behaviour, a zero or one value indicates whether to initiate transitions. If the value is present, it can be either allow-discrete or normal keyword.

    CSS transition – With Two Values

    The following example demonstrates that transition is applied to the padding property with a duration of 2s. When you hover over the box, padding increases to 15px and the background color changes to greenyellow −

    Open Compiler

    <html><head><style>
       .box {
    
      font-size: 14px;
      width: 100px;
      padding: 5px;
      transition: padding 2s;
      background-color: lightskyblue;
    } .box:hover {
      background-color: greenyellow;
      padding: 15px;
    } </style></head><body><div class="box">Hover over me</div></body></html>

    CSS transition – delay Value

    The following example demonstrates that transition is applied to the padding property. The transition takes 2s to complete, and it starts after a delay of 0.5s −

    Open Compiler

    <html><head><style>
       .box {
    
      font-size: 14px;
      width: 100px;
      padding: 5px;
      transition: padding 2s .5s;
      background-color: lightskyblue;
    } .box:hover {
      background-color: greenyellow;
      padding: 15px;
    } </style></head><body><div class="box">Hover over me</div></body></html>

    CSS transition – easing Function

    The following example demonstrates that transition is applied to the background-color property. When you hover over the box, background color changes to green-yellow, starting a smooth color transition with an ease-in-out timing function over the 4s duration −

    Open Compiler

    <html><head><style>
       .box {
    
      font-size: 14px;
      width: 100px;
      padding: 15px;
      transition: background-color 4s ease-in-out;
      background-color: lightskyblue;
    } .box:hover {
      background-color: greenyellow;
    } </style></head><body><div class="box">Hover over me</div></body></html>

    CSS transition – easing Function and delay

    The following example demonstrates that transition is applied to the padding property. When you hover over the box, background color changes to green-yellow and padding increases to 15px, starting a smooth transition over the duration of 4s with an ease-in-out timing function and a delay of 0.7s −

    Open Compiler

    <html><head><style>
       .box {
    
      font-size: 14px;
      width: 100px;
      padding: 5px;
      transition: padding 4s ease-in-out 0.7s;
      background-color: lightskyblue;
    } .box:hover {
      background-color: greenyellow;
      padding: 15px;
    } </style></head><body><div class="box">Hover over me</div></body></html>

    CSS transition – behavior Value

    The following example demonstrates that transition is applied to the background-color property. When you hover over the box, background color changes to green-yellow, starting a smooth transition over the duration of 5s using the allow-discrete timing function −

    Open Compiler

    <html><head><style>
       .box {
    
      font-size: 14px;
      width: 100px;
      padding: 10px;
      transition: background-color 5s allow-discrete;
      background-color: lightskyblue;
    } .box:hover {
      background-color: greenyellow;
    } </style></head><body><div class="box">Hover over me</div></body></html>

    CSS transition – Applied To Two Properties

    The following example demonstrates that transition will be applied on text color in 2s and on margin-left in 4s. The text color will transition in 2s, while the left margin will take 4s −

    Open Compiler

    <html><head><style>
       .box {
    
      font-size: 14px;
      width: 100px;
      padding: 10px;
      transition: color 2s, margin-left 4s;
      background-color: lightskyblue;
    } .box:hover {
      color: red;
      margin-left: 70px;
    } </style></head><body><div class="box">Hover over me</div></body></html>

  • 3D Transforms

    CSS transform are used to animate elements in three-dimensional space by using properties like translate, scale and rotate. In other words, these functions let you rotate, scale, and move elements along the X, Y, and Z axes, adding depth and perspective to your designs.

    2D Transform

    3D Transform

    Table of Contents

    • CSS 3D Translate()
    • CSS 3D Rotate()
    • CSS 3D Scale()
    • CSS 3D Transform Related Properties

    CSS 3D Translate()

    CSS translate3d() function moves an element in 3D space by specifying offsets along the X, Y, and Z axes, where the Z-axis controls depth (distance towards or away from the viewer). The following example shows a box that moves in 3D space when hovered over. The perspective property is used to give a sense of depth for 3d effect.

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        body {
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
        }
        /* Container with Perspective */
        .container {
            perspective: 800px; 
        }
        /* The Box Element */
        .box {
            width: 200px;
            height: 200px;
            background-color: #4CAF50;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 1.5em;
            border-radius: 10px;
            /* Initial 3D Transformation */
            transform: translate3d(50px, 50px, 100px) 
                        rotateX(15deg) rotateY(25deg);
            transition: transform 0.6s ease;
        }
        /* Hover State with Different 3D Transformation */
        .box:hover {
            transform: translate3d(-50px, -50px, -100px);
            background-color: #2ecc71;
            cursor: pointer;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="box"&gt;
            3D Box
        &lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS 3D Rotate()

    CSS rotate3d() function allows you to rotate an element around a specified axis in 3D space by defining the X, Y, and Z components of the rotation vector and the angle of rotation. Here is an example showing a box that rotates in 3D space when we hover over it, creating a dynamic visual effect.

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        body {
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
        }
        /* Container with Perspective */
        .container {
            perspective: 800px; 
        }
        /* The Box Element */
        .box {
            width: 200px;
            height: 200px;
            background-color: #4CAF50;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 1.5em;
            border-radius: 10px;
            /* Initial 3D Rotation */
            transform: rotate3d(1, 1, 1, 45deg);
            transition: transform 0.6s ease;
        }
        /* Hover State with Different 3D Rotation */
        .box:hover {
            transform: rotate3d(1, 1, 0, -45deg);
            background-color: #2ecc71;
            cursor: pointer;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="box"&gt;
            3D Box
        &lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    CSS 3D Scale()

    CSS scale3d() function scales an element in 3D space by specifying scaling factors along the X, Y, and Z axes, allowing for uniform or non-uniform scaling. The following example shows a box that scales in 3D space when hovered over, creating a visually appealing zoom effect.

    Example

    Open Compiler

    <!DOCTYPE html><html lang="en"><head><style>
    
        body {
            height: 300px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f0f0f0;
            font-family: Arial, sans-serif;
        }
        /* Container with Perspective */
        .container {
            perspective: 800px; 
        }
        /* The Box Element */
        .box {
            width: 150px;
            height: 150px;
            background-color: #4CAF50;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 1.5em;
            border-radius: 10px;
            /* Initial 3D Scaling */
            transform: scale3d(1, 1, 1) rotate3d(1, 1, 0, -45deg);
            transition: transform 0.6s ease;
        }
        /* Hover State with Different 3D Scaling */
        .box:hover {
            transform:  scale3d(1.5, 1.5, 0.5);
            background-color: #2ecc71;
            cursor: pointer;
        }
    &lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="container"&gt;&lt;div class="box"&gt;
            3D Box
        &lt;/div&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>