简体   繁体   中英

center div in the vertically in middle of another div

I would like to centre the header-headline div vertically in the middle of the header div but no matter what i try i cannot get it to sit in the middle. Can anyone advise?

HTML

<div class="main-wrapper-onepage">
<!-- header -->
<div class="header">
<div class="header-headline">Dan Morris</div>
</div>
<!-- header-end -->
</div>

CSS

.header {
    width:100%;
    height:667px;
    background-image:url(../images/header_background.jpg);
    background-size:cover;
    text-align:center;
}

.header-headline {
    width:100%;
    text-align:center;
    font-size:70px;
    color:#FFF;
    font-family: 'Montserrat', Helvetica, Arial, sans-serif;
    display:inline-block;
    vertical-align:top;

}

JSfiddle here: https://jsfiddle.net/w77pdnxh/

You could vertically center the header using flexbox's align-items: center . Code as follows:

.header {
  ...
  display: flex;
  align-items: center;
}

JSFiddle Here

Set the position of header-header to relative and then top:50% as shown in the jsfiddle below. header-header will be relative to the outer div and placed at a point halfway between the top and bottom.

https://jsfiddle.net/w77pdnxh/2/

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