Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center one image over another

Tags:

css

image

center

I have 2 images - one is the main image and the other is like a picture frame that I'd like to position over the top of the main image. The picture frame image is a png with a transparent center so the main image shows through.

The dimensions of the images are important - the inner main image has to be smaller than the frame so it is only visible through the center:

main.jpg = 367 x 550

frame.png = 405 x 597

I thought I had it with the following code...

<div style="background-image:url('/main.jpg') no-repeat scroll center center transparent;">
<img style="width:100%; max-width:100%;" src="/frame.png">
</div>

...which works great until you see the screen on a mobile phone; the frame.png stretches because I've given the width as 100% but the background main.jpg doesn't stretch along with it.

I need the design to be fluid, so I need the images to stretch.

Is there a way to make sure the background stretches the same as the main image?

I've tried all kinds of different methods to get this working, absolutely positioning the frame in a div floating over the main image, etc but I couldn't get the main image to appear centered horizontally and vertically when I did that.

Is there any way to achieve what I want without resorting to javascript?

The reason I'm using 2 images by the way is because of file size. I need the main image to be jpg so I can keep it small, but I also need the transparency on the frame so that has to be png :(

like image 815
Damian Avatar asked Sep 01 '13 07:09

Damian


People also ask

How do I put an image in the center of another picture?

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 position one image on top of another?

As the simplest solution. That is: Create a relative div that is placed in the flow of the page; place the base image first as relative so that the div knows how big it should be; place the overlays as absolutes relative to the upper left of the first image.

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 I overlap an image in CSS?

We're applying a negative right margin on a floated left element that allows content to overlap. -100% is equal to the width of the container so it shifts the image to the left and allows the bottom image to render beneath it as if it's not in the DOM. Codepen here: HTML.


1 Answers

I usually use this:

HTML:

<div id="frame">
    <img id="myImg" src="main.jpg">
</div>

CSS:

#frame {
    position: relative;

    width: 597px;
    height: 405px;

    background-image: url(frame.png);
    background-position: center;
    background-size: cover; }

#myImg {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;

    display: block;
    margin: auto; }

This works for all images and other elements with fixed dimensions, or a set max-width and max-height.

I hope this works for you :)

like image 173
Wouter Raateland Avatar answered Oct 12 '22 06:10

Wouter Raateland