Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div as square in flexbox

Tags:

html

css

flexbox

I have a CSS problem that I don't know how to solve. The situation is as follows. I have a <div> which is a flex-item, and which stretches vertically so that it fills the available height of its flex-container. Depending on the size of the browser window this <div> is a rectangle, sometimes taller than wide, sometimes wider than tall.

Now I want to position another <div> inside of this rectangle, which shall be a square. It should be as large as possible with respect to the surrounding rectangle, but it must always be visible entirely.

The typical approach to create a square is to set the padding-bottom to a percent value (which is the same percent value as for width). This actually creates a square, but since in this approach the height always follows the width, it stretches the out rectangle if it's wider than tall.

How can I solve this issue, so that the square is always contained within the boundaries of the size of the rectangle that was calculated by flexbox, ideally without using JavaScript?

Update: Meanwhile, I solved it using JavaScript, but I'd still be interested in a pure CSS solution, if there is any.

like image 735
Golo Roden Avatar asked Sep 29 '17 08:09

Golo Roden


People also ask

How do you make a square in flexbox?

Making square tiles with flexbox is simple! Just add flex-wrap and width , and everything will magically work as together!

Why is flexbox not wrapping?

The initial value of the flex-wrap property is nowrap . This means that if you have a set of flex items that are too wide for their container, they will overflow it.

Does Z Index work with flexbox?

Example. Note: z-index only works on positioned elements (position: absolute, position: relative, position: fixed, or position: sticky) and flex items (elements that are direct children of display: flex elements).

What are the disadvantages of using flexbox?

Disadvantages. While flexbox is supported by most major browsers, it's still newer than the traditional box model. This means older browsers don't support it as well (some not at all). There are also more inconsistencies across different browsers.

What is CSS Flexbox layout module?

CSS Flexbox Layout Module. Before the Flexbox Layout module, there were four layout modes: Block, for sections in a webpage. Inline, for text. Table, for two-dimensional table data. Positioned, for explicit position of an element.

How do I use the Flexbox model?

The flexbox properties are supported in all modern browsers. To start using the Flexbox model, you need to first define a flex container. The element above represents a flex container (the blue area) with three flex items.

What is the Flex property in CSS?

The flex property is a shorthand property for the flex-grow, flex-shrink, and flex-basis properties. Example Make the third flex item not growable (0), not shrinkable (0), and with an initial length of 200 pixels:

How to add a space between two DIVS in a page?

All you gotta do is make the parent of those two div's (the left logo and the right menu items) a display flex and justify-content: space-between or justify-content: space-around like so


1 Answers

Not sure if this is what u you are looking for...

.flex {
  display: flex;
}
.item {
  width: 50%;
}

.square {
  width: 100%;
  background: red;
}

.square:after {
  content: "";
  display: block;
  padding-bottom: 100%;
}
<div class="flex">
  <div class="item">
    <div class="square">
      
    </div>
  </div>
  <div class="item">
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  </div>
</div>
like image 70
Mike Trinh Avatar answered Oct 22 '22 15:10

Mike Trinh