Sub topic 10
Logical values for text-align, float and clear
This chapter assumes you know the box model, basic float and text-align behavior, and the dir and lang attributes. It also assumes you have read the earlier chapters on writing modes, the block and inline axes, and start/end sides.
Logical properties cover more than margins and sizes. Several older properties take keyword values such as left and right. CSS added flow-relative keywords for these: start, end, inline-start and inline-end. This chapter covers text-align, float, clear, and resize.
The problem with left and right keywords
Take a paragraph with text-align: left. In English that is the normal reading edge. In Arabic or Hebrew, text starts at the right, so a left-aligned paragraph looks wrong. It sits against the end of the line instead of the start.
The same issue affects images floated to one side of a text column. If float: left puts a figure beside the start of English text, the figure ends up on the far side of Arabic text.
Flow-relative keywords fix this. They refer to the start or end of the line, and the browser resolves them from direction and writing-mode.
text-align: start and end
text-align accepts start and end.
startaligns to the line's inline-start edge.endaligns to the line's inline-end edge.centerandjustifyare already direction-neutral.leftandrightstay physical.
.note {
text-align: start;
}
.price {
text-align: end;
}In left-to-right text, .note aligns left and .price aligns right. In right-to-left text, they swap. In vertical-rl text, the inline axis runs top to bottom, so start means the top and end means the bottom.
start is also the initial value of text-align. Content that you do not style already follows the writing direction. Declaring text-align: start mainly matters when you need to undo an inherited center or right.
Where the alignment comes from
The browser uses the direction of the block that contains the line. Setting dir="rtl" on a parent changes the result for its children unless they set their own direction.
<div class="notes">
<p dir="ltr" lang="en">Invoice total</p>
<p dir="rtl" lang="ar">المجموع</p>
</div>
<style>
.notes p {
text-align: end;
}
</style>The English paragraph aligns right. The Arabic paragraph aligns left. Each paragraph's end edge is different.
float: inline-start and inline-end
float accepts inline-start and inline-end. They move the box to the start or end edge of the containing block's line, depending on direction.
.figure {
float: inline-start;
margin-inline-end: 1rem;
text-align: start;
}In left-to-right text, the figure floats left and the margin keeps a gap on its right. In right-to-left text, it floats right and the gap is on its left. Pair the float with a logical margin so the gap follows the float. A float: inline-start with margin-right: 1rem would give the gap on the wrong side in right-to-left text.
float is about the inline axis only. There are no block-start or block-end float values. Floating along the block axis is not something CSS offers.
clear: inline-start and inline-end
clear mirrors float. It lets a box move down past floats on a specific side.
.caption {
clear: inline-start;
}
.footer-note {
clear: both;
}clear: inline-start clears only floats on the start side. clear: both clears floats on both sides and is already direction-neutral.
Match the clear to the float. If you float with inline-start, clear with inline-start. Mixing physical and logical values can work in left-to-right text and then fail in right-to-left, because clear: left does not clear a float that went right.
resize
resize controls whether a user can drag to resize an element, usually a textarea. Its keywords were originally horizontal and vertical, which are physical. The logical values inline and block resize along the inline or block axis.
textarea {
resize: block;
}In horizontal writing modes, block means vertical resizing. In a vertical writing mode, block means horizontal resizing. A textarea that should only grow in the direction text stacks is resize: block in every writing mode.
Support for resize: block and resize: inline is good in current browsers, but check your support targets if you maintain older ones.
How the keywords map
The table shows which physical edge each logical keyword resolves to in three common modes.
| Mode | start / inline-start | end / inline-end | block resize axis |
|---|---|---|---|
| `horizontal-tb`, `ltr` | left | right | vertical |
| `horizontal-tb`, `rtl` | right | left | vertical |
| `vertical-rl`, `ltr` | top | bottom | horizontal |
Common errors
- Using
text-align: leftfor body text on a site that supports right-to-left languages. The text sits against the wrong edge. Usestart. - Using
float: inline-startwith a physical margin. The float adapts but the gap does not. - Clearing with
clear: leftafter floating withinline-start. In right-to-left text the clear does nothing. - Expecting
float: block-startto exist. It does not. - Assuming
startis always left. It is only left in left-to-right horizontal text. - Setting
directionon an inner element and forgetting thattext-align: startfollows the containing block's direction.
Combining the values
This example builds a media object. The image sits at the start of the text and the layout adapts without extra rules.
<article class="media" dir="rtl" lang="ar">
<img class="media-img" src="photo.jpg" alt="Product photo" width="96" height="96">
<p class="media-text">...</p>
<p class="media-meta">...</p>
</article>
<style>
.media-img {
float: inline-start;
margin-inline-end: 1rem;
}
.media-text {
text-align: start;
}
.media-meta {
clear: inline-start;
text-align: end;
}
</style>With dir="rtl", the image floats right, the gap is on its left, the text starts at the right, and the meta line clears the right-hand float and aligns left. Remove dir="rtl" and everything mirrors, with no changed CSS.
When physical values are right
Some layouts depend on the screen and not on the text. A fixed side panel that must stay on the physical left because of a map or a hardware-specific layout may need left. Likewise, text over an image whose subject sits on the left of the picture should stay text-align: left. Use physical values when the design is tied to the screen or to a picture, and logical values when it is tied to reading flow.
Exercise
A figure uses float: left; margin-right: 1rem; and a caption below it uses clear: left;. Rewrite the three declarations so they mirror correctly in right-to-left text, and say where the figure appears when dir="rtl".
Show answer
Use float: inline-start; margin-inline-end: 1rem; on the figure and clear: inline-start; on the caption. With dir="rtl", the inline-start side is the right, so the figure floats right, the gap is on its left, and the caption clears the right-hand float.
Exercise
A textarea in a writing-mode: vertical-rl layout should only be resizable in the direction that lines stack. Which resize value do you use, and which physical direction does it allow?
Show answer
Use resize: block. In vertical-rl, the block axis is horizontal, so the user can resize the textarea horizontally.