Square div with fixed height using only CSS - html

Everything is in the title !
How can I do to make my div's width equals to its fixed height using CSS only ?
HTML Code
<div class="container">
<div class="square"></div>
</div>
CSS Code
.square{
height:80%;
}
I know I can do the opposite (fixed width) using
.square{
width : 20%;
padding-top:20%
}
because padding-top is relative the width of the container.
I also know I can do it using simple JQuery but don't want to use javascript.
Thanks,

Use vw unit :
.square {
background: #000;
width: 50vw;
height: 50vw;
}
<div class="square"></div>

You need to use Viewport-percentage lengths vw
.square {
background: #000;
width: 20vw;
height: 20vw;
}
<div class="square"></div>

It seems like you want your div to be inside a .container which can be any size. This is easy if the size of that .container is based on the viewport width. I've created an example, below, which shows a block of square divs 2x2 inside of a .container. The .container can be any size. It is important the height and width of the container be based on viewport width.
.container {
width: 90vw;
height: 90vw;
margin: 0 5vw;
}
.container > div {
width: 48%;
height: 48%;
margin: 1%;
display: inline-block;
}
.square1 {
background: #aaa;
}
.square2 {
background: #bbb;
}
.square3 {
background: #ccc;
}
.square4 {
background: #ddd;
}
<div class="container">
<div class="square1"></div><div class="square2"></div><div class="square3"></div><div class="square4"></div>
</div>

If you don't want to use vw, try this out.
https://jsfiddle.net/zofoeLyL/2/
CSS
.square{
width:20%;
position:relative;
background-color:#005f95;
}
.square:before {
display: block;
padding-top: 100%;
}
HTML
<div class="square">
</div>
That will make the height always equal to whatever width % you decide to use.

Related

Calculating margin-top value to occupy remaining height in fluid design

Basically, the container has both fluid height and width. Kindly consider the following:
<div class="container">
<span class="header">Hello!</span>
<div class="box"></div>
<div class="box"></div>
</div>
Both of the box divs take say 40% height each, assume I apply 15% height for the span element and the remaining 5% for its margin-top value. As expected it will not sum up to 100% of the container height since
margin-top is calculated based on the width as far as I know. How do I calculate the margin-top percentage value in this case so all elements including the margin would sum up to the full height of the container? here's my CSS Code, thanks in advance.
.container {
height: 80%;
width: 80%;
}
.header {
height: 15%;
display: inline-block;
margin-top: 5%; /*how to calculate this correctly*/
}
.box {
height: 40%;
}
I think you can easily obtain what you want by using flexbox and margin-top:auto on header:
body,html {
height:100%;
margin:0;
}
.container {
height: 80%;
width: 80%;
display:flex;
flex-direction:column;
border:1px solid;
box-sizing:border-box;
}
.header {
flex:0 0 15%;
background:red;
align-self: flex-start; /* To have same visual behavior as inline-block */
margin-top:auto /* this will do the trick*/
}
.box {
flex:0 0 40%;
background:yellow;
}
<div class="container">
<span class="header">Hello!</span>
<div class="box">a</div>
<div class="box">b</div>
</div>
I prefer flexbox solution by #Temani, but in case you need to support old version of browsers like IE that does not support flexbox.
You can just add an empty span before header or use pseudo css element and give it a 5% height, this will give you the same margin top effect you need even for old browsers.
html,
body{
height:100%;
}
.container {
display:inline-block;
height: 80%;
width: 80%;
background-color:red;
}
.container:before {
content:" ";
display:inline-block;
width:100%;
height:5%;
background-color:red
}
.header {
height: 15%;
width:100%;
display: inline-block;
background-color:blue
}
.box {
display:inline-block;
width:100%;
height: 40%;
background-color:green
}
<div class="container">
<span class="header">Hello!</span>
<div class="box">1</div>
<div class="box">2</div>
</div>

Fit multiple images to parent height while maintaining its aspect ratio

How do I make these 3 images fit to its parent div height while maintaining the image's aspect ratio?
<div id="myM">
<div class="ab">
<img src="http://img42.com/2lWNS+" class="cd"/>
<img src="http://img42.com/2lWNS+" class="cd"/>
<img src="http://img42.com/2lWNS+" class="cd"/>
</div>
</div>
css:
#myM{
width: 300px;
height: 200px;
background-color: cyan;
}
.ab{
width: 100%;
float: right;
}
.cd{
max-height:33%;
width:auto;
}
Here is a Fiddle
I think the problem is with the float removing the container from the flow. Instead, you can make the container an inline-block and use right-align.
https://jsfiddle.net/9uyww2j0/2/
Without changing your HTML, my new CSS is this:
#myM{
width: 300px;
height: 200px;
background-color: cyan;
text-align: right;
}
.ab {
display: inline-block; /* So text-align affects it */
height: 100%
}
.cd{
display: block; /* So takes full width */
height:33%;
}

How to position a div with equal margins for left, right, and top

I would like to achieve a layout that looks like this:
I am interested in a css/html only solution, so no javascript required.
The widths of both divs are dynamic, so I cannot use any static margins.
The spacing between the sides of the divs, and the top, should be the same.
I tried using margin: auto auto 0 auto on the inner div, as you can see in this jsfiddle, but it only works for left and right.
Note, the following attempt doesn't answer the question fully, since the width of the child cannot be dynamic.
The idea is to use a percentage width + percentage margin-top values on the child. It's a responsive layout, see the comments in the code, and try it out on different window sizes.
JSFiddle: http://jsfiddle.net/jkoycs6e/
body {
margin: 0;
}
.outer {
height: 100vh; /*for demo only*/
background: teal;
overflow: auto;
}
.inner {
width: 80%;
background: gold;
margin: auto;
margin-top: 10%; /* 100%-80%/2 */
}
<div class="outer">
<div class="inner">
hello<br/>hello<br/>hello
</div>
</div>
This is not possible. At least not without using javascript. There is no css-only solution.
If you put align="center" in your div you'll get to the middle of the screen every time but it's not going to be supported in HTML5 so I recommend the 50:50 approach.
div
{
text-align:center;
margin-top:50%;
margin-bottom:50%;
}
Hope that helps. ^^
Set the outer parent's overflow to auto and give your margin-top a relative value. Something like this:
.outer {
background: blue;
overflow: auto;
}
.inner {
background:yellow;
width: 100px;
height: 100px;
margin: 1em auto 0 auto;
}
<div class="outer">
<div class="inner">
</div>
</div>
This seems to work:
.outer {
height: 500px;
width: 300px;
background: blue;
position: relative;
}
.inner {
width: 80%;
height: 200px;
background:green;
position: absolute;
margin-left: 10%;
margin-right: 10%;
margin-top: 10%;
margin-bottom: 0;
}
You can change the percentages marked for the margins as per your intended value for k.
Here's the fiddle
EDIT: Note that the width of inner has to be set in terms of percentages for this to work. Also note that when a margin is specified in terms of percentage, the margin's value is computed as a percentage of the width of the container. Even for the vertical margins, the percentage is applied on the width (and NOT the height) of the container.
Here's an SO post that's helpful in understanding how to position elements with respect to their container.
This answer doesn't actually make use of the margin property, nor does it have only two div.
body {
font-size: 26px;
text-align: center;
font-family: monospace;
}
#container {
display: inline-block;
position: relative;
width: 100%;
}
#dummy {
margin-top: 20%;
}
#element {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: silver
/* show me! */
}
#wrapper {
display: table;
width: 100%;
}
#row {
display: table-header-group;
}
#left {
display: table-cell;
background-color: chartreuse;
width: 20%;
}
#incenter {
display: table-cell;
background-color: aqua;
}
#right {
display: table-cell;
background-color: chartreuse;
width: 20%;
}
<div>
<div id="container">
<div id="dummy"></div>
<div id="element">
k (20%)
</div>
</div>
<div id="wrapper">
<div id="row">
<div id="left">width = k (20%)</div>
<div id="incenter">incenter</div>
<div id="right">width = k (20%)</div>
</div>
</div>
</div>
Another example with measurements in pixels is here.
For explanation refer:
https://stackoverflow.com/a/12121309/2534513
https://stackoverflow.com/a/6615994/2534513
I have actually combined techniques mentioned in above two answers to make this one.
Using JavaScript would have been a lot easier.

Centering divs in CSS - how does this work?

I can't understand how this works and why it centers. Why is it width: 70%; and not 50% / 25%?
I cannot get my head around this. I'm just trying to learn the basics on web dev. I'm completely stumpted.
body,
html {
margin: 0;
padding: 0;
}
.wrapper {
margin: 0 auto;
width: 70%;
}
.top-header {
background-color: yellow;
width: 100%;
height: 100px;
}
.main-content {
background-color: red;
width: 100%;
height: 25px;
}
<div class="wrapper">
<div class="top-header">
</div>
<div class="main-content">
</div>
<div class="bottom-footer">
</div>
</div>
width: 70% is just an option, it very well can be 50% or 25%. It doesn't matter. What matters is having a width when you use margin: auto. Setting the margin to auto will auto-calculate the distant of the element from its container and adjust the element in the center accordingly
setting elements to the centre in CSS, like div here we use the attribute margin: auto;
the width only ensures that it doesn't take the whole page and stretching outside of it's container.
you can also specify the margin values explicitly.
i'd also recommend using max-width instead as it will improve the resizing on your site when you want to use it on mobile.
another thing, if you want to centre text inside the div you use the text-align: center;
Why is it width: 70%; and not 50% / 25%?
This is purely the preference of the author. It could be any width, or max-width.
The important thing is that it is not 100% or auto which would cause the element to fill the entire width and make the idea of centring pointless.
div {
height: 1em;
border: 1px solid black;
background: #aaa;
margin: 1ex auto;
width: 70%;
}
div+div {
width: 50%;
}
div+div+div {
width: 25%;
}
div+div+div+div {
width: 100px;
}
div+div+div+div+div {
max-width: 80ex;
}
div+div+div+div+div {
max-width: 20px;
}
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>

How to horizontally center a larger div of variable width, within a smaller div of variable width

I want to achieve the following effect: http://jsfiddle.net/3KJta/1/
However the solution I have uses a known width for the small div and the larger div. I need this to work with variable sized divs. The use case for this is a tooltip that appears above a smaller flexible sized element. The tooltip content isn't known and so the width could be anything.
So far I have:
<div class="small">
<div class="smaller"></div>
<div class="larger"></div>
</div>
and
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
div {
border: 2px solid black;
}
.small {
width: 50px;
height: 50px;
position: absolute;
left: 200px;
top: 50px;
text-align: center;
}
.smaller {
width: 20px;
height: 20px;
border-color: red;
display: inline-block;
}
.larger {
width: 200px;
height: 200px;
border-color: blue;
display: inline-block;
margin-left: -75px /* NOTE: in reality, .small has a variable width, and so does .larger, so i can't just take off this fixed margin */
}
If you are ok with using css3 and only support modern browsers you can use transform: translateX(-50%); to center the bigger box (currently supported browsers).
See this example: http://jsfiddle.net/2SQ4S/1/
If you use and extra element you can do it:
<div class="small">
<div class="smaller"></div>
<div class="larger">
<div>I'm extra</div>
</div>
</div>
CSS
.larger {
position:relative;
left:50%;
width:8000%;
margin-left:-4000%;
text-align:center;
border:none;
}
.larger div {
width: 200px;
height: 200px;
border-color: blue;
margin:auto;
}
http://jsfiddle.net/3KJta/4/
although that does cause some issues with content being wider than the page so you would need it all in a container with overflow:hidden:
http://jsfiddle.net/3KJta/7/
All a bit ugly though. Perhaps there's a solution where you can avoid doing this. Maybe a JS solution that measures the size of the content you're trying to show and offsets it.