My code is like this.
<div class="outer">
<div class="inner">some text here
</div>
</div>
CSS:
.outer {
max-width:1024px;
background-color:red;
margin:0 auto;
}
.inner {
width:50px;
border:1px solid white;
position:fixed;
}
The inner div
, which is positioned left
by default, needs to be positioned against the right
w.r.t. the outer div
and not to the page itself.
If I style it to be right:0px;
, then it aligns 0px
from the browser's right, not the outer div
's right.
NOTE : My outer div
is not fixed width; it adjust as per screen resolution. It's a responsive website design.
Use the "inline-block" value of the display property to display the inner <div> as an inline element as well as a block. Set the text-align property on the outer <div> element to center the inner one. This property only works on inline elements.
Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.
Use position fixed. Position fixed makes it so that an element stays at it's specified place even if the user scrolls up or down.
You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.
Two options. Simply float the inner div right or else use absolute positioning to achieve it.
For floating simply set float:right on the inner DIV and overflow:hidden on the outer DIV.
For absolute positioning simply set position:relative on the outer DIV and set position: absolute and right:0 top:0 on the inner DIV.
Why don't you use position:absolute
position:fixed
always relative to the browser
.outer {
width:200px;
height:600px;
background-color:red;
margin:0 auto;
position:relative
}
.inner {
width:50px;
border:1px solid white;
position:absolute; right:0
}
DEMO
If it is compulsory to use position:fixed
then you can assign the margin-left
value, since you are using fixed with for both the divs.
.inner {
width:50px;
border:1px solid white;
position:fixed; margin-left:150px
}
DEMO 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With