March 12, 201511 yr I have a containing div that holds a background image. Within this I have another div containing text which I want to center horizontally and vertically. I have played around for a while and am not getting the result I want. I can center the text using text-align: center to the container div, but if I add a padding-top to this div the background image starts to zoom in. Any ideas? <div class="header-background-image" style='background-image: url(<?php header_image(); ?>)'> <div class="container"> <?php dynamic_sidebar( 'home-page-banner' ); ?> </div> </div> header-background-image .header-background-image { padding-bottom: 25%; background: no-repeat center; background-size: cover!important; -moz-background-size: cover!important; -webkit-background-size: cover!important; } container (bootstrap) .container { padding-right:15px; padding-left:15px; margin-right:auto; margin-left:auto; }
March 12, 201511 yr Firstly you don't need all those importants on the background size. If running something like autoprefixer (You should be) then you don't need to worry about vendor prefixes, it will add them where needed. I can see you are using the padding-bottom % hack to make the header-background-image div maintain it's aspect ratio. But then you are placing content inside of this, this kind of messes that up. That content should be positioned absolutely inside of the container, then you can use a transform to get it posiioned in the middle, .header-background-image { position: relative; background-size: no-repeat; background-position: center center; background-size: cover; } .header-background-image__container { position: absolute; top: 50%; left: 50%; padding: 0 15px; transform: translate3d(-50%, -50%, 0); } Don't use the bootstrap container class for this inner container.
March 12, 201511 yr Author Thanks Web Guru, I see what your saying now, but unfortunately the image disappears now and I believe because there is no height specified to the div with the background image. If I add a height to header-background-image the image becomes un-responsive? <div class="header-background-image" style='background-image: url(<?php header_image(); ?>)'> <div class="header-background-image__container"> <?php dynamic_sidebar( 'home-page-banner' ); ?> </div> </div> .header-background-image { position: relative; background-size: no-repeat; background-position: center center; background-size: cover; } .header-background-image__container { position: absolute; top: 50%; left: 50%; padding: 0 15px; transform: translate3d(-50%, -50%, 0); }
Create an account or sign in to comment