2

I am trying to make a paragraph to fit the width of a text however, I am getting this white space on line breaks. I am looking for any solution that wouldn't affect text and wouldn't require JavaScript (that could cause a reflow). Doesn't have to be inline-block.

* {
    margin: 5px;
    padding: 5px;
}
div {
    background: gray;
}
p {
    background:white;
    border-bottom: 1px dotted black;
    display: inline-block;
    max-width: 130px;
}
<div id=container>
    <p>technique and statement a landscape or discovery a injection or fic</p>
</div>

Demo Fiddle

Is there any way that I can make paragraph width match the width of the longest line?

Here is the expected and current result: White space problem

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
TwistedOwl
  • 1,195
  • 1
  • 17
  • 30

2 Answers2

0
* {
    margin: 5px;
    padding: 5px;
}
div {
    background: gray;
    max-width: 130px; // you can ignore this if you dont need to be 130 px
}
p {
    background:white;
    border-bottom: 1px dotted black;
    display: inline-block;
    width:100%;
    
}
Salil Rajkarnikar
  • 588
  • 1
  • 7
  • 26
0

As @skyline3000 mentioned:

"match the longest line" - as @James said, there is only one line. You are artificially making line wraps with the max-width. You either need to put real line wraps into the text, or continue to adjust the max-width.

You need to manually specify line breaks which work for me - handling line breaks is rather an easy job for regular users.

body {
    background: gray;
}
#container {
    background: white;
    display: inline-block;
}
span {
    border-bottom: 1px dotted black;
    max-width: 130px;
    background: white;
}
<div id=container>
    <span>technique and<br>statement alandscape<br> ordiscovery ainjectn or
fic i</span>
</div>

JsFiddle

Also, I have decided to move to the span element and wrap it into display: inline-block to achieve this result. I don't know if this result is satisfying to you, I'm not sure if I was trying to fight HTML/CSS spec here. Real line breaks are probably the only solution.

TwistedOwl
  • 1,195
  • 1
  • 17
  • 30