设置绝对仓位和保证金

发布于 2025-01-07 09:56:55 字数 418 浏览 1 评论 0原文

我想将元素的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

木有鱼丸 2025-01-14 09:56:55

我知道这不是一个非常及时的答案,但有一种方法可以解决这个问题。您可以在绝对定位的元素内添加一个“间隔”元素,该元素具有负底部边距,并且高度与负底部边距大小相同。

HTML:

<div id="container">
    <div class="spacer"></div>
</div>

CSS:

// same container code, but you should remove the margin-bottom as it has no effect

// spacer code, made it a class as you may need to use it often
.spacer {
    height: 50px;
    margin: 0 0 -50px 0;
    /* margin: 20px 0 -50px 0; use this if you want #container to have a 'bottom padding', in this case of 20px */
    background: transparent; /* you'll need this if #container's parent element has a different background from #container itself */
}

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 absolutely with a negative bottom margin and a height that is the same size as the negative bottom margin.

HTML:

<div id="container">
    <div class="spacer"></div>
</div>

CSS:

// same container code, but you should remove the margin-bottom as it has no effect

// spacer code, made it a class as you may need to use it often
.spacer {
    height: 50px;
    margin: 0 0 -50px 0;
    /* margin: 20px 0 -50px 0; use this if you want #container to have a 'bottom padding', in this case of 20px */
    background: transparent; /* you'll need this if #container's parent element has a different background from #container itself */
}
何止钟意 2025-01-14 09:56:55

基于 Joey 的回答,为了获得更清晰的标记,请使用 CSS :after-选择器添加用于所需底部边距的垫片。

CSS

#container:after {
    position: absolute;
    content: "";
    bottom: -40px;
    height: 40px;
    width: 1px;
}

Building upon Joey's answer, for a cleaner markup, use the CSS :after-selector to add a spacer for the desired bottom margin.

CSS

#container:after {
    position: absolute;
    content: "";
    bottom: -40px;
    height: 40px;
    width: 1px;
}
以往的大感动 2025-01-14 09:56:55

当您的元素位于其顶部时,您希望 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 an absolutely-positioned element if the element has no top property.

软甜啾 2025-01-14 09:56:55

您需要将position设置为relative才能设置其margin

margin-bottommargin-leftmargin-right当元素处于位置:绝对时,工作。

例子:
HTML:

<img src="whatever.png"/>

CSS:

img {
   position: relative;
   margin: 0 auto;
}

You need to set the position to relative in order to set its margin.

The margin-bottom, margin-left and margin-right will NOT work when the element is in position: absolute.

Example:
HTML:

<img src="whatever.png"/>

CSS:

img {
   position: relative;
   margin: 0 auto;
}
谁把谁当真 2025-01-14 09:56:55

我有一个绝对位置,需要在右侧设置边距。
这是我想出的方法:

position:absolute;
  top:0px;
  left:10px;  
  width: calc(99% - 10px);
  height:80%;

I have an absolute position and need to set a margin in right side.
here the way i come up with:

position:absolute;
  top:0px;
  left:10px;  
  width: calc(99% - 10px);
  height:80%;
二智少女 2025-01-14 09:56:55

对于某些用例,将 position:absolute 更改为 position:relative 也可以解决此问题。如果你能改变一下自己的定位,这将是解决问题的最简单的方法。否则,乔伊的垫片就可以了。

For some use cases, changing position: absolute to position: 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.

鱼窥荷 2025-01-14 09:56:55

我已经找到解决办法了!!!

设置容器的最小高度,位置需要从绝对位置更改为继承,将顶部属性设置为您选择的值,并且显示需要为内联块。

当我尝试使用绝对位置、移动具有 top 属性的元素并尝试添加边距时,这对我有用。

min-height: 42px;
display: inline-block;
top: 6px;
position: inherit;

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.

min-height: 42px;
display: inline-block;
top: 6px;
position: inherit;
冷情 2025-01-14 09:56:55

由于我事先不知道高度,因此我在绝对定位的末尾添加了一个不可见的元素,其高度是我想要的边距。

在我的例子中,绝对定位的元素是一个表格,所以我添加了一个额外的空行,高度为 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.

ぃ双果 2025-01-14 09:56:55

将其放在其他元素中并为其设置边距:

.nav-item {
  margin: 0;
  margin-right: 10px;
  margin-left: 10px;
  display: inline;
  padding: 0 20px;
}

.nav-link {
  position: absolute;
  color: #000;
  transition: all 0.5s ease;
  text-decoration: none;

}

.nav-link:hover {
  color: #14aae6;
  transform: translateY(-.35em) scale(1.1);
}
<ul>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 1 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 2 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 3 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 4 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 5 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 6 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 7 </a>
  </li>
</ul>

Put it in other element and set margin to that:

.nav-item {
  margin: 0;
  margin-right: 10px;
  margin-left: 10px;
  display: inline;
  padding: 0 20px;
}

.nav-link {
  position: absolute;
  color: #000;
  transition: all 0.5s ease;
  text-decoration: none;

}

.nav-link:hover {
  color: #14aae6;
  transform: translateY(-.35em) scale(1.1);
}
<ul>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 1 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 2 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 3 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 4 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 5 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 6 </a>
  </li>
  <li class="nav-item">
    <a class="nav-link" href="#"> Item 7 </a>
  </li>
</ul>

靖瑶 2025-01-14 09:56:55

如果用padding就可以了。

尝试 :after 伪类。

#parent {
  padding-bottom: 10px;
  background: gray;
  width: 300px;
  height: 100px;
}

#child {
  position: relative;
  width: 100%;
  height: 100%;
}

#child:after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: yellow;
}
<div id='parent'>
  <div id='child'></div>
</div>

If it's okay to use padding.

Try :after pseudo class.

#parent {
  padding-bottom: 10px;
  background: gray;
  width: 300px;
  height: 100px;
}

#child {
  position: relative;
  width: 100%;
  height: 100%;
}

#child:after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: yellow;
}
<div id='parent'>
  <div id='child'></div>
</div>

浪菊怪哟 2025-01-14 09:56:55

您可以简单地在 Container 元素内创建一个 div 元素,为其指定相对位置并使用 Bottom 属性。

<div id="container">
    <div class="spacer"></div>
</div>




.spacer{
position: relative;
bottom: 2px;
}

You can simply create a div element inside the Container element, give it a position relative and use the bottom property.

<div id="container">
    <div class="spacer"></div>
</div>




.spacer{
position: relative;
bottom: 2px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文