I have a div and I need to get a small background image into the vertical center of that DIV using CSS.
Can anyone help please?
Thanks.
Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.
First, set a background image using the background-image property. Next, set the background-repeat property to no-repeat . Then, write the fixed option for the background-attachment property. After that, apply the background-position property to the center center option and the background-size property to cover .
Answer: Use the CSS vertical-align Property You can align an image vertically center inside a <div> by using the CSS vertical-align property in combination with the display: table-cell; on the containing div element.
To vertically center:
#con {
background: url(image.jpg) no-repeat left center;
}
To horizontally center:
#con {
background: url(image.jpg) no-repeat center top;
}
#somediv {
background: url (image.jpg) no-repeat 50% 50%;
}
the 50% centers it horizontally and vertically.. alternatively you can use center center
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With