JQUERY: Resize div to window width

前端 未结 4 736
有刺的猬
有刺的猬 2021-02-09 20:56

I am trying to force the width of a div to the width of a browser window. So that even if the window is resize the adjusts it\'s width.

I have been researching for about

4条回答
  •  离开以前
    2021-02-09 21:17

    Without a container limiting the width, a div should span the width of the browser window by default. You can explicitly set the width to 100%, but that shouldn't be necessary:

    Hello world

    I think CSS is more appropriate here, but you can do this in jQuery like this:

    $("#div1").width($(window).width());
    

    To run the above code whenever the window is resized, you can do this:

    $(window).resize(function(){
        $("#div1").width($(window).width());
    });
    

    Here's a jsFiddle. For the sake of demonstration, the div expands on button click.

提交回复
热议问题