Tutorial Playlist
In this tutorial, we delve deep into one of the critical components of CSS - padding. We will examine its roles, its significance in creating effective layouts, and the best practices to harness this in your web design practices skillfully.
In the realm of web design, padding in CSS holds an instrumental role in structuring and fine-tuning the layout. It defines the spacing around an element's content, directly influencing the positioning and appearance of elements on a web page. Gaining a thorough understanding of padding and how it interacts with other CSS properties is an essential stepping stone in mastering web design.
Padding in CSS refers to the space that surrounds the content inside an element. This space does not include the border or margin but strictly lies between the content and the border, impacting the overall element dimensions.
Padding's primary use in CSS is to create space around an element's content, enhancing the visual separation between the content and its border. This extra space effectively breaks the monotony, making the design more readable and visually pleasing, thus improving the overall user experience.
The padding of an element is the space between the content and its border. This property can contain from one to four values:
Property | Description |
padding-left | Used to set the left padding of a specific element. |
padding-right | Used to set the right padding of a specific element. |
padding-top | Used to set the top padding of a specific element. |
padding-bottom | Used to set the bottom padding of a specific element. |
Here is an example:
Code:
<!--top padding is 10px
right padding is 5px
bottom padding is 15px
left padding is 20px-->
<!DOCTYPE html>
<html>
<head>
<style>
p.upGradpadding {
 border: 1px solid red;Â
 padding:10px 5px 15px 20px;Â
}
</style>
</head>
<body>
<h1>The padding-top,The padding-right,The padding-bottom,The padding-left Property</h1>
<p class="upGradpadding">A paragraph with a 10px top,5px right,15px bottom and 20px left padding</p>
</body>
</html>
Explanation:
The CSS style defines a class selector p.upGradpadding, which targets <p> (paragraph) elements with the class name upGradpadding. Inside the p.upGradpadding CSS block, the following properties are defined:
The paragraph element with the class upGradpadding is defined as follows: <p class="upGradpadding">A paragraph with a 10px top, 5px right, 15px bottom, and 20px left padding</p>
Visually, the paragraph will be displayed with padding as follows:
As a result, the content of the paragraph will be pushed away from each side of the paragraph by the specified padding values, and a red border will surround the entire paragraph.
Note: Negative values are not allowed.
Code:
<!DOCTYPE html>
<html>
<head>
<style>
div {
 border: 1px solid black;
 padding: 25px 20% 3em 30px;
 background-color: lightblue;
}
</style>
</head>
<body>
<h2>The padding property - 4 values</h2>
<div>This div element has a top padding of 25px, a right padding of 20%, a bottom padding of 3em,and a left padding of 30px.</div>
</body>
</html>
Explanation:
The CSS style applies to all <div> elements on the page. Inside the <style> block, the following properties are defined for the <div> element:
The content of the <div> element is "This div element has a top padding of 25px, a right padding of 20%, a bottom padding of 3em, and a left padding of 30px."
Visually, the <div> element will be displayed as follows:
Additionally, the <div> element will have a light blue background color due to the background-color: lightblue; property.
Code:
<!DOCTYPE html>
<html>
<head>
<style>
div {
 border: 1px solid black;
 background-color: pink;
 padding-top: 50px;
 padding-right: 100px;
 padding-bottom: 35px;
 padding-left: 80px;
}
</style>
</head>
<body>
<h2>Using individual padding properties</h2>
<div>This div element has a top padding of 50px, a right padding of 100px,
a bottom padding of 35px,and a left padding of 80px.</div>
</body>
</html>
Explanation:
The CSS style applies to all <div> elements on the page.
Inside the <style> block, the following properties are defined for the <div> element:
The content of the <div> element is "This div element has a top padding of 50px, a right padding of 100px, a bottom padding of 35px, and a left padding of 80px."
Visually, the <div> element will be displayed as follows:
Additionally, the <div> element will have a pink background color due to the background-color: pink; property. The black border, as specified by border: 1px solid black;, will surround the entire <div> element, making the padding visually distinguishable from the background.
Code:
<!DOCTYPE html>
<html>
<head>
<style>
div.ex1 {
 width: 300px;
 background-color: green;
}
div.ex2 {
 width: 300px;
 padding: 25px;
 box-sizing: border-box;
 background-color: yellow;
}
</style>
</head>
<body>
<h2>Padding and element width - with box-sizing in upGradTutorial</h2>
<div class="ex1">This div is 300px wide.</div>
<br>
<div class="ex2">The width of this div remains at 300px, in spite of the 50px of total left and right padding, because of the box-sizing: border-box property.
</div>
</body>
</html>
Employing CSS padding offers numerous advantages.
The ability to understand and apply CSS padding is a fundamental skill for every web designer or developer. It provides you with significant control over the layout and appearance of a webpage, thus directly contributing to an enhanced user experience and readability. By mastering CSS padding, you can ensure that your designs are not just visually appealing but also highly responsive, catering to a broad spectrum of screen sizes.
Remember, creating a well-designed webpage is about striking a balance between its content, layout, and user experience, and CSS padding plays an instrumental role in achieving this equilibrium. To learn more about padding in CSS and other aspects of CSS, check out certifiable and industry-relevant courses from upGrad.
Padding refers to the space between an element's content and its border, while margin in CSS is the space outside the border. Both can impact the overall layout and design of a webpage.
CSS padding shorthand allows you to set padding for all four sides of an element in a single line of code. The order is top, right, bottom, and left.
Yes, padding can be applied to HTML elements using CSS to control the spacing around the content of these elements.
padding-top applies padding to the top side of an element, while html padding-left applies padding to the left side. They help in controlling the spacing around the content in different directions.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...