i am trying to set 3 divs inside the parent div, the parent div will resize after the biggest height of the child divs but i also want the other 2 child divs to take the same size as the biggest one in height.
<div id="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
the parent div will be 600px width and no height given so it will expand depending of the child div biggest height.
#parent {
width: 600px;
overflow: auto;
}
.child {
width: 200px;
float: left;
}
This is what i got so far but if the child divs have different heights, the parent div height is the biggest of the child div heights, but the other 2 child divs dont take the height of the parent ( of the biggest child div in height).
Thank you in advance,
Daniel!
This: http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks and this: http://buildinternet.com/2009/07/four-methods-to-create-equal-height-columns/ may help.
One way would be to use the new feature from CSS display: grid
#parent {
display: grid;
width: 100%;
grid-template-columns: auto auto auto;
/* Just for styling */
text-align: center;
}
.child {
/* Just for styling */
box-sizing: border-box;
background-color: lightyellow;
border: 2px dashed red;
padding: 20px;
}
<div id="parent">
<div class="child">This is first</div>
<div class="child">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat dolor ante, eu ultricies eros molestie nec. Fusce laoreet laoreet nibh, vel vehicula nunc varius ut</div>
<div class="child">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat dolor ante, eu ultricies eros molestie nec. Fusce laoreet laoreet nibh, vel vehicula nunc varius utLorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat dolor ante, eu ultricies eros molestie nec. Fusce laoreet laoreet nibh, vel vehicula nunc varius ut</div>
</div>
Or another one would be the display: flex.
#parent {
display: flex;
flex-flow: row nowrap;
}
.child {
width: 33.3333%; /* Force the column to be a third of the parent width */
/* Just for styling */
box-sizing: border-box;
background-color: lightyellow;
border: 2px dashed red;
text-align: center;
padding: 20px;
}
<div id="parent">
<div class="child">The first one</div>
<div class="child">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat dolor ante, eu ultricies eros molestie nec. Fusce laoreet laoreet nibh, vel vehicula nunc varius ut</div>
<div class="child">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat dolor ante, eu ultricies eros molestie nec. Fusce laoreet laoreet nibh, vel vehicula nunc varius utLorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse volutpat dolor ante, eu ultricies eros molestie nec. Fusce laoreet laoreet nibh, vel vehicula nunc varius ut</div>
</div>
Related
I am currently converting an old page away from using a <table> layout and have been trying to implement the behaviour using flexbox. The aim is to have 3 columns, the first two centred horizontally and vertically, with the last column containing a lot of text, which is displaying ok.
I am trying to use a media query to wrap the third column under the first two when the browser width is small as follows:
The code snippet below achieves the correct layout but will obviously fail on small widths and just produce a single column:
I am guessing this needs flex-wrap somehow?
Are multiple rows also possible with a single container row? Or is it best to have one container per row?
.row {
display: flex;
justify-content: center;
padding: 5px;
margin: 2px;
border: 1px solid #000;
font-size: 0.7em;
}
.col1, .col2, .col3 {
background-color: #999;
padding: 8px;
margin: 2px;
}
.col1, .col2 {
flex: 1;
/* Horizontal centring */
text-align: center;
/* Add vertical centring */
display: flex;
flex-direction: column;
justify-content:center;
}
.col3 {
flex: 3;
background-color:#fff;
}
#media(max-width: 400px){
.row {display: block;}
}
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
You can get rid of the row element with some tricks, specially if you know beforehand the width of the col1 and col2 elements. Notice the width of the col3 element is quite artificial:
.row {
display: flex;
flex-wrap: wrap;
width: 95%;
border: solid 1px red;
}
.col1,
.col2 {
background-color: lightblue;
padding: 10px;
background-clip: content-box;
}
.col3 {
flex-basis: calc(100% - 194px);
padding: 5px;
}
.col1,
.col2,
.col3 {
margin: 10px 0px;
}
.col1 {
border: solid 1px black;
border-right-width: 0px;
margin-left: 10px;
}
.col2 {
border: solid 1px black;
border-left-width: 0px;
border-right-width: 0px;
}
.col3 {
border: solid 1px black;
border-left-width: 0px;
}
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
Also, a solution for the normal layout, including 40% flex-basis for col1 and col2. They should be 50%, but since there are some margins and paddings, it's better to be on the low side and the the flex-grow compensate it
.row {
display: flex;
justify-content: center;
padding: 5px;
margin: 2px;
border: 1px solid #000;
font-size: 0.7em;
}
.col1,
.col2,
.col3 {
background-color: #999;
padding: 8px;
margin: 2px;
}
.col1,
.col2 {
flex: 1;
/* Horizontal centring */
text-align: center;
/* Add vertical centring */
display: flex;
flex-direction: column;
justify-content: center;
}
.col3 {
flex: 3;
background-color: #fff;
}
#media(max-width: 400px) {
.row {
flex-wrap: wrap;
}
.col1,
.col2 {
flex-basis: 40%;
}
.col3 {
flex-basis: 80%;
}
}
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
<div class="row">
<div class="col1">
Column 1
</div>
<div class="col2">
Column 2
</div>
<div class="col3">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse suscipit tortor vel orci pulvinar, eu euismod sem maximus. Mauris tempus sem eget massa tristique, ut maximus tortor volutpat. In efficitur.
</div>
</div>
You would give your .row definition flex-wrap: wrap; and set a min-width value for column 3. When it gets below that width it will drop down as you have indicated above.
I am experimenting a bit with calc on css to define some external space (like an external margin)
For example in this 3 column layout, the resulting width of the central column is a bit diferent than the other two, but I can not figure out how to make the text the same width.
There are some requirements I need.
I need to use only padding, not margin.
I can not add padding to the container C3 in this case.
I want to solve it using the logic under calc. I don't know if the percentages are applied first, or I need to define the paddings first to the computer later solve the percentages...
I prefer to use the border-box model, because it is solving me a lot of problems on other places of my code... but probably I can sacrifice this one.
body {
margin: 0;
padding 0;
}
* {
box-sizing: border-box;
}
p {
text-align: justify;
}
.C3 {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.C3>div {
width: 33.33%;
padding: 50px;
}
.C3>div:first-child {
background-color: #DFD;
width: calc(33.33% + 140px);
padding-left: 140px;
}
.C3>div:last-child {
background-color: #FEE;
width: calc(33.33% + 140px);
padding-right: 140px;
}
<section class="C3">
<div>
<p>1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros.Nunc est augue, varius sagittis aliquam a, mollis et sapien. In mollis adipiscing leo non bibendum.</p>
</div>
<div>
<p>2. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros.</p>
</div>
<div>
<p>3. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros. Nunc est augue, varius sagittis aliquam a, mollis et sapien. In mollis adipiscing leo non bibendum.</p>
</div>
</section>
Any ideas?
Since you're using box-sizing: border-box, the width already includes the padding. The 140px are being added twice.
width: 33.33% is all that's needed.
4castle answer made me realize that yes, I was repeating something, but i found it that was the 50px.
So in my margin I needed to substract 50px from the calc, and now I have 90px.
The diference is subtle but now I have the exact same column text size.
body {margin: 0; padding 0;}
*{box-sizing: border-box;}
p {
text-align: justify;}
.C3 {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;}
.C3>div {
width: 33.33%;
padding: 50px;}
.C3>div:first-child {
background-color: #DFD;
width: calc(33.33% + 90px);
padding-left: 140px;}
.C3>div:last-child {
background-color: #FEE;
width: calc(33.33% + 90px);
padding-right: 140px;}
<section class="C3">
<div>
<p>1. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros.Nunc est augue, varius sagittis aliquam a, mollis et sapien. In mollis adipiscing leo non bibendum.</p>
</div>
<div>
<p>2. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros.</p>
</div>
<div>
<p>3. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus et enim justo, vitae vulputate eros. Morbi nec ligula orci. Donec vel risus eros. Nunc est augue, varius sagittis aliquam a, mollis et sapien. In mollis adipiscing leo non bibendum.</p>
</div>
</section>
My HTML is similar to the following example
<div id="wrapper">
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
<div id="d">d</div>
</div>
On desktop I'd like the divs to display next to each other which is of course trivial.
On mobile I'd like have table-like layout with similar to the following
b, c and d have flexible height so a would have to adjust to that.
Is that possible to do without wrapping b,c and d in a separate div?
Yes, you can do this entirely with flexbox...of course you're going to need to decide on a width for the first div at smaller viewport sizes but I assume that you have that in mind already ready for the required media query.
#wrapper {
height: 100vh;
width: 90vw;
border: 1px solid grey;
display: flex;
flex-direction: column;
flex-wrap: wrap;
margin: 10px auto;
}
#wrapper div {
flex: 1 0 auto;
border: 1px solid white;
background: lightblue;
text-align: center;
}
#a {
flex-grow: 1;
height: 100%;
flex-basis: 50%;
}
#media screen and (min-width: 640px) {
#a {
height: auto;
flex-grow: none;
flex-basis: auto;
}
}
<div id="wrapper">
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
<div id="d">d</div>
</div>
Codepen Demo
Use only flexbox when it's needed: on desktop version:
* { box-sizing: border-box; }
body { margin: 0; }
#a { background: tomato; }
#b { background: forestgreen; }
#c { background: dodgerblue; }
#d { background: orange; }
#wrapper {
overflow: hidden;
position: relative;
}
#wrapper > div {
float: left;
width: 50%;
margin-left: 50%;
padding: 5px;
}
#wrapper > #a {
position: absolute;
height: 100%;
margin-left: 0;
}
#media all and (min-width: 768px) {
#wrapper {
display: flex;
}
#wrapper > div {
margin-left: 0;
}
#wrapper > #a {
position: relative;
height: auto;
}
<div id="wrapper">
<div id="a">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl.</div>
<div id="b">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper. Duis arcu massa, scelerisque vitae, consequat in, pretium a, enim. Pellentesque congue. Ut in risus volutpat libero pharetra tempor. Cras vestibulum bibendum augue. Praesent egestas leo in pede. Praesent blandit odio eu enim. Pellentesque sed dui ut augue blandit sodales. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam nibh.</div>
<div id="c">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius.</div>
<div id="d">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam. Maecenas ligula massa, varius a, semper congue, euismod non, mi. Proin porttitor, orci nec nonummy molestie, enim est eleifend mi, non fermentum diam nisl sit amet erat. Duis semper.</div>
</div>
I'm sure there's a simple solution to this, but I've been beating my head up against it for an hour or two and not making much progress.
Basically, I've got a wrapper div (testwrap_outer) containing a secondary wrapper div (testwrap_inner) that holds together an image thumbnail div (test1), and a caption div (test2).
I need the caption div (test2) to scale height according to its content, the secondary wrapper (testwrap_inner) to contain that div and float next to any other secondary wrapper divs, and the main wrapper (testwrap_outer) to contain all of them.
I apologize for doing such a poor job explaining, so I've provided a picture to illustrate what I mean here. Here is a fiddle.
HTML
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue mi at aliquet blandit. Praesent tristique, dui sit amet iaculis mollis, nunc elit semper nisi, vitae finibus nulla dui in enim. In lacinia aliquam tempus. Nunc sollicitudin ac massa non porttitor. Maecenas quam urna, semper ut mauris id, lacinia consequat libero. Vivamus neque diam, vestibulum a est eget, aliquam tempus magna. Morbi sed tellus lobortis, condimentum mi id, finibus felis.</p>
<div class=testwrap_outer>
<div class=testwrap_inner>
<!-- THUMBNAIL IMAGE -->
<div class=test1>
<img src="http://i.imgur.com/5KObDyq.jpg">
</div>
<!-- THUMBNAIL CAPTION -->
<div class=test2><b>TEST2</b>
<br>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue mi at aliquet blandit. Praesent tristique, dui sit amet iaculis mollis, nunc elit semper nisi, vitae finibus nulla dui in enim.</div>
</div>
<div class=testwrap_inner>
<!-- THUMBNAIL IMAGE -->
<div class=test1>
<img src="http://i.imgur.com/5KObDyq.jpg">
</div>
<!-- THUMBNAIL CAPTION -->
<div class=test2><b>TEST2</b>
<br>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue mi at aliquet blandit. Praesent tristique, dui sit amet iaculis mollis, nunc elit semper nisi, vitae finibus nulla dui in enim.</div>
</div>
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi congue mi at aliquet blandit. Praesent tristique, dui sit amet iaculis mollis, nunc elit semper nisi, vitae finibus nulla dui in enim. In lacinia aliquam tempus. Nunc sollicitudin ac massa non porttitor. Maecenas quam urna, semper ut mauris id, lacinia consequat libero. Vivamus neque diam, vestibulum a est eget, aliquam tempus magna. Morbi sed tellus lobortis, condimentum mi id, finibus felis.</p>
CSS
body {
background: #cccccc;
font-family: Arial, Helvetica, sans-serif;
min-width: 900px;
}
.testwrap_outer {
border: 1px solid red;
position: relative;
}
.testwrap_inner {
border: 1px solid blue;
float: left;
margin: 5px;
padding: 4px;
width: 296px;
}
.test1 {
border: 1px solid purple;
position: relative;
float: left;
opacity: 1.0;
width: 80px;
height: 80px;
overflow: hidden
}
.test1 img {
height: 100%;
}
.test1 img:hover {
opacity: 0.6;
}
.test2 {
border: 0px solid green;
position: relative;
float: left;
text-align: justify;
text-justify: inter-word;
padding: 6px;
width: 200px;
}
Any help is much appreciated.
Add inside testwrap_outer an empty div with clear:both style. This will tidy up the layout and make the outer div behave as a container supposed to be behave.
Updated fiddle:
http://jsfiddle.net/a3hz8dss/1/
just include overflow:hidden in your class testwrap_outer, also there is no need of position:relative in your code!!
CSS:
.testwrap_outer{
border: 1px solid red;
overflow:hidden;
}
Fiddle Demo
I'm building a page layout with 3 divs: a header and a footer with fixed heights in pixels, and a content div in the middle of the page that should fill the remaining screen height. Furthermore, I want to be able to set height to 100% in the inner content divs, because one of them will host a kind of drawing area that need to fill the remaining screen height. So, it's especially important that inner divs do not leak under the header or footer. So far, I achieved a 100% valid CSS solution that work in all majors browsers except Internet Explorer 6 & 7.
Is there anything I can do to fix my layout for IE6 & 7? Or, do you see another way to do what I want?
Here is the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The #content div fill the remaining height and appears to have a height</title>
<style TYPE="text/css">
<!--
* {
margin: 0;
padding: 0;
}
html, body,
#container{
height: 100%;
}
#container{
position: relative;
}
#header,
#footer{
height: 60px;
line-height: 60px;
background: #ccc;
text-align: center;
width : 100%;
position: absolute;
}
#header{
top: 0;
}
#footer{
bottom: 0;
}
#content{
position: absolute;
top:60px;
bottom: 60px;
width : 100%;
overflow: auto;
border-top: 1px solid #888;
border-bottom: 1px solid #888;
}
#inner-content{
overflow: auto;
background-color: #FC0;
height: 100%;
}
p{
margin-bottom: 10px;
}
-->
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>Header</h1>
</div>
<div id="content">
<div id='inner-content'>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis turpis vel
quam dictum hendrerit eu non elit. Donec ultricies ullamcorper libero a molestie.
Donec auctor nulla vitae tortor ullamcorper posuere. Etiam fringilla tristique blandit.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis turpis vel
quam dictum hendrerit eu non elit. Donec ultricies ullamcorper libero a molestie.
Donec auctor nulla vitae tortor ullamcorper posuere. Etiam fringilla tristique blandit.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis turpis vel
quam dictum hendrerit eu non elit. Donec ultricies ullamcorper libero a molestie.
Donec auctor nulla vitae tortor ullamcorper posuere. Etiam fringilla tristique blandit.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis turpis vel
quam dictum hendrerit eu non elit. Donec ultricies ullamcorper libero a molestie.
Donec auctor nulla vitae tortor ullamcorper posuere. Etiam fringilla tristique blandit.
</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis turpis vel
quam dictum hendrerit eu non elit. Donec ultricies ullamcorper libero a molestie.
Donec auctor nulla vitae tortor ullamcorper posuere. Etiam fringilla tristique blandit.
</p>
</div>
</div>
<div id="footer">
<h1>Footer</h1>
</div>
</div>
</body>
</html>
Thanks in advance for your help.
Live example here.
I ended up using Javascript to achieve the same thing