Home > Web Front-end > CSS Tutorial > Which property in CSS specifies the right padding of an element?

Which property in CSS specifies the right padding of an element?

PHPz
Release: 2023-09-12 19:13:10
forward
705 people have browsed it

CSS 中哪个属性指定元素的右填充?

In CSS, the padding property allows us to add extra space between the border of an HTML element and its content. Right padding means only adding space between the element content and the right border.

Here we will learn two different properties to specify the right padding of an element.

Use padding-right CSS property

The ‘padding-right’ property specifies the right padding of an element in CSS. Whenever we specify right padding for an element, the width of that element is equal to the actual width of the right padding.

grammar

Users can specify the right padding of elements according to the following syntax.

padding-right: 100px;
Copy after login

Example 1

In the example below, the parent div contains multiple child divs. Using the "padding-right" CSS property, we specify a right padding of "300px" for the parent div. Additionally, we specify a padding-right of 100px for all child div elements.

In the output, the user can observe that there is 300px space between the right border and the child div. Additionally, there is 100px of free space between the right border and the text content.

<html>
<style>
   .parent {
      width: 300px;
      height: 400px;
      border: 2px solid red;
      padding-right: 300px;
      display: flex;
      flex-wrap: wrap;
   }
   .child {
      width: 200px;
      height: 100px;
      border: 2px solid green;
      padding-right: 100px;
   }
</style>
<body>
   <h3>Using the <i> padding-right CSS property </i> to add the padding at right in the HTML element.</h3>
   <div class = "parent">
      <div class = "child"> This is a child div. </div>
      <div class = "child"> This is a child div. </div>
      <div class = "child"> This is a child div. </div>
   </div>
</body>
</html>
Copy after login

Example 2

In the example below, we create a card component and add an image to it. Additionally, we've given the card div a right padding of 10px. In the output, the user can observe a 10px space on the right.

<html>
<style>
   .card {
      width: 520px;
      height: 100px;
      background-color: grey;
      padding-right: 10px;
   }
</style>
<body>
   <h3>Using the <i> padding-right CSS property </i> to add the padding at right in the HTML element.</h3>
   <div class = "card">
      <img src = "https://www.tutorialspoint.com/images/logo.png" alt = "">
   </div>
</body>
</html>
Copy after login

Using CSS Fill Properties

The padding property is used to set the padding on all four sides of an element. We can set the right padding to a value and the other sides to 0. The first value represents the top, the second represents the right, the third represents the bottom, and the fourth represents the left. So we're going to keep 0 as the value except for the second value.

grammar

Users can use the padding CSS property to specify the right padding of an element according to the following syntax.

padding: 0 value 0 0;
Copy after login

Example 3

In the example below, we have added a card div and added some text inside the container div. Additionally, we are giving "5rem" padding on the right side of the container div element. The user can observe the 5rem spacing between the right border of the container div and its content.

<html>
<style>
   .container {
      width: 10rem;
      height: 10rem;
      background-color: #f08a8a;
      padding: 0 5rem 0 0;
   }
</style>
<body>
   <h3>Using the <i> padding CSS property </i> to add the padding at right in the HTML element.</h3>
   <div class = "container">
      <div class = "card">
         <h3>This is a card inside the container div. The right padding is 2rem.</h3>
      </div>
   </div>
</body>
</html>
Copy after login

Users learn to specify correct padding for HTML elements, and they can use the "padding-right" or "padding" CSS properties. If we use the padding attribute, we only need to specify the second value and leave the other values ​​at 0.

The above is the detailed content of Which property in CSS specifies the right padding of an element?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template