div内的图标垂直对齐

时间:2014-12-21 14:51:29

标签: html css

这里的JSFiddle演示:
http://jsfiddle.net/5q0nzrw8/

CSS:

.post-nav {margin: 20px 0;}

.post-nav-prev, .post-nav-next {float: left; padding: 10px; border: 1px solid #aaa; background: #fff; width: 298px;}
.post-nav-prev {text-align: left;}
.post-nav-next {text-align: right; margin-left: 20px;}

.post-nav-prev a, .post-nav-next a {font-family: Arial; font-weight: 500; font-size: 20px; -webkit-transition: all 0.4s ease; transition: all 0.4s ease; text-decoration: none;}
.post-nav-prev a:hover, .post-nav-next a:hover {color: #000;}

.post-nav-prev-icon, .post-nav-next-icon {}
.post-nav-prev-icon:before, .post-nav-next-icon:before {font-family: Arial; font-size: 28px; color: #aaa;}
.post-nav-prev-icon:before {float: left; content: '\00AB'; padding-right: 10px;}
.post-nav-next-icon:before {float: right; content: '\00BB'; padding-left: 10px;}

HTML:

<div class="post-nav">
    <div class="post-nav-prev">
        <span class="post-nav-prev-icon"></span>
        <a href="#" rel="prev">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod.</a>
    </div>
    <div class="post-nav-next">
        <span class="post-nav-next-icon"></span>
        <a href="http://mydivision.net/2014/11/the-division-user-interface-styleguide-geleakt/" rel="next">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod</a>
    </div>
</div>

我想要达到的目的是“»”和“«”是垂直居中的 结果应如下所示:

不幸的是,我似乎太愚蠢了,所以我希望有人可以帮助我。 :)

4 个答案:

答案 0 :(得分:6)

使用display: table-cell;&amp; vertical-align: middle;

添加以下css

.post-nav-prev-icon,.post-nav-next-icon{
 display: table-cell;
 vertical-align: middle;}

.post-nav-prev a,.post-nav-next a{display: table-cell;}

并更改.post-nav-next html,如下所示

<div class="post-nav-next">
<a href="http://mydivision.net/2014/11/the-division-user-interface-styleguide-geleakt/" rel="next">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod</a>

<span class="post-nav-next-icon"></span>
</div>

工作演示 http://jsfiddle.net/5q0nzrw8/2/

答案 1 :(得分:1)

post-nav {
    margin: 20px 0;
}

.post-nav-prev, .post-nav-next {
    float: left; 
    padding: 10px; 
    border: 1px solid #aaa;
    background: #fff; width: 280px;
}

//使用float:left左移你的div

.post-nav-prev {
    text-align: left;
    float: left
}

//使用float:right向右移动你的div

.post-nav-next {
    text-align: right;
    margin-left: 20px;
    float: right
}

.post-nav-prev a, .post-nav-next a {
    font-family: Arial;
    font-weight: 500; font-size: 20px;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
    text-decoration: none;
}

.post-nav-prev a:hover, .post-nav-next a:hover {
    color: #000;
}

.post-nav-prev-icon, .post-nav-next-icon {
}

.post-nav-prev-icon:before, .post-nav-next-icon:before {
    font-family: Arial;
    font-size: 28px;
    color: #aaa;
}

.post-nav-prev-icon:before {
    float: left;
    content: '\00AB';
    padding-right: 10px;
}

.post-nav-next-icon:before {
    float: right;
    content: '\00BB';
    padding-left: 10px;
}

答案 2 :(得分:1)

我会完全删除图标元素并直接应用于其父级(在制作position:relative 之后)。

&#13;
&#13;
/*mini reset of box-sizing for current elements*/
.post-nav *{
    -webkit-box-sizing:border-box;
    -moz-box-sizing:border-box;
    -ms-box-sizing:border-box;
    -o-box-sizing:border-box;
    box-sizing:border-box;
}

.post-nav {margin: 20px 0;}

.post-nav-prev, .post-nav-next {float: left; padding: 10px; border: 1px solid #aaa; background: #fff; width: 298px;position:relative;}
.post-nav-prev {text-align: left;padding-left:30px;}
.post-nav-next {text-align: right; margin-left: 20px;padding-right:30px;}

.post-nav-prev a, .post-nav-next a {font-family: Arial; font-weight: 500; font-size: 20px; -webkit-transition: all 0.4s ease; transition: all 0.4s ease; text-decoration: none;}
.post-nav-prev a:hover, .post-nav-next a:hover {color: #000;}


.post-nav-prev:before, .post-nav-next:before {
    font-family: Arial; 
    font-size: 28px; 
    color: #aaa;
    display:block;
    position:absolute;
    top:0;
    bottom:0;
    line-height:1.5em;
    height:1.5em;
    width:30px;
    margin:auto;
    text-align:center;
}
.post-nav-prev:before {left:0; content: '\00AB';}
.post-nav-next:before {right:0; content: '\00BB';}
&#13;
<div class="post-nav">
  <div class="post-nav-prev">
    <a href="#" rel="prev">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod.</a>
  </div>
  <div class="post-nav-next">
    <a href="http://mydivision.net/2014/11/the-division-user-interface-styleguide-geleakt/" rel="next">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod</a>
  </div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

尝试添加float:left;和浮动:右边的prev / next,然后为其父级设置宽度:

.post-nav {margin:20px 0; width:700px;}

.post-nav-prev,。post-nav-next {float:left;填充:10px; border:1px solid #aaa;背景:#fff;宽度:298像素;} .post-nav-prev {text-align:left; float:left;} .post-nav-next {text-align:right; margin-left:20px; float:right;}

.post-nav-prev a,。post-nav-next a {font-family:Arial; font-weight:500; font-size:20px; -webkit-transition:全部0.4s轻松;过渡:全部0.4秒轻松; text-decoration:none;} .post-nav-prev a:悬停,。post-nav-next a:悬停{color:#000;}

    .post-nav-prev-icon, .post-nav-next-icon {}
    .post-nav-prev-icon:before, .post-nav-next-icon:before {font-        family: Arial; font-size: 28px; color: #aaa;}
.post-nav-prev-icon:before {float: left; content: '\00AB'; padding-right: 10px;}
.post-nav-next-icon:before {float: right; content: '\00BB'; padding-left: 10px;}

http://jsfiddle.net/5q0nzrw8/3/