Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right align fixed position div inside a div

Tags:

html

css

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.

like image 960
Ravish Kumar Avatar asked Mar 01 '13 09:03

Ravish Kumar


People also ask

How do I align a div inside another div to the right?

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.

How do you fix an element inside a div?

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.

How do you get a div to stay in place?

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.

How do I make a div align?

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.


2 Answers

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.

like image 54
Billy Moat Avatar answered Sep 17 '22 19:09

Billy Moat


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

like image 45
Sowmya Avatar answered Sep 18 '22 19:09

Sowmya