2014-09-20 52 views
1

这里是我想要达到更好的解释如何浮动两个锚链接,水平和垂直对齐文本?

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

这里填充是一个基本的布局与display: table。的display: table

优点:

  • 轻松垂直居中与vertical-align: middle

  • 文本两个div的维持基于其他内容

Have an example!

相同的高度

HTML/CSS

.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; 
 
}
<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>

+0

这是完美的!我已经尝试添加一个非常长的内容,它的作品完美! http://jsfiddle.net/bendaggers/wn2aznxt/16/ – 2014-09-20 08:08:24

+0

@KareenLagasca - 很高兴听到:)'display:table'并不总是得到它应得的认可。它可以非常有用! – misterManSam 2014-09-20 08:10:46

+0

当然,(CSS)表格有其自身的缺点。小心使用它们。 – 2014-09-20 08:20:43

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;} 

添加根据需要

+0

我编辑我的小提琴。你能再看看吗?并且锚内的文字应该在中间(水平和垂直对齐)。 – 2014-09-20 07:58:06