如何浮动两个锚点链接,水平和垂直对齐文本?

时间:2014-09-20 07:52:18

标签: html css

这是我为实现更好的解释而努力实现的形象

enter image description here

我正在处理的是:

You can see the live example of what I'm trying to copy for better explanation

Here's my code and fiddle

<div class="holdmetight">
    <div class="prev"><a href="/">TITLE OF PREVIOUS POST HERE</a></div>
    <div class="next"><a href="/">TITLE OF NEXT POST HERE</a></div>
</div>

.holdmetight {width:100%; max-width:1023px; position:relative; display:block; }
.next, .prev { min-height:35px; height:100%; text-align:center; height:100px;}
.prev { display:block; background:#CCC; width: 100% auto; }
.next {float:left; border-right:1px solid #eceff0; background: #AAA;}

2 个答案:

答案 0 :(得分:2)

以下是display: table的基本布局。

display: table的好处:

  • 使用vertical-align: middle

  • 轻松将文字垂直居中
  • 两个div都根据其他内容保持相同的高度

Have an example!

HTML / CSS

&#13;
&#13;
.holdmetight {
    display: table;
    border: solid 1px #000;
    border-top: solid 2px #000;
}

/* the > selector targets direct div children of .holdmetight */
.holdmetight > div {
    display: table-cell;
    vertical-align: middle;
    background: #aaa;
    padding: 10px;
    text-align: center;
    width: 50%;
}
.prev {
    border-right: solid 1px #000;
}
&#13;
<div class="holdmetight">
    <div class="prev">
        <a href="/">
          TITLE OF PREVIOUS POST HERE.
          This is a particulary long title 
          that will push both divs down.
        </a>
    </div>
    <div class="next">
        <a href="/">
          TITLE OF NEXT POST HERE
        </a>
    </div>
</div>
&#13;
&#13;
&#13;   

答案 1 :(得分:0)

DEMO

    .holdmetight {width:100%; max-width:1023px; position:relative; display:block; }
.next, .prev { min-height:35px; height:100%; text-align:center; height:100px; display:inline-block;}
.prev { background:#CCC; width: 100% auto; }
.next { border-right:1px solid #eceff0; background: #AAA;}

根据需要添加填充