Padding in CSS: installation methods and drawing features

Table of contents:

Padding in CSS: installation methods and drawing features
Padding in CSS: installation methods and drawing features
Anonim

According to the box model, CSS display padding or margin is the amount of padding from the outer border of an element to its content. CSS allows you to control the amount of padding for each side of the block separately or for all sides at the same time. To achieve the correct display of the element, it is important to understand the peculiarities of calculating padding.

CSS Box Model
CSS Box Model

Padding property group

There is a whole group of properties to describe padding in CSS. This is:

  • padding-top - controls the size of the top margin;
  • padding-right - changes the right padding;
  • padding-bottom - controls the size of the bottom padding;
  • padding-left - sets the left padding width.

The common property padding in CSS allows you to define all four fields in one rule. The instruction can take from one to four arguments:

4 parameters. Paddings are listed clockwise, starting from the top. This entry is used if each side has its own indent size


// padding: padding-top padding-right padding-bottom padding-left.element { padding: 10px 20px 30px 40px; }

3 parameters. If aside margins are equal, they can be grouped into one argument. Listing order: top, left and right, bottom margin


// padding: padding-top padding-right+padding-left padding-bottom.element { padding: 10px 20px 30px; }

2 parameters. Pairwise grouping of top-bottom and left-right margins


//padding: padding-top+padding-bottom padding-right+padding-left.element { padding: 10px 20px; }

1 parameter. Sets the size for all paddings at once


.element { padding: 10px; }

Place in block structure

The amount of padding in CSS is included by default in the total amount of space an element takes up.


.block1 { width: 100px; height: 100px; padding-right: 30px; padding-top: 20px; }

For the

.block1

element, the actual size will be 130px wide and 120px high. The

width

and

height properties determine the size of the content area by default, while CSS padding is measured separately.

Paddings are added to the size of the content part
Paddings are added to the size of the content part

Units of measure

You can specify the amount of padding in any distance units used in CSS - pixels, percentages, em or rem.

Important: if the padding is specified as a percentage, it will always be measured from the width of the PARENT element, even if it is a top or bottom margin.


.parent { width: 200px; height: 100px; }.block2 { height: 30px; width: 100px padding-bottom: 10% }

For block

block2 valuethe bottom padding will be 20px (10% of 200px) and the actual height will be 50px.

Percent padding
Percent padding

Unlike margins, padding in CSS cannot be less than zero. If a negative value is passed to the instruction, it will simply be ignored by the browser.

Inline element margins

Determining padding for elements with an inline display type has its own subtleties.

For blocks that are part of a line, top and bottom margins are always ignored as they might break the layout.


span { padding-left: 30px; padding-right: 20px; padding-top: 50px; padding-bottom: 40px }

To the left and right of the

span elements, the appropriate indentation instructions will appear, but there will be no vertical line shift. In fact, the browser will set the required fields for the block, but this does not matter, since they do not affect its place in the general flow.

Paddings for inline elements
Paddings for inline elements

The CSS padding property is well supported by all browsers, including the oldest ones, and can also be animated with animation and transition statements.

Popular topic

Editor's choice

  • The Psijic Order from The Elder Scrolls - who are they?
    The Psijic Order from The Elder Scrolls - who are they?

    The Psijics are the name of a mysterious and ancient Order from the Elder Scrolls game universe. Its members practice magic and are notorious for being at odds with the Thalmor. Even more interesting information about the Psijic Order can be found in today's guide

  • Three easy ways to turn off notifications in Viber
    Three easy ways to turn off notifications in Viber

    "Viber" today is one of the most popular and widespread instant messengers. Sometimes a situation arises that the sound of constantly incoming messages bothers or distracts a person from important matters. How to turn off notifications in Viber? The article presents three of the simplest and easiest ways to do this, and they will help you set the silent mode on both Android and iOS

  • How to advertise on Instagram yourself?
    How to advertise on Instagram yourself?

    One of the most effective methods of website promotion and business development at the moment is advertising on social networks. Social networks, where people actively communicate, are more willing to pay attention to useful posts, join groups, like posts and make reposts, are an almost inexhaustible source of traffic. A properly built advertising campaign will allow you to get new customers, keep the attention of existing ones and again win over those who were dissatisfied with the service

  • Google - what is it?
    Google - what is it?

    The article talks about the multidisciplinary corporation Google. Its services and the company's work as a device manufacturer are considered

  • How to remove a user from important friends on VKontakte: all ways
    How to remove a user from important friends on VKontakte: all ways

    Important friends of "VKontakte" are determined automatically. When friends, family, and a loved one appear first on the list, this is convenient. But relationships can change for various reasons, and the need to constantly see a person’s page may disappear. In this article, we will tell you how to remove from important VKontakte friends those users whom you do not want to see at the top of the list