0

I have an image tag that I managed to align nicely to the rest of the divs in one section. However, as I resize the window, the image starts shrinking or expanding. What could I do in CSS to prevent this from happening?

 .img-test {
     width: 33.87%; 
     position: absolute;
     max-width: none;
   } 

 .clothes {
    background-color: #d04925;
    float: right;
    height: 805px;
  }

The image and the div with the .clothes class are one next to the other and it should stay that way.

Nick94
  • 3
  • 3

3 Answers3

0

You need height attribute to be set to some value to prevent image from resizing. Example

.img-test{
         height: 200px;
         position: absolute;
         min-width: 300px;
         width: 33.87%; 
         } 

This will help. Unless the image is inside a div whose height is changing.

Duck_dragon
  • 440
  • 5
  • 17
0

You can use the max-width, min-width, max-height, min-height attributes to prevent the image from resizing. Here's a link with more information. w3schools

amanhasnoname
  • 236
  • 1
  • 4
  • 16
0

Hello and welcome to StackOverflow!

You set your image to a percentage value, or in other words to a fraction of the parent container. So if the parent container shrinks, the fraction of it gets smaller and the image shrinks, too! Now there are ways to prevent this, you could set a min-width, which defines a minimum width for your image. So it will shrink to this width, but it won't shrink below.

.img-test {
  width: 33.87%;
  min-width: 300px;
}

In this example, your image would never be smaller than 300px. You could also omit the min-width Attribute, and set a fixed width directly. But since you mentioned, that you managed to make it „look nicely“, this will propably wreck your whole UI, if the viewport of the browser is too small.

So I would recommend to consider rethinking your layout, if it only works with some specific widths. One way to do this are media queries. You define breakpoints in your CSS, and if the viewport gets smaller (or bigger), different CSS rules apply. You can read more about this here and here.

Just a small off-topic addition: The default value of max-width is none and it is not inherited, so there is no reason to set it to this value.

Matthias Seifert
  • 2,033
  • 3
  • 29
  • 41