10

I'm trying to create an image looking like the cover image here, using only css and html. I've tried different things but nothing has worked so far.

This is my html code:

<div id="container">
    <img id="image" src="...">
</div>

What css code should I use?

Simon
  • 179
  • 2
  • 3
  • 12

9 Answers9

20

Set the image's width to 100%, and the image's height will adjust itself:

<img style="width:100%;" id="image" src="...">

If you have a custom CSS, then:

HTML:

<img id="image" src="...">

CSS:

#image
{
    width: 100%;
}

Also, you could do File -> View Source next time, or maybe Google.

Cilan
  • 13,101
  • 3
  • 34
  • 51
6

If you want to have same ratio you should create a container and hide a part of the image.

.container{
  width:100%;
  height:60px;
  overflow:hidden;
}
.img {
  width:100%;
}
<div class="container">
  <img src="http://placehold.it/100x100" class="img" alt="Our Location" /> 
</div>

   
Hamza Khanzada
  • 1,439
  • 1
  • 22
  • 39
4

In order not to scratch the image you will need to use:

max-height: 200px;
width: auto;
Brane
  • 3,257
  • 2
  • 42
  • 53
3

This can done several ways. I usually do it from my class.

From class

.image
{
    width:100%;


}

and for this your html would be:

<img class="image" src="images/image_name">

or if you want to style it using inline styling then you would just have:

<img style="width:100%; height:60px" id="image" src="images/image_name">

I however recommend doing it from your external style-sheet because as your project grows you will realize that the entire thing is easier managed with separate files for your html and your css.

1

Set the height of the parent element, and give that the width. Then use a background image with the rule "background-size: cover"

    .parent {
       background-image: url(../img/team/bgteam.jpg);
       background-repeat: no-repeat;
       background-position: center center;
       -webkit-background-size: cover;
       background-size: cover;
    }
Mark
  • 209
  • 1
  • 4
1
#image {
  width: 100%;
  height: 100px; //static
  object-fit: cover;
}
indrajeet
  • 837
  • 10
  • 17
  • When answering a six year old question with eight existing answers it is important to point out what new aspect of the question your answer addresses, and perhaps noting if any of the techniques you use were introduced since the question was asked.. Also code only answers can generally be improved by explaining how and why they work. – Jason Aller Jun 30 '20 at 20:44
  • thank you for pointing out well I had the same problem today in ionic and none of the above solutions work for me. So I fixed my using above code. And I don't want to apply any styles to parent div. None of the answers use `object-fit: cover;` so I thought to share here. – indrajeet Jun 30 '20 at 22:37
0

If you don't want to lose the ratio of your image, then don't bother with height:300px; and just use width:100%;.

If the image is too large, then you will have to crop it using an image editor.

Badacadabra
  • 8,043
  • 7
  • 28
  • 49
Martin42006
  • 63
  • 1
  • 8
  • It is not just okay to look at a comment from an answer I edited and then edited so it shows *this* answer, just in hope that you will get the check mark... – Cilan Dec 24 '13 at 16:24
  • sorry when i started writing this your comment wasn't there. i would have added a comment but i cant as i don't yet have 50 rep. i wasn't trying to take credit for someone work. – Martin42006 Dec 24 '13 at 16:28
0
<div id="container">
    <img style="width: 100%; height: 40%;" id="image" src="...">
</div>

I hope this will serve your purpose.

Palak Jain
  • 664
  • 6
  • 19
0

you can use pixels or percent.

<div id="container">
<img id="image" src="...">
</div>

css

#image
{
width:100%;
height:
}