I want two inline-block
elements to remain on the same row, regardless of their width
or margin
.
The quick solution is to apply white-space: nowrap
to the container. Flex and floats are alternatives that also work.
I'm just stuck on figuring out a particular wrapping behavior with white-space: normal
.
Here's the situation:
- There are two
inline-block
elements in a block-level container. - The width of the container is fixed at 500px.
- The width of each child is fixed at 200px.
- The container is set to
overflow: hidden
.
With or without white-space: nowrap
, Box A will never wrap. The size of its width
or margin-left
doesn't matter; Box A will simply disappear into the void of overflow: hidden
.
Here's Box A with margin-left: 400px
(container has overflow: hidden; white-space: normal
):
Notice in the image above how Box B has wrapped. It did not disappear into overflow: hidden
.
Here's Box B with margin-left: 250px
(container unchanged from above; box A reset):
Here's Box B with margin-left: 400px
:
Unlike Box A, Box B refuses to stay on the first line and just hide.
So the rule seems to be:
With
white-space: normal
, only the first element on the line will respectoverflow: hidden
. Subsequent elements will wrap but respectoverflow: hidden
on subsequent lines.
Testing this theory with a third element seems to prove it correct:
Here's Box B with margin-left: 350px
and a new Box C with margin-left: 350px
:
So it seems that one element cannot force another element to respect overflow: hidden
on their common parent.
Anybody know exactly what's going on, and/or where in the spec this behavior is defined?
Is this an issue of overflow, wrapping, inline-block, or maybe a combination of factors?
I've checked here but didn't find anything: https://drafts.csswg.org/css-text-3/#white-space-property