CSS Position Div Over Another Div - css

I have two divs that I am trying to stack over each other but the one I want on top is not showing. I want the blue background div to lay on top of the red background div. Any advice? The reason why I want to overlay the blue div is because the container is a centered grid and I want the red div to be the background for the first half of the page.
JSFIDDLE
CSS
.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: relative;
background: red;
}
.buddy-content {
position: absolute;
top: -629px;
z-index: 10;
background: blue;
}
.container {
max-width: 1000px;
margin: 0 auto;
overflow:hidden;
position:relative;
padding: 0 10px;}

You have made the second div absolute so you don't need to give the negative value for top. The second div is hiding because you top -629px; Try making the top:0 and see. And also for your current code. Remove the overflow hidden and put z-index like this:
.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: relative;
z-index:9;
background: red;
}
.buddy-content {
position: absolute;
top: -629px;
z-index: 10;
background: blue;
}
.container {
max-width: 1000px;
margin: 0 auto;
width:200px;
height:200px;
position:relative;
padding: 0 10px;
}

.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: absolute;
background: red;
}
.buddy-content {
position: absolute;
z-index: 10;
background: blue;
}
<div class="buddy BlueGradient">
</div>
<div class="container">
<div class="buddy-content">
ROGER
</div>
</div>
https://jsfiddle.net/kt77cp3e/6/
just add z-index : higher to the div that you want to show on top and set z-index low to the other one ..
ant one thing your code is working good just you need to remove " top : -629px;"
that thing is not allowing blue div to be on top just it is showing at the -629 px position..!!!!

If you can update your code like this, it may solve the issue:
Demo:https://jsfiddle.net/kt77cp3e/7/
CSS:
html, body {
height:100%;
width:100%:
}
.container {
width:50%;
height:100%;
background:red;
position:relative;
}
.container>div {
position:relative;
left:0;
right:0;
}
.container>div:first-child {
top:0;
height:50%;
background:blue
}
.container>div:last-child {
bottom:0;
height:50%;
background:green
}
HTML:
<div class="container">
<div></div>
<div></div>
</div>
Update: Considering the latest updated code, I think you should remove overflow:hidden from the container styles. That should do the trick
You should set the dimension on the .container div.
CSS:
.container {
position:relative;
width:100px; //You may modify these values
height:100px
}
Demo: https://jsfiddle.net/kt77cp3e/1/

.buddy { width: 50%; height: 629px; display: inline-block; position: relative; background: red;}
.buddy-content { position: absolute; top: 0px; z-index: 10; background: blue; }
.container {max-width: 1000px; margin: 0 auto; overflow:hidden; position:relative; padding: 0 10px; position: relative;}
<div class="container">
<div class="buddy BlueGradient">
<div class="buddy-content">ROGER</div>
</div>
</div>
This brings the text "Roger" with blue background on top of the red background

Related

CSS - How to set a nested div against body bottom? (Image included)

How can I achieve the styling shown in the picture? Consindering the following scenario: I got 2 nested div elements, by which the parent is "relative positioned" and the child is "absolute positioned"! And the child div is always "fixed to the bottom" of the body element, when browser is scaled. I don't get this to work...
Here is the code, where I am using padding-bottom: 100%. But this is not a good solution! Is there a way to realise this with only CSS 2.1 API?
body {
min-height: 100%;
background-color: grey;
}
.parent {
height: 70px;
width: 440px;
left:200px;
background-color: blue;
position: relative;
}
.child {
display: block;
position: absolute;
width: 100px;
right:0px;
background-color: yellow;
padding-bottom: 100%;
}
<body>
<div class="parent">
<div class="child">Fix to bottom</div>
</div>
</body>
Don't take 2nd div as child. You want it to stick to bottom and parent div's height will disturb it while scalling.
I hope this helps :)
body {
min-height: 100%;
background-color: grey;
}
.parent {
height: 70px;
width: 400px;
left:100px;
background-color: blue;
position: relative;
top:70px;
}
.another-parent {
display: block;
height:60%;
position: absolute;
bottom:0;
width: 100px;
right:22%;
background-color: yellow;
}
<body>
<div class="parent"></div>
<div class="another-parent">Fix to bottom</div>
</body>

CSS: Align div on bottom

I try to align a div at the bottom of a parent div. I tried the following:
JS Fiddle
I thought it would work with:
.bottom-menu {
position: absolute;
bottom: 0;
height: 50px;
background-color: yellow;
}
But as you can see in the fiddle it does not work. How can i do it?
You need to give a width to it as well. also you should use position: relative on the parent (the default is static). See updated fiddle: http://jsfiddle.net/wuf0m41z/1/
You could also specify right and left properties, and set them to 0, instead of setting width exclusively:
HTML:
<div class="container">
<div class="top-menu"></div>
<div class="bottom-menu"></div>
</div>
CSS:
.container {
height: 400px;
background-color: green;
position:relative;
}
.top-menu, .bottom-menu {
height: 50px;
background-color: yellow;
}
.bottom-menu {
position: absolute;
bottom: 0;
right:0;
left:0;
}
JSFiddle
just add the position:relative; to your container and don't forgetto add a width:100%; to your bottom menu.
here is an example:
http://jsfiddle.net/leojavier/wuf0m41z/10/
Try it !!!
.container {
height: 400px;
position: relative;
background-color: green;}
.top-menu {
height: 50px;
background-color: yellow;}
.bottom-menu {
height: 50px;
background-color: yellow;
position:absolute;
bottom:0;
width:100%;}
Your code is correct now just have to add width.
.bottom-menu {
position: absolute;
bottom: 0;
height: 50px;
background-color: yellow;
width:100%;
}

Margin-left and position left give different values with the same percentage

I have a div with:
width:100%;
max-width:100%;
position:relative;
overflow:hidden;
An immediate child of this div is:
.my-class {
position:absolute;
bottom:6px;
padding-left:12px;
}
I want the child div to line up with some other content. Nothing outside of this div is effecting it. When I use left:30% I get one number, when I use margin-left:30% I get a different one (which in this case is what I want).
Does margin-left take padding into account and left doesn't?
Or is there some other factor I've not considered?
Yes.Padding is affecting the margin.Take a look at this example:
div, span {
border: 1px solid #000;
height: 80px;
width: 80px;
}
.left, .marginLeft {
background: #aaf;
margin: 10px 0 0 10px;
padding: 10px;
position: relative;
}
.abs {
background: #faa;
position: absolute;
top: 0;
}
.left .abs {
left: 100px;
}
.marginLeft .abs {
margin-left: 100px;
}
<h3>Left</h3>
<div class="left">
parent
<div class="abs">left</div>
</div>
<h3>Margin left</h3>
<div class="marginLeft">
parent
<div class="abs">margin left</div>
</div>

Simple div disposition issue

Simple question: I've got two divs, I want them side by side, like this:
.details-left {
background: yellow;
float: left;
width: 50%;
}
.details-right {
background: silver;
width: 50%;
float: left;
}
<div class="details-left">left</div>
<div class="details-right">right</div>
The question is, can I have the same effect using float in only one if the divs? The specific issue is, the yellow div can contain float: left, the silver div can't. I want to achieve the same effect without using float on the silver div.
How can I achieve it?
Here's a fiddle
Use display:inline-block to achieve what you want.
.details-left {
background: yellow;
float: left;
width: 50%;
}
.details-right {
background: silver;
width: 50%;
display:inline-block;
}
<div class="details-left">left</div>
<div class="details-right">right</div>
UPDATED FIDDLE
You can use display: inline-block for the silver div
http://jsfiddle.net/94Lxd5c4/
.details-right {
background: silver;
width: 50%;
display: inline-block;
}
Or you may just define overflow: hidden; or overflow: auto; . The behaviour of the silver div will be the same, whether you add float: left or not (it won't do any difference)
http://jsfiddle.net/ogwvjjs9/2/
.details-right {
background: silver;
width: 50%;
overflow: auto;
}
.details-left {
background: yellow;
float: left;
width: 50%;
}
.details-right {
background: silver;
width: 50%;
position : absolute;
left : 50%;
}
<div class="details-left">left</div>
<div class="details-right">right</div>
I hope this is what you are looking for
Use display:table and display:table-cell to achieve your need.
I added one more div that will act as a table
< div class="details">
Below div act as a table cell
< div class="details-left">left< /div>
< div class="details-right">right< /div>
<div class="details">
<div class="details-left">left</div>
<div class="details-right">right</div>
</div>
html
.details { display:table; width:100%; overflow:hidden;}
.details-left {
background: yellow;
display:table-cell;
width: 50%;
}
.details-right {
background: silver;
width: 50%;
display:table-cell;
}
css
demo: http://jsfiddle.net/ravinthranath/skuvnvqL/

Keeping a DIV at bottom-center of its parent DIV

My HTML structure is basically this -
<div id="header">
<div class="container">
</div>
</div>
<div id="content">
<div class="container">
</div>
</div>
<div id="footer">
<div class="container">
</div>
</div>
Ignore any elements except <div id="header">
I want to align <div class="container"> inside <div id="header"> at exactly bottom center. I'm using the following CSS code-
#header{ width:1062px; height:326px; background-color:#110000; text-align:center; position:relative; }
#header .container{ width:940px; height:262px; background-color:#220000; margin:0px auto; position:absolute; bottom:0px; }
There are height differences between the parent (#header) and child (#header .container) DIVs. Removing position:absolute; from the child centers it but it sticks to the parent's top instead of bottom. Keeping position:absolute; sticks it at the bottom but aligns it to the left.
How do I align it both center AND bottom at the same time?
I tried all the solution above but it didn't work when you resize the browser window. This solution is mostly to be applied when you don't know the element's width. Or if the width is changed on resize.
After making some research I tried the following and it worked perfectly on all screen sizes.
#somelement {
position: absolute;
left: 50%;
bottom: 0px;
-webkit-transform: translateX(-50%);
transform: translateX(-50%)
}
I shared this for anyone still facing this issue.
try in this way:
#header .container{
width: 940px;
height: 262px;
background-color: #220000;
position: absolute;
bottom: 0 ;
left: 50%;
margin-left: -470px;
}
try this
#header .container {
width: 940px;
height: 262px;
background-color: #220000;
margin: 0px auto;
position: absolute;
bottom: 0px;
left: 61px;
}
use this:
#header{
width:1062px; height:262px; background-color:#110000; text-align:center;
position:relative;text-align: center; vertical-align: bottom;padding-top:64px;
}
#header .container{
width:940px;
height:262px;
background-color:#999000;
margin:0px auto;
bottom:0px;
margin-bottom: 0px;
padding-top: 0px;
}
Here the jsfiddle
UPDATE:
As DenisVuyka said in comment, i should add that the above sample was as answer to this particular question with fixed height for DIV.
If you want that height of DIV don't break up things then for example you should use padding-top:10%; in the #header and height:100% in #header .container CSS.
#header{
width:462px; height:262px; background-color:#110000; text-align:center;
position:relative;text-align: center; vertical-align: bottom;padding-top:10%;
}
#header .container{
width:300px;
height:100%;
background-color:#999000;
margin:0px auto;
bottom:0px;
margin-bottom: 0px;
padding-top: 0px;
}
Here is a demo: http://jsfiddle.net/d6ct6/ .
I was trying to get this to work in my project as well. I've edited this jsfiddle:
http://jsfiddle.net/d6ct6/
<div id="header">
<div class="container">
</div>
</div>
#header {
height:100vh;
background-color:#110000;
position:relative;
}
#header .container{
width:300px;
height:40px;
background-color:#999000;
bottom:0px;
position:absolute;
left:calc((100% - 300px)/2);
}
But I've found this only works when the width of .container is fixed.
If the width of .container is not fixed you would need javascript to find it's width and then change that width in the calc.
When the widths are responsive, use this:
HTML
<div id="header">
<div id="container">
</div>
</div>
CSS
#header {
height:100vh;
background-color:#110000;
position:relative;
}
#container{
width:300px;
height:40px;
background-color:#999000;
bottom:0px;
position:absolute;
}
JS
$(document).ready(function () {
var parentWidth = $('#header').width();
var trapWidth = $('#container').width();
var deadCenter = (parentWidth - trapWidth);
var deadHalf = Number( deadCenter / 2 );
$('#container').css("right", deadHalf);
});
In case you care more about having the inside div aligned in the center and can manually set the vertical alignment.
DEMO Height I used was first div height - second div height.
#header .container{ width:940px; height:262px; background-color:red; margin:0 auto; position:relative; top: 64px; }
I would take advantage of CSS table display properties and do the following:
#header {
width:1062px;
height:326px;
background-color:#110000;
text-align:center;
display: table-cell;
vertical-align: bottom;
}
#header .container {
width:900px;
height:262px;
background-color:#cccccc;
color: white;
display: inline-block;
}
Set the #header block to display: table-cell and set vertical-align: bottom to align the child's bottom edge to the bottom edge of the parent.
The child .container element had display: inline-block and this will allow it to respond the text-align: center property of the parent.
This will work regardless of the width of the child .container.
Demo fiddle: http://jsfiddle.net/audetwebdesign/p9CxE/
This same problem was bedevilling me for an hour or so, until I realised I could add an intermediary div; this separated the vertical alignment issue from the centering.
.dparent {
border: 1px solid red;
width: 300px;
height: 300px;
position: absolute;
}
.dchild {
border: 1px solid blue;
margin-left: auto;
margin-right: auto;
width: 200px;
height: 100px;
bottom: 0px;
position: relative;
}
.dmid {
border: 1px solid green;
width: 100%;
position: absolute;
bottom: 0;
<div class="dparent">
<div class="dmid">
<div class="dchild"></div>
</div>
</div>
Do the vertical alignment first, with an absolute position and the 0 bottom. Then do the centering with margin-left and margin-right set to auto.
You might try this solution for any concerned width:
width:100%;
position:absolute;
bottom: 5px;
left:50%;
margin-left:-50%;
Good luck!

Resources