Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify right margin in fixed positioning

I have no idea how to title this properly, but here is my problem:

I have this layout:

<html>
<body>
    <div id="content">this is my page</div>
    <div id="button">magic button</div>
</body>
</html>

css:

#button {
  position: fixed;
  bottom: 20px;
  background-color: #f00;
  padding: 5px;
  left: 50%;
  margin-left: 250px;

}

html, body{
  height: 100%;
}
#content {
  margin: 0 auto;
  width: 700px;
  min-height: 100%;
  background-color: #eee;
}​

See fiddle here: http://jsfiddle.net/n6UPF/

image

My page works just as I want it, the button is exactly where I want it to be.

But if I change the text on my button, it is no longer positioned properly.

I would like to position it "fixed" relative to the right edge of my content area.

Can this be done in pure CSS?

like image 913
Sam Saffron Avatar asked Sep 18 '12 02:09

Sam Saffron


People also ask

How do you give a position to margin fixed?

You can use margin: 0 auto with position: fixed if you set left and right . This also works with position: absolute; and vertically.

Does margin work with position fixed?

Margin does not work because position of the header is fixed. You have to use padding-top on your contentwrap. Save this answer.

What is the right margin?

The margin-right property in CSS is used to set the right margin of an element. It sets the margin-area on the right side of the element. Negative values are also allowed. The default value of margin-right property is zero.

What is margin-left and margin-right?

margin-bottom : the percentage as specified or the absolute length. margin-left : the percentage as specified or the absolute length. margin-right : the percentage as specified or the absolute length. margin-top : the percentage as specified or the absolute length.


2 Answers

If modifying the HTML is acceptable, you can use a wrapper:

<div id="button-wrapper">
    <div id="button">magic button</div>
</div>
#button-wrapper {
    bottom: 40px;
    left: 50%;
    margin-left: 350px;
    position: fixed;
}

#button {
    background-color: red;
    padding: 5px;
    position: absolute;
    right: 10px;
    white-space: nowrap;
}

http://dabblet.com/gist/3740941

No, it's not really pretty, but...

like image 62
Ry- Avatar answered Oct 17 '22 04:10

Ry-


Do you mean...

#button
{
    position: fixed;
    right: 20px;
}

...or whatever distance you want on the right? Or something else?

like image 1
Mallioch Avatar answered Oct 17 '22 04:10

Mallioch