For working professionals
For fresh graduates
More
1. Introduction
6. PyTorch
9. AI Tutorial
10. Airflow Tutorial
11. Android Studio
12. Android Tutorial
13. Animation CSS
16. Apex Tutorial
17. App Tutorial
18. Appium Tutorial
21. Armstrong Number
22. ASP Full Form
23. AutoCAD Tutorial
27. Belady's Anomaly
30. Bipartite Graph
35. Button CSS
39. Cobol Tutorial
46. CSS Border
47. CSS Colors
48. CSS Flexbox
49. CSS Float
51. CSS Full Form
52. CSS Gradient
53. CSS Margin
54. CSS nth Child
55. CSS Syntax
56. CSS Tables
57. CSS Tricks
58. CSS Variables
61. Dart Tutorial
63. DCL
65. DES Algorithm
83. Dot Net Tutorial
86. ES6 Tutorial
91. Flutter Basics
92. Flutter Tutorial
95. Golang Tutorial
96. Graphql Tutorial
100. Hive Tutorial
103. Install Bootstrap
107. Install SASS
109. IPv 4 address
110. JCL Programming
111. JQ Tutorial
112. JSON Tutorial
113. JSP Tutorial
114. Junit Tutorial
115. Kadanes Algorithm
116. Kafka Tutorial
117. Knapsack Problem
118. Kth Smallest Element
119. Laravel Tutorial
122. Linear Gradient CSS
129. Memory Hierarchy
133. Mockito tutorial
134. Modem vs Router
135. Mulesoft Tutorial
136. Network Devices
138. Next JS Tutorial
139. Nginx Tutorial
141. Octal to Decimal
142. OLAP Operations
143. Opacity CSS
144. OSI Model
145. CSS Overflow
146. Padding in CSS
148. Perl scripting
149. Phases of Compiler
150. Placeholder CSS
153. Powershell Tutorial
158. Pyspark Tutorial
161. Quality of Service
162. R Language Tutorial
164. RabbitMQ Tutorial
165. Redis Tutorial
166. Redux in React
167. Regex Tutorial
170. Routing Protocols
171. Ruby On Rails
172. Ruby tutorial
173. Scala Tutorial
175. Shadow CSS
178. Snowflake Tutorial
179. Socket Programming
180. Solidity Tutorial
181. SonarQube in Java
182. Spark Tutorial
189. TCP 3 Way Handshake
190. TensorFlow Tutorial
191. Threaded Binary Tree
196. Types of Queue
197. TypeScript Tutorial
198. UDP Protocol
202. Verilog Tutorial
204. Void Pointer
205. Vue JS Tutorial
206. Weak Entity Set
207. What is Bandwidth?
208. What is Big Data
209. Checksum
211. What is Ethernet
214. What is ROM?
216. WPF Tutorial
217. Wireshark Tutorial
218. XML Tutorial
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.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.