html

How Can I Add Background Color Only for Padding?

The background color that is applied to the padding section creates a visual separation between the content and the surrounding elements. It helps to create a more flexible design and increases readability by creating a contrast between the neighboring elements and content. This blog demonstrates different methods to add background color which is only for padding.

How Can I Add a Background Color Only for Padding?

As there is not any CSS property that applies background color only in the padding area. It becomes a very tricky task, especially while maintaining the element box model as well. Fortunately, this effect can be created using some CSS properties.

Follow the below guide to get an explanation of these:

Method 1: Using “background-clip” Property

The “background-clip” property helps to apply style only to a specific part of the elements box model. This property restricts the styles to apply only on specific portions such as “padding”. Let practice an example:

Example

In the HTML file, create a div and assign it a class of “onlyPadding”. Then, add the properties which are described in the code snippet:

<style>
.onlyPadding {
 padding: 20px;
 margin: 5%;
 background-color: seagreen;
 background-clip: padding-box;
 border:5px dashed dimgray;
}
</style>

<div class="onlyPadding" >
 <h2>Welcome to Linuxhint</h2>
</div>

 

The explanation of the code is given below:

  • First, set the “padding” and “margin” properties to 20px and “5%”, respectively.
  • Next, the “background-color” property sets the background of the div element to a “seagreen” colour.
  • After that, the “background-clip” property is set to “padding-box” which allows expanding the background to the padding area only.
  • In the end, the “border” property is set to a 5px weight dashed line for better visualization.

After executing the code snippet, the webpage looks like this:

The output displays that the background color only displays in the padding area leaving the border and margin area transparent.

Method 2: Using Pseudo Elements

With the help of pseudo-element, the code becomes much cleaner, more flexible, and more efficient. The good thing about “Pseudo Elements” is their cross-browser compatibility which makes them a better option for adding background color only for padding.

Example

First, select the HTML element class on which the background color styles are going to apply. Then add the following code:

.onlyPadding
{
 padding: 20px;
 color: white;
 margin:5%;
 position: relative;
 z-index: 0;
 background-color: red;
}

 

The explanation of the above-stated properties:

  • First, utilize the “padding”, “color” and “margin” properties to make the content of the div prominent and visible.
  • After that, set the “position” property to a relative which ensures that the pseudo-element is positioned relative to the parent element.

Next, add the “::after” pseudo-element to the “<div>” HTML element for the creation of a new child. After that, assign it a margin from the top, left, right and bottom sides that set its “content” property:

.onlyPadding::after
{
 content: " ";
 position: absolute;
 top: 20px;
 left: 20px;
 right: 20px;
 bottom: 20px;
 background-color: blue;
 z-index: -1;
}

 

After executing the above code, the webpage looks like this:

The output displays that the padding background color is changed only.

Method 3: Using Parent Element

The user can perform the same function using the “parent element” as a wrapper for the child div. Follow the below example for a better understanding:

Example

Wrap the child “<div>” element with parent div and assign it a class of “parent”:

<div class="parent">
 <div class="onlyPadding">This is some text.</div>
</div>

 

Next, assign the child HTML “<div>” element a background colour of “blueviolet”:

.onlyPadding {
 background-color: blueviolet;
}

 

After that, add “padding” and “background-color” properties to the parent class. This background colour is the padding of the child “<div>” element:

.parent {
 margin: 5%;
 background-color: seagreen;
 padding: 20px;
}

 

After executing the above code snippet, the webpage looks like this:

The output illustrates that the padding of the “<div>” element has a distinct colour.

Conclusion

To apply background color only in the padding area, use “background-clip”, “pseudo-element” and “parent class”. Set the “padding-box” value to the “background-clip” property to apply style only in the padding section. The “pseudo-elements” of “:before” and “:after” selectors can be utilized with the help of the “z-index” property. The user can perform the same function using the “parent element” as a wrapper for the child div.

About the author

Abdul Moeed

I'm a versatile technical author who thrives on adaptive problem-solving. I have a talent for breaking down complex concepts into understandable terms and enjoy sharing my knowledge and experience with readers of all levels. I'm always eager to help others expand their understanding of technology.