简体   繁体   中英

vertical align of an icon inside div

JSFiddle demo here:
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>

What I want to achieve is that the "»" and "«" are vertically centered.
The result shall look like this:

Unfortunately I seem to be too stupid to get it done, so I hope someone here can help me. :)

use display: table-cell; & vertical-align: middle;

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

and change .post-nav-next html like below

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

working demo http://jsfiddle.net/5q0nzrw8/2/

post-nav {
    margin: 20px 0;
}

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

// use float:left to move on the left your div

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

// use float:right to move on the right your 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;
}

I would remove the icon elements completely and apply directly to their parent ( after making the position:relative ).

 /*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';} 
 <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> 

Try adding float: left; and float: right to your prev/next and then set a width for their parent:

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

.post-nav-prev, .post-nav-next {float: left; padding: 10px; border: 1px solid #aaa; background: #fff; width: 298px;} .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: 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;}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM