Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the function of the padding attribute in CSS

Shulou Source: shulou.com Published: 2022-05-31 13:04:49 09月11日 Update

Most people do not understand the knowledge points of this article "the role of padding attributes in CSS", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the role of padding attributes in CSS" article.

CSS padding attribute

The CSS padding attribute defines the inner margin of the element. The padding property accepts a length or percentage value, but negative values are not allowed.

For example, if you want all H2 elements to have a 10-pixel inner margin on each side, just do this:

H2 {padding:10px;} you can also set the inner margins of each side in the order of top, right, bottom, and left, each side can use different units or percentage values:

H2 {padding:10px0.25em2ex20%;}

Unilateral inside margin attribut

Also set the top, right, bottom, and left inner margins by using the following four separate properties:

◆ padding-top

◆ padding-right

◆ padding-bottom

◆ padding-left

As you may have thought, the following rules have exactly the same effect as the abbreviated rules above:

H2 {padding-top:10px; padding-right:0.25em; padding-bottom:2ex; padding-left:20%;}

Percentage value of the inner margin

As mentioned earlier, you can set a percentage value for the inner margin of an element. The percentage value is calculated relative to the width of its parent element, just like the outer margin. So, if the width of the parent element changes, they will also change.

The following rule sets the inner margin of a paragraph to 10% of the parent element width:

P {padding:10%;} for example: if the parent element of a paragraph is a div element, then its inner margin is calculated according to the width of div.

ThisparagraghiscontainedwithinaDIVthathasawidthof200pixels.

Note: the upper and lower inner margin is the same as the left and right inner margin; that is, the percentage of the upper and lower inner margin is set relative to the width of the parent element, not to the height.

CSS inside margin attribute

The above is about the content of this article on "what is the role of padding attributes in CSS". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Tags: Attribute element percentage content function value rule up and down article paragraph ratio knowledge article face different same consistent value pixel unit Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Apple Docker Shulou Information