Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div Square, width size based on 100% height

I'm trying to make a responsive square with the width size based on the (100%) height of the element. I believe it's impossible using only CSS.

The square width should be equal to the height (100% of the large container. The large container is more than 100% of the screen). The ratio has to be width=height to keep the square.

like image 657
Sebastien-Gath Avatar asked Dec 20 '13 16:12

Sebastien-Gath


People also ask

How do you give a height 100% to a div?

Using 100vh instead means that the p tag will be 100% height of the body regardless of the div height.

Why is my div width 100%?

div is block element. Block elements are 100% width of parent element, if width is not specified. Show activity on this post. it's taking up all the available space based on it's parent container, exactly what it's supposed to do.

How do you make a div 100 width?

What you could do is set your div to be position: absolute so your div is independent of the rest of the layout. Then say width: 100% to have it fill the screen width. Now just use margin-left: 30px (or whatever px you need) and you should be done.


4 Answers

You could do this with a tiny inline image. No JS, no extra files.

.container {
  height: 150px;
  width: 100%;
  text-align: center;
  background: #acd;
}
    	
.square {
  display: inline-block;
  height: 100%;
  background: #691;
}
<div class="container">
  <div class="square">
    <img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" height="100%">
  </div>
</div>
like image 153
spaceman Avatar answered Sep 16 '22 14:09

spaceman


For a CSS-only solution (where you're sizing relative to the screen size), use viewport units. For example:

@media screen and (orientation:landscape) {
    .box{
        height: 100vh;
        width: 100vh;
    }
}
@media screen and (orientation:portrait) {
    .box{
        height: 100vw;
        width: 100vw;
    }
}

(You may want to reduce it to 98 units to eliminate scrolling)

Works great for divs that need to take up a precise proportion of screen space.

JSFiddle here.

like image 20
Escher Avatar answered Sep 16 '22 14:09

Escher


I think this can be a good 'css only' solution for you. Cross browser working.

http://absolide.tumblr.com/post/7317210512/full-css-fluid-squares

Good to highlight this nice css rule:

If the vertical paddings (and margins) are specified in percent (%) values the size is a percent of the width of the containing element.

like image 33
Stefano Ortisi Avatar answered Sep 18 '22 14:09

Stefano Ortisi


Take a look... at the aspect-ratio property.

This property makes creating a square div based on height, in the easiest method possible. Here's some example code:

h2 {
  font-family: calibri;
}

#parent {
  height: 96px;
  width: 256px;
  background: grey;
  margin-bottom: 16px;
}

#child {
  height: 80px;
  aspect-ratio: 1 / 1;
  background: lightgrey;
}

#anotherParent {
  height: 96px;
  width: 256px;
  background: grey;
}

#anotherChild {
  height: 50%;
  aspect-ratio: 1 / 1;
  background: lightgrey;
}
<h2>Absolute height (80px/96px)</h2>
<div id="parent">
  <div id="child">
  </div>
</div>

<h2>Relative height (50%)</h2>
<div id="anotherParent">
  <div id="anotherChild">
  </div>
</div>

Here are a couple of links to help you understand the aspect-ratio property:

  • https://css-tricks.com/almanac/properties/a/aspect-ratio/
  • https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio
  • https://support.squarespace.com/hc/en-us/articles/115008538927
like image 39
Tigerrrrr Avatar answered Sep 20 '22 14:09

Tigerrrrr