Position 2 absolute DIV inside 1 relative DIV within bootstrap container - html

I am trying to position 2 divs within 1 div inside a bootstrap container... One located top left and one located top right.
I can get the one top left to sit where I want it to, but the bottom right one, when putting right: 20px sits to the right of the browser itself, and not within the container.
Here is my HTML & CSS:
<div id="header">
<div class="container">
<div class="div1">
Top left content
</div>
<div class="div2">
Bottom right content
</div>
</div>
</div>
#header {
height: 625px;
position: relative;
}
#header .div1 {
position: absolute;
top: 20px;
}
#header .div2 {
position: absolute;
bottom: 20px;
right: 20px;
}
Any suggestions on how to get the bottom right div to sit to the right of the container and not the browser, would be greatly appreciated.
Thanks

Your absolute divs are placed inside the #header element since you applied position:relative on it.
Bootstrap doesn't have explicitly set relative position for container class. You need to set position for container and then your absolute divs will be placed inside the container:
.container{
position: relative;
}
So, it would be good to replace the following:
#header {
height: 625px;
position: relative;
}
With this:
#header .container{
height: 625px;
position: relative;
}

<style>
#header {
height: 625px;
position: relative;
}
#header .div1 {
float: left;
top: 20;
}
#header .div2 {
position: absolute;
bottom: 20;
right: 0;
}
</style>
<div id="header">
<div class="container">
<div class="div1">
Top left content
</div>
<div class="div2">
Bottom right content
</div>
</div>
</div>

Related

Bootstrap row background image becomes wider than parent grid width

I am using bootstrap, I wanted one div behind the other div, so used z-index en position: absolute and relative.
When doing this, every div under the div with z-index: 1 goes behind this div, while I want it to stay under it.
The div also becomes wider than the max-width when using 100%
<div class="row" id="MENUROW">
<div class="col-md-12" id="MENUCOLUMN"><h1>SHOP</h1></div>
</div>
<div class="row" id="MAINROW"> <!-- this has the background-image -->
<div class="col-md-12" id="MAINCOLUMN">
</div>
</div>
CSS:
#MENUROW
{
position: relative;
height: 80px;
background-color: transparent;
z-index: 2;
}
#MAINROW
{
position: absolute;
z-index: 1;
top: 60px; /*because there is 1 div above the menu div, this div needs to be just under that div, behind the menu div */
width: 100%;
background-image: url(../images/background.jpg);
background-size: cover;
}
when doing this the background image goes wider (to the right) than the width of the parent div.
https://jsfiddle.net/2cs60vrr/3/ example, just made the background red to show how wide it should be, the background image goes much wider
Point 1
You didn't used .container class in your HTML. Bootstrap has a structure to get it's maximum feature. You must need to use .container. Bootstrap structure is below:
<div class="container">
<div class="row">
<div class="col-*-*">
Your Content
</div>
</div>
</div>
Make your html as above to solve this issue.
Point 2
If you want not change your html, then use this code below to any .row to solve this issue.
margin-left:0;
margin-right:0;
I am sorry if we are unsure what you are looking for but is that what you want?
.grid {
margin: 0 auto;
max-width: 100%;
width: 100%;
background-color: #fff;
}
.row {
width: 100%;
margin-bottom: 0px;
display: flex;
}
#MENUROW {
position: absolute;
height: 80px;
background-color: red;
z-index: 2;
}
#MAINROW {
position: absolute;
z-index: 1;
top: 0;
width: 100%;
max-width: 1400px;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/e/ed/Palais_Garnier.jpg);
background-size: cover;
}
https://jsfiddle.net/norcaljohnny/xt9c9d2r/2/
You should put the wrapper around the whole thing to position:relative;
And both rows to position:absolute;
That's it.
When using position:absolute; the block goes to the absolute top left corner of the closest parent html tag that has a position:relative;. If there is no parent with position:relative; your absolute positioned items go to the upper left corner of your screen.
(the first row is not a parent of the second, but they are siblings. The wrapper "grid" is the parent of the 2 rows)
<div class="grid">
<div class="row" id="MENUROW">
<div class="col-md-12" id="MENUCOLUMN">
<h1>SHOP</h1>
</div>
</div>
<div class="row" id="MAINROW">
<div class="col-md-12" id="MAINCOLUMN">
text
</div>
</div>
</div>
And CSS
.grid {
position: relative;
}
.row {
width: 100%;
}
#MENUROW {
position: absolute;
background-color: red;
z-index: 1;
}
#MAINROW {
position: absolute;
z-index: 2;
background-image: url(https://upload.wikimedia.org/wikipedia/commons/e/ed/Palais_Garnier.jpg);
background-size: cover;
}
Here is your updated example:
https://jsfiddle.net/2cs60vrr/6/

CSS How to position div inside div so that it will always be 100%

Using the CSS property 'position', how would it be possible to make a div inside another div always 100% height of the parent div, with a margin of 40px on the top and on the bottom? It needs to be adjusting, so that if the parent div is 700px in height, the child div will be 620px (700px - 80px from margins). Here is an example of what I mean:
Here the parent div (green) is tall, so the child (orange) must stretch to fit the space.
And here the parent (green) is squashed, so the child (orange) must compensate by squashing itself to fit.
Thank you in advance.
Edit:
Here is the html Im working with:
<div id="center-page">
<p id="center-page-title">Blog</h1>
<div id="content">
</div>
</div>
Try this:
#center-page {
position: relative;
background: green;
height: 700px;
}
#content {
position: absolute;
background: orange;
top: 40px;
bottom: 40px;
left: 0;
right: 0;
}
<div id="center-page">
<p id="center-page-title">Blog</h1>
<div id="content">
</div>
</div>
You can try absolute positioning with top and bottom values. Something like this:
#child{
position: absolute;
top: 40px;
bottom: 40px;
left: 0;
right: 0;
background: blue;
}
Here is an example:
https://jsfiddle.net/Lys72mgy/

right side of fixed div overflow its parent

I am trying to put a position:fixed div inside an another div. I want a fixed div which has a width:100%; so it will be great for mobile and desktop at the same time.
Here is my JSfiddle
SF wants some code:
<div id="container">
<div id="item">This div is good div</div>
<div id="fixed">Right side of this div overflow its parent!!! </div>
</div>
An element with position: fixed; ignores the parent size because it is relative only to the viewport:
MDN:
Fixed positioning is similar to absolute positioning, with the exception that the element's containing block is the viewport.
You can:
Try giving it position: absolute; and set the container to position: relative;.
Use position: fixed; and set the size explicitly.
You can use the calc() method to adapt the viewport size. Just subtract right and left margin from the 100%:
Edit: I added a min-height to the body to see the "fixed-effect" on scrolling
body {
margin: 0;
min-height: 1000px;
}
#container {
margin: 10px;
background: black;
color: white;
}
#item {
height: 50px;
width: 100%;
}
#item {
background: blue;
}
#fixed {
height: 50px;
width: calc(100% - 20px);
background: green;
position: fixed;
}
<div id="container">
<div id="item">Normal div</div>
<div id="fixed">Fixed div</div>
</div>

How do I position a div under a fixed position div

I have a position:fixed div at the top of my page, so that when a user scrolls down the menu always stays at the top.
How do I position another div element underneath the fixed div.
I'm using CSS and HTML.
I'm using a smooth scrolling jQuery and need each section header to appear just under the menu bar.
Something like this — http://codepen.io/sergdenisov/pen/pJyMGb:
HMTL:
<div class="menu">
<div class="menu-item">Home</div>
<div class="menu-item">About</div>
<div class="menu-item">Demo</div>
<div class="menu-item">Contact</div>
</div>
<div class="menu-item menu-item_sub">Contact</div>
CSS:
body {
height: 2000px;
}
.menu {
position: fixed;
background: blue;
width: 100%;
}
.menu-item {
display: inline-block;
padding: 30px;
}
.menu-item_sub {
position: fixed;
left: 0;
top: 60px;
}
Do everything in absolute position domain.
.1(class){
position: absolute;
left: 10px;
top20px;
}
Like above classify each object with a class and set their position.

parent relative positioned and floated left child absolute positioned

I'm trying to have a set of parents with float left applied to them, but also with relative position, and inside each of them I have a div with position absolute like this:
<div class="wrapper">
<div class="pa">
<div id="apDiv1"><img src="images_1/logo-u16785.png" width="194" height="190"></div>
</div>
<div class="pa">
<div id="apDiv4"><img src="images_1/zilogo-u16782.png" width="194" height="190"></div>
</div>
</div>
and the css
.wrapper {
width:1300px;
position: relative;
}
.pa {
float:left;
width:650px;
position: relative;
}
#apDiv1 {
position: absolute;
width: 650px;
height: 190px;
z-index: 4;
left: 39px;
top: 10px;
}
#apDiv4 {
position: absolute;
width: 650px;
height: 190px;
z-index: 4;
left: 39px;
top: 10px;
}
now my problem, if u see, the 2 selectors has the same top and left, but because they are in 2 different, parent which are floated left and with position relative I thought the selectors should be positioned relative to their parent, but they are on top of each other, why??
thanks!!!
I'm not sure if this is what you're looking for but first make sure your CSS classes and DIVs have the same name.
I changed float: left; from the ".pa" div to display: inline-block;.
Check my Fiddle
It's possibly because the absolute-positioned elements aren't aware of the floated elements' boundaries, but I suspect most likely because you have styles for #apDiv1 and #apDiv2 but your actual element IDs are "apDiv1" and "apDiv4".