简体   繁体   English

如何把position一个元素放到右边?

[英]How to position an element to the right side?

I am trying to place a css element to the right side of the header but not sure exactly how to do it.我正在尝试将 css 元素放置到 header 的右侧,但不确定具体如何操作。 I tried using:我尝试使用:

 position: Absolute; top: 20px; right 0px; 

That would work but if you adjust the browser the text moves with it.那会起作用,但是如果您调整浏览器,文本会随之移动。

I created a JFiddle that you can find here:我创建了一个 JFiddle,您可以在这里找到它:

http://jsfiddle.net/rKWXQ/ http://jsfiddle.net/rKWXQ/

This way you can see what I am trying to do.这样你就可以看到我正在尝试做什么。 I have a text inside a wrapped div element that says Call Now (555) 555-5555.我在包装的 div 元素中有一个文本,上面写着 Call Now (555) 555-5555。

Here is the header element and inside of that I have a right_header element.这是 header 元素,其中有一个 right_header 元素。

    <div id="header">
        <span class="right_header">Call Now (555) 555-5555</span>
    </div>

Here is my Header CSS:这是我的 Header CSS:

   /* Header */
   #header {margin: auto; width: 1007px; height: 123px; background: url(../images/logo.png) no-repeat 20px; background-color: #37352b; border: 1px solid #862209;}
  .right_header{color: #fff; position: absolute; top: 70px; right: 0px}

Can someone please tell me the proper way to accomplish this please?有人可以告诉我完成此操作的正确方法吗?

Note the left side will have a logo there that will not load in JFiddle!请注意,左侧将有一个不会在 JFiddle 中加载的徽标!

Thanks!谢谢!

You can easily just float it to the right, no need for relative or absolute positioning. 您可以轻松地将其float到右侧,无需relativeabsolute定位。

.right_header {
    color: #fff;
    float: right;
}

Updated jsFiddle - might need to add some padding / margins - like this . 更新了jsFiddle - 可能需要添加一些padding / margins - 像这样

Two more ways to do it:还有两种方法:

  1. Using margins on the element you want to position to the right of its parent.在您想要 position 到其父元素右侧的元素上使用边距。
.element {
  margin-right: 0px;
  margin-left: auto;
}
  1. Using flexbox on the parent element:在父元素上使用 flexbox:
.parent {
  display: flex;
  justify-content: right;
}

As JoshC mentioned, using float is one option. 正如JoshC所说,使用float是一种选择。 I think your situation suggests another solution, though. 我认为你的情况提出了另一种解决方案。

You need to set position: relative on your #header element in order for the position: absolute on your #right_header to take effect. 您需要在#header元素上设置position: relative ,以使#right_header上的position: absolute生效。 once you set that, you are free to position #right_header however you want relative to #header 一旦你设置了它,你可以自由地定位#right_header但是你想要相对于#header

You can do this way also if you want to do with position, Try this please 你也可以这样做,如果你想做位置,请试试这个

 #header {margin: auto; position:relative; width: 1007px; height: 123px; background: url(../images/logo.png) no-repeat 20px; background-color: #37352b; border: 1px solid #862209;}


.right_header{color: #fff; position: absolute; top: 0px; right: 0px}

The answer using floats from JoshC will work fine, however, I think there is a reason this is not working. 使用来自JoshC的浮点数的答案可以正常工作,但是,我认为这是有原因的。

The reason your code does not work, is that the absolute position is relative to the which has dimensions of 0x0. 你的代码不起作用的原因是绝对位置是相对于维度为0x0的。

The '' should be absolutely position in order for this code to work. ''应该是绝对的位置,以便此代码可以工作。

#header {margin: auto; width: 1007px; height: 123px; background: url(../images/logo.png) no-repeat 20px; background-color: #37352b; border: 1px solid #862209;}

change it to... 把它改成......

#header {margin: auto; position: absolute; left: 0px; right: 0px; top 0px; height: 123px; background: url(../images/logo.png) no-repeat 20px; background-color: #37352b; border: 1px solid #862209;}

<div><button>Continue</button></div> to make button on the right of div <div><button>Continue</button></div>在div右边制作按钮

<style>
button {
 display:block;
 margin-left:auto;
}
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM