Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Raaah! Frustration with centering DIV

Featured Replies

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

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.

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.