Problem in centering images

Asked

Viewed 108 times

-1

I’m having some problems centering my images. I basically wanted to center my images in the center of my page. But if for example I go to another PC with another resolution my site gets all deformed.

I was wondering if anyone could help me.

css:

}
.imagens1{
    position: absolute; 
    top: 220px; 
    left:310px; 
}

.imagens2{
    position: absolute; 
    top: 220px; 
    left: 550px; 
}

.imagens3{
    position: absolute; 
    top: 220px; 
    left: 790px; 
}

.imagens4{
    position: absolute; 
    top: 220px; 
    left: 1030px; 
}

html:

<center>
    <div class="w3-content w3-row">
    <div class="w3-col s4 imagens1">
        <br>
        <img class="mySlides1 w3-animate-fading" src="1a.jpg" height="312" width="237">
        <img class="mySlides1 w3-animate-fading" src="comida1.png" height="312" width="237">
        <img class="mySlides1 w3-animate-fading" src="cozinha1.jpg" height="312" width="237">       
    </div>
    <div class="w3-col s4 imagens2">
        <br>
        <img class="mySlides2 w3-animate-fading" src="1b.jpg" height="312" width="237">
        <img class="mySlides2 w3-animate-fading" src="comida2.png" height="312" width="237">
        <img class="mySlides2 w3-animate-fading" src="cozinha2.jpg" height="312" width="237">
    </div>
    <div class="w3-col s4 imagens3">
        <br>
        <img class="mySlides3 w3-animate-fading" src="1c.jpg" width="237px" height="312px">
        <img class="mySlides3 w3-animate-fading" src="comida3.png" width="237px" height="312px">
        <img class="mySlides3 w3-animate-fading" src="cozinha3.png" width="237px" height="312px">
    </div>
    <div class="w3-col s4 imagens4">
        <br>
        <img class="mySlides4 w3-animate-fading" src="1d.jpg" width="237px" height="312px" >
        <img class="mySlides4 w3-animate-fading" src="comida4.png" width="237px" height="312px">
        <img class="mySlides4 w3-animate-fading" src="cozinha4.jpg" width="237px" height="312px">
    </div>
</center>
  • The images are side by side?

  • Yes, they as much as I want. But n are in the center of the page. so much to the right. and if I go to another pc with a different resolution, my site gets all messed up. That’s basically it.

  • Use Percentages instead of pixels so it never looks the same on all computers

  • some tips: first, note that the widthand height of all images is the same, so why not remove from the html and for a single class in the css. Second, if you use position: absolute and fixed measures in positioning, will not work in any resolution, should think in mediates in %, but try to use margin: 0 auto to center horizontally.

3 answers

1

<style type="text/css">

    .flex{

        display: flex;
        flex-wrap: wrap;
        width: 70%;
        margin: auto;

    }

    .center{

        justify-content: center;
        position: relative;
    }

    .img{width: calc(25% - 2px); height: 200px; background: tomato; border: 1px solid #bdbdbd;}

    .img:hover{background: #bdbdbd;}

    @media(max-width: 920px){

        .img{width: calc(50% - 2px); height: 200px; background: tomato; border: 1px solid #bdbdbd;} 

    }

    @media(max-width: 768px){

        .img{width: calc(100% - 2px); height: 200px; background: tomato; border: 1px solid #bdbdbd;}    

    }



</style>

<div class="flex center">

    <div class="img"></div>
    <div class="img"></div> 
    <div class="img"></div>
    <div class="img"></div>
    <div class="img"></div>
    <div class="img"></div>
    <div class="img"></div>
    <div class="img"></div> 

</div>          

Just adapt the percentages you need and put the img tags inside the div’s with width 100%.

0

One of the many ways for you to center an image is to use margins and percentages that unlike pixels, does not depend on the screen resolution.

.img {
  margin-left: 35%;
}
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBw8PDQ8ODxAPEA8NDw8PEA0PDg8PEBANFREWFhUVFRUYHSggGBolHRUWITEhJSkrLi4uGB8zODMsNygtLisBCgoKDg0OGhAQGislICUtLSstLy0tLS0tLS0rKy0tLS0tLS0tLS0rLSstLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAMgAyAMBEQACEQEDEQH/xAAbAAABBQEBAAAAAAAAAAAAAAAAAQIDBQYEB//EAEIQAAICAAIGBQkHAgMJAAAAAAECAAMEEQUGEiExURMiQWFxByNCUnKBkaGxFBYyYpLB0UNjM1NzFSQ0RIKDssLw/8QAGwEBAAIDAQEAAAAAAAAAAAAAAAEFAgMEBgf/xAA0EQACAQMBBQUHBAMBAQAAAAAAAQIDBBExBRIhQVETFWFxgRQiMkKRodEGM7HhNFLwwSP/2gAMAwEAAhEDEQA/APcYAQAgBACAEAIAQAgBACAEAIAQAgBACAEAIAQAgBACAEAIAQAgB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Pf2++YSgpamupSjPVGF0t5O8RXm2Gdbl7EbJLP4PynPKg+RxTs5L4TLYzAYig5XU2J7SHL4zS6bWpzSpyjqjlF0x3TAetmZAG8k5ATF8Flkxi5PCNVo+sVVhe3ix5tPP3EnVm5fQ9jaW6oUlHnz8zoN007h1DTdJUAT4bB32nzdbt35ED4zppWlSp8MWa5VYR1Zd4HVNzkb3Cj1E3n48JZ0dkPWo/RHLUvV8qNJgtH1UDKtAvM8WPiZcUbenSWII4Z1JTfvM6puMAgBACAEAIAQAgBACAEAIAQAgBACANdAwyYAg9hAIgFZi9W8Fac3w1RJ7QoU/KYuEXyNbpQeqOBdR9Hq4da2BHDKxsh7pqqW0Jx3WKVKNOanFcUdH3VwvJ/1mc3dlDo/qd3tdQkTVnCD0CfF2MyWzrdcjF3VR8zso0Xh6/wVVjv2QfrN8LalD4Yo1yqzlqzrAm81iwAgBACAEAIAQAgBACAEAIAQAgBACAEAIAQAgBACAEAIAQAgBACAEAIAQAgBACAf/9k=" class="img" alt="" />

-1

To center the images Horizontally and Vertically can do just as in this example.

With transform: translate(-50%,-50%) will center both horizontally and vertically. The field left and top have to be specified.

How do you know the width and the height this way works well

  • While this link may answer the question, it is best to include the essential parts of the answer here and provide the link for reference. Replies per link only can be invalidated if the page with the link is changed. - Of Revision

  • Well, I explained that I used the transform: translate(-50%,-50%) which is basically the answer

Browser other questions tagged

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