How to ensure an image is centralized and responsive

Asked

Viewed 4,366 times

2

Good morning!

I wonder how I can do with CSS so that an image is centered in a way that is responsive in all monitors.

  • 1

    You could share what you’ve already tried including your HTML and CSS to ask?

4 answers

2

https://jsfiddle.net/4zhh7LnL/ Look at this fiddle, what I did was this

I added margin: 0 auto and text-align:center, with this, will already center any div on any monitor, the rest was only demonstrative

  • does not make the image responsive

  • @Murilogambôa If you set the image with width: 100%, it will cover the size of the div, I mean, whatever size you change, you have to change in div and not in the picture

  • But that doesn’t make it responsive

  • this can flatten the image, or even cause the image to burst the size of the parent div, causing it to cover elements below the parent element

2


Well, to be responsive, besides the margin: 0 auto;, need to leave the image with display: block; and, more importantly, max-width: 100%;. Thus, images larger than the screen are resized.

https://jsfiddle.net/dudaskank/wy17vd5h/

div#responsive-imgs {
}
div#responsive-imgs img {
  display: block;
  margin: 5px auto;
  max-width: 100%;
}
<div id="responsive-imgs">
  <img src="https://lh3.googleusercontent.com/-jr0_VjsnLbk/TXgyw-U3RAI/AAAAAAAAAOM/xr23lB2vJ9o/s1600/pequena-sereia1a.gif" />
  <img src="http://www.icmbio.gov.br/portal/images/stories/imgs-unidades-coservacao/queimada_grande.jpg" />
</div>

  • this does not make the image responsive

2

Well, first, to leave a responsive image, you’ll need the following pattern for the element img:

For displaying wider than higher images

img{
  min-width:100%;
  max-height:100%;
}

For displaying images higher than wide

img{
  min-height:100%;
  max-width:100%;
}

After setting this pattern, set the following patterns for the parent element:

div{
  position:relative;
  overflow:hidden; /*Utilizar para não exibir o corte automático da imagem*/
  /*Deve-se definir um min-height e min-width para que a imagem apareça corretamente no local desejado, e com o tamanho desejado, por exemplo:*/
  width: 50%;
  min-height: 200px;
}

The CSS above will define the size of the image display box. After that, the image should receive the following properties:

img{
   position:absolute;
   left:50%;
   top:50%;
   transform:translate(-50%,-50%);
}

This will cause the image to start in the center of the parent div, however, Transform will cause the center of the image to be aligned to the center of the parent div.

As described above, below follows the functional example in a Fiddle

*{
 margin:0;
 padding:0;
 box-sizing:border-box;

}
figure{
 position:relative;
 min-height:180px;
 width:160px;
 overflow:hidden;
}
figure img{
 min-width:100%;
 max-height:100%;
 position:absolute;
 left:50%;
 top:50%;
 transform:translate(-50%,-50%);
}
<figure>
  <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxITEhUSEhIVFRUXGBsaGBcYGBoaFxoXGhcXGBgXFxUYHSggGBolGxUXITEhJSkrLi4uFx8zODMtNygtLisBCgoKDQ0NDg0NDi0ZFRkrNy0tLSsrKysrKysrLSsrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrKysrK//AABEIAKkBKgMBIgACEQEDEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAFAgMEBgcAAQj/xABBEAABAwEFBQQHBgUDBQEAAAABAAIRAwQFEiExBkFRYXEigbHwBxMykaHB0SNCUmJy4RQkM6KyNILxFUODksI1/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAH/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwDW7sMVa7fzA+8BTqrlApGLW8fiY0+4kKbWQIJXrU0nGIHwUsJpqdCBSS90ZleqLedfBTc+JgSgH3vahgcZLcjvz03c1jT7B6y1OBa9zZMQczxM6CNZVmt16HCX1HxwbO47zHwHVAqt64R2RhBy54d/SUDP/TKTZwy45yT7A103ujioNoHa7IcGgRJ1MSJ5b0tl5YiSTlOngOmaW+oXjOADoPh3IIFWoRIExHHcpVgseJoO5xid4O73pmqTEHz3KRZa+AZSJzHCRujqgtVOtgim09gcc+1945+5HbH6rNxZJIiNIkQfPJUyyWskZnv6+KL07S5uY93JBernoUmmaZjlu5wFYgs0st4mQWmD8DvhaFdtfHTa5BKXLlyDkmo3JKXFBAe1Q7U3JT6yi1hkgpV/0dVQbwZmVpV/M7JWeXk3MoC2ybuzCvFjbkqLslvV8sQyQIvGlLHDl8lSrupQ54Kv1qbLSqVYmfavCCHd1P7ZwRS5qUVKjeP0UOyNArkcUXu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cXerptLy4GC+o6cUxmMvFFn/AETVfQ9Pkgl7O4WME6ZuOeQxZwDwA+qh7S21hqdgxlmN+R8NFzv6b/0H/EqvX9/qHfpd/mg51pj7yn2K3xvlVyt/UPQeJUuh8kF4uW8+0IOavVwXo81Cx2/PPTDGoI34jv5rKbh9sdVsVz6dzfmgzLbG5X06zsWeIkg8ZOZVZNHcte9I/wDp2fq+RWX/AHh3oBNegk2arhciFq0CGuQaHsztOLOWtc4YD7XEdFe7NtPSfmNOsH3LBGe0fPFXO6NR/t8EGtUrxpuGsdVmu1uwNS0V316FakcRnC6Qd2/uVio7+nyK9fu7kFCfsvbKAGOi4gb29ofBesJAzB71q92aKHe+jun1QY5fTp0z+KJ7H3IKsOeCRIlHqutPqjFx/wBB/RvgEDduvay0GllGmOBcBHuO9Z5bdqqlOpiGhPuIyOXQAo3b/a9/gqJtBqeo8EGu7JbR+vZLXZjUHQ/RM2O/ajba6zPxZDFJM9k5iOKqHoo9up3KwW7/APV/8A/yKDSLgrPc12NwcA6GnfEaFFUE2V9h/wCv/wCWo2g//9k=">
</figure>

  • Open the image in a new tab, and you’ll see the full size of it, it has auto-cut that makes it responsive

Any questions, I’m here to help

0

In case you can use @media screen... and give None float (if you have placed left or right float in img) and apply a margin 5px auto. ex:

.logo-2{
    float: right;
    width: 150px;
    height: 60px;
    background-image: url('../imagens/logo2.png');
    background-size: 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
    /*background-color: orange;*/
}

@media screen and (max-width: 510px){
        .logo-2{
            float: none;
            margin: 5px auto;

        }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.