CSS Logical Sizing
In this page:
selector {
inline-size: length;
block-size: length;
max-inline-size: length;
}
Positioning with the inset Shorthand
inset is the logical shorthand equivalent of setting top, right, bottom, and left all at once -- inset: 0 pins a positioned element to fill its containing block exactly, identical in effect to writing all four physical properties individually.
It requires the element to have position set to relative, absolute, fixed, or sticky, just like the physical offsets do.
- Use inset: 0 as a quick, readable way to make an absolutely positioned overlay fill its entire parent container.
- Accepted values:
- inset: <length> — such as 0 or 10px, sets top, right, bottom and left
- inset: <percentage> — relative to the containing block
- inset: auto — no offset (default)
- inset: 10px 20px — one to four values, in top, right, bottom, left order
- inset: 0 — makes an absolutely positioned box fill its container
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Positioning with the inset Shorthand
<style>
.container {
position: relative;
width: 200px;
height: 100px;
background: #eee;
border: 2px dashed #999;
}
.overlay {
position: absolute;
inset: 0;
background: dodgerblue;
color: white;
}
</style>
<div class="container">
<div class="overlay">Fills the parent exactly</div>
</div>
Axis-Specific Positioning with inset-block and inset-inline
inset-block sets the start and end offsets along the block axis (top and bottom by default), while inset-inline sets the offsets along the inline axis (left and right by default).
Splitting inset into these two axis-specific pairs is useful when you only want to control positioning along one flow direction and leave the other axis untouched.
- Use inset-inline: 0 alone to stretch an element edge-to-edge horizontally while leaving its vertical position controlled separately.
- Accepted values:
- inset-block: <length> — top and bottom in horizontal writing
- inset-inline: <length> — left and right in horizontal writing
- inset-block-start | inset-block-end — single sides
- inset-inline-start | inset-inline-end — single sides
- auto — no offset (default)
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Axis-Specific Positioning with inset-block and inset-inline
<style>
.box {
position: absolute;
inset-inline: 0;
background: crimson;
color: white;
padding: 8px;
}
</style>
<div style="position: relative; background: #eee; min-height: 60px;">
<div class="box">Stretched edge-to-edge horizontally only</div>
</div>
Capping and Flooring Height with max-block-size and min-block-size
max-block-size and min-block-size are the logical equivalents of max-height and min-height, capping or flooring an element's size along the flow-relative block axis rather than the physical vertical axis.
In the default horizontal writing mode they behave exactly like their physical counterparts, but they automatically flip to control width instead if the writing mode changes to vertical.
- Prefer max-block-size and min-block-size over max-height and min-height in components meant to support vertical writing modes, like Japanese-language sites.
- Accepted values:
- max-block-size: <length> | <percentage> — caps the height (in horizontal writing)
- max-block-size: none — default, no cap
- min-block-size: <length> | <percentage> — sets a floor
- min-block-size: 0 — default floor
- min-content | max-content | fit-content — content-based sizes for either property
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Capping and Flooring Height with max-block-size and min-block-size
<style>
.card {
min-block-size: 100px;
max-block-size: 200px;
background: lightyellow;
border: 2px solid #cc9900;
padding: 10px;
}
</style>
<div class="card">Height capped and floored along the block axis</div>
Preventing Overly Narrow Content with min-inline-size
min-inline-size is the logical equivalent of min-width, preventing an element from shrinking narrower than a given size along the flow-relative inline axis.
It is especially useful inside flexible layouts like flexbox or grid, where items can otherwise shrink to the point of clipping their own content or icons.
- Set min-inline-size on flex items containing icons or short labels so they never shrink small enough to visually break in a cramped flex row.
- Accepted values:
- min-inline-size: <length> — floor for the width in horizontal writing
- min-inline-size: <percentage> — relative to the container
- min-inline-size: auto — default
- min-inline-size: 0 — no floor
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Preventing Overly Narrow Content with min-inline-size
<style>
.container {
display: flex;
gap: 8px;
}
.icon-label {
min-inline-size: 80px;
background: steelblue;
color: white;
padding: 8px;
}
.container > div:not(.icon-label) {
background: #ddd;
padding: 8px;
}
</style>
<div class="container">
<div class="icon-label">Icon</div>
<div>Other content</div>
</div>
Flow-Relative Margins with margin-block Variants
margin-block sets the start and end margins along the block axis in one declaration, while margin-block-start and margin-block-end target just one side individually -- the logical equivalents of margin-top and margin-bottom.
Together with the margin-inline family already used for left/right spacing, this completes a fully writing-mode-aware margin system.
- Use margin-block-start and margin-block-end instead of margin-top and margin-bottom in any component intended to support right-to-left or vertical writing modes.
- Accepted values:
- margin-block: <length> | <percentage> — both block sides
- margin-block-start: <length> — top in horizontal writing
- margin-block-end: <length> — bottom in horizontal writing
- margin-block: auto — absorbs free space, e.g. centering flex or grid items
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Flow-Relative Margins with margin-block Variants
<style>
.box {
margin-block: 20px;
background: lightyellow;
border: 1px solid #ccc;
padding: 8px;
}
.box2 {
margin-block-start: 10px;
margin-block-end: 30px;
background: lightblue;
border: 1px solid #99c;
padding: 8px;
}
</style>
<div class="box">Same top and bottom margin</div>
<div class="box2">Different top and bottom margin</div>
- Mixing inset with individual top/left declarations on the same element, which can create confusing, hard-to-predict overrides.
- Assuming min-inline-size always means a minimum horizontal size, when it flips to vertical automatically in a vertical writing mode.
- Forgetting that inset requires position to be set to something other than static, exactly like the physical top/right/bottom/left properties do.
- inset is the logical shorthand for top, right, bottom, and left combined, with inset-block and inset-inline offering axis-specific control.
- max-block-size and min-block-size cap and floor an element's size along the flow-relative block axis instead of physical height.
- min-inline-size guards against content on the flow-relative inline axis shrinking too far, mirroring the physical min-width property.
inset and margin-block/-inline logical sizing properties are supported in all modern browsers; always test writing-mode: vertical-rl pages specifically, since that is where logical and physical axes actually diverge.
Chapter Quiz — Complete all 25 topics to unlock
0/25 topics done
Complete these topics first:
- CSS Container Queries
- CSS Subgrid
- CSS Logical Properties
- CSS Logical Sizing
- CSS Writing Modes
- CSS Aspect Ratio
- CSS Object Fit
- CSS object-position
- CSS Masking
- CSS Math Functions
- CSS Motion Path
- CSS @supports
- CSS @property
- CSS At-Rules
- CSS Cursor
- CSS Will Change
- CSS User Interface
- CSS Print Styles
- CSS Pagination (Print)
- CSS Dark Mode
- CSS Accessibility
- CSS Performance
- CSS Preprocessors
- CSS Frameworks Overview
- CSS Interview Prep