Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center image vertically/horizontally over another without dimensions

I'm trying to make an image appear to have a play button that is centered. I will not know the exact size of the main image however, since it's dynamically generated.

Right now, the overlay image (a play button) is in the upper left corner. How can I have the play button show centered horizontally and vertically?

Thank you.

<div class="videobox">
<img src="/mainimage.png">
<span></span>
</div>

.videobox { position: relative; }

.videobox span {
position:absolute;
left: 0px;
bottom: 0px;
width: 100%;
height: 100%;
z-index: 1;
background: transparent url(../img/elements/playbutton.png) no-repeat;
}
like image 228
user749798 Avatar asked Mar 13 '13 00:03

user749798


People also ask

How do you center align vertically and horizontally?

For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.

How do you centralize an image?

To center an image with CSS Grid, wrap the image in a container div element and give it a display of grid . Then set the place-items property to center. P.S.: place-items with a value of center centers anything horizontally and vertically.

How do you center an image vertically and horizontally in HTML?

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.


1 Answers

.videobox span {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 1;
  background: url(../img/elements/playbutton.png) no-repeat center center;
}
like image 88
xiaoyi Avatar answered Oct 27 '22 03:10

xiaoyi