I have 2 Divs both of which are subject to absolute position condition. First Div has top: 200px and second one has top: 400px. However surprisingly, when I see them in Computers with different screen size, their relative positions look quite different. For example, when I see them in Laptop they appear to be closer to each other, whereas when I see then in wide-screen Desktop, they appear to be far.
I wonder, since I have position: absolute for both Divs, shouldn't their relative position be same regardless the screen size I chose?
What I need to do to make them appear in the same position relative to each other regardless the screen size?
CSS details for both Divs
.one {
margin: 0 auto;
padding: 32px 0 0 0;
background-color: rgba(248, 248, 255, 0.15);
height: 140px;
position: absolute;
top: 134px;
width: 100%;
text-align: center;
}
.two {
position: absolute;
margin: auto;
top: 237px;
bottom: 0;
left: 0;
right: 0;
text-align: center;
align-items: center;
}
Thanks for your advice.
The problem is that you're using bottom: 0 for the second div, instead of a height. This means that the bottom of your second div will always be at the bottom of the page, so if you resize it, this div will become taller proportionally to the height of your screen size.
To fix this, I removed the top, left and right positioning and instead applied a height (like in your first div) and a width of 100%.
.one {
position: absolute;
margin: 0 auto;
padding: 32px 0 0 0;
background-color: pink;
height: 140px;
top: 134px;
width: 100%;
text-align: center;
}
.two {
height: 100px;
width: 100%;
position: absolute;
margin: auto;
background-color: steelblue;
top: 237px;
text-align: center;
align-items: center;
}
<div class="one"></div>
<div class="two"></div>
This can be one solution.
*{box-sizing:border-box;}
.one {
position: absolute;
margin: 0 auto;
padding: 32px 0 0 0;
background-color: pink;
height: 140px;
top: 134px;
width: 100%;
text-align: center;
}
.two {
height: 140px;
width: 100%;
position: absolute;
margin: auto;
background-color: steelblue;
top: 274px;
text-align: center;
align-items: center;
}
<div class="one"></div>
<div class="two"></div>
The other solution is to just group them into 1 div, make make that div absolute.
The advantage of this is that u dont have to maintain positioning of 2 divs. Only the parent is enough
* {
box-sizing: border-box;
}
.parent {
position: absolute;
top: 134px;
width:100%;
left:0;
right:0;
}
.one {
margin: 0 auto;
padding: 32px 0 0 0;
background-color: pink;
height: 140px;
width: 100%;
text-align: center;
}
.two {
height: 140px;
width: 100%;
margin: auto;
background-color: steelblue;
text-align: center;
align-items: center;
}
<div class="parent">
<div class="one"></div>
<div class="two"></div>
</div>
In other to make look same regardless of screen size, you have to set the properties say left=0 , right=0 and bottom=0for bottom div and then you could control the height using different top values. Doing this you have make both div behave like a block container with absolute position and their placement will not shift each other like relative.
.one {
margin: 0 auto;
padding: 32px 0 0 0;
background-color: red;
height: 140px;
position: absolute;
top: 134px;
left: 0;
right: 0;
bottom:0;
width: 100%;
text-align: center;
border: 1px solid blue;
}
.two {
position: absolute;
margin: auto;
top: 237px;
bottom: 0;
left: 0;
right: 0;
text-align: center;
align-items: center;
background-color: green;
border: 1px solid blue;
}
<div class="one">
1
</div>
<div class="two">
2
</div>
Related
I want the elements inside .bannerinner class to be in the exact middle alignment of the .banner class but it doesn't really work that way. I'm open to any advices, any help would be greatly appreciated, thanks from now.
I have this .banner class "absolute" to my "relative" background
.banner {
background-color: rgba(209, 29, 155, 0.212);
padding: 8px 15px;
overflow: hidden;
height: 10vh;
width: 150vh;
max-height: 10vh;
min-height: 10vh;
position: absolute;
bottom: 9vh;
}
and also inside this .banner class there is a .bannerinner class
.bannerinner {
padding: 0;
margin: 0;
left: 2vh;
position: absolute;
bottom: 50%;
vertical-align: middle;
}
Set .innerbanner to left:50% and top:50%. This puts the upper left corner of .innerbanner to the center of .banner.
Then transform .innerbanner back to -50% of its own width and height. This sets the center of .innerbanner exactly to the center of .banner.
See code example:
div {
box-sizing: border-box;
}
.background {
position: relative;
width: 80%;
height: 400px;
padding: 0.5em;
margin: 0 auto;
background-color: #bbb;
}
.banner {
position: absolute;
width: 80%;
height: 200px;
padding: 8px 15px;
left: 10%;
bottom: 2vh;
overflow: hidden;
background-color: rgba(209, 29, 155, 0.212);
}
.bannerinner {
position: absolute;
left: 50%;
top: 50%;
width: 200px;
transform: translate(-50%, -50%);
text-align: center;
background-color: #fff;
border: 1px solid #000;
}
<div class="background">
<p>Background: relative</p>
<div class="banner">
<p>Banner: absolute </p>
<div class="bannerinner">
<p>Inner Banner: absolute</p>
</div>
</div>
</div>
Hi I have a question about my code.
Here is my problem.
I have two items and one frame. Frame has position relative a two items (children) have position absolute. They should be always on the same point inside my frame but when I am resizing the position of children are different in relation with frame. How to make the children always stay on same position for responzive design? Is it possible?
Try to change height of window in example too (not only width)
https://codesandbox.io/s/adoring-jackson-c6fth?file=/index.html:0-900
.frame {
width: 70vh;
height: 90vh;
border: 10px solid red;
margin: 10px auto;
position: relative;
}
.objA {
width: 130%;
position: absolute;
height: 40%;
bottom: -10%;
left: -20;
border: 2px solid green;
background: rgba(10, 101, 10, 0.7);
z-index: 2;
}
.objB {
width: 10%;
position: absolute;
height: 20%;
bottom: 20%;
left: 30%;
background: red;
z-index: 1;
}
<div class="frame"></div>
<div class="objA"></div>
<div class="objB"></div>
I post here my real example image for better imagination what the problem is.
All objects are positioned absolute (Waves, stars, robot etc...) Each wave should in every resized situation be in same position. Good to know is that every wave is separatly.
Make sure the positions and heights/widths are all in percents and most important: place your items inside the main <div class="frame"></div> element. You can start this way:
* {
box-sizing: border-box;
}
body {
background: none #014653;
margin: 0;
}
.frame {
width: 70vw;
height: 90vh;
border: 10px solid #5cb9b8;
background: none #5cb9b8;
margin: 5vh auto;
position: relative;
border-radius: 6px;
}
.frame-stage {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
border: 5px solid #a2cece;
background: none #014653;
border-radius: 5px;
}
.objA {
width: 10%;
position: absolute;
height: 20%;
bottom: 10%;
left: -20;
border: 2px solid green;
background: rgba(10, 101, 10, 0.7);
z-index: 2;
}
.objB {
width: 10%;
position: absolute;
height: 20%;
bottom: 30%;
left: 30%;
background: red;
z-index: 1;
}
<div class="frame">
<div class="frame-stage">
<div class="objA"></div>
<div class="objB"></div>
</div>
</div>
Updated codesandbox here.
I have included 2 photos and my css code below. I am trying to bring my #bottomBar up so its directly under and that its the same width as the black space - you can see in the images below.
Depending on if i use position relative or position absolute i get the desired affect of either being directly under the black space or being the same width. But never both at the same time.
I am using a bootstrap templet in my html, would that cause the issues that i am getting?
css position relative
css position absolute
body{
margin: 0;
padding: 0;
}
.container{
padding: 0;
width: 100vw;
height: 100vh;
background-color: black;
}
#header{
display: flex;
padding: 5px;
width: 100%;
height: 10vh;
background: linear-gradient(to right,white, #a5c6ff, #0066ff);
}
h1{
position: relative;
margin: auto;
font-size: 6vw;
}
#img{
width: 15vw;
}
.navbar{
padding: 5px;
background: linear-gradient(to right,white, #a5c6ff, #0066ff);
}
.navbar-toggler{
display: flex;
padding: 0;
margin: 0;
width: 10vw;
height: 4vh;
}
.navbar-toggler-icon{
height: 3vh;
width: 9vw;
margin: auto;
}
#bottomBar{
position: absolute;
top: 100%;
width: 100%;
}
I know this is a common issue but I just can't work this out. No matter how many combinations of settings I try, the footer won't stay on the bottom of the page. It will just sit under whatever else is above it.
body {
margin: 0;
background-color: #ACFAB7;
}
# container {
margin: 0 auto;
padding: 40px;
}
#header {
z-index: 0;
height: 78px;
background-color: #2ecc71;
}
#footer {
z-index: 2;
height: 40px;
width: 100%;
padding: 0px;
position: relative;
background-color: #2ecc71;
/*display required to center text*/
display: table;
text-align: center;
margin-left: auto;
margin-right: auto;
}
#image {
z-index: 1;
margin: 20px auto;
padding: 50px;
}
/*Centers text within the header*/
span {
display: table-cell;
vertical-align: middle;
}
You have a lot of problems. This solution is for:
Fixing your footer at the end of the page.
Centering the contents (both vertically and horizontally).
Fixes
Get rid of display: table.
Get rid of width: 100%.
Change relative to fixed.
#footer {
z-index: 2;
line-height: 40px;
padding: 0px;
position: fixed;
background-color: #2ecc71;
text-align: center;
left: 0; right: 0;
bottom: 0;
}
<div id="footer">Copyrights.</div>
position: fixed; and bottom: 0; should do the trick. Add width and height as neccessary.
.footer {
position: fixed;
bottom: 0;
width: 100%;
height: 30px;
background-color: aquamarine;
}
<div style="background-color: lightgrey;height: 800px">
Page content
</div>
<div class="footer">
this is the footer
</div>
You can use position: fixed; bottom: 0;
#footer {
z-index: 2;
height: 40px;
width: 100%;
padding: 0px;
background-color: #2ecc71;
text-align: center;
margin-left: auto;
margin-right: auto;
position:fixed;
bottom:0;
left: 0;
}
<div>
<footer id="footer">Footer</footer>
</div>
I'm trying to create a "button" with 2 sections (each is 50% of the height of the div) separated by an horizontal bar. Each of the sections has centered text. The size of the button is going to be manipulated using javascript, and I'm trying to avoid also using javascript to position the elements inside the "button".
What I have so far is http://jsfiddle.net/u5u7d31p/2/, but i'm having a problem centering the horizontal bar. If I change the position of the separator to relative, the bar is centered, but then it changes the position of the bottom part of the text. I can also change the margin to a static value (margin: 0 63px;) to center it, but I would like to avoid it if there is an easier solution that doesn't require javascript.
.img_overlay .separator{
position: absolute;
top: -1px;
left: 0;
height: 3px;
width: 70px;
margin: 0 auto;
background: #444;
}
Any ideas? Thanks.
All codes are ok. Just put this css below to .img_overlay .separator class.
Full code is below:
.img_overlay .separator {
position: absolute;
top: -1px;
left: 0;
height: 3px;
width: 70px;
margin: 0 auto;
background: #444;
right: 0;
}
view my demo on jsfiddle
.img{
float: left;
background-repeat:no-repeat;
background-size:100% 100%;
border-radius: 4px;
width: 200px;
height: 51px;
background: red;
overflow: hidden;
}
.img_overlay{
width: 100%;
height: 100%;
background: #222;
color: #ddd;
position: relative;
opacity: 0.8;
}
.img_overlay>div{
display: block;
width: 100%;
height: 50%;
text-align: center;
position: relative;
}
.img_overlay .middle{
position: relative;
top: 50%;
transform: translateY(-50%);
}
.img_overlay .separator{
height: 3px;
width: 70px;
margin: 0 auto;
background: #444;
}
<div class="img">
<div class="img_overlay">
<div class="img_show_details">
<div class="middle">details</div>
</div>
<div class="img_open">
<div class="separator"></div>
<div class="middle">open</div>
</div>
</div>
</div>
All I did was taking off :
.img_overlay .separator{
position: absolute;
top: -1px;
left: 0;
}
This following fix works okay in firefox and chrome but mess in IE.
I fixed height in div, top in middle and top in separator
.img_overlay>div {
display: block;
width: 100%;
height: 40%;
text-align: center;
position: relative;
}
.img_overlay .middle {
position: relative;
top: 60%;
transform: translateY(-50%);
}
.img_overlay .separator {
position: relative;
top: 5px;
left: 0;
height: 3px;
width: 70px;
margin: 0 auto;
background: #444;
}
here's the demo in jsfiddle.