Logical properties for margins, borders and padding - CSS: Cascading Style Sheets 编辑

The Logical Properties and Values specification defines flow-relative mappings for the various margin, border, and padding properties and their shorthands. In this guide we take a look at these.

If you have looked at the main page for CSS Logical Properties and Values you will see there are a huge number of properties listed. This is mostly due to the fact that there are four longhand values each for margin, border, and padding side, plus all the shorthand values.

Mappings for margins, borders, and padding

The specification details mappings for each logical value to a physical counterpart. In the table below I have given these mapped values assuming that the writing-mode in use is horizontal-tb — with a left to right direction. The inline direction therefore runs horizontally — left to right — and margin-inline-start would be equivalent to margin-left.

If you were using a horizontal-tb writing mode with a right-to-left text direction then margin-inline-start would be the same as margin-right, and in a vertical writing mode it would be the same as using margin-top.

Logical propertyPhysical Property
border-block-endborder-bottom
border-block-end-colorborder-bottom-color
border-block-end-styleborder-bottom-style
border-block-end-widthborder-bottom-width
border-block-startborder-top
border-block-start-colorborder-top-color
border-block-start-styleborder-top-style
border-block-start-widthborder-top-width
border-inline-endborder-right
border-inline-end-colorborder-right-color
border-inline-end-styleborder-right-style
border-inline-end-widthborder-right-width
border-inline-startborder-left
border-inline-start-colorborder-left-color
border-inline-start-styleborder-left-style
border-inline-start-widthborder-left-width
border-start-start-radiusborder-top-left-radius
border-start-end-radiusborder-bottom-left-radius
border-end-start-radiusborder-top-right-radius
border-end-end-radiusborder-bottom-right-radius
margin-block-endmargin-bottom
margin-block-startmargin-top
margin-inline-endmargin-right
margin-inline-startmargin-left
padding-block-endpadding-bottom
padding-block-startpadding-top
padding-inline-endpadding-right
padding-inline-startpadding-left

There are also some additional shorthands, made possible because we have the ability to target both block or both inline edges of the box simultaneously. These shorthands have no physical equivalent.

PropertyPurpose
border-blockSets border-color, border-style, and border-width for both block borders.
border-block-colorSets border-color for both block borders.
border-block-styleSets border-style for both block borders.
border-block-widthSets border-width for both block borders.
border-inlineSets border-color, -style, and -width for both inline borders.
border-inline-colorSets border-color for both inline borders.
border-inline-styleSets border-style for both inline borders.
border-inline-widthSets border-width for both inline borders.
margin-blockSets all the block margins.
margin-inlineSets all the inline margins.
padding-blockSets the block padding.
padding-inlineSets the inline padding.

Margin examples

The mapped margin properties of margin-inline-startmargin-inline-endmargin-block-start, and margin-inline-end can be used instead of their physical counterparts.

In the example below I have created two boxes and added different sized margins to each edge. I have added an extra container with a border in order to make the margin more obvious to see.

One box uses physical properties and the other logical properties. Try changing the direction property to rtl to cause the boxes to display in a right-to-left direction, the margins on the first box will stay in the same place, while the margins on the inline dimension of the second box will switch.

You can also try changing the writing-mode from horizontal-tb to vertical-rl. Again, notice how the margins stay in the same place for the first box, but switch around to follow the text direction in the second.

Margin shorthands

As we can now target both sides of a box — either both inline sides or both block sides — there are new shorthands available, margin-inline and margin-block, which accept two values. The first value will apply to the start of that dimension, the second to the end. If you only use one value it is applied to both.

In a horizontal writing mode this CSS would apply a 5px margin to the top of the box and a 10px margin to the bottom.

.box {
  margin-block: 5px 10px;
}

Note: The shorthand properties margin-inline and margin-block shipped in Firefox 66. As these are new properties check browser support before using.

Padding examples

The mapped padding properties of padding-inline-startpadding-inline-endpadding-block-start, and padding-inline-end can be used instead of their physical counterparts.

In the example below I have two boxes, one of which is using physical padding properties and the other logical padding properties. With a writing-mode of horizontal-tb, both boxes should appear the same.

Try changing the direction property to rtl to cause the boxes to display in a right-to-left direction. The padding on the first box will stay in the same place, whereas the padding on the inline dimension of the second box will switch.

You can also try changing the writing-mode from horizontal-tb to vertical-rl. Again, notice how the padding stays in the same place for the first box, but switches around to follow the text direction in the second.

Padding shorthands

As with margin, there are two-value shorthands for padding — padding-inline and padding-block — which allow you to set the padding of the two inline, and two block dimensions, respectively.

In a horizontal writing-mode this CSS would apply 5px of padding to the top of the box and 10px of padding to the bottom:

.box {
  padding-block: 5px 10px;
}

Note: The shorthand properties padding-inline and padding-block shipped in Firefox 66. As these are new properties check browser support before using.

Border examples

The border properties are the main reason that Logical Properties and Values seems to have so many properties, as we have the longhands for the color, width, and style of the border on each side of a box, along with the shorthand to set all three at once for each side. As with margin and padding we have a mapped version of each physical property.

The demo below uses some longhands and three shorthand values. As with the other demos try changing the direction property to rtl to cause the boxes to display in a right-to-left direction, or changing the writing-mode from horizontal-tb to vertical-rl.

New border shorthands

There are two-value shorthands to set the width, style and, color of the block or inline dimension, and two-value shorthands to set all three values in the block or inline dimension. The below code, in a horizontal writing mode, would give you a 2px green solid border on the top and bottom of the box, and a 4px dotted purple border on the left and right.

.box {
  border-block: 2px solid green;
  border-inline-width: 4px;
  border-inline-style: dotted;
  border-inline-color: rebeccapurple;
}

Note: these two value shorthands shipped in Firefox 66, check browser support before using as other browsers may not have implemented them yet.

Flow relative border-radius properties

The specification has fairly recently added flow-relative values for the border-radius longhands. These have not yet been implemented by any browser. The below example, in a horizontal writing-mode, would set the top-right border radius to 1em, the bottom-right to 0, the bottom-left to 20px and the top-left to 40px.

.box {
  border-end-start-radius: 1em;
  border-end-end-radius: 0;
  border-start-end-radius: 20px;
  border-start-start-radius: 40px;
}

Indicating logical values for the 4-value shorthand syntax

The specification makes a suggestion for the four-value shorthands such as the margin property, however the final decision on how this should be indicated is as yet unresolved, and is discussed in this issue.

Using any four-value shorthand such as margin, padding, or border will currently use the physical versions, so if following the flow of the document is important, use the longhand properties for the time being.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:62 次

字数:19495

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文