DigitalJohn
DigitalJohn

Reputation: 281

Two column layout, fixed right column

I am stuck with a seemly simple two column CSS layout. Typically this layout is simple but I am building a responsive site and need the columns to collapse in the correct order for mobile, on top of each other.

On desktop, I need the right column to be fixed size, say 200px and the rest of the area to be taken up by the left column. Naturally I need the columns to clear and push content below down.

On mobile, they would just stack. So the left column is above the right column.

As mentioned before, this type of layout is usually accomplished simply by floating one of the columns and/or setting large margins, but this approach requires the left and right to be swapped in the document flow and would make the collapsed mobile version impossible.

I have even looked at display table and table-cell, this works relatively well for the most part, but unfortunately FireFox does not support absolute positioned elements within the layout breaking some of the content within.

I'm a seasoned developer, surly accomplishing this should be simpler that I am finding?

Upvotes: 4

Views: 5921

Answers (4)

ap-o
ap-o

Reputation: 170

Not a very elegant solution, but its working. They key is to wrap the contents of the left column and add a margin-right equal to the width of the right column / sidebar. On the right column, set the width and a negative margin-left equal to its width.

.left {
    float:left;
    width:100%;
    background-color: green;
}
.left-content {
    margin-right:120px;
}
.right {
    float:right;
    width: 120px;
    margin-left: -120px;
    background-color: red;
}

Here's a working fiddle http://jsfiddle.net/heyapo/9Z363/

Upvotes: 6

George Yates
George Yates

Reputation: 1247

Option 1:

You can use absolute positioning, margins, and box sizing to achieve what you're looking for. See this JSFiddle for an example, with the code below. Just change the margin on the left column, the width on the right column, and the breakpoint to what fits your purpose.

HTML:

<div class="wrap">
    <div class="left">
        Left
    </div>
    <div class="right">
        Right
    </div>
</div>

CSS:

.wrap {
    position: relative;
}

.left {
    -webkit-box-sizing: border-box;
    -moz-box-sixing: border-box;
    box-sizing: border-box;
    margin-right: 60px;
    background-color: green;
}

.right {
    position: absolute;
    right: 0;
    top: 0;
    width: 50px;
    background-color: red;
}

@media only screen and (max-width: 500px){
    .left {
        margin: 0;
    }
    
    .right {
        width: auto;
        position: static;
    }
}

Option 2:

See this option, using calc() as a width on the left column, and a clearfix on the wrap, we can use floated columns with a fixed right column that will push down the content below it.

HTML:

<div class="wrap">
    <div class="left">
        Left
    </div>
    <div class="right">
        Right with a ton of content
    </div>
</div>
Here's some pushed down content

CSS:

.wrap {
    position: relative;
}

.left {
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    float: left;
    width: calc(100% - 60px);
    background-color: green;
}

.right {
    float: right;
    width: 50px;
    background-color: red;
}

@media only screen and (max-width: 500px){
    .left {
        width: auto;
        float: none;
    }
    
    .right {
        float: none;
        width: auto;
        position: static;
    }
}

.wrap:before,
.wrap:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.wrap:after {
    clear: both;
}

/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.wrap {
    *zoom: 1;
}

Upvotes: 3

kristjan reinhold
kristjan reinhold

Reputation: 2048

A possibility is <div class="visible-xs"> So you build your stuff the way you want and then use this in case of a mobile. Wouldnt recommend though =D

Upvotes: 0

ben.kaminski
ben.kaminski

Reputation: 976

I'm not sure if you're building this responsive site from scratch or using a library such as Twitter Bootstrap to do so, that would be my first question.

Secondly, if the table-cell option worked well across all but Firefox, you could write some Mozilla specific CSS using the answer found here: mozilla specific css

This would allow you to tweak just the CSS for FireFox. There are also plenty of FF specific CSS extensions here: https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Mozilla_Extensions

Upvotes: 0

Related Questions