简体   繁体   中英

Vertically align :after pseudo element

I'm trying to move an :after pesudo element to come at the end of a div, sort of like a footer.

<div class="box">
    Some content here
</div>

.box {
     height: 60vh;
}

.box:after {
     content: "";
     vertical-align: bottom;
}

However, the pseudo content is always placed imediately after "Some content here", rather than as a footer for the .box... any ideas?

You can use position property to put :after like a footer:

.box {
    height: 60vh;
    border: 1px #AAA solid;
    position: relative;     // <-- relative
}
.box:after {
    content: "test";
    position: absolute;    // <-- absolute
    bottom: 0;
    right: 0;
    left: 0;
    background: #DDD;
}

Demo: http://jsfiddle.net/oc45nyux/

Simply add display: block; to :after and remove vertical-align: bottom;

.box:after {
     display: block;
     content: " ";
}

Vertical-align only works with two scenarios: inline-block or table-cell

.box:after {
     content: "";
     vertical-align: bottom;
     display: inline-block;
     height: 100%; /* also add this line of code to make it work*/
}

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