在我解释之前...

这是HTML部分:

<div class="HeadingTabs">
   <ul>
      <li><a href="http://google.com" class="TabLink">Google</a></li>
   </ul>
   <div class="TitleTab">This is some very very long title. This is some very very long title. This is a very long title.</div>
</div>


这是CSS部分:

.HeadingTabs {
    display: block;
    padding: 8px 8px 8px 2px;
    margin: 0;
    border: 0;
    background: transparent;
}

.HeadingTabs ul {
    display: inline;
    margin: 0 0 0 10px;
    float: right;
    position: absolute;
    bottom: 0;
    right: 8px;
}

.HeadingTabs li {
    display: inline;
    margin: 0;
}

.TitleTab {
    margin: 0;
    display: inline;
    font-weight: bold;
    text-decoration: none;
    padding: 5px 10px;
    line-height: 2.6;
    white-space: nowrap;

/* I haven't included the styling info like
borders and background to avoid unnecessary
distractions in code. */

}


现在...如您所见,ul元素向右浮动,并且绝对位于父div的右下角。这就是我说的“绝对定位的浮动元素”的意思。

尽管给了它一个空白,但我无法防止标题(<div class="TitleTab">元素)突入其中。下图应清楚说明。

html - 如何在绝对定位的 float 元素周围定义边距(CSS)?-LMLPHP

我想念什么?

注意事项:


我无法修改HTML。我唯一的选择就是CSS。
我希望标题环绕ul元素。所以,我不能使用宽度。
我使用position: absolute;是因为我希望ul元素保持在div的底部,就在内容div的正上方(只是图像中的截止部分)。


PS:我不太精通CSS。

最佳答案

absolute:position函数被设计为突出显示。

你应该尝试浮动元素而不是没有absolute:position

.HeadingTabs ul {
    margin:10px;
   float: right;
}


 .TitleTab {
     float:left;
     margin: 0;
     font-weight: bold;
     text-decoration: none;
     padding: 5px 10px;
     line-height: 2.6;
     white-space: nowrap; // you need to remove no wrap, so it wraps instead of cuts off
 }

10-04 22:06
查看更多