Can't fixed it...
I have a div with 2 divs inside. The first one determinates height and the second one takes that height. But chrome don't want to do it.
<div style="display: table; height: 100%; width: 100%; min-height: 100%;">
<div FIRST-DIV-HEIGHT-VARIABLE WITH FLOAT LEFT></div>
<div style="box-sizing: border-box; display: table; float: right;
height: auto; min-height: 100%; padding: 0 20px 81px; position: relative;
width: 280px; -moz-box-sizing: border-box; -ms-box-sizing: border-box;
-webkit-box-sizing: border-box; -khtml-box-sizing: border-box;
-o-box-sizing: border-box;"></div>
</div>
Flexboxes are the easiest way to create columns of equal height:
<style>
.container {
display: flex;
display: -mx-flexbox; // IE10
display: -webkit-flex; // Safari
flex-grow: 0;
-ms-flex-grow: 0; // IE10
-webkit-flex-grow: 0; // Safari
}
.left, .right { width: 50%; }
.left {
background-color: #c66;
height: 200px;
}
.right { background-color: #66c; }
</style>
<div class="container">
<div class="left">
<p>This is the left DIV.</p>
</div>
<div class="right">
<p>This is the right DIV.</p>
<p>Its height is equal to the left DIV.</p>
</div>
</div>
Flexboxes do not work in IE9 and older. Suresh idea above works in older browsers, but there is a mistake in the code (table cells don't float) and inline CSS is better avoided:
<style>
.container { display: table; }
.row { display: table-row; }
.left, .right {
display: table-cell;
width: 50%;
}
.left {
background-color: #c66;
height: 200px;
}
.right { background-color: #66c; }
</style>
<div class="container">
<div class="row">
<div class="left">
<p>This is the left DIV.</p>
</div>
<div class="right">
<p>This is the right DIV.</p>
<p>Its height is equal to the left DIV.</p>
</div>
</div>
</div>
Add display:table-cell property for your child div. So that automatically adjust the height of sibling divs.
<div style="display:table; width:100%; height:100%; border:1px solid #f000ff;">
<div style="height:400px; display:table-cell; background-color:#ff000f;">My Main Content</div>
<div style="display:table-cell; float:right; height:100%; background-color:#ff00ff;">My Sample Content</div>
</div>
Check the Working JSFIDDLE HERE
In the above fiddle I have assigned the first child div height as 400px. If you modify this height, the right side div automatically adjust its height. Check it out and let me know if any issues.
Related
I'm trying to align div # alignBottom1 and # alignBottom2 down without removing the float left from the parent div and without using position absolute or margin top.
How can I do?
This is my code:
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>
Thanks for any help!
You can do this easily if you turn the parent container into a flexbox.
In your sample, I gave the parent a height value so that you can see the effect of using flexbox and justifying the content to the end of it's parent.
#alignBottom {
display: flex;
flex-flow: column nowrap;
justify-content: flex-end;
height: 100px; /* giving the element a height to exaggerate the effect */
}
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div id="alignBottom" class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>
CSS flexbox helps aligning the content inside the container in multiple ways with few lines of code. This might work for you.
#TotContainer {
height: 900px;
}
#container {
max-height: 90%
}
.col-sm-6 {
width: 50%;
float: left;
height: 100%;
padding: 10px;
}
.col-sm-6:nth-child(2){
/* adding this */
display: flex;
flex-direction: column;
justify-content: flex-end;
/* adding some height to the container for better visibility od effect */
height: 80px;
}
.col-sm-12 {
width: 100%;
float: left;
background: yellow;
}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
<div id="TotContainer">
<div id="container">
<div class="col-sm-6" style="background:blue;">XXXXXX</div>
<div class="col-sm-6" style="background:red;">
<div id="alignBottom1">Text to align at the bottom 1</div>
<div id="alignBottom2">Text to align at the bottom 2</div>
</div>
<div class="col-sm-12">footer</div>
</div>
</div>
I'm trying to have one div, with a height of 100% in it's container (which has a height of 50%) and two divs next to that, which each have a height of 50%.
Here's an example of what I mean:
I would also like to have a margin between all the divs, as shown in the picture above.
Here's my code so far:
<div style="height: 50%;">
<div style="height: 100%; float: left; margin-right: 15px;">
<p>Content</p>
</div>
<div style="float: right; height: 50%;">
<p>Content</p>
</div>
<div style="float: right; height: 50%;">
<p>Content</p>
</div>
</div>
JsFiddle: https://jsfiddle.net/ne4njtvr/
Like this maybe?
Note, if you need to support older browsers, this can be done using display: table as well
html, body {
margin: 0;
height: 100%;
}
.wrapper {
display: flex;
height: 100%;
}
.wrapper .left,
.wrapper .right {
flex: 1;
display: flex;
flex-direction: column;
}
.wrapper .right div {
flex: 1;
}
.wrapper .right div ~ div {
flex: 2;
}
div {
border: 1px solid;
box-sizing: border-box;
}
<div class="wrapper">
<div class="left">
Left
</div>
<div class="right">
<div>
Right - Top
</div>
<div>
Right - Bottom
</div>
</div>
</div>
I have the following problem on a website i am building:
3 columns of equal height and 1/3 width but on the last column there is a small white gap on the right hand side. I cant figure out why, here is what I'm talking about:
enter image description here
See the white line by the right hand side of the blog image.
The code I'm am using for the 1/3rd column is:
.thirdBox {
float: left;
width: 33.33%;
width: calc(100% / 3);
padding: 20px 40px;
box-sizing: border-box;
min-height: 400px;
display: table;
}
and the background images:
.thirdBox:nth-of-type(3) {
background: url("imagelinkhere...") no-repeat 50% 50%;
background-size: cover;
}
The issue is that 100%/3 is 33.33% in most browsers, not quite the exact width you want it to be.
Instead of using calc() to find each table's width, I would use display:flex; on the parent of all three elements you want to be in one row.
This is the best I can help you with without any HTML structure. Please post that and I may be able to help you more.
.parentElement{
display:flex;
}
.firstBox, .secondBox, .thirdBox {
padding: 20px 40px;
flex:1;
}
.firstBox{
background:blue;
}
.secondBox{
background:red;
}
.thirdBox{
background:green;
}
<div class="parentElement">
<div class="firstBox"></div>
<div class="secondBox"></div>
<div class="thirdBox"></div>
</div>
Here is a solution using display: table:
<div class="row">
<div class="col">
<div class="col-inner">
<span>Menu</span>
</div>
</div>
<div class="col">
<div class="col-inner">
<p>Some text here</p>
</div>
</div>
<div class="col">
<div class="col-inner">
<span>Blog</span>
</div>
</div>
</div>
Css:
.row {
background-color: #999;
}
.row:after {
content: "";
display: table;
clear: both;
}
.col {
float: left;
width: 33.33%;
min-height: 400px;
}
.col-inner {
display: table;
width: 100%;
min-height: 400px;
box-sizing: border-box;
padding: 20px 40px;
}
.col:first-child,
.col:last-child {
background-color: yellow;
}
.col-inner span,
.col-inner p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
I'm having a bit of difficulty in displaying a table. I use display:table and display:table-cell a lot for sections usually. Especially when I just want to center the content of a section vertically. So to say, I have the following HTML:
<div class="wrapper">
<div class="cell">
<div class="red">
</div>
</div>
</div>
and the following css applied to the html:
html,body {
height: 100%;
}
.wrapper {
display: table;
width: 100%;
height: 100%;
}
.wrapper * {
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
}
.red {
margin-right: auto;
margin-left: auto;
background: red;
height: 200px;
width: 200px;
}
Now there is a small problem. I want to add a section header to this particular section and the section header has to be a child of .wrapper, so the HTML changes as below :
<div class="wrapper">
<div class="section-heading">
<h1>section heading</h1>
</div>
<div class="cell">
<div class="red">
</div>
</div>
</div>
Now the problem with using display table and table-cell is that when I add a header to the section, I can't add it without it affecting the other child elements of .wrapper . So how do I add a heading (when the heading is added in the above HTML the .cell div seems to be moving horizontally slightly)?
Of course I could use absolute positioning, but I was just wondering, is there something that can be done, without taking the heading element out of the flow?
FIDDLE HERE
Did you try adding display:table-row to the section-heading?
.wrapper > .section-heading{
display:table-row;
height:auto;
}
Fiddle: http://jsfiddle.net/7xo353hg/4/
You can make heading container display: table-row:
.section-heading {
display: table-row;
text-align: center;
}
Check the demo:
html, body {
height: 100%;
}
.wrapper {
display: table;
width: 100%;
height: 100%;
}
.wrapper * {
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
}
.red {
margin-right: auto;
margin-left: auto;
background: red;
height: 200px;
width: 200px;
}
.section-heading {
display: table-row;
text-align: center;
}
<div class="wrapper">
<div class="section-heading">
<h1>section heading</h1>
</div>
<div class="cell">
<div class="red"></div>
</div>
</div>
I want the Content A, Content B, and Content C columns to be horizontally centered. I have this code been trying to add
http://jsfiddle.net/hsX5q/24/
HTML:margin: 0 auto to .columns-container and it doesn't work. Could anyone help?
/*************************
* Sticky footer hack
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
************************/
/* Stretching all container's parents to full height */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
/* Setting the container to be a table with maximum width and height */
#container {
display: table;
height: 100%;
width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */
.section {
display: table-row;
height: 1px;
}
/* The last-but-one section should be stretched to automatic height */
.section.expand {
height: auto;
}
/*************************
* Full height columns
************************/
/* We need one extra container, setting it to full width */
.columns-container {
display: table-cell;
height: 100%;
width: 300px;
margin: 0 auto;
}
/* Creating columns */
.column {
/* The float:left won't work for Chrome for some reason, so inline-block */
display: inline-block;
/* for this to work, the .column elements should have NO SPACE BETWEEN THEM */
vertical-align: top;
height: 100%;
width: 100px;
}
/****************************************************************
* Just some coloring so that we're able to see height of columns
****************************************************************/
header {
background-color: yellow;
}
#a {
background-color: pink;
}
#b {
background-color: lightgreen;
}
#c {
background-color: lightblue;
}
footer {
background-color: purple;
}
<div id="container">
<header class="section">
foo
</header>
<div class="section expand">
<div class="columns-container">
<div class="column" id="a">
<p>Contents A</p>
</div>
<div class="column" id="b">
<p>Contents B</p>
</div>
<div class="column" id="c">
<p>Contents C</p>
</div>
</div>
</div>
<footer class="section">
bar
</footer>
</div>
If you add text-align: center to the declarations for .columns-container then they align centrally:
.columns-container {
display: table-cell;
height: 100%;
width:600px;
text-align: center;
}
/*************************
* Sticky footer hack
* Source: http://pixelsvsbytes.com/blog/2011/09/sticky-css-footers-the-flexible-way/
************************/
/* Stretching all container's parents to full height */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
/* Setting the container to be a table with maximum width and height */
#container {
display: table;
height: 100%;
width: 100%;
}
/* All sections (container's children) should be table rows with minimal height */
.section {
display: table-row;
height: 1px;
}
/* The last-but-one section should be stretched to automatic height */
.section.expand {
height: auto;
}
/*************************
* Full height columns
************************/
/* We need one extra container, setting it to full width */
.columns-container {
display: table-cell;
height: 100%;
width:600px;
text-align: center;
}
/* Creating columns */
.column {
/* The float:left won't work for Chrome for some reason, so inline-block */
display: inline-block;
/* for this to work, the .column elements should have NO SPACE BETWEEN THEM */
vertical-align: top;
height: 100%;
width: 100px;
}
/****************************************************************
* Just some coloring so that we're able to see height of columns
****************************************************************/
header {
background-color: yellow;
}
#a {
background-color: pink;
}
#b {
background-color: lightgreen;
}
#c {
background-color: lightblue;
}
footer {
background-color: purple;
}
<div id="container">
<header class="section">
foo
</header>
<div class="section expand">
<div class="columns-container">
<div class="column" id="a">
<p>Contents A</p>
</div>
<div class="column" id="b">
<p>Contents B</p>
</div>
<div class="column" id="c">
<p>Contents C</p>
</div>
</div>
</div>
<footer class="section">
bar
</footer>
</div>
This does, though, require that you reset the .column elements to text-align: left (assuming you want them left-aligned, obviously (JS Fiddle demo).
Sometimes you have things other than text inside a table cell that you'd like to be horizontally centered. In order to do this, first set up some css...
<style>
div.centered {
margin: auto;
width: 100%;
display: flex;
justify-content: center;
}
</style>
Then declare a div with class="centered" inside each table cell you want centered.
<td>
<div class="centered">
Anything: text, controls, etc... will be horizontally centered.
</div>
</td>
Short snippet for future visitors - how to center horizontal table-cell (+ vertically)
html, body {
width: 100%;
height: 100%;
}
.tab {
display: table;
width: 100%;
height: 100%;
}
.cell {
display: table-cell;
vertical-align: middle;
text-align: center; /* the key */
background-color: #EEEEEE;
}
.content {
display: inline-block; /* important !! */
width: 100px;
background-color: #00FF00;
}
<div class="tab">
<div class="cell">
<div class="content" id="a">
<p>Content</p>
</div>
</div>
</div>
Just add this class in your css
.column p{
text-align:center;
}