0

I'm currently working with flexbox and it forces my p tag's width to its max-width.

Anyone knows how to set a p tag's width to where line breaks?

.container {
  max-width: 190px;
  border: solid 1px #000;
  display: flex;
  justify-content: space-between;
}

.container p {
  background-color: #000;
  color: #fff;
}

.long {
  width: 114px;
}
<div class="container">
  <p>here</p>
  <p>this is short</p>
</div>

<div class="container">
  <p>here</p>
  <p>this is a very long lineeeeeee</p>
</div>

<br> Expected: p's width should only be up to where line breaks

<div class="container">
  <p>here</p>
  <p class="long">this is a very long lineeeeeee</p>
</div>
Dejan.S
  • 18,571
  • 22
  • 69
  • 112
misay
  • 1

2 Answers2

0

You can use gap: property in css to create gap between two elements of flexbox or grid.

.container {
  max-width: 190px;
  border: solid 1px #000;
  display: flex;
  gap: 3rem;
  justify-content: space-between;
}

.container p {
  background-color: #000;
  color: #fff;
}

.long {
  width: 114px;
}
<div class="container">
  <p>here</p>
  <p>this is short</p>
</div>

<div class="container">
  <p>here</p>
  <p>this is a very long lineeeeeee</p>
</div>

<br> Expected: p's width should only be up to where line breaks

<div class="container">
  <p>here</p>
  <p class="long">this is a very long lineeeeeee</p>
</div>
0

You can use width: min-content, and then apply white-space: nowrap, because if you don't apply this white space property every word would be in a new row

.container {
  max-width: 250px;
  border: solid 1px #000;
  display: flex;
  justify-content: space-between;
}

.container p {
  background-color: #000;
  color: #fff;
  width:min-content;
  white-space: nowrap;
}
<div class="container">
        <p>here</p>
        <p>this is short</p>
    </div>

    <div class="container">
        <p>here</p>
        <p>this is a very long lineeeeeee</p>
    </div>

    <br> Expected: p's width should only be up to where line breaks

    <div class="container">
        <p>here</p>
        <p class="long">this is a very long lineeeeeee</p>
    </div>
Szegoo
  • 227
  • 1
  • 9