Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed margins on a responsive layout?

Simply put, I am working on a responsive web layout with columns and rows. Each column width is set relevant to a predefined total width:

column width / total width = width %

and the height is fixed.

Now the problem is, I want the content width to be fluid but the margin and padding to be of fixed width as below.

enter image description here

enter image description here

The code looks something like this:

HTML

<body>
   <div id="left">Left</div>
   <div id="middle">Middle</div>
   <div id="right">Right</div>
</body>

CSS

div {
   float: left;
   padding: 0.5em;
   box-sizing: border-box;
   width: 33.3333333333333%; height 5em; line-height: 5em; text-align: center;
}    

#left {
   background-color: red;
}

#middle {
   background-color: green;
}

#right {
   background-color: blue;
}

http://jsfiddle.net/langoon/Zxn8E/2/

I have thought of some solutions but none of them seem to do what I am looking for :

  1. Set 'box-sizing' to 'margin-box'. Not supported in most browsers.
  2. Instead of margins, use borders. I might want that border and style it differently later.
  3. Nest each box in an outer 'div'. I prefer to keep the number of elements for the DOM to process at a minimum.
  4. Set negative margins as suggested here Fluid column layout with fixed pixel margins between them?. Seems to require an wrapping 'div' for each row and that might pose problems for me.
  5. JavaScript. No thanks.

Am I trying to have my cake and eat it at the same time or is there a way to do this without the drawbacks?

like image 888
unitario Avatar asked Feb 23 '26 02:02

unitario


2 Answers

With :before / :after

I had fun with :before and :after pseudos, covering half of each gutter with a rectangle: http://jsfiddle.net/PhilippeVay/Zxn8E/11/ (note where the ending tags are, to prevent any whitespace between inline-block elements)

Compatibility: IE8+ as IE7 and lesser don't understand these pseudos.

With Flexible Box Layout Module

This CSS3 module will let you have equal width columns with gutter inside and none outside, exactly what you want to achieve. No fiddle as the syntax changed twice in a few months, I still haven't looked at the new one.
Compatibility on caniuse.com (no IE9-, no Opera)

like image 121
FelipeAls Avatar answered Feb 25 '26 16:02

FelipeAls


I think the best way to solve this is using a negative margin and the box-sizing property. A quick example:

HTML:

<ul>
    <li><div class="red">Red</div></li>
    <li><div class="green">Green</div></li>
    <li><div class="blue">Blue</div></li>
</ul>

CSS:

ul
{
    list-style: none;
    margin: 0 0 0 -15px; /* Note the negative margin */
    padding: 0;
    overflow: hidden;
}

li
{
    float: left;
    width: 33.3333%;
    padding: 0 0 25px 15px;
    box-sizing: border-box;
}

/* Some decorations */
div { padding: 25px 0; color: white; text-align: center; }
div.red { background: red; }
div.green { background: green; }
div.blue { background: blue; }

Result:

enter image description here

Live Demo

If you want to see this in action, I've created a Fiddle.

like image 41
Jonathan Avatar answered Feb 25 '26 15:02

Jonathan