Automatically resize images with browser size using CSS

前端 未结 4 763
感情败类
感情败类 2020-11-27 12:07

I want all (or just some) of my images getting resized automatically when I resize my browser window. I\'ve found the following code - it doesn\'t do anything though.

<
4条回答
  •  温柔的废话
    2020-11-27 12:38

    This may be too simplistic of an answer (I am still new here), but what I have done in the past to remedy this situation is figured out the percentage of the screen I would like the image to take up. For example, there is one webpage I am working on where the logo must take up 30% of the screen size to look best. I played around and finally tried this code and it has worked for me thus far:

    img {
    width:30%;
    height:auto;
    }
    

    That being said, this will change all of your images to be 30% of the screen size at all times. To get around this issue, simply make this a class and apply it to the image that you desire to be at 30% directly. Here is an example of the code I wrote to accomplish this on the aforementioned site:

    the CSS portion:

    .logo {
    position:absolute;
    right:25%;
    top:0px;
    width:30%;
    height:auto;
    }
    

    the HTML portion:

    
    

    Alternatively, you could place ever image you hope to automatically resize into a div of its own and use the class tag option on each div (creating now class tags whenever needed), but I feel like that would cause a lot of extra work eventually. But, if the site calls for it: the site calls for it.

    Hopefully this helps. Have a great day!

提交回复
热议问题