Search code examples
htmlcssimageposition

Image position equivalent to background position


I am working on a website and I want to style an image like you style a background with background-position: center. Is there an equivalent to this for a regular image?

Thanks in advance,

Luuk

EDIT:

The image is responsive and contained in a container.

CSS:

.img-container {
max-height: 300px;
overflow: hidden;
}

.img-container img {
max-height: 100%;
max-width: 100%;
}

Solution

  • I would do something like this to position the image centered.

    .img-container {
      display: flex;
      justify-content: center;
      align-items: center;
      max-height: 300px;
      overflow: hidden;
    }
    
    .img-container img {
      max-height: 100%;
      max-width: 100%;
    }