4

The block size must not be greater than necessary.

I need to avoid the space to the right of the red line...

enter image description here

.item {
  display: flex;
  align-items: center;
}

.item-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: green;
  margin-right: 20px;
}

.item-text {
  outline: 1px solid green;
  font-size: 20px;
  max-width: 100px;
}
<div class="item">
  <div class="item-icon"></div>
  <div class="item-text">lorem a morem</div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • I guess this is still not possible, take a look here [stackoverflow](http://stackoverflow.com/questions/13672760/max-width-adjusts-to-fit-text) – vadber Dec 12 '16 at 09:40

2 Answers2

0

The problem you're having is simply how CSS works when wrapping text.

You can avoid the problem by adding a <br> tag where you want the break to occur.

Something like this:

.item {
  display: flex;
  align-items: center;
}

.item-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: green;
  margin-right: 20px;
}

.item-text {
  outline: 1px solid green;
  font-size: 20px;
  max-width: 100px;
}
<div class="item">
  <div class="item-icon"></div>
  <div class="item-text">lorem a<br>morem</div>
</div>

Here's a more detailed explanation: Make container shrink-to-fit child elements as they wrap

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
-1

Use word-break property, like:

.item-text {
  word-break: break-all;
}

Have a look at the snippet below:

.item {
  display: flex;
  align-items: center;
}

.item-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: green;
  margin-right: 20px;
}

.item-text {
  outline: 1px solid green;
  font-size: 20px;
  max-width: 100px;
  word-break: break-all;
}
<div class="item">
  <div class="item-icon"></div>
  <div class="item-text">lorem a morem</div>
</div>

Hope this helps!

Saurav Rastogi
  • 9,575
  • 3
  • 29
  • 41