Sub topic 10
Logical values for text-align, float and clear
This chapter assumes you know the box model, how float and clear work, and how dir and writing-mode set a document's direction. The earlier chapters in this series covered the block and inline axes and the start and end sides.
The problem with left and right
Margins, padding and borders have logical property names. text-align, float and clear keep their names but accept logical keywords. text-align: left still means the left edge in every language. In a right-to-left page, that leaves Arabic or Hebrew text pinned to the wrong side.
The logical keywords follow the inline axis. They resolve against the element's direction and writing-mode.
| Physical value | Logical value | Resolves to, in left-to-right text |
|---|---|---|
| `text-align: left` | `text-align: start` | left |
| `text-align: right` | `text-align: end` | right |
| `float: left` | `float: inline-start` | left |
| `float: right` | `float: inline-end` | right |
| `clear: left` | `clear: inline-start` | left |
| `clear: right` | `clear: inline-end` | right |
In right-to-left text, each logical value resolves to the opposite physical side.
text-align
The initial value of text-align is start. If you never set it, text already follows the writing direction. Set start or end only when you need to override an inherited value, such as a centered parent. Keep center and justify as they are. They have no direction.
.caption {
text-align: end;
}text-align-last accepts the same start and end keywords.
float and clear
Pair a logical float with a logical margin, so both sides of the layout flip together.
.figure {
float: inline-start;
margin-inline-end: 1rem;
text-align: start;
}In English, the figure floats left and the gap sits on its right. In Arabic, the figure floats right and the gap sits on its left.
Use the same keywords with clear, so a following element drops below the float:
.after-figure {
clear: inline-start;
}clear: both needs no change.
resize
The resize property also has logical values. resize: inline lets the user drag only along the inline axis. resize: block allows only the block axis. In a vertical writing mode, resize: inline permits vertical dragging, where resize: horizontal would not.
textarea {
resize: block;
}Common errors
- Combining
text-align: leftwithpadding-inline-start. The text stays left in right-to-left pages while the padding moves right. - Treating
inline-startas "top" in vertical modes. Invertical-rlwith left-to-right direction, the inline start is at the top, so the float goes to the top of the block. - Assuming old browsers understand
float: inline-start. Support for these float keywords arrived later than fortext-align: start. Check your target browsers. A physicalfloat: leftdeclaration placed before the logical one gives a fallback.
.figure {
float: left;
float: inline-start;
}Browsers that do not recognize the second declaration discard it and keep the first.
Exercise
A page is rendered with dir="rtl". An image has float: inline-start and margin-inline-end: 1rem. On which physical side does the image float, and on which side is the gap?
Show answer
The image floats to the right, because inline-start is the right side in right-to-left text. The 1rem gap is on the image's left, which is its inline-end side.