Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS prevent div flex from stretching child

Tags:

html

css

flexbox

The moment div has the flex display property it stretches the paragraph. I seem to be missing something but no property I can think to put on the flex div changes this. How can I prevent this behavior? (without the flex property I get the result on the right in the image)

enter image description here

div {
  display: flex;
  justify-content: center;
  flex-direction: column;
  height: 200px;
  width: 100px;
}

p {
  background-color: green;
  display: inline;
}
<div>
  <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
  </p>
</div>
like image 991
K41F4r Avatar asked Oct 23 '25 16:10

K41F4r


1 Answers

add another wrapper:

div {
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align:center;
  height: 200px;
  width: 100px;
}

p span{
  background-color: green;
}
<div>
  <p>
    <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod</span>
  </p>
</div>

Related question to understand why your inline is ignored:

Are flex items a block level element?

Usage of display property of flex box items

like image 177
2 revsTemani Afif Avatar answered Oct 26 '25 07:10

2 revsTemani Afif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!