You can use the child selector >
, thus #conteudo > img
and apply width
and height
with 100%;
, so it will only affect the image that is direct child of #conteudo
Example:
#conteudo{
width:600px;
height:600px;
float:left;
background-color:#ff1;
display: initial;
margin: auto;
z-index: 6;
overflow: hidden;
}
#conteudo > img {
width: 100%;
height: 100%;
}
<div id='conteudo'>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/Hazard_X.svg/500px-Hazard_X.svg.png">
</div>
The image may get distorted so you can choose to use 100%
only at height or width, which will not fill, to fill everything or distort the image (if it is more rectangular than the "parent" element) or else must exceed the width or height, for example see how it gets distorted:
#conteudo{
width:600px;
height:600px;
float:left;
background-color:#ff1;
display: initial;
margin: auto;
z-index: 6;
overflow: hidden;
}
#conteudo > img {
width: 100%;
height: 100%;
}
<div id='conteudo'>
<img src="https://upload.wikimedia.org/wikipedia/commons/f/f7/Stack_Overflow_logo.png">
</div>
Fill in and maintain the ratio
To exceed the size and still be able to maintain the ratio can use min-width
and min-heigth
, for example:
#conteudo{
width:600px;
height:600px;
float:left;
background-color:#ff1;
display: initial;
margin: auto;
z-index: 6;
overflow: hidden;
}
#conteudo > img {
min-width: 100%;
min-height: 100%;
}
<div id='conteudo'>
<img src="https://upload.wikimedia.org/wikipedia/commons/f/f7/Stack_Overflow_logo.png">
</div>
This way will maintain the proportion of the image size in relation to the "parent" element without distorting.
Background-sized
CSS has a property called background-size
, which allows you to adjust the background size
background-size: cover
It will adjust until it takes over
#conteudo{
width:600px;
height:600px;
float:left;
display: initial;
margin: auto;
z-index: 6;
overflow: hidden;
background:#ff1 url(https://upload.wikimedia.org/wikipedia/commons/f/f7/Stack_Overflow_logo.png) no-repeat; /*aplica a imagem*/
background-size: cover; /*Ajusta o tamanho para "cobrir"*/
}
<div id='conteudo'>Olá mundo</div>
background-size: contain
Will adjust to occupy width or height
#conteudo{
width:600px;
height:600px;
float:left;
display: initial;
margin: auto;
z-index: 6;
overflow: hidden;
background:#ff1 url(https://upload.wikimedia.org/wikipedia/commons/f/f7/Stack_Overflow_logo.png) no-repeat; /*aplica a imagem*/
background-size: contain; /*Ajusta o tamanho para "cobrir"*/
}
<div id='conteudo'>Olá mundo</div>
the way you gave me gave it right !! would have how to do with a background?!
– Mateus Alcântara
@Mateusalcântara has yes, one minute
– Guilherme Nascimento
Thanks in advance
– Mateus Alcântara
@Mateusalcântara edited the answer
– Guilherme Nascimento
Thank you Guilherme, but in my case the background has not worked yet, when I click on an image and it is opened inside the background, it is a space " left "
– Mateus Alcântara
@Mateusalcântara you saw that are two example, one with cover and the other with contain né?
– Guilherme Nascimento
Yes, I noticed, I tested with both
– Mateus Alcântara
@Mateusalcântara sends the link of your site to see
– Guilherme Nascimento
You’re not staying yet, I can send you some other way?
– Mateus Alcântara
@Mateusalcântara knows how to use https://codepen.io/pen/?
– Guilherme Nascimento
@Mateusalcântara I think you do not understand how to use the codepen, It is not just going out pasting the codes. :(
– Guilherme Nascimento
@Mateusalcântara so I asked if you knew how to use codepen, it shows that you do not know. That’s what I wanted to know. There’s a way to send the images, but it’s kind of hard to explain. Do the following, evnia the HTML+css+images via Dropbox or googledrive and send me the link to download here in the comments.
– Guilherme Nascimento
https://drive.google.com/open?id=0B7BMqcQw2lmWbjV4eGZ2d2VXN2c
– Mateus Alcântara
@Mateusalcântara change of
background:#ff1 url(images/J2.png);
forbackground:#ff1 url(../images/J2.png);
and change from<div class="conteudo" id="conteudo" ondrop="drop(event)" ondragover="allowDrop(event)" style="background:#fff">
for<div class="conteudo" id="conteudo" ondrop="drop(event)" ondragover="allowDrop(event)">
– Guilherme Nascimento