1 Summary
<div>
and <span>
elements¹ with an assigned max-width
property seem to always consume the maximum width if there's an implicit line break, even if they may not typographically require it. Is there a pure HTML/CSS way for them to not consume the entire max-width
in these cases?
I make some comments on whether this could be a duplicate question in section 5.
2 Context / Problem Demonstration
I have the following minimum example to highlight the problem:
div {
display: inline-block;
max-width: 200px;
border: 1px black solid;
}
<div style="width: 200px">200px width reference</div><br />
<br />
<div>Case 1: abc def ghi</div><br />
<div>Case 2: abc def ghi jkl mno pqr stu</div><br />
<!-- Problematic case! -->
<div>Case 3: abc def ghi jkl mno<br />pqr stu</div><br />
Here in JS Fiddle. Here as a rendered image in Gecko.
Description²:
- Case 1 (okay): The div-box only grows to 127 px in width, as expected.
- Case 2 (problematic): The div-box seems to take the full width of 200 px, even though – due to the implicit line break – this seems unnecessary. It has the same effect as introducing superfluous right-padding.
- Case 3 (okay): When inserting the line break explicitly through a
<br>
element, the max-width seems to calculate properly and the element takes a width of 180 px.
3 Questions
My questions are:
- Is it possible to have a
<div>
only consume the typographically required width against a providedmax-width
in pure HTML/CSS without having to place a<br>
line break explicitly? - Differently phrased, is it possible for a
<div>
element to take the same width against a providedmax-width
regardless of whether the line break is implicit or explicit (through<br>
)? - Or… is there something in the HTML/CSS standard that dictates that this is exactly the defined behaviour?
4 The Objective
We're planning to use HTML elements such as these with text-align: center
in design elements with a maximum width. Their contents will be variable. For layout & design reasons, the left/right-paddings should be 0 px. Due to the effect above however, the <div>
elements always introduce artificial left/right-paddings when an implicit line break occurs.
5 Duplicate?
I would say this isn't a strict duplicate of Stackoverflow 14596213: Shrink DIV to text that's wrapped to its max-width?, because I'm looking for pure HTML/CSS solutions.
I would also say that this isn't a duplicate of Stackoverflow 67305201: Span ocupies whole div after line break [duplicate], because this question was closed, and the referred answers concern themselves with parent containers, which are absent in my example.
¹ And many others, I'm just using <div>
and <span>
elements as representatives.
² In the description, I'm using the exactly calculated widths as they appear on my system. The exact values could differ on other systems. They should not matter, I only list them for demonstrative purposes.
``` element). The end result is that they display exactly the same content. However case 2 has a 200 px width, case 3 has a 180 px width. From a typesetting and semantic perspective, it is not necessary for case 2 to consume the additional 20 px and introduce “artificial padding”, yet it does. (Why would it consume the max-width, when 180 px are enough, as demonstrated when placing the explicit ```
```?) – Zealotus Oct 25 '21 at 14:41