0

I have a nested flex container. Here is the code:

<div class="parent-container">
  <div class="child-container">
    <span class="color-block"></span>
    <span>This is a long long long long long text.</span>
  </div>
</div>
.parent-container {
  display: flex;
  background: orange;
}

.child-container {
  display: flex;
  background: green;
}

.color-block {
  background: yellow;
  flex: 0 0 15px;
  align-self: stretch;
}

Look at the result at https://codepen.io/crupest/pen/Lqwxpp.

This is the least code that reproduces my problem. Of course there are other elements in parent container.

My question is that why the last word "text" wraps even when there is remaining space at right? And how to make it not wrap? Or is there any workaround?

Thanks!


Update:

My purpose is that I want a color label in front of the text.

Thanks for @Kata Csortos pointing out that I need to say my purpose.

Community
  • 1
  • 1
crupest
  • 124
  • 1
  • 1
  • 10

3 Answers3

2

JsFiddle

.color-block {
    background: yellow;
    flex: 0 0 0;
    align-self: stretch;
    padding-left: 15px;
}

Why don't you try like this above code? Remove flex: 0 0 15px to flex: 0 0 0 and then add padding-left:15px;

Karuppiah RK
  • 3,894
  • 9
  • 40
  • 80
0

use whitespace: nowrap; to unwrap in same line

.child-container {
    display: flex;
    background: green;
    white-space: nowrap;
}
Santhosh Kumar
  • 543
  • 8
  • 22
0

What you is your goal with this, how should it look like? If you put the span within another div with flex display and put padding on it for example, the text wont wrap.

HTML

<div class="parent-container">
  <div class="child-container">
    <div class="color-block">
      <span>This is a long long long long long text.</span>
    </div>
  </div>
</div>

CSS

.parent-container {
  display: flex;
  background: blue;
}

.child-container {
  display: flex;
  padding-left: 20px;
  background: green;
}

.color-block {
  background: yellow;
  padding: 5px 10px;
  align-self: stretch;
  display: flex;
}

Also made a nicer version using ::before pseudo element, check it out here: https://jsfiddle.net/de6n1sr7/

Kata
  • 142
  • 1
  • 7
  • Sorry for not explaining! I just want a color block label in front of the text to indicate the state of the item like "it is done!". – crupest Feb 23 '19 at 09:49
  • Oh okay! I changed css a bit, now check it out. The child container only need a left padding, the 'status' will be indicated then. – Kata Feb 23 '19 at 09:54
  • Despite the strange question I mentioned, your solution is truly good! Really thanks for helping. – crupest Feb 23 '19 at 09:59
  • For what you want to do, I can strongly suggest using the ::before pseudo element, I find it more elegant than stacking divs. I added a fiddle showing this. – Kata Feb 23 '19 at 10:16
  • Yeah! I'm not quite familar with those pseudo element. It truly seems very elegant. I will learn them later! Thanks for coming up with this idea! – crupest Feb 23 '19 at 17:28