Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating 3 Perfectly Equal Columns that take up 100% on the Browser Window Width

Tags:

css

I have 3 square images of the same size that are floating next to each other. I want the three images, in total, to take up the full 100% of the browser window width, with no gaps. Giving each image a width of 33.33333333% works in Firefox, but does not work in most other browsers at certain widths, which can sometimes leave a small gap to the right of the 3rd image.

This may be a problem with many solutions, but nothing I've tried so far works reliably.

like image 459
cmal Avatar asked Sep 29 '11 01:09

cmal


People also ask

How do I make three equal columns in CSS?

We can create a 3-column layout grid using CSS flexbox. Add the display: flex property to the container class. Set the flex percentage value to each column. Here for three-layer, we can set flex: 33.33%.

How do I make columns equal width in HTML?

Just add style="table-layout: fixed ; width: 100%;" inside <table> tag and also if you do not specify any styles and add just style=" width: 100%;" inside <table> You will be able to resolve it.

How do I make all the columns the same width in CSS?

The CSS to make all the columns equal in width is as follows. The table-layout: fixed; rule specifies that the table is to take its dimensions from the <table> element's width and the width of the first row of cells (or the <col> elements, if you have them).


2 Answers

Try this:

HTML

<div class="container">     <div class="column">         <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />     </div>     <div class="column">         <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />     </div>     <div class="column">         <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />     </div> </div> 

CSS

html, body {     margin:0;     padding:0;     width:100%;     height:100%; }  .container {     width:100%; }  .column {     width:33.33333333%;     float:left; }  .column img {     width:100%;     height:auto; } 

Demo

http://jsfiddle.net/andresilich/2p8uk/

Single page demo

http://fiddle.jshell.net/andresilich/2p8uk/show/

CSS3 demo

html, body {     margin:0;     padding:0;     width:100%;     height:100%; }  .container {     display:-moz-box;     display:-webkit-box;     display:box;     -moz-box-orient:horizontal;     -webkit-box-orient:horizontal;     box-orient:horizontal;     width:100%; }  .column {     -moz-box-flex:1;     -webkit-box-flex:1;     box-flex:1;     background-color:#ddd; }  .column img {     width:100%;     height:auto; } 

Demo

http://jsfiddle.net/andresilich/2p8uk/2/

Single page demo

http://fiddle.jshell.net/andresilich/2p8uk/2/show/


Update: (safari, sorta, fix) Safari does not equate 33.33% to 100% like other browsers, you can either use my CSS3 demo, which does the sizing automatically through CSS, or you can encase everything inside a container with a 101% width and just hide that extra 1% with overflow:hidden; off of the third image. Try this:

<div class="container">     <div class="inner">         <div class="column">             <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />         </div>         <div class="column">             <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />         </div>         <div class="column">             <img src="http://nssdc.gsfc.nasa.gov/planetary/image/neptune_voy2.jpg" />         </div>     </div> </div>  html, body {     margin:0;     padding:0;     width:100%;     height:100%; }  .container {     width:100%; }  .inner {     width:101%;     overflow:hidden; }  .column {     width:33.33333333%;     float:left; }  .column img {     width:100%;     height:auto; } 

Demo: http://fiddle.jshell.net/andresilich/2p8uk/4/

like image 146
Andres Ilich Avatar answered Sep 23 '22 02:09

Andres Ilich


For those that come here now (oct 2015) you can use calc:

width:calc(100% / 3);

This will give you exactly 100% when added up. There are some browserbugs but you can use it without many issues: http://caniuse.com/#feat=calc

like image 32
Cyril Mestrom Avatar answered Sep 20 '22 02:09

Cyril Mestrom