CSS - Align div to bottom respective his parent div - html

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>

Related

Recursive DIVs with borders: the base case

It's possible to obtain the following masterpiece of recursive positioning of DIVs
by using the following code.
html, body { width: 100%; height: 100%; margin: 0; }
.outer {
width: 100%; height: 100%;
display: flex; flex-direction: column;
}
.inner {
margin: 10px;
border: 10px solid gray; border-radius: 10px;
display: flex; flex-direction: column;
flex: 1;
}
<div class="outer">
<div class="inner">
<div class="inner">
<div class="inner">
</div>
</div>
</div>
</div>
But something is odd about it. My attempts to display a border for the outer box fail (and box-sizing: border-box; doesn't help).
Can you modify this code to use styling for just html, body as well as one-box, where one-box would be used for both the outer DIV as well as an arbitrary nesting of inner DIVs?
Or does HTML5/CSS3 really require this baroque treatment of the base case when recursively nesting DIVs?
My attempt:
html, body {
width: 100%; height: 100%; margin: 0;
box-sizing: border-box;
}
.one-box-to-rule-them {
width: 100%; height: 100%;
display: flex;
flex-direction: column;
flex: 1;
margin: 10px;
border: 10px solid gray; border-radius: 10px;
box-sizing: border-box;
}
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
</div>
</div>
</div>
</div>
fails. Adding a border to the outer box using a unified box makes the boxes go out of bounds.
Explanation
A teacher who solves the problem I was having is valuable. A teacher who (also) points out how my knowledge was misleading me is invaluable. There are at this moment four (nice and correct) answers, but the insight into what I was misunderstanding is provided in a comment made by Temani Afif. Let me illustrate it with a diagram.
My mistake was thinking that when one changes box-sizing from its default content-box and specifies instead box-sizing: border-box;, the box calculation includes all four, content, padding, border, and margin. As its names implies, box-sizing: border-box; includes in the calculation the border-box. It does not include the margin.
You can rely on flexbox and the default stretch alignment to do this without the need of specifying height or width or box-sizing:
body {
height: 100vh;
margin: 0;
display: flex;
}
.inner{
margin: 8px;
border: 8px solid gray;
border-radius: 10px;
display: flex;
flex-grow: 1;
}
<div class="inner">
<div class="inner">
<div class="inner">
<div class="inner">
<div class="inner">
</div>
</div>
</div>
</div>
</div>
Remove the width: 100% from all elements, replace margins with paddings, and remove the redundant flex rules:
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
}
.one-box-to-rule-them {
height: 100%;
padding: 10px;
border: 10px solid gray;
border-radius: 10px;
}
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
</div>
</div>
</div>
</div>
Your problematic rule is margin because no matter if you set your box-sizing to border-box, margin is never included in the dimensions of the element in the box-model, it is basically "outer-spacing". You also don't need the flexbox rules for what you want! But of course you need some kind of spacing for your desired effect, I therefore chose to create a pseudo-element that will receive the border, position it absolutely to fit the dimensions of the parent and give the parent a double-border-width padding to mimic the spacing you had before:
html, body {
width: 100%; height: 100%; margin: 0;
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
.one-box-to-rule-them {
height: 100%;
padding: 20px;
position: relative
}
.one-box-to-rule-them:before {
border: 10px solid gray; border-radius: 10px;
content: '';
position: absolute; top: 0; right: 0; bottom: 0; left: 0;
}
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
<div class="one-box-to-rule-them">
</div>
</div>
</div>
</div>
EDIT: #Ori Drori solution is cleaner, but uses the two-classes approach, mine uses just one but is more "ugly" CSS (in my opinion) =D
EDIT: #Temani Afif should be the accepted solution!
Is this what you want?
if I understand it correctly the the problem with width 100% only
Like this:
html, body { height: 100%; margin: 0; }
.outer {
height: 100%;
display: flex; flex-direction: column;
}
.inner {
margin: 10px;
border: 10px solid gray; border-radius: 10px;
display: flex; flex-direction: column;
flex: 1;
}
<div class="outer">
<div class="inner">
<div class="inner">
<div class="inner">
</div>
</div>
</div>
</div>

Make child containers inherit parent container height, if sibling is overflowing

I created an outer container, in which are at least two child containers. One of those 2 containers (inner container 1) has a content which make it overflow in its height. By that the outer container is growing as well.
The 2nd inner container is inheriting the height of the outer container, but it just retrieves the height of the outer container which is set to a certain value.
How do I make the second container grow to the full height of the outer container?
Hint: I kinda have the feeling that it's not possible at all.
Here a snippet:
* {
box-sizing: border-box;
}
.padding {
padding: 10px;
}
.outer-container {
height: 400px;
width: 100%;
background-color: yellow;
overflow-y: scroll;
}
.overflowing-container {
width: 20%;
display: inline-block;
background-color: salmon;
overflow-y: visible;
}
.inherit-height-container {
height: inherit;
width: 70%;
display: inline-block;
background-color: salmon;
vertical-align: top;
}
.large-element {
height: 250px;
width: 100%;
margin-top: 5px;
background-color: lightsalmon;
}
<div class="outer-container padding">
<div class="overflowing-container padding">
<div class="large-element"></div>
<div class="large-element"></div>
<div class="large-element"></div>
</div>
<div class="inherit-height-container"></div>
</div>
If you can edit your html, you can just add an inner wrapper and make it flex:
* {
box-sizing: border-box;
}
.padding {
padding: 10px;
}
.outer-container {
height: 400px;
width: 100%;
background-color: yellow;
overflow-y: auto;
}
.inner-container {
display: flex;
}
.overflowing-container {
width: 20%;
display: inline-block;
background-color: salmon;
margin-right: 5px; /* not sure if you want this gap */
}
.inherit-height-container {
/* remove height from this */
width: 70%;
display: inline-block;
background-color: salmon;
vertical-align: top;
}
.large-element {
height: 250px;
width: 100%;
margin-top: 5px;
background-color: lightsalmon;
}
<div class="outer-container padding">
<div class="inner-container">
<div class="overflowing-container padding">
<div class="large-element"></div>
<div class="large-element"></div>
<div class="large-element"></div>
</div>
<div class="inherit-height-container"></div>
</div>
</div>

one third container leaving white space at end css

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;
}

Center divs vertically within divs

So, I have a page (height 100%) split into two rows (top half 30%, bottom half 70%).
https://jsfiddle.net/qL0s07nr/
I have an h2 tag enclosed within one column in the top half which I want to center vertically, and I have 3 other columns in the bottom half which need centering vertically too.
<div class="container gb">
<div class="row" style="height:30%; background: #f6f6f6;">
<div class="twelve columns">
<h2 style="text-align: center;">30%</h2>
</div>
</div>
<div class="row" style="height:70%; background: #d4fff0">
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:red;"></div>
</div>
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:blue;"></div>
</div>
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:green;"></div>
</div>
</div>
</div>
My css for the columns is as follows:
width: 100%;
float: left;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
Full CSS:
.container.gb {
width: 100%;
max-width: 100%;
height: 100%;
}
.container {
position: relative;
width: 100%;
max-width: 960px;
margin: 0 auto;
padding: 0 20px;
box-sizing: border-box;
}
.column, .columns {
width: 100%;
float: left;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.one-third.column {
width: 30.6666666667%;
}
html, body {
height: 100%;
}
But for the life of me, I can't understand why it won't work. I am targeting the correct div, but it won't center for some reason. Any help is most appreciated.
Check this version: http://jsfiddle.net/leojavier/ko8wke5z/
<div class="container gb">
<div class="row" style="height:30%; background: #f6f6f6;">
<div class="twelve columns">
<h2 style="text-align: center;">30%</h2>
</div>
</div>
<div class="row rowb" style="height:70%; background: #d4fff0">
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:red;"></div>
</div>
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:blue;"></div>
</div>
<div class="one-third column">
<div style="height: 100px; width: 100px; background-color:green;"></div>
</div>
</div>
</div>
full CSS
.container.gb {
width: 100%;
max-width: 100%;
height: 100%;
}
.container {
position: relative;
width: 100%;
max-width: 960px;
margin: 0 auto;
padding: 0 20px;
box-sizing: border-box;
}
.column, .columns {
width: 100%;
float: left;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.one-third.column {
width: 30.6666666667%;
display: table-cell;
text-align: center;
vertical-align: middle;
float: none;
}
.rowb {
display: table;
width: 100%;
}
html, body {
height: 100%;
}
you can try it here: http://jsfiddle.net/leojavier/ko8wke5z/
The div that contains your <h2> does not use the full height of the row. Add his to your css:
.twelve.columns{height:100%}
If that class is going to get used again somewhere that style won't work give that div another class or an id (or an inline style).
Flexbox requires a flex container and flex items - it's a parent:children relationship. If you're using flexbox and presumably don't need older browser support, you can get rid of all those floats and specified widths. Your responsive layout can be done entirely with flexbox.
See this fiddle for responsive centered columns with matching gutters: https://jsfiddle.net/qL0s07nr/1/
Also, this flexbox guide is an excellent resource: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

CSS height 100% in automatic brother div not working in Chrome

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.