Skip to main content
Participant
Jan 23, 2018 11:34 PM UTC
Answered

How to position image for it to always be in the middle of the page?

I know how to center an image with bootstrap .center-block but how do I center an image on a whole page?

This topic is closed to new replies. Start a new post to keep the conversation going.
osgood_Correct answer
Legend
Jan 24, 2018 11:21 AM UTC

If you just want an image centered vertically and horizontally on a page all by itself you can use Flexbox. Flexbox is supported in 97.5% of modern browsers - I cant think of a reason not to use it. Its been a main-stream workflow for a couple of years now in the professional developer world, so use it unless you need to support IE8 or IE9, which many have already dumped a long time ago.

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Flexbox - Center Vertically and Horizontally</title>

<style>

body {

display: flex;

align-items: center;

justify-content: center;

height: 100vh;

overflow:hidden;

}

</style>

</head>

<body>

<img src="images/image.jpg" alt="">

</body>

</html>

10 replies

Participant
Jan 24, 2018 5:28 AM UTC

Still not centered... My index at: Index - Pastebin.com

This is what I get with your code:

Translate
B i r n o u
Legend
Jan 24, 2018 7:21 AM UTC

I just copy paste your code, into a blank document, place Dw in live view... and the image is centered.... but ... there is a but... if your screen resolution is wider than 800 ... necessarily this will block the width of the container and will cause a misaligned ...
same for the height ....
so in this case, and to fit the screen ... replace the container width and height CSS by

height: 100vw; 

width: 100vw; 

Translate
Participant
Jan 24, 2018 4:40 PM UTC

That worked but there is one problem...

Translate
Nancy OShea
Community Expert
Community Expert
Jan 24, 2018 12:09 AM UTC

Which version of Bootstrap are you using -- 3.7 or the newest 4?

Translate
Nancy O'Shea— Product User & Community Expert
Participant
Jan 24, 2018 12:30 AM UTC

3.3.7

Translate
BenPleysier
Community Expert
Community Expert
Jan 24, 2018 4:03 AM UTC

Please give show us the code - HTML and the relevant CSS

Translate
Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!