Responsive 2-column CSS layout including sidebar with fixed width?

我的梦境 提交于 2019-12-03 04:49:47

I think the approach with all those position: absolute is not the best one.

  1. You want to have a container element with a max-width and margin: 0 auto. Not sure why you might want a min-width as well; you can of course.

  2. Any block element that you put automatically takes all the width of the parent element. Hence, there's no problem with the header and footer, just position them in the right spot and they will render properly.

  3. As you correctly did, use a container element for your main section. Since the elements are rendered left to right, you might want to write the aside before the #primary.

  4. Apply float: left to the aside with your desired fixed-width.

  5. The main content #primary element will take the remaining space automatically (or just apply width: auto. Note: the remaining space is the remaining space of the parent element, so, if you set the min-width on the parent element, don't expect your #primary to be narrower!

  6. Now you will have a problem: the container element #main will not get the proper height. That's because of an issue with float. To force a parent element to get to the height of its floated children, use overflow: hidden.

You should be ready to go. Here is a slightly modified version of your code.

1 = With flexbox: http://jsfiddle.net/rudiedirkx/CjWbv/2/

#main {
    display: flex;
}
#primary {
    background: #bbb;
    flex: 1;
}
aside {
    background: #ddd;
    flex: 0 0 200px;
}

2 = with calc(): http://jsfiddle.net/rudiedirkx/CjWbv/

#main:after { clearfix here }
#primary {
    float: left;
    background: #bbb;
    width: calc(100% - 200px);
}
aside {
    background: #ddd;
    float: right;
    width: 200px;
}

Both will require a fallback for older browsers (and vendor prefixing). Take your pick from the other answers.

Until we get flexbox, I believe the best way to do what you are looking for is to simply use the css display: table;. You will need to set the container to display: table;, and then each of the columns, just set to display: table-cell;. This will eliminate the need for using floats or any other JS magic. Also, this works back to IE8. I know it feels sketchy using anything with the word "table" for layout, but just give it a try.

Check out this link for more info.

Here is an updated version of your code.

EDIT: example code

html: Assuming this markup

<div id="main">
    <div id="sidebar"></div>
    <div id="content"></div>
</div>

css:

#main {
    display: table;
    width: 100%;
}
#sidebar {
    display: table-cell;
    width: 200px;
}
#content {
    display: table-cell;
    /* no need to specify width. 
     * width will auto fill remaining width of parent `table` #main.
     * so 100% - 200px */
}
#main { position: relative; overflow:hidden;}

This stops the sidebar misbehaving.

You can fix it with adding margin-bottom: FOOTER_HEIGHT to the aside or use the following method instead:

<div id="container">
    <div id="header">
        YOUR_HEADER
    </div>
    <div id="body>
        <div id="sidebar">
            YOUR_SIDE_BAR_CONTENT
        </div>
        <div id="main">
            YOUR_CONTENT
        </div>
        <br />
    </div>
    <div id="footer">
        YOUR_FOOTER
    </div>
</div>

And the CSS:

#sidebar {
    width: ...;
    height: ...;
    float: right;
}
#main {
    margin-right: WIDTH_OF_SIDE_BAR;
}
#body > br {
    clear: both;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!