CSS Layout Push Div Bottom - html

I've been trying different ways but couldn't achieve what I want.
<div id="parent">
<div id="child-1"></div>
<div id="child-2"></div>
<div id="child-3"></div>
</div>
So I have the #parent at height: 100vh.
#child-1 should have height: 100% of parent.
#child-2 and #child-3 should have width: 100% and height: auto and they should be stacked on top of each other at position bottom: 0.
I've been trying to set parent relative and two childs absolute but the first child's height gets ignored.. I tried with display flex but first child's height is not 100% of parent.. I'm very confused how to do this.
Can someone help?
Here is what I'm trying to achieve: jsfiddle.net

You have to first get the bottom value of #child-2 dynamically as you said it should be on the top of #child-3.
You need to apply jQuery to get the height of #child-3 dynamically and then applying the height value of #child-3 to the bottom value of child-2, just like
#child-2 {
bottom: height-of-child-3;
}
Look at this Codepen
Or look at the snippet below:
height_child_three = $('#child-3').height();
$('#child-2').css({
position: 'absolute',
bottom: height_child_three
});
#parent {
width: 100vw;
height: 100vh;
background: #000;
position: relative;
}
#child-1 {
width: 100%;
height: 100%;
background: #eee;
}
#child-2 {
width: 100%;
background: #a0ea0e;
}
#child-3 {
position: absolute;
bottom: 0;
width: 100%;
background: #30e30e;
}
body { margin: 0; } /* A small reset */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
<div id="child-1">
<strong>I'm child 1</strong>
</div>
<div id="child-2">
<strong>I'm child 2</strong>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione deleniti voluptate commodi distinctio, repellendus qui, placeat laboriosam eligendi! Ducimus reiciendis officiis debitis placeat adipisci quae hic tempore vitae suscipit nemo.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam sed aliquid, laborum nisi quos excepturi hic! Molestias hic consectetur dolor! Perferendis iste, quisquam quaerat ab, odio ducimus! Odio, minima error?</p>
</div>
<div id="child-3">
<strong>I'm child 3</strong>
</div>
</div>
Hope this helps!

Is this what you need?
HTML:
<div id="parent">
<div class="child-1"></div>
<div class="child-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error voluptatum necessitatibus dolorem soluta laudantium cupiditate maiores neque, aliquid accusamus autem saepe tempora, itaque possimus, eaque deleniti odio atque enim omnis.</div>
<div class="child-3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa, illo est dolor dolores placeat deleniti quae consequuntur eum ipsum blanditiis laboriosam quod repellendus fugit! Odio quis rem vel a dolores.</div>
</div>
CSS:
html,
body,
div {
margin: 0;
padding: 0;
}
*,
*:after,
*:before {
box-sizing: border-box;
}
#parent {
position: relative;
width: 100vw;
height: 100vh;
background: #ccc;
}
.child-1 {
width: 100%;
height: 100%;
background: red;
}
.child-2 {
width: 100%;
height: auto;
padding: 30px;
background: blue;
}
.child-3 {
width: 100%;
height: auto;
padding: 30px;
background: green;
}
Here you can see a solution just using plain CSS. CODEPEN

Related

Image is not covering the whole div

I am trying to make my image cover the whole div which is 50% of the parent div. I used object-fit:cover but it's still not working. The problem is as I reduce the width of window the image also shortens.
In (1) the width is full
The 2nd picture is 900px window size.
* {
box-sizing: border-box;
}
body {
background: yellow;
}
section {
background: red;
widtth: 100%;
min-height: 700px;
}
article {
background: green;
width: 50%;
min-height: 700px;
padding: 100px;
float: left;
}
picture {
float: left;
width: 50%;
min-height: 700px;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
}
section::after {
content: "";
clear: both;
display: block;
}
<body>
<section class="about">
<article>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur expedita laudantium, ea eos fugiat dolores laboriosam voluptas illo deleniti pariatur ratione nobis perferendis in consectetur rerum ipsa debitis quis numquam! Lorem ipsum dolor
sit amet consectetur adipisicing elit. Veniam unde placeat ratione magnam tempore velit accusamus ipsam quaerat aspernatur maiores?</p>
</article>
<picture>
<img src="https://www.loveinartsz.com/wp-content/uploads/2021/03/b04803919effa1914ae6754d8bee30fb.jpg" alt="">
</picture>
</section>
</body>
I have linked the code pen link below for reference.
https://codepen.io/YASH_KR18/pen/LYObNrB
Simple solution, add display:flex to its parent element which is picture will make it work.
* {
box-sizing: border-box;
}
body {
background: yellow;
}
section {
background: red;
width: 100%;
min-height: 700px;
}
article {
background: green;
width: 50%;
min-height: 700px;
padding: 100px;
float: left;
}
picture {
float: left;
width: 50%;
min-height: 700px;
display:flex
}
img {
max-width: 100%;
max-height: 100%;
object-fit: cover;
}
section::after {
content: "";
clear: both;
display: block;
}
<body>
<section class="about">
<article>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur expedita laudantium, ea eos fugiat dolores laboriosam voluptas illo deleniti pariatur ratione nobis perferendis in consectetur rerum ipsa debitis quis numquam! Lorem ipsum dolor
sit amet consectetur adipisicing elit. Veniam unde placeat ratione magnam tempore velit accusamus ipsam quaerat aspernatur maiores?</p>
</article>
<picture>
<img src="https://www.loveinartsz.com/wp-content/uploads/2021/03/b04803919effa1914ae6754d8bee30fb.jpg" alt="">
</picture>
</section>
</body>
Because you are using the extra picture tag for that
I am avoiding that tag because I see no need of that in this whole code. If you want to use the picture tag vary badly we have to think of something else. Here take a look:
HTML
<body>
<section class="about">
<article>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur expedita laudantium, ea eos fugiat dolores laboriosam voluptas illo deleniti pariatur ratione nobis perferendis in consectetur rerum ipsa debitis quis numquam! Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam unde placeat ratione magnam tempore velit accusamus ipsam quaerat aspernatur maiores?</p>
</article>
<img src="https://www.loveinartsz.com/wp-content/uploads/2021/03/b04803919effa1914ae6754d8bee30fb.jpg" alt="">
</section>
</body>
CSS
*{
box-sizing:border-box;
}
body{
background:yellow;
}
section{
background:red;
widtth:100%;
min-height:700px;
}
article{
background:green;
width:50%;
min-height:700px;
padding:100px;
float:left;
}
img{
float:left;
width:50%;
min-height:700px;
}
section::after{
content:"";
clear:both;
display:block;
}
Floating elements (removing them from the normal flow of the html structure) is the source of a lot of problems. If you would use a flexbox or gridbox for the parent element then there would be no need to float the children to position them next to eachother and then I believe your problem is solved. Well that is if the snippet below does what you're after at least! If not I might not understand your question yet.
/* Colors for visibilty */
body{ background-color: yellow;}
article{ background-color: green; padding: 100px;}
picture{ background-color: red; }
/* The problem fix*/
section
{
display: grid;
grid-template-columns: 50% 50%;
width: 80%;
max-width: 1100px;
min-height: 700px;
margin: auto;
}
picture img{
object-position: center;
object-fit: cover;
width: 100%;
height: 100%;
}
<section>
<article>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur expedita laudantium, ea eos fugiat dolores laboriosam voluptas illo deleniti pariatur ratione nobis perferendis in consectetur rerum ipsa debitis quis numquam! Lorem ipsum dolor sit amet consectetur adipisicing elit. Veniam unde placeat ratione magnam tempore velit accusamus ipsam quaerat aspernatur maiores?</p>
</article>
<picture>
<img src="https://www.loveinartsz.com/wp-content/uploads/2021/03/b04803919effa1914ae6754d8bee30fb.jpg" alt="">
</picture>
</section>

CSS to fit sidebar under topbar

I need to create an empty topbar and an empty sidebar as below. I want to stack them neatly so that the top of the sidebar touches the bottom of the topbar. For that, I am using percentage. However, I just can't get them to do that.
I saw some other threads suggesting to set html and body with height: 100%; and width : 100%. But in my app, the layout below is not in the main html document.
#nav-bar {
position: absolute;
top: 0;
left: 0;
background-color: blue;
height: 10%;
width: 100%;
}
#side-bar {
position: absolute;
top: 0;
left: 0;
background-color: #011a21;
height: 90%;
width: 7.5%;
/*margin-top: 10%;*/
}
<div id="nav-bar"> </div>
<div id="side-bar"></div>
Your #nav-bar has a height: 10%, and you can set the same value for the top rule, selector #side-bar.
#nav-bar {
position: absolute;
top: 0;
left: 0;
background-color: blue;
height: 10%;
width: 100%;
}
#side-bar {
position: absolute;
top: 10%;
left: 0;
background-color: #011a21;
height: 90%;
width: 7.5%;
/*margin-top: 10%;*/
}
<div id="nav-bar"> </div>
<div id="side-bar"></div>
I tend to use flex box for this, Maybe grid would be smarter though. I took also care of some scrolling things. Using min with and height 0 on flex parents is key for that.
body,
section {
min-height: 0;
min-width: 0;
}
body {
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
nav {
height: 2rem;
background: green;
}
section {
flex: 1 1 0%;
display: flex;
}
aside {
width: 4rem;
background: blue;
height: 100%;
}
main {
background: pink;
flex: 1 1 0%;
overflow: auto;
padding: 1rem;
}
div {
border: 1px black dashed;
height: 300%;
}
<nav></nav>
<section>
<aside></aside>
<main>
<div>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem.
<br> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem.
<br> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem.
<br> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem.
<br>
</div>
</main>
</section>

Parent div Not getting height if child div is absolute

I just stuck in position, I used position:relative for parent and position:absolute for child now parent div did't get height and i don't want to use min-height or height. You can see the red border on top which is the parent div border.
fiddle code
.box {
text-align: center;
border: 1px solid red;
width: 500px;
margin: 0 auto;
position: relative;
}
.content {
width: 50%;
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
}
<div class="box">
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Assumenda adipisci vel, dolore aspernatur iste iure blanditiis quam esse repudiandae aperiam debitis doloribus necessitatibus placeat tempora voluptate totam exercitationem neque quae.
</div>
</div>
Help me please ?
Thanks
You could just make the outer box absolute, if your textbos has to be positioned absolute.
EDIT: Without being able to edit the HTML structure, you need specific heights or some JavaScript. More Information about position
.box {
text-align: center;
border: 1px solid red;
width: 500px;
margin: 0 auto;
position: absolute;
}
.content {
width: 50%;
left: 0;
right: 0;
margin: 0 auto;
}
<div class="box">
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Assumenda adipisci vel, dolore aspernatur iste iure blanditiis quam esse repudiandae aperiam debitis doloribus necessitatibus placeat tempora voluptate totam exercitationem neque quae.
</div>
</div>
Its not possible without javascript but you can get this if add a child element inside the '.content'...
.box {
text-align: center;
width: 500px;
margin: 0 auto;
position:relative;
}
.content {
border: 1px solid red;
width: 100%;
position:absolute;
left:0;
right:0;
}
.inner{
margin: 0 auto;
width: 50%;
}
<div class="box">
<div class="content">
<div class="inner">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Assumenda adipisci vel, dolore aspernatur iste iure blanditiis quam esse repudiandae aperiam debitis doloribus necessitatibus placeat tempora voluptate totam exercitationem neque quae.
</div>
</div>
</div>
Absolute positioning takes an element out of normal flow, so it can not change the measures of its parent any more. Try this, it works fine:
.box {
text-align: center;
border: 1px solid red;
width: 500px;
height:100%;
margin: 0 auto;
}
.content {
width: 50%;
margin: 0 auto;
}

Make element move out of container

Example HTML/CSS:
.wrapper {
width: 100%;
}
.container {
width: 400px;
margin: 0 auto;
border: 1px solid;
}
<div class="wrapper">
<div class="container">
<div class="element">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi libero veritatis dolores facere, eaque aspernatur, magnam repellendus eveniet, ullam magni accusamus accusantium itaque a illo totam vitae. In, earum quos.</div>
</div>
</div>
If there any chance to move left edge of .element to left edge of .wrapper, while its right edge stays snapped to right edge of .container. In other words, I want to override left margin of .container by adding some rules to .element. I've tried something like:
.element {
margin-left: -100%
}
But it moved the whole element, as I expected.
You could position the element absolutely relative to the .wrapper, and then use calc() to determine what 50% - 200px is:
Example Here - borders added for demonstration purposes..
.wrapper {
width: 100%;
position: relative;
}
.container {
width: 400px;
height: 60px;
margin: 0 auto;
border: 2px solid;
}
.element {
position: absolute;
left: 0;
right: calc(50% - 200px);
border: 2px solid #f00;
}
<div class="wrapper">
<div class="container">
<div class="element">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sequi libero veritatis dolores facere, eaque aspernatur, magnam repellendus eveniet, ullam magni accusamus accusantium itaque a illo totam vitae. In, earum quos.</div>
</div>
</div>
You can set a position, but you will need javascript to calc the correct width.
.element {
position:absolute;
left:0;
}
JQuery
$( document ).ready(function() {
var w = ($(".container").outerWidth()/2) + $(".container").width();
$(".element").width(w);
});

css nested min-height layout

I try to achieve a Layout with nested min-height divs and a sliding footer.
The problem of course is that die inner min-height div is not expanding to the full heights of the outer div because the outer divs height is set with min-height.
here is the html:
<div class="container">
<section class="pos-container">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus, voluptates, qui eos dignissimos quae nobis at provident voluptatum dicta nesciunt possimus iusto vitae nihil hic assumenda aspernatur quos vel necessitatibus.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus, voluptates, qui eos dignissimos quae nobis at provident voluptatum dicta nesciunt possimus iusto vitae nihil hic assumenda aspernatur quos vel necessitatibus.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus, voluptates, qui eos dignissimos quae nobis at provident voluptatum dicta nesciunt possimus iusto vitae nihil hic assumenda aspernatur quos vel necessitatibus.</p>
</section>
</div>
<footer>
i'm footer
</footer>
and the css:
body {
background-color: grey;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow-y: scroll;
}
.container {
z-index: 1;
position: relative;
width: 100%;
min-height: 100%;
background-color: pink;
margin-bottom: 6em;
}
.pos-container {
position: relative;
width: 50em;
min-height: 100%;
margin: auto;
background-color: green;
}
footer {
z-index: 0;
position: fixed;
bottom: 0;
width: 100%;
height: 6em;
}
FIDDLE #1
In this Fiddle the height of the inner div (green) is not expanding to the height of the outer div(pink).
FIDDLE #2
Seems fixed if i set the height of the outer div from min-height to height but there is another problem if the height of the inner div is more than 100% as you can see in FIDDLE #3
Is there any pure css solution for this problem?
Thanks in advance!
Remove the height for body or make it height:auto;
Check this FIDDLE
CSS change
body{
height:100%; // remove this and add below line
height:auto;
}