Change div width live with jQuery

前端 未结 5 1209
闹比i
闹比i 2021-01-01 13:54

Is it possible to change a div\'s width live with jQuery? And if it is, how?

What I want is to change its width depending on the browser\'s window width

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-01 14:39

    It is indeed possible to change a div elements' width in jQuery:

    $("#div").css("width", "300px");
    

    However, what you're describing can be better and more effectively achieved in CSS by setting a width as a percentage:

    #div {
        width: 75%;
        /* You can also specify min/max widths */
        min-width: 300px;
        max-width: 960px;
    }
    

    This div will then always be 75% the width of the screen, unless the screen width means the div will be smaller than 300px, or bigger than 960px.

提交回复
热议问题