CSS - block-size 属性



CSS 的 **block-size** 属性根据元素的书写模式确定元素的水平或垂直大小。当书写模式为垂直时,会影响元素的宽度;当书写模式为水平时,会影响元素的高度。

语法

block-size: auto | length | percentage | initial | inherit;

属性值

描述
auto 设置元素的默认块级大小。默认值。
长度值 以 px、cm、pt 等单位设置块级大小。
百分比 以百分比值设置块级大小。
initial 将属性设置为其默认值。
inherit 从父元素继承该属性。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

CSS 块大小属性示例

以下示例说明了使用不同值的 **block-size** 属性。

带有 auto 值的块大小属性

要让浏览器决定块的大小,我们使用 auto 值。auto 值将默认值设置为 block-size。在以下示例中,带有和不带有书写模式的 block-size 都使用了 auto 值。

示例

Open Compiler
<!DOCTYPE html> <html> <head> <style> div { background-color: lightgreen; border: solid black 1px; block-size: auto; } #auto-vertical { writing-mode: vertical-rl; } #auto-horizontal { writing-mode: horizontal-tb; } </style> </head> <body> <h2> CSS block-size property </h2> <p> a) Auto Block Size </p> <div > <p> This is a p tag with auto block size </p> </div> <p> b) Auto Block Size with Vertical Writing Mode </p> <div id="auto-vertical"> <p> This is p tag with auto block size and vertical writing mode. </p> </div> <p> c) Auto Block Size with Horizontal Writing Mode </p> <div id="auto-horizontal"> <p> This is p tag with auto block size and horizontal writing mode. </p> </div> </body> </html> </html>

带有长度值的块大小属性

要根据我们的选择设置块的大小,我们可以用长度值指定大小(例如 10px、25px 等)。在以下示例中,带有和不带有书写模式的 block-size 都使用了 80px 的大小。

示例

Open Compiler
<!DOCTYPE html> <html> <head> <style> div { background-color: lightgreen; border: solid black 1px; block-size: 80px; } #length-vertical { writing-mode: vertical-rl; } #length-horizontal { writing-mode: horizontal-tb; } </style> </head> <body> <h2> CSS block-size property </h2> <p> a) 80px Block Size </p> <div > <p> This is a p tag with 80px block size </p> </div> <p> b) 80px Block Size with Vertical Writing Mode</p> <div id="length-vertical"> <p> This is p tag with 80px block size and vertical writing mode. </p> </div> <p> c) 80px Block Size with Horizontal Writing Mode </p> <div id="length-horizontal"> <p> This is p tag with 80px block size and horizontal writing mode. </p> </div> </body> </html> </html>

带有百分比值的块大小属性

要根据我们的选择设置块的大小,我们可以用百分比指定大小(例如 10%、15% 等)。在以下示例中,带有和不带有书写模式的 block-size 都使用了 40% 的大小。

示例

Open Compiler
<!DOCTYPE html> <html> <head> <style> body { height: 100vh; } div { background-color: lightgreen; border: solid black 1px; block-size: 40%; } #percent-vertical { writing-mode: vertical-rl; } #percent-horizontal { writing-mode: horizontal-tb; } </style> </head> <body> <h2> CSS block-size property </h2> <p> a) 40% Block Size </p> <div> <p> This is a p tag with 40% block size </p> </div> <p> b) 40% Block Size with Vertical Writing Mode </p> <div id="percent-vertical"> <p> This is p tag with 40% block size and vertical writing mode. </p> </div> <p> c) 40% Block Size with Horizontal Writing Mode </p> <div id="percent-horizontal"> <p> This is p tag with 40% block size and horizontal writing mode. </p> </div> </body> </html> </html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
block-size 57.0 79.0 41.0 12.1 44.0
css_properties_reference.htm
广告