A
A
andead2019-11-12 16:40:47
css
andead, 2019-11-12 16:40:47

How to make an element the same width as the content in flexbox?

There is a menu on flex:

<ul>
  <li>Itemitemitemitemitem itemitemitemitemitem 1</li>
  <li>Itemitem 2</li>
  <li>Itemitem 3</li>
  <li>Itemitem 4</li>
  <li>Itemitem 5</li>
</ul>

ul {
  display: flex;
  margin: 0;
  padding: 0;
  list-style: none;
}
li {
  float: left;
  margin: 2px;
  padding: 5px 10px;
  background: #E1E1E1;
}

https://jsfiddle.net/xandeadx/fLj9s3ug/14/
When the elements do not have enough width, the text in them is wrapped on a new line and the following picture is obtained: The
5dcab5aa5879a505276687.png
question is how to make long elements correctly calculate the width (without prescribing the width of individual elements) :
5dcab5ddb67b3955593558.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ice, 2019-11-12
@IceRD

I will suggest adding for li flex:1
https://jsfiddle.net/ucsox3m8/

I
Igor, 2019-11-12
@Igor_307

if i understand correctly,

li:first-child{
    white-space: nowrap;
}

https://jsfiddle.net/kbag0j9h/

N
Natalia, 2019-11-12
Ivanova @Natalia_ai

Look here https://codepen.io/Natalia_ai_ivanova/pen/JjjaRjg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question