Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-sizing width with fit-content in css

Tags:

html

css

I'm trying to prevent a parent division to be smaller than it's children using min-width and fit-content.

I first setup a division .parent with a min-width: fit-content. I, then added, a child with width: 100px and min-width: fit-content. Finaly, I added enough characters to the children to bust the 100px;

.parent {
  border: 1px solid red;
  width: fit-content;
}

.children {
  border: 1px solid green;
  min-width: fit-content;
  width: 100px;
}
<div class="parent">
  <div class="children">
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  </div>
</div>

DEMO: https://codepen.io/osasseville/pen/NadrgB?editors=1100

I would expect the parent to fit the content of the children, which fit the content of the characters.

Strangely, if I change the children's width to 1%, the min-width is respected.

like image 290
Olivier Sasseville Avatar asked Sep 25 '17 17:09

Olivier Sasseville


People also ask

How do I set width to fit-content?

Using inline-block property: Use display: inline-block property to set a div size according to its content.

How do I set auto width in CSS?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

What is width fit-content CSS?

The fit-content behaves as fit-content(stretch) . In practice this means that the box will use the available space, but never more than max-content . When used as laid out box size for width , height , min-width , min-height , max-width and max-height the maximum and minimum sizes refer to the content size.

Can width be auto in CSS?

Width: autoThe initial width of block-level elements like <div> or <p> is auto , which makes them take the full horizontal space of their containing block. When an element has auto as a value for width, it can have margin, padding, and border without becoming bigger than its parent element.


2 Answers

The code is working as it should. The parent div will only stretch its width up to the contents inside it, i.e the child div.

And here you have defined the width of child div as 100px, so the parent div width will also expand only till 100px, and about the text, which is overflowing outside child div, is not considered as content for parent div.

If you want the parent to fit the content of the children, you should change the width of child div to fit-content

like this

.parent {
  border: 1px solid red;
  width: fit-content;
}

.children {
  border: 1px solid green;
  width: fit-content;
}
<div class="parent">
  <div class="children">
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  </div>
</div>
like image 164
Anupam Raut Avatar answered Nov 12 '22 19:11

Anupam Raut


Ok, so I am not sure I totaly understand what you want to do but we can start from this answer. I dont't know how much you know about CSS so dont't be offended by my answer.

  • First, in HTML most elements have, by default, two types of rendering (this is really simplified) : block-level or inline. A block-level element will take the width of its parent. An inline element will take the width of its content.

  • So if you understand that principle you'll see that having the parent element to be as wide as its children which is as wide as its content is pretty simple. Here is an exemple:

.parent {
   border: 1px solid red;
   /* This will make the parent as wide as its content */
   display: inline-block;
}

.children {
   border: 1px solid green;
   /* This is just so that we see if it's working */
   max-width: 100px;
}
<div class="parent">
  <div class="children">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
</div>

Now, of course there are other ways to do it, but this is the simplest solution. The best solution will depend on your context.

like image 24
Pipo Avatar answered Nov 12 '22 19:11

Pipo