- CSS vertically align a background image that has background-size: cover
- CSS Style
- HTML Body
- Related
- background-position-y
- Try it
- Syntax
- Values
- Formal definition
- Formal syntax
- Examples
- Basic example
- HTML
- CSS
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- Absolute Center (Vertical & Horizontal) an Image
- Comments
CSS vertically align a background image that has background-size: cover
The following tutorial shows you how to use CSS to do «CSS vertically align a background image that has background-size: cover».
CSS Style
The CSS style to do «CSS vertically align a background image that has background-size: cover» is
div < background:url(http://placekitten.com/400/400); background-size:cover; width:300px; height:150px; background-position:0% 30%; >
HTML Body
The following iframe shows the result. You can view the full source code and open it in another tab.
html> head> meta name="viewport" content="width=device-width, initial-scale=1"> style id="compiled-css" type="text/css"> div < background: url(http://placekitten.com/400/400); background-size: cover; width: 300px; height: 150px; background-position: 0% 30%; > body> div>
Related
- CSS vertical align my images with Texts (Demo 2)
- CSS vertical align text with center of image in HTML
- CSS vertical align this images
- CSS vertically align a background image that has background-size: cover
- CSS vertically align a DIV next to an image
- CSS vertically align a form and image
- CSS vertically align an image in unknown size to the center of a div
demo2s.com | Email: | Demo Source and Support. All rights reserved.
background-position-y
The background-position-y CSS property sets the initial vertical position for each background image. The position is relative to the position layer set by background-origin .
Try it
The value of this property is overridden by any declaration of the background or background-position shorthand properties applied to the element after it.
Syntax
/* Keyword values */ background-position-y: top; background-position-y: center; background-position-y: bottom; /* values */ background-position-y: 25%; /* values */ background-position-y: 0px; background-position-y: 1cm; background-position-y: 8em; /* Side-relative values */ background-position-y: bottom 3px; background-position-y: bottom 10%; /* Multiple values */ background-position-y: 0px, center; /* Global values */ background-position-y: inherit; background-position-y: initial; background-position-y: revert; background-position-y: revert-layer; background-position-y: unset;
The background-position-y property is specified as one or more values, separated by commas.
Values
Aligns the top edge of the background image with the top edge of the background position layer.
Aligns the vertical center of the background image with the vertical center of the background position layer.
Aligns the bottom edge of the background image with the bottom edge of the background position layer.
The offset of the given background image’s horizontal edge from the corresponding background position layer’s top horizontal edge. (Some browsers allow assigning the bottom edge for offset).
The offset of the given background image’s vertical position relative to the container. A value of 0% means that the top edge of the background image is aligned with the top edge of the container, and a value of 100% means that the bottom edge of the background image is aligned with the bottom edge of the container, thus a value of 50% vertically centers the background image.
Formal definition
Initial value | 0% |
---|---|
Applies to | all elements |
Inherited | no |
Percentages | refer to height of background positioning area minus height of background image |
Computed value | A list, each item consisting of: an offset given as a combination of an absolute length and a percentage, plus an origin keyword |
Animation type | a repeatable list of |
Formal syntax
background-position-y =
[ center | [ [ top | bottom | y-start | y-end ]? ? ]! ]#
=
|
Examples
Basic example
The following example shows a simple background image implementation, with background-position-x and background-position-y used to define the image’s horizontal and vertical positions separately.
HTML
CSS
div width: 300px; height: 300px; background-color: skyblue; background-image: url(https://mdn.dev/archives/media/attachments/2020/07/29/17350/3b4892b7e820122ac6dd7678891d4507/firefox.png); background-repeat: no-repeat; background-position-x: center; background-position-y: bottom 10px; >
Result
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 18, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
Absolute Center (Vertical & Horizontal) an Image
Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.
Comments
columna uno columna dos, la cual es bastante más alta que la anterior, y además la podemos hacer todavía más alta y la columna 1 ya no se centra
div.horizontal < display: flex; justify-content: center; >div.vertical < display: flex; flex-direction: column; justify-content: center; >
good one! but the problem is that if you re size the browser you can’t scroll to the top. i.e if i have a menu on top i cant view it!
Why does everyone in the universe write background shorthand wrong? This is the correct way and the only way that works in ALL browsers that support background shorthand: background: #FFF url() repeat fixed left top; /rant
Craig, syntactically, you can have your properties in any arrangement: background: #fff url() fixed left top; is essentially the same as background: url() fixed 0 0 #fff; either way you look at it, it’s doing the same exact thing.
#arrr < position:absolute; top:0; left:0; width:100%; height:100%;>#black-flag < position:absolute; left:50%; bottom:145px;>#black-flag img
YOU ARE A KING MAN I would advice THIS PAGE for anyone whos searching for a solution regarding absolute centering of Image inside a DIV. WOOOOW!
what happen when you have different image size but whatever the image sizes you want all them in center. normally in jquery gallery case. i am struggling with this one from last two days. any help will be highly appreciated.
document.getElementById('myimage').style.margin-top = document.getElementById('myimage').style.height / 2; document.getElementById('myimage').style.margin-left = document.getElementById('myimage').style.width/ 2;
and if you wanted to do a bunch of them you would have to make an array maybe and cycle through them? O.o anyone else?
.
What about a pure CSS solution for vertically centering an image in a div that is POSITION:FIXED
.
Thanks a lot!
Hi, I have posted about 50 post to my blog. Now I have to change my template blog with a new one. I have upload the picture too.
The problem with my new template is my picture that I have uploaded before its not in the position I wanted. I have to re-upload the picture so its fix to my new template. Is there any can help me with this. So the picture that I have upload before its force to (300X300 pixel). Thank you before.
Here’s a simple jQuery code that checks the image size and then centers it… perfect for when you don’t know what the size of the image would be. https://github.com/devasaur/vlign.js/blob/master/vlign.js
I LOVE YOU!! Ahem, thanks heaps. I was trying to figure out how to place a graphic using absolute position on a centered body container. I realized you made the graphic centered by ‘left: 50%’ and then using my container width I divided by 2 and altered the measurements to get a perfect fit ;). ‘left: 50%’ is a very important element when using absolute position. Dammit, if I only knew this before then I wouldn’t change my design and css code :(. Oh well, you live and learn 😀 Thanks again and much appreciated.
CHEERS 🙂
I want an image in the background of my webpage (top center). Different image on every page.
On top of that I want to have a 1000px wide Div in the center of the page with the content.
I want it all to stay in the middle when I resize the browser-window.
How?
position: relative; top: 50%; margin-top: -25%;
This worked for me to align vertically center. I can’t use half height bacuase i dont know the height. So i used percentage
Thank you, Matthew Buchanan! This is perfect — nice and clean, and works for varying image sizes. It works for me in Firefox, Chrome, Safari, Opera, and IE 8 and 9.
Hey Matthew it worked for me to center image both vertically and horizontally within a container. Thanks 🙂
@charset "utf-8"; /* CSS Document */ * < margin:0; padding:0; >html, body < height: 100%; min-height:100% !important; width:100%; >#cuerpo < position:absolute; top: 50%; /*esta a la mitad*/ margin-top: -25%; /*le resta la mitad del alto*/ width: 700px; height: 500px; width: 100%; >#header < position:relative; margin: 0 auto 0 auto; width: 700px; height: 106px; background:#09F; >#contenido
Thanks for the tutorial, I am trying to place a paragraph below the centralised image, but when I do this the paragraph remains at the top left hand side of the browser window. Is there a way around this?
Based on Chris’ table approach, this worked great for me… html, body, #wrapper <
height:100%;
width: 100%;
margin: 0;
padding: 0;
border: 0;
>
#wrapper td <
vertical-align: middle;
text-align: center;
> .content <
width: 600px;
height: 400px;
background-color: red;
margin-left: auto;
margin-right: auto;
> And the html…
Great article! I love your site, it’s bookmarked : )
I use your method of absolute centering in all my websites.
/* Make the height 100% */
#container ul li display:inline-block;
height:100%;
>
/* Then vertical-align the crap outta it */
#container ul li img vertical-align:middle;
>
/* NOTE: The images don’t have fixed sizes either 🙂 This works in Firefox, been using it for a while so I must have cross browser tested it but can’t remember anymore. Give it go! */
Better and complete: style for a extensible box with title text and one image inside.
The position: relative and the width are not an obligation. div.frameStretch -moz-border-image: url(yourimageborder.png) 24 stretch; -moz-border-radius: 24px;
-webkit-border-image: url(yourimageborder2.png) 24 stretch; -webkit-border-radius: 24px;
-o-border-image: url(.yourimageborder.png) 24 stretch; -o-border-radius: 24px;
-ms-border-image: url(yourimageborder.png) 24 stretch; -ms-border-radius: 24px;
border-image: url(yourimageborder.png) 24 fill stretch; border-radius: 24px;
box-pack: center; box-shadow: 0px 1px 3px #122c53; text-shadow: -1px -1px 1px #cccccc, 1px 1px 1px #ffffff;> /* — For the title in the box — */
div.frameStretch h2
/* — To one image inside, vertical align, at right of the box —*/
div.frameStretch img
That background image is simply a brilliant kiss technique. Just wanted to drop a line, to say thanks a lot for this and so many other useful posts.
Hello, it’s uses basically a how center images , and it can take a server timestamp to start with. The css can also be easily modified to one’s own likings. http://www.phphunt.com/111/how-to-center-images
Hi Chris – or any genius out there, is there a “responsive” way of doing this? So when you resize your browser using “img