设置绝对仓位和保证金
我想将元素的 position
设置为 absolute
并设置 margin-bottom
,但似乎 margin-bottom< /code> 没有效果。
HTML:
<div id='container'></div>
CSS:
#container {
border: 1px solid red;
position: absolute;
top: 0px;
right: 0px;
width: 300px;
margin-bottom: 50px; // this line isn't working
}
I would like to set an element's position
to absolute
and have a margin-bottom
, but it seems that the margin-bottom
doesn't have an effect.
HTML:
<div id='container'></div>
CSS:
#container {
border: 1px solid red;
position: absolute;
top: 0px;
right: 0px;
width: 300px;
margin-bottom: 50px; // this line isn't working
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我知道这不是一个非常及时的答案,但有一种方法可以解决这个问题。您可以在绝对定位的元素内添加一个“间隔”元素,该元素具有负底部边距,并且高度与负底部边距大小相同。
HTML:
CSS:
I know this isn't a very timely answer but there is a way to solve this. You could add a "spacer" element inside the element positioned
absolute
ly with a negative bottom margin and a height that is the same size as the negative bottom margin.HTML:
CSS:
基于 Joey 的回答,为了获得更清晰的标记,请使用 CSS
:after
-选择器添加用于所需底部边距的垫片。CSS
Building upon Joey's answer, for a cleaner markup, use the CSS
:after
-selector to add a spacer for the desired bottom margin.CSS
当您的元素位于其顶部时,您希望
margin-bottom
做什么?如果元素没有
top
属性,则margin-bottom
只会对绝对定位的元素执行任何操作。What are you expecting
margin-bottom
to do when your element is positioned by its top side?margin-bottom
will only do anything to anabsolute
ly-positioned element if the element has notop
property.您需要将
position
设置为relative
才能设置其margin
。margin-bottom
、margin-left
和margin-right
当元素处于位置:绝对
时,将不工作。例子:
HTML:
CSS:
You need to set the
position
torelative
in order to set itsmargin
.The
margin-bottom
,margin-left
andmargin-right
will NOT work when the element is inposition: absolute
.Example:
HTML:
CSS:
我有一个绝对位置,需要在右侧设置边距。
这是我想出的方法:
I have an absolute position and need to set a margin in right side.
here the way i come up with:
对于某些用例,将
position:absolute
更改为position:relative
也可以解决此问题。如果你能改变一下自己的定位,这将是解决问题的最简单的方法。否则,乔伊的垫片就可以了。For some use cases, changing
position: absolute
toposition: relative
solves this problem as well. If you can change your positioning, it will be the simplest way to solve the problem. Otherwise, the spacer of Joey does the trick.我已经找到解决办法了!!!
设置容器的最小高度,位置需要从绝对位置更改为继承,将顶部属性设置为您选择的值,并且显示需要为内联块。
当我尝试使用绝对位置、移动具有 top 属性的元素并尝试添加边距时,这对我有用。
I have found the solution!!!
set a min-height of the container, the position will need to be changed from absolute to inherit, set the top property to a value of your choice and the display will need to be inline-block.
This worked for me when I tried using position absolute, moving the element with top property and try to add a margin.
由于我事先不知道高度,因此我在绝对定位的末尾添加了一个不可见的元素,其高度是我想要的边距。
在我的例子中,绝对定位的元素是一个表格,所以我添加了一个额外的空行,高度为 100px。然后,本应位于表格底部的边框改为“tr:nth-last-of-type(2) td”。
希望这有帮助。
Since I don't know the height in advance, I instead added an invisible element at the end of the absolutely positioned with a height that I wanted the margin to be.
In my case, the absolutely positioned element is a table, so I added an extra empty row with a height of 100px. Then the border that was supposed to be at the bottom of the table went on "tr:nth-last-of-type(2) td" instead.
Hope this helps.
将其放在其他元素中并为其设置边距:
Put it in other element and set margin to that:
如果用padding就可以了。
尝试
:after
伪类。If it's okay to use padding.
Try
:after
pseudo class.您可以简单地在 Container 元素内创建一个 div 元素,为其指定相对位置并使用 Bottom 属性。
You can simply create a div element inside the Container element, give it a position relative and use the bottom property.