Setting height of sidebar to match container

て烟熏妆下的殇ゞ 提交于 2019-12-11 05:46:54

问题


I have a layout where the container holds a sidebar. I need the sidebar to be the exact same height as the container. Please see this site where the sidebar is #leftbar and the container is #container

If you look at the site, you will see what I mean. I either need a pure css solution or a jquery solution.

One important thing to keep in mind The nav is a vertical accordion, so when certain buttons are clicked, the sidebar, #leftbar will expand vertically. The solution needs to accommodate growth, so simply matching their heights using jquery isn't a viable solution.


回答1:


Add the following properties to these elements:

#cotainer {
    position: relative
}

#leftbar {
    position: absolute;
    top: 0;
    bottom: 0;
}

#righthome {
    margin-left: 270px; /* looks to be about the width of the leftbar */
}

Giving the #leftbar the properties show, will ensure the top sticks to the top of the next parent element with position: relative, and the same for the bottom edge.

However this has a side effect. The #righthome element becomes the first thing in its parents box layout model, and therefore overlaps the left bar. To remedy this you give it a left margin to put it back in the place it would have been.



来源:https://stackoverflow.com/questions/4611456/setting-height-of-sidebar-to-match-container

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!