Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full width image with fixed height

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?

like image 778
Simon Avatar asked Dec 24 '13 16:12

Simon


People also ask

How can I fix the height and width of a picture?

The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels.

How do you make a full width image responsive?

To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.

What is a full width image?

First, take note of roughly how wide the image shows up on the page. Background images: 1600x900px. Read this article for more guidelines. Full-width images: At least 1400px wide for the highest quality.

How can I set a fixed image size in HTML?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.


2 Answers

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.

like image 127
Cilan Avatar answered Sep 25 '22 13:09

Cilan


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>

   
like image 36
danielpenalverramila Avatar answered Sep 23 '22 13:09

danielpenalverramila