CSS:将div高度调整为另一个包含填充

时间:2013-07-02 08:23:13

标签: css html

Final result

如图所示,我实际上有一个定义宽度为100%但没有定义高度的div,而是定义了高度以匹配内容和顶部和底部填充的百分比。 问题是我想在右侧有一些导航按钮,它们必须在垂直中间完美对齐。 我已经包含了一个我做过的小提琴,但在所有情况下都没有显示在中间。 什么是最佳解决方案。

HTML

<div class="title">Title
    <ul id="bullets">
      <li></li>
      <li></li>
    <ul>
</div>

CSS

.title {
    width:100%;
    background:#365F95;
    background-size: contain;
    font-size:130%;
    color:#FFF;
    padding:5% 0;
    text-align:center;
    position:relative;
}
.title ul {
    position:absolute;
    right: 0;
    top: 0%;
}
.title ul li {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 10px;
    background: #FFF;
    box-shadow: inset 0 1px 3px black, 0 0 1px 1px #202020;
    margin: 0 2px;
}

http://jsfiddle.net/HJLEe/2/

6 个答案:

答案 0 :(得分:2)

看看这个fiddle这个想法是垂直对齐列表

position: absolute;
top: 50%;
margin-top: - half-height;

答案 1 :(得分:1)

不是最好的解决方案,但它确实有效。

HTML:

<div class="title">Title
    <ul id="bullets">
        <li></li>
        <li></li>
    <ul>
</div>

CSS:

.title {
    width:100%;
    background:#365F95;
    background-size; cover;
    font-size:130%;
    color:#FFF;
    padding:5% 0;
    text-align:center;

}
.title ul {
position:relative;
float: right;
margin-top: 0px;
}
.title ul li {
    display: inline-block;
    width: 10px;
height: 10px;
    border-radius: 10px;
    background: #FFF;
    box-shadow: inset 0 1px 3px black, 0 0 1px 1px #202020;
    margin: 0 2px;
}

JSFIDDLE

答案 2 :(得分:0)

尝试在ul class中使用position relative

 .title ul {
 position: relative;
              }

jsfiddle.net

答案 3 :(得分:0)

试试这个css

.title ul {
position: absolute;
right: 0;
top: 0%;
padding-top: inherit;
margin: 0;
}

答案 4 :(得分:0)

尝试以百分比设置保证金。像这样......

.title ul {
    position:absolute;
    right: 0;
    top: 0%;
    vertical-align :middle;
    margin-top : 5%;
}

答案 5 :(得分:0)

使用vertical-align:middle和helper伪元素来保持高度的另一个选项。

演示:http://jsfiddle.net/HJLEe/13/

代码的基本补充:

.title ul {
    position:absolute;
    right: 0;
    top: 0;
    bottom: 0;
}

/* style of LIs not changed */

.title ul:before {
    content: '';
    display: inline-block;
    height: 100%; /* makes the effective line height equal to the height of the container */
    vertical-align: middle; /* align the vertical center with the vertical middle of the text, including our bullets */
}