Cut image inside a CSS div

Asked

Viewed 2,069 times

0

I need any image size to be centered inside the div:

Example below:

inserir a descrição da imagem aqui

I have this css code to make the image circle:

.anuncio_dialogo {
    background-color: #FFF; 
    width: 128px; 
    height:128px; 
    margin-bottom: 5px;
}

To make the circle I’m using another CSS class

.img-circle {
    border-radius: 50%;
 }

To call in the view I’m using:

<img src="<?=base_url() . $anuncio->imagemPrincipal?>" class="anuncio_dialogo circle">

Example of how everything is currently:

inserir a descrição da imagem aqui

Note that the first image is distorted because it is larger in width, the others being a perfect square.

  • The class in the image there, right? you put "Circle", but in css you put "img-Circle"

2 answers

2

0


I believe I have some ways to solve this, one of them would be the usa div have a position: relative and a position: absolute in the image, ai vc centralizes using margin and top

Ex:

<div class="container-image">
   <img src="source-image.jpg" class="centralize-image" />
</div>

Ai no css:

.container-image{  
    width: 128px;   
    height: 128px;   
    position: relative;  
    overflow: hidden;  
}

.centralize-image{  
    position: absolute;  
    top: 50%;  
    left: 50%;  
    margin-top: -(ALTURA-IMAGEM / 2)px;  
    margin-left: -(LARGURA-IMAGEM /2)px;  
} 

See the example above running on JSFIDDLE

Or use the image as background in css ex:

<div class="container-image"></div>


.container-image{  
    width: 128px;   
    height: 128px;   
    position: relative;  
    overflow: hidden; 
    background: url(source-image.jpg) no-repeat center center; 
}

See the example above running on JSFIDDLE

If you think about it there are many other ways, with these two I think it’s easier for you to have some ideas than to do.

Browser other questions tagged

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