Category: Advanced Features

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

    <!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

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

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

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

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

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

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

    <!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

    <!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

    <!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

    <!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

    <!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>
  • 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

    <!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

    <!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

    <!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

    <!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:

    <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

    <!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

    <!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

    <!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

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

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

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

    <!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

    <!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

    <!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

    <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 −

    <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 −

    <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 −

    <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 −

    <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 −

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

    CSS transition – Related Properties

    Following is the list of CSS properties related to transition:

    propertyvalue
    transition-delayDetermines the amount of time to wait before starting a transition effect when a property’s value changes.
    transition-durationDetermines amount of time that a transition animation should take to complete.
    transition-propertySpecifies which CSS properties should have a transition effect applied.
    transition-timing-functionDetermines which intermediate values are generated for CSS properties affected by a transition effect.

  • Styling Text

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

    How to Style Text in CSS?

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

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

    Properties for Styling Text

    The following table lists CSS properties for styling text:

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

    Setting Text Color in CSS

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

    Example

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

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

    Setting Text Alignment

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

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

    Example

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

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

    Aligning the Last Line in CSS

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

    Example

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

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

    Aligning Text with text-justify

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

    Example

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

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

    Setting Text Direction in CSS

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

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

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

    Example

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

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

    Applying Text Decoration in CSS

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

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

    Syntax

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

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

    Example

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

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

    Using CSS text-transform for Text Styling

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

    Example

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

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

    Adding Text Emphasis in CSS

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

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

    Example

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

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

    CSS Text Indentation Property

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

    Example

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

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

    Letter Spacing in CSS

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

    Example

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

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

    Applying word-spacing in CSS

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

    Example

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

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

    Wrapping Text with white-space Property

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

    Example

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

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

    CSS Line-Break Property

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

    Example

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

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

    CSS Word-Break Property

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

    Example

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

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

    CSS line-height Property

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

    Example

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

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

    Creating Text Shadows with CSS

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

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

    Example

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

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

    CSS text-orientation Property

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

    Example

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

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

  • text shadow Property

    The text-shadow property is used to add a shadow effect to text. It allows you to specify the color, offset, blur-radius, and spread-radius of the shadow.

    Possible Values

    • <color>:
      • Sets the color of the shadow.
      • It is optional.
      • It can be specified either before or after the offset values.
      • Any value for color can be specified, such as, name, HEX or RGB value.
    • <offset-x><offset-y>:
      • Any length value, specifying the x and y values.
      • x value represents the shadow’s horizontal distance from text.
      • y value represents the shadow’s vertical distance from text.
      • If x and y values equal 0, the shadow appears behind the text.
    • <blur-radius>
      • Any length value, specifying the value of blur-radius.
      • It is optional.
      • To make the blur look bigger, you need to provide higher value.
      • If no value is passed, it is taken as 0.

    Applies to

    All the HTML elements.

    DOM Syntax

    object.style.textShadow = "5px 5px 3px red";
    
    • The first two (5px,5px) values specify the length of the shadow offset i.e the X-coordinate and the Y-coordinate.
    • The third value (3px) specifies the blur radius.
    • The last value (red) describes the color of the shadow.

    CSS text-shadow – Simple Shadow Effects

    Following is the example which demonstrates how to set the shadow around a text. This may not be supported by all the browsers −

    <html><head></head><body><h2>Text Shadow</h2><p style="text-shadow: 5px 5px 3px yellow;">Text shadow</p><p style="text-shadow: 10px 5px #00ffff;">Text shadow</p><p style="text-shadow: blue 0px 0px 2px">Text shadow</p><p style="text-shadow: rgb(26, 69, 105) 0px 0px 10px">Text shadow</p></body></html>
  • caret color Property

    CSS caret-color property specifies the color of the cursor, which is the visible marker, also known as the text input cursor. It is used with input elements such as input forms, text boxes, textarea etc. which use the cursor and are editable.

    Syntax

    caret-color: auto | color | transparent | initial | inherit;

    Property Values

    ValueDescription
    autoThe browser uses the currentColor for the caret. Default.
    colorThe color of the caret can be specified in different formats (color names, hex, rgb etc.).
    transparentThe caret is not visible.
    initialIt sets the property to its default value.
    inheritIt inherits the property from the parent element.

    Examples of CSS Caret Color Property

    The following examples explain the caret-color property with different values.

    Caret Color Property with Auto Value

    To let the browser decide the color of the cursor, which uses the current text color, we use the auto value. The current text color will be applied to the cursor. This is shown in the following example.

    Example

    <!DOCTYPE html><html><head><style>
    
      div {
         display: grid;
         gap: 20px;
         width: 60%;
      }
      .inp {
         border: 2px solid black;
         font-size: 16px;
         padding: 5px;
         caret-color: auto;
      }
      .text {
         color: blue;
      }
      .textarea {
         color: red;
      }
    </style></head><body><h2>
      CSS caret-color property
    </h2><div><label>
         caret-color: auto (color of the text will
         be applied to caret)
      &lt;/label&gt;&lt;input type="text" value="Default cursor color."
      class=" inp text" 
      /&gt;&lt;textarea rows="10" 
      class=" inp textarea"&gt;Default caret color.
      &lt;/textarea&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Caret Color Property with Color Values

    To give a color of our choice to the cursor, we can specify the color in different format (color names, hex values, rgb values, hsl values etc.). The specified color will be applied to the cursor. This is shown in the following example.

    Example

    <!DOCTYPE html><html><head><style>
    
      div {
         display: grid;
         gap: 20px;
         width: 60%;
      }
      .inp {
         border: 2px solid black;
         font-size: 20px;
         padding: 5px;
      }
      .text1 {
         caret-color: orange;
      }
      .text2 {
         caret-color: #ff4d94;
      }
      .text3 {
         caret-color: rgb(0, 204, 204);
      }
      .textarea {
         caret-color: hsl(120, 100%, 50%);
      }
    </style></head><body><h2>
      CSS caret-color property
    </h2><div><label>
         caret-color: color values (specified color will
         be applied to caret.)
      &lt;/label&gt;&lt;input type="text" value="Green caret color."
      class=" inp text1" 
      /&gt;&lt;input type="text" value="Pink cursor color."
      class=" inp text2" 
      /&gt;&lt;input type="text" value="Blue cursor color."
      class=" inp text3" 
      /&gt;&lt;textarea rows="10" 
      class=" inp textarea"&gt;green cursor color.
      &lt;/textarea&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

    Caret Color Property with Transparent Value

    To make the cursor transparent such that it is not visible, we use the transparent value. This is shown in the following example.

    Example

    <!DOCTYPE html><html><head><style>
    
      div {
         display: grid;
         gap: 20px;
         width: 60%;
      }
      .inp {
         border: 2px solid black;
         font-size: 16px;
         padding: 5px;
         caret-color: transparent;
      }
    </style></head><body><h2>
      CSS caret-color property
    </h2><div><label>
         caret-color: transparent (cursor color 
         will not be visible)
      &lt;/label&gt;&lt;input type="text" value="transparent caret."
      class="inp" 
      /&gt;&lt;textarea rows="10" 
      class=" inp"&gt;transparent caret. 
      &lt;/textarea&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>
  • box decoration break Property

    CSS box-decoration-break property specifies how the background, border, border-image, box-shadow, margin, padding and clip-path of an element should behave when the content is broken across multiple lines or columns. It controls whether these properties should be continuous or fragmented across the line breaks.

    Syntax

    box-decoration-break: slice | clone | initial | inherit;

    Property Values

    ValueDescription
    sliceIt means that the padding, border, and background of the element will be rendered as if the content were not broken, resulting in continuous rendering across line breaks. Default Value
    cloneEach box fragment is rendered individually with its defined border, padding, and margin wrapping it. The border-radius and box-shadow are applied to each fragment separately.
    initialIt sets the property to its default value.
    inheritIt inherits the property from the parent element.

    Examples of CSS Box Decoration Break Property

    The following examples explain the box-decoration-break property with different values.

    Box Decoration Break Property with Slice Value

    To let the box decoration properties be rendered continuously such that they break at the edges of the element fragment, we use the slice value. This is shown in the following example.

    Example

    <!DOCTYPE html><html><head><style>
    
      span {
         -webkit-box-decoration-break: slice;
         box-decoration-break: slice;
      }
      .box {
         background-color: lightblue;
         border: 5px solid green;
         padding: 10px;
         border-radius: 15px;
         line-height: 3;
      }
    </style></head><body><h2>
      CSS box-decoration-break property
    </h2><span class="box">
         CSS
      &lt;br&gt;
         box-decoration-break property 
      &lt;br&gt; 
         with slice
      &lt;br&gt;
         Value. 
      &lt;br&gt;
         See how the 
      &lt;br&gt; 
         properties are applied 
      &lt;br&gt;
         to the elements.
    </span></body></html>

    Box Decoration Break Property with Clone Value

    To let each individual element fragment have individual box decoration properties separately, we use the clone value. The value treats each element broken across multiple lines as individual element and applies the properties to each of them. This is shown in the following example.

    Example

    <!DOCTYPE html><html><head><style>
    
      span {
         -webkit-box-decoration-break: clone;
         box-decoration-break: clone;
      }
      .box {
         background-color: lightblue;
         border: 5px solid green;
         padding: 10px;
         border-radius: 15px;
         line-height: 3;
      }
    </style></head><body><h2>
      CSS box-decoration-break property
    </h2><span class="box">
      CSS
    <br>
      box-decoration-break property 
    <br>
      with clone
    <br>
      Value. 
    <br>
      See how the 
    <br>
      properties are applied 
    <br>
      to the elements.
    </span></body></html>