Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS background image on DIV's vertical center

Tags:

html

css

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.

like image 444
Satch3000 Avatar asked Apr 15 '11 11:04

Satch3000


People also ask

How do I center an image in a div using CSS?

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.

How do I center a background in a div?

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 .

How do I align an image vertically in HTML?

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.


2 Answers

To vertically center:

#con {
    background: url(image.jpg) no-repeat left center;
}

To horizontally center:

#con {
    background: url(image.jpg) no-repeat center top;
}
like image 67
sandeep Avatar answered Oct 17 '22 08:10

sandeep


#somediv {
background: url (image.jpg) no-repeat 50% 50%;
}

the 50% centers it horizontally and vertically.. alternatively you can use center center

like image 36
clairesuzy Avatar answered Oct 17 '22 08:10

clairesuzy