Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a flexbox parent have the width of its children?

Tags:

css

flexbox

I have a parent element, with two elements inside:

<div class="parent">
  <div class="child">
     one
  </div>
  <div class="child">
    two
  </div>
</div>

Currently, the .parent is 100% wide. I would like it only to be the width of the two children.

.parent {
  display: flex;
  background-color: aliceblue;
}

How can I make the parent to be the width of it's contents, and not wider?

See codepen for a live demo.

like image 459
mikemaccana Avatar asked Nov 15 '15 16:11

mikemaccana


People also ask

How do you make a div full width of a parent?

Method 2: We can make the display attribute of the child container to table-row and display attribute of parent container to table, that will take all the height available from the parent div element. To cover all the width, we can make the width of parent div to 100%.

How do I give my flexbox a width?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.

How do you make flexbox children 100% Height of their parents using CSS?

Getting the child of a flex-item to fill height 100%Set position: relative; on the parent of the child. Set position: absolute; on the child. You can then set width/height as required (100% in my sample).

How do you make a child div element wider than the parent div?

It CAN have a wider width by giving both elements explicit widths, obv with the inner element being wider, and the parent element using overflow: visible; so that the child element's content can be seen. You could also give the child element a negative margin to bump it outside of its container's bounds.


1 Answers

Change the parent's display from flex to inline-flex.

codepen demo

like image 197
j08691 Avatar answered Sep 19 '22 17:09

j08691