I want my div to adapt its height to always equal its width. The width is percental. When the parent's width decreases, the box should decrease by keeping its aspect ratio.
How to do this is CSS?
I want my div to adapt its height to always equal its width. The width is percental. When the parent's width decreases, the box should decrease by keeping its aspect ratio.
How to do this is CSS?
Works on almost all browsers.
You can try giving padding-bottom
as a percentage.
<div style="height:0;width:20%;padding-bottom:20%;background-color:red">
<div>
Content goes here
</div>
</div>
The outer div is making a square and inner div contains the content. This solution worked for me many times.
Here's a jsfiddle
box-sizing: content-box;
in case you add border on the entity. Else it can distort the 1:1 ratio. (happened when adding fat border-radius) –
Eichman position: absolute;
on the inner div
to make it work. Elements with absolute positioning are ignored when the parent dimensions are calculated. That means you can add content, but also padding, borders, etc to the inner element. :) See this fiddle. –
Penchant To achieve what you are looking for you can use the viewport-percentage length vw
.
Here is a quick example I made on jsfiddle.
HTML:
<div class="square">
<h1>This is a Square</h1>
</div>
CSS:
.square {
background: #000;
width: 50vw;
height: 50vw;
}
.square h1 {
color: #fff;
}
I am sure there are many other ways to do this but this way seemed the best to me.
vw
: caniuse.com/#search=vw –
Abhorrent flex
. –
Rusell HTML
<div class='square-box'>
<div class='square-content'>
<h3>test</h3>
</div>
</div>
CSS
.square-box{
position: relative;
width: 50%;
overflow: hidden;
background: #4679BD;
}
.square-box:before{
content: "";
display: block;
padding-top: 100%;
}
.square-content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
color: white;
text-align: center;
}
max-width
–
Fresnel It is as easy as specifying a padding bottom the same size as the width in percent. So if you have a width of 50%, just use this example below
id or class{
width: 50%;
padding-bottom: 50%;
}
Here is a jsfiddle http://jsfiddle.net/kJL3u/2/
Edited version with responsive text: http://jsfiddle.net/kJL3u/394
overflow:hidden
or other absolute positioning of children inside the container. Conveniently exactly like @gidzior's answer –
Aretta Another way is to use a transparent 1x1.png with width: 100%
, height: auto
in a div
and absolutely positioned content within it:
html:
<div>
<img src="1x1px.png">
<h1>FOO</h1>
</div>
css:
div {
position: relative;
width: 50%;
}
img {
width: 100%;
height: auto;
}
h1 {
position: absolute;
top: 10px;
left: 10px;
}
<svg viewBox='0 0 1 1'></svg>
–
Wilkey This is what I came up with. Here is a fiddle.
First, I need three wrapper elements for both a square shape and centered text.
<div><div><div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat
volutpat.</div></div></div>
This is the stylecheet. It makes use of two techniques, one for square shapes and one for centered text.
body > div {
position:relative;
height:0;
width:50%; padding-bottom:50%;
}
body > div > div {
position:absolute; top:0;
height:100%; width:100%;
display:table;
border:1px solid #000;
margin:1em;
}
body > div > div > div{
display:table-cell;
vertical-align:middle; text-align:center;
padding:1em;
}
display:table-cell
is not supported in IE7 or older. –
Jazminejazz © 2022 - 2024 — McMap. All rights reserved.