Set a DIV height equal with of another DIV

后端 未结 7 2409
终归单人心
终归单人心 2020-12-08 11:06

I have two DIVs, .sidebar and .content and I want to set .sidebar to keep the same height with the .content.

I\'ve tried the f

7条回答
  •  抹茶落季
    2020-12-08 11:36

    I used this technique to force the footer element to remain at the bottom of the page based on the height of another element. In your case you would; getting sidebar and content divs to keep the same height can do the following.

    1. Get the height of the main div; I'm guessing that is the .content div; and assign it to a variable.

      var setHeight = $(".content").height();

    2. then you can use jQuery to get the sidebar and assign the content height using the variable.

      $(".sidebar").height(setHeight);

    It is as simple as that. The final code will look like this.

    `var setHeight = $(".content").height();`
    `$(".sidebar").height(setHeight);`
    

    Here are more examples. Set div height using jquery (stretch div height).

提交回复
热议问题