CSS padding-left Property

CSS padding-left property sets the left side padding to HTML Element(s).

The syntax to specify a value for padding-left property is

padding-left: value;

The following table gives the possible values that could be given to padding-left property.

ValueDescriptionExamples
lengthAny valid CSS length unit. Refer CSS Length Units tutorial.padding-left: 10px;
padding-left: 5pt;
padding-left: 2cm;
Percentage ValuePadding in percent of the width of the containing element.padding-left: 20%;
initialSets padding-left to default value.padding-left: initial;
inheritInherits padding-left value from its parent element.padding-left: inherit;

Examples

In the following examples, we set left side padding to HTML Element(s) with a few allowed values for padding-left property.

padding-left property with CSS Length Units

In the following example, we apply padding-left property to paragraphs with a few different CSS Length Unit values.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        p {
            border: 1px solid black;
            margin: 10px;
            width: 80px;
        }
        #p1 {
            padding-left: 20px;
        }
        #p2 {
            padding-left: 2cm;
        }
        #p3 {
            padding-left: 1in;
        }
        #p4 {
            padding-left: 4ex;
        }
        #p5 {
            padding-left: 4rem;
        }
    </style>
</head>
<body>
    <p id="p1">Hello World</p>
    <p id="p2">Hello World</p>
    <p id="p3">Hello World</p>
    <p id="p4">Hello World</p>
    <p id="p5">Hello World</p>
</body>
</html>

Note: border and display properties have been specified only to understand the bounds of the HTML Elements and thus understand the extent of padding applied for these HTML Elements.

padding-left property with Percentage Values

In the following example, we apply padding-left property to paragraphs with a few different percentage values.

If padding-left is specified in percentage values, and if width of the HTML element changes, then the padding also changes. For example, try to resize the browser window, and observe the left side padding for these HTML Elements.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        p {
            border: 1px solid black;
            margin: 10px;
            width: 80px;
        }
        #p1 {
            padding-left: 5%;
        }
        #p2 {
            padding-left: 10%;
        }
        #p3 {
            padding-left: 15%;
        }
        #p4 {
            padding-left: 20%;
        }
        #p5 {
            padding-left: 25%;
        }
    </style>
</head>
<body>
    <p id="p1">Hello World</p>
    <p id="p2">Hello World</p>
    <p id="p3">Hello World</p>
    <p id="p4">Hello World</p>
    <p id="p5">Hello World</p>
</body>
</html>

padding-left property for a few HTML elements

In the following example, we take a few HTML elements like h1, p, a, span, and pre, and set padding-left for these HTML elements.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        h1, p, a, pre {
            display: block;
            border: 1px solid;
            display: inline-block;
        }
        h1 {
            padding-left: 50px;
        }
        p {
            padding-left: 50px;
        }
        a {
            padding-left: 50px;
        }
        span {
            padding-left: 50px;
            background-color: #EEE;
        }
        pre {
            padding-left: 50px;
        }
    </style>
</head>
<body>
    <h1>Heading 1</h1>
    <br>
    <p>A paragraph.</p>
    <br>
    <a href="#">Anchor Text</a>
    <br>
    <p>A paragraph with a <span>span text</span>.</p>
    <br>
    <pre>pre
hello
world</pre>
</body>
</html>

Conclusion

In this CSS Tutorial, we learned about padding-left property, and how to use this property for HTML Elements, with examples.