Make image responsive html

How to Make Images Responsive with CSS

Responsive images automatically adjust to display an image based on the user’s device. They are used to fit the size of the screen.

Setting the width of the image

Example of creating a responsive image:

html> html> head> title>Title of the document title> style> img < width: 100%; > style> head> body> h2>Responsive image example h2> img src="/uploads/media/default/0001/01/25acddb3da54207bc6beb5838f65f022feaa81d7.jpeg" alt="Aleq" /> body> html>

If you want a responsive image, but up to a limit, use the max-width property. It will indicate the image width in pixels, maximum 100% of the width of its container. It means the following (considering that your image width is 300px):

  • If the container is 200px, the image will be 300px (max-width: 100%).
  • If the container is 300px, the image will be 300px (max-width: 100%).
  • If the container is 1400px, the image will remain 300px.

There are fixed items, i.e. a fixed navigation menu, for which we will need to set a specific height. Most of the time, the height and the width are in symmetry. If the width of an image is reduced to adapt to the responsive, the height will be reduced too. If the width is fixed, the height will be fixed too.

Let’s see an example to visualize what we have said for responsive images.

Example of creating a responsive image using the max-width and height properties:

html> html> head> title>Title of the document title> style> img < max-width: 100%; height: auto; > style> head> body> h2>Responsive image example with max-width and height h2> div> img src="/uploads/media/default/0001/01/25acddb3da54207bc6beb5838f65f022feaa81d7.jpeg" alt="Aleq" /> div> body> html>

If you want to set multiple images, you will need the srcset attribute. The srcset defines the set of images that will be allowed to the browser to choose between.

Читайте также:  Php fpm address already in use

To work properly, we recommend using the srcset attribute with the element, not the .

Let’s examine an example where multiple images are set and see how they are changed when the specific screen width is defined.

Example of creating multiple responsive images:

html> html> head> title>Title of the document title> head> body> picture> source media="(min-width: 650px)" alt="img_1" srcset="/uploads/media/news_gallery/0001/01/thumb_348_news_gallery_list.jpeg"> source media="(min-width: 430px)" alt="img_2" srcset="/uploads/media/news_gallery/0001/01/thumb_316_news_gallery_list.jpeg"> img src="/uploads/media/news_gallery/0001/01/thumb_366_news_gallery_list.jpeg" alt="img" style="width:50%;"> picture> p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. p> body> html>

How to make background images responsive

There are 3 methods of making images responsive.

  1. When the background-size property is set to «contain», the background image will scale, and try to fit the content area. However, the image will keep the proportional relationship between the image’s width and height.

Example of making a background image responsive using the background-size property with the «contain» value:

html> html> head> title>Title of the document title> meta name="viewport" content="width=device-width, initial-scale=1.0"> style> div.img-block < width: 100%; height: 450px; background-image: url("/uploads/media/default/0001/01/25acddb3da54207bc6beb5838f65f022feaa81d7.jpeg"); background-repeat: no-repeat; background-size: contain; border: 1px solid black; > h1 < padding: 0 10px; color: #fff; > style> head> body> p>Resize the browser window to see the effect. p> div class="img-block"> h1>Your Background Image h1> div> body> html>
  1. When the background-size property is set to «100% 100%», the background image will stretch to cover the entire content area.

Example of making a background image responsive using the background-size property with percentages:

html> html> head> title>Title of the document title> meta name="viewport" content="width=device-width, initial-scale=1.0"> style> div.img-block < width: 100%; height: 450px; background-image: url("/uploads/media/default/0001/01/25acddb3da54207bc6beb5838f65f022feaa81d7.jpeg"); background-size: 100% 100%; border: 1px solid black; > h2 < padding: 0 10px; color: #fff; > style> head> body> p>Resize the browser window to see the effect. p> div class="img-block"> h2> Your background Image h2> div> body> html>
  1. When the background-size property is set to «cover», the background image will scale to cover the entire content area. Note that some parts of the background image may be clipped.
Читайте также:  Ширина блока

Example of making a background image responsive using the background-size property with the «cover» value:

html> html> head> title>Title of the document title> meta name="viewport" content="width=device-width, initial-scale=1.0"> style> div.img-block < width: 100%; height: 450px; background-image: url("/uploads/media/default/0001/01/25acddb3da54207bc6beb5838f65f022feaa81d7.jpeg"); background-size: cover; border: 1px solid red; > h2 < padding: 0 10px; color: #fff; > style> head> body> p>Resize the browser window to see the effect. p> div class="img-block"> h2>Your background Image h2> div> body> html>

Источник

CSS Responsive Image Tutorial: How to Make Images Responsive with CSS

Cem Eygi

Cem Eygi

CSS Responsive Image Tutorial: How to Make Images Responsive with CSS

The majority of today’s websites are responsive. And if you need to center and align image on those site, you need to learn how to make images fluid or responsive with CSS.

I posted a tutorial video that explains how to make a responsive website step by step a couple of weeks ago. In the video we made an image responsive. But in this post, I would like to give a bit more detail about how to make images responsive.

You will also learn some of the general problems that can occur when you’re trying to make images responsive – and I will try to explain how to solve them.

How to Make Images Responsive with CSS

Should I Use Relative or Absolute Units?

Making an image fluid, or responsive, is actually pretty simple. When you upload an image to your website, it has a default width and height. You can change them both with CSS.

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.

For example, if you define a fixed width of 500px, your image won’t be responsive – because the unit is absolute.

That’s why you should instead assign a relative unit like 50%. This approach will make your images fluid and they will be able to resize themselves regardless of the screen size.

Should I Use Media Queries?

One of the questions I get asked the most is whether you should use media queries or not.

A media query is another important feature of CSS that helps make a website responsive. I won’t go into further details here but you can read my other post later to learn how to use media queries in more detail.

Читайте также:  Python search value in list

The answer to that question is: “it depends”. If you want your image to have different sizes from one device to another, then you will need to use media queries. Otherwise, you won’t.

Now for this example, your image has a 50% width for any kind of screen. But when you want to make it full-size for mobile devices you need to get help from media queries:

@media only screen and (max-width: 480px) < img < width: 100%; >>

So based on the media query rule, any device smaller than 480px will take the full size of the width of the screen.

You can also watch the video version of this post below:

Therefore, you can define a max-width property for the image and set it to 100%, which shrinks the image of 500px to the space of 360px. So you will be able to see the complete image on a smaller size screen.

The good thing is that, since you are using a relative unit, the image will be fluid in any device smaller than 500px.

Unfortunately, the screen size will get somewhat larger than 500px, but the image won’t because it has a default 500px of width. This approach will break the responsiveness of the image.

To fix this, you need to use the width property again, which makes the max-width property useless.

What about Heights?

Another common problem you may run across has to do with the height property. Normally, the height of an image automatically resizes itself, so you don’t need to assign a height property to your images (because it kinda breaks the image).

But in some cases, you might have to work with images that must have a fixed-height. So when you assign a fixed height to the image, it will still be responsive but it won’t look good.

Fortunately, there is another property that CSS offers to fix this problem…

Solution: The Object-Fit Property

To have more control over your images, CSS provides another property called object-fit. Let’s use the object-fit property and assign a value, which will make your image look better:

If needed, you can also use the object-position property (in addition to object-fit) to focus on a specific part of the image. Many people are not aware of the object-fit property, but it can be helpful to fix these kinds of problems.

I hope this post has helped you understand and solve your problems with responsive images. If you want to learn more about web development, feel free to check out my Youtube channel.

Источник

Оцените статью