CSS block-size Property
The block-size property sets an element's size along the block axis, which depends on its writing mode.
Unlike height, which always refers to the physical vertical dimension, block-size follows the logical direction in which blocks of content are arranged. In horizontal writing modes, it corresponds to height. In vertical writing modes, it corresponds to width.
block-size Syntax
selector {
block-size: 200px;
}
The property accepts lengths, percentages, automatic sizing, intrinsic sizing keywords, and supported sizing functions.
.automatic {
block-size: auto;
}
.fixed {
block-size: 200px;
}
.relative {
block-size: 12rem;
}
.percentage {
block-size: 50%;
}
.minimum-content {
block-size: min-content;
}
.maximum-content {
block-size: max-content;
}
.fit-content {
block-size: fit-content;
}
.calculated {
block-size: calc(100% - 20px);
}
.limited {
block-size: fit-content(300px);
}
Support for some newer sizing functions and keywords varies by browser and layout context.
CSS-wide values such as inherit, initial, revert, revert-layer, and unset are also accepted.
Negative sizes are invalid.
Property Values
| Value | Description |
|---|---|
auto |
Lets the browser determine the block size using the element's content and layout rules. |
<length> |
Specifies a block size using a nonnegative CSS length such as px, em, or rem. |
<percentage> |
Specifies a block size relative to the corresponding block dimension of the containing block, subject to percentage sizing rules. |
min-content |
Uses the element's minimum intrinsic block size. |
max-content |
Uses the element's maximum intrinsic block size. |
fit-content |
Uses intrinsic sizing constrained by the available space. |
fit-content(<length-percentage>) |
Uses a supplied size limit with intrinsic sizing calculations, where supported. |
stretch |
Sizes the element's margin box to fill the available block-axis space, where supported. |
calc() |
Calculates a size using compatible CSS lengths, percentages, and arithmetic. |
The initial value is auto. The property is not inherited and generally applies to the same elements as the physical width and height properties, subject to their usual layout restrictions.
The computed value depends on the specified sizing expression. Interpolable numeric sizes can be animated, while intrinsic sizing keywords require additional browser support for smooth interpolation.
How block-size Works
CSS layout uses two logical axes: the inline axis and the block axis.
The inline axis follows the direction in which text is arranged within a line. The block axis follows the direction in which lines or blocks of content are stacked.
The block-size property sets the dimension parallel to the block axis.
Horizontal Writing
.horizontal-box {
writing-mode: horizontal-tb;
block-size: 150px;
}
In horizontal writing, the block axis is vertical. The element's logical block size therefore corresponds to its physical height.
Vertical Writing
.vertical-box {
writing-mode: vertical-rl;
block-size: 150px;
}
In vertical writing, the block axis is horizontal. The element's logical block size therefore corresponds to its physical width.
Size vs. Content
Setting a block size does not guarantee that all content fits inside the element. If the content needs more space than the specified size, overflow may occur.
.content-box {
block-size: 100px;
overflow: auto;
}
The element has a specified block size of 100 pixels, and scrollbars can appear if its content overflows.
Writing Modes and the Block Axis
The meaning of block-size depends on the element's computed writing-mode.
| Writing Mode | Block Axis | Physical Equivalent |
|---|---|---|
horizontal-tb |
Vertical, top to bottom | height |
vertical-rl |
Horizontal, right to left | width |
vertical-lr |
Horizontal, left to right | width |
sideways-rl |
Horizontal, right to left | width |
sideways-lr |
Horizontal, left to right | width |
Horizontal Example
.horizontal {
writing-mode: horizontal-tb;
inline-size: 240px;
block-size: 120px;
}
The element is physically 240 pixels wide and 120 pixels tall when using the default content-box sizing model.
Vertical Example
.vertical {
writing-mode: vertical-rl;
inline-size: 240px;
block-size: 120px;
}
The element is physically 120 pixels wide and 240 pixels tall under the same sizing model.
Text Direction vs. Writing Mode
The direction property controls inline text direction, but it does not independently switch the block axis from vertical to horizontal.
For example, both left-to-right and right-to-left text normally use a vertical block axis under writing-mode: horizontal-tb.
The auto Value
The auto value lets the browser calculate the block size using the element's layout rules.
.automatic-box {
block-size: auto;
}
For an ordinary block-level element in horizontal writing mode, the block size generally expands to accommodate its content.
Content-Driven Sizing
.message-panel {
padding: 20px;
background-color: #e8f2fb;
block-size: auto;
}
The panel can grow along the block axis as additional content is added.
Auto in Other Layouts
In Flexbox, Grid, positioned layout, and replaced elements, automatic sizing follows the rules of the applicable layout model. It does not always mean that the element simply matches the height of its text.
When to Use auto
Use auto when content should determine the element's block size and a fixed dimension is unnecessary.
Length Values
Length values specify a block size using CSS units.
Pixels
.fixed-panel {
block-size: 180px;
}
The element's block size is set to 180 pixels, subject to its box-sizing model and applicable minimum and maximum constraints.
Root-Relative Units
.relative-panel {
block-size: 12rem;
}
The size is based on the root element's font size.
Viewport Units
.viewport-panel {
block-size: 50vh;
}
The block size is 50% of the viewport height. In vertical writing mode, this still uses the viewport's physical height because vh is a physical viewport unit.
Logical Viewport Units
.logical-viewport-panel {
block-size: 50vb;
}
The vb unit refers to the viewport dimension along the block axis of the relevant writing mode.
Calculated Lengths
.calculated-panel {
block-size: calc(100vh - 120px);
}
The block size is calculated by subtracting 120 pixels from the viewport height.
Length values are useful when a component needs predictable dimensions, but avoid unnecessarily restricting content that may grow.
Percentage Values
A percentage block-size is calculated relative to the corresponding block dimension of the containing block when that dimension can be resolved.
.parent {
block-size: 300px;
}
.child {
block-size: 50%;
}
In a suitable normal-flow layout, the child receives a block size of 150 pixels.
A Definite Containing Block
Percentage sizing is most predictable when the containing block has a definite size along the block axis.
.outer {
block-size: 400px;
}
.inner {
block-size: 25%;
}
The inner element's block size is 100 pixels when the percentage resolves against the outer element's 400-pixel block size.
Indefinite Containing Block Size
If the containing block's block size depends on its content, a percentage block size may behave as auto or follow special layout rules rather than resolving to a fixed number.
Percentage Sizing in Grid and Flexbox
Grid and Flexbox have additional rules for resolving percentages. A percentage may become resolvable through layout even when it would not resolve in ordinary block formatting.
Test percentage sizing in the actual layout context instead of assuming that every percentage produces a definite dimension.
Intrinsic Sizing Keywords
Intrinsic sizing keywords let the browser determine the block size using the content's natural sizing requirements.
min-content
.minimum-size {
block-size: min-content;
}
The element uses its minimum intrinsic block size as defined by the applicable sizing and layout rules.
max-content
.maximum-size {
block-size: max-content;
}
The element uses its maximum intrinsic block size.
fit-content
.fitted-size {
block-size: fit-content;
}
The browser uses an intrinsic size constrained by the available space, according to the fit-content sizing formula.
fit-content() Function
.limited-size {
block-size: fit-content(300px);
}
The function form supplies an explicit sizing limit. Support and applicability can differ from the fit-content keyword, so verify browser compatibility before relying on it.
stretch
.stretched-size {
block-size: stretch;
}
Where supported, stretch sizes the element's margin box to fill the available space along the block axis.
Intrinsic sizing values can be useful for content-driven components, but their results depend on the formatting context and available space.
Working with box-sizing
The box-sizing property determines which parts of an element's box are included in the specified block-size.
content-box
.content-box-example {
box-sizing: content-box;
block-size: 150px;
padding: 20px;
border: 5px solid #1769aa;
}
The content box is 150 pixels along the block axis. Padding and borders increase the total outer dimension.
In horizontal writing mode, the border-box height becomes 200 pixels: 150 pixels of content, 40 pixels of padding, and 10 pixels of borders.
border-box
.border-box-example {
box-sizing: border-box;
block-size: 150px;
padding: 20px;
border: 5px solid #1769aa;
}
The specified 150-pixel size includes the content, padding, and borders. The content area is reduced to accommodate the padding and borders.
Recommended Component Sizing
.component {
box-sizing: border-box;
block-size: 200px;
padding: 20px;
}
Using border-box can make component dimensions easier to manage because padding and borders are included in the declared size.
Minimum and Maximum Block Size
The min-block-size and max-block-size properties constrain an element's logical block dimension.
Minimum Block Size
.minimum-panel {
block-size: auto;
min-block-size: 180px;
}
The element has a minimum block size of 180 pixels but can grow if its layout requires more space.
Maximum Block Size
.maximum-panel {
max-block-size: 300px;
overflow: auto;
}
The element is constrained to a maximum block size of 300 pixels, and overflowing content can become scrollable.
Combining All Three Properties
.resizable-panel {
min-block-size: 120px;
block-size: 200px;
max-block-size: 400px;
}
The preferred block size is 200 pixels, subject to the minimum and maximum constraints.
Content-Friendly Minimum Sizing
.content-card {
min-block-size: 180px;
block-size: auto;
padding: 20px;
}
This approach is often preferable to a fixed block size for cards, panels, and sections containing variable amounts of text.
block-size and inline-size
The block-size and inline-size properties work together to control an element's logical dimensions.
| Property | Controls | Horizontal Writing | Vertical Writing |
|---|---|---|---|
block-size |
Block-axis dimension | Height | Width |
inline-size |
Inline-axis dimension | Width | Height |
Logical Component Dimensions
.logical-card {
inline-size: 280px;
block-size: 160px;
}
In horizontal writing mode, the element is 280 pixels wide and 160 pixels tall.
In vertical writing mode, the physical dimensions are reversed.
Adapting to Writing Modes
.writing-mode-card {
inline-size: 240px;
block-size: 140px;
}
.vertical-layout .writing-mode-card {
writing-mode: vertical-rl;
}
The logical dimensions remain associated with the inline and block axes even when the writing mode changes.
This makes logical sizing useful for components that must support different writing systems without separate physical width and height rules.
Logical vs. Physical Sizing
Physical sizing properties refer to fixed physical axes. Logical sizing properties follow the element's writing mode.
Physical Sizing
.physical-box {
width: 240px;
height: 140px;
}
The element's width and height remain associated with the physical horizontal and vertical axes.
Logical Sizing
.logical-box {
inline-size: 240px;
block-size: 140px;
}
The dimensions follow the inline and block axes of the element's writing mode.
Mixing Logical and Physical Properties
.mixed-box {
height: 100px;
block-size: 150px;
}
In horizontal writing mode, both declarations affect the physical height. The later block-size declaration wins when the declarations have equal cascade priority.
In vertical writing mode, height controls the physical height while block-size controls the physical width.
Cascade Considerations
Logical and physical sizing properties are not independent when they map to the same physical dimension. The normal CSS cascade determines which declaration takes effect.
For clarity, prefer a consistent logical sizing approach when building writing-mode-aware components.
Responsive Layouts
Logical sizing properties can help create responsive components that adapt to content and writing modes.
A Minimum Block Size
.responsive-card {
min-block-size: 180px;
block-size: auto;
padding: 20px;
}
The card has a minimum size along the block axis but can grow as its content changes.
Viewport-Relative Sizing
.full-screen-section {
min-block-size: 100vb;
}
The vb unit follows the viewport's logical block axis, making it useful for writing-mode-aware sections.
Using clamp()
.adaptive-panel {
block-size: clamp(160px, 30vb, 360px);
}
The block size is constrained between 160 and 360 pixels, with a preferred size of 30% of the viewport's logical block dimension.
Media Queries
.responsive-panel {
block-size: 240px;
}
@media (max-width: 600px) {
.responsive-panel {
block-size: auto;
min-block-size: 160px;
}
}
On narrower viewports, the panel switches from a fixed block size to content-driven sizing with a minimum.
Remember that media query width conditions refer to physical viewport dimensions, even when the element uses logical sizing properties.
block-size Example
The following example demonstrates how the same logical dimensions produce different physical shapes in horizontal and vertical writing modes.
Both boxes use inline-size: 220px and block-size: 120px. The writing mode determines which physical dimension each property controls.
<div class="block-size-demo">
<div class="demo-item">
<p>horizontal-tb</p>
<div class="demo-box horizontal">Logical sizing</div>
</div>
<div class="demo-item">
<p>vertical-rl</p>
<div class="demo-box vertical">Logical sizing</div>
</div>
</div>
.block-size-demo {
display: flex;
flex-wrap: wrap;
gap: 30px;
align-items: flex-start;
}
.block-size-demo .demo-item {
padding: 10px;
}
.block-size-demo .demo-item p {
margin-bottom: 10px;
font-weight: bold;
}
.block-size-demo .demo-box {
box-sizing: border-box;
inline-size: 220px;
block-size: 120px;
display: flex;
align-items: center;
justify-content: center;
padding: 15px;
border: 3px solid #1769aa;
border-radius: 6px;
background-color: #e8f2fb;
color: #173b5e;
font-weight: bold;
text-align: center;
}
.block-size-demo .horizontal {
writing-mode: horizontal-tb;
}
.block-size-demo .vertical {
writing-mode: vertical-rl;
}
horizontal-tb
vertical-rl
In horizontal writing mode, the first box is 220 pixels wide and 120 pixels tall.
In vertical writing mode, the second box is 120 pixels wide and 220 pixels tall.
The logical sizes remain unchanged. Only their mapping to the physical axes changes.
Important Considerations
Keep the following points in mind when using block-size:
- Logical dimension: The property controls size along the block axis rather than a permanently fixed physical axis.
- Initial value: The default is
auto. - Horizontal writing:
block-sizecorresponds to physical height. - Vertical writing:
block-sizecorresponds to physical width. - Writing mode: The element's computed
writing-modedetermines the relevant physical dimension. - Text direction: Changing
directionalone does not switch the block axis between horizontal and vertical. - Automatic sizing: The
autovalue follows the applicable layout rules. - Percentages: Percentage sizes depend on the containing block and whether its block dimension is definite.
- Negative values: Negative block sizes are invalid.
- Box model: The
box-sizingproperty determines whether padding and borders are included in the specified size. - Minimum and maximum:
min-block-sizeandmax-block-sizeconstrain the resulting size. - Logical pairs: Use
inline-sizewithblock-sizeto control both logical dimensions. - Physical properties:
heightorwidthmay affect the same dimension, depending on writing mode. - Overflow: Fixed block sizes can cause content to overflow if the available space is insufficient.
- Intrinsic sizing: Keywords such as
min-content,max-content, andfit-contentfollow content-based sizing rules. - Browser compatibility: Basic logical sizing is widely supported, but newer sizing keywords and functions may have different support levels.
- Responsive design: Logical viewport units and content-friendly minimum sizes can help components adapt to different layouts.
- Accessibility: Avoid fixed dimensions that clip text when users increase font size or text spacing.
Use block-size when an element's dimensions should follow its writing mode, especially in reusable components and multilingual layouts.
Browser Support
The block-size property has Baseline Widely available status and is supported in modern versions of Chrome, Edge, Firefox, Safari, and other major browsers.
Basic length, percentage, and automatic sizing values have broad support. Compatibility for newer sizing keywords and functions should be checked separately.
Providing a Fallback
.panel {
height: 200px;
block-size: 200px;
}
In horizontal writing mode, the physical height declaration provides a fallback for browsers that do not recognize block-size.
In vertical writing mode, block-size maps to physical width, so the fallback may require a different physical property if older browser support is necessary.
Feature Detection
@supports (block-size: 200px) {
.panel {
block-size: 200px;
}
}
The @supports rule can be used to apply logical sizing only when the browser recognizes the property and value.
Visit Can I Use? to review current browser compatibility.
Specifications
The block-size property is defined in the CSS Logical Properties and Values Module Level 1 specification.
CSS Logical Properties and Values Module Level 1
Additional sizing behavior is defined in the CSS Box Sizing Module Level 3 specification.
