8

Need some help with the CSS for generating a grid of perfect squares. Div's look like this, but I'd like to have each of them look like a perfect square - not a rectangle. Setting width and height in css doesn't do it. :-\

<div class="square" /> ... <div class="square" /> <div class="linebreak" />

<div class="square" /> ... <div class="square" /> <div class="linebreak" />

web-tiki
  • 99,765
  • 32
  • 217
  • 249
ina
  • 19,167
  • 39
  • 122
  • 201

4 Answers4

5

You need to combine these style rules to get what you need. The float property ensures they stack in a horizontal row, the block rule allows you to set the height and width of the element and the overflow hidden rule stops it from expanding with the content.

.square {
    float: left;
    width:200px;
    height:200px;
    display:block;
    overflow:hidden;
}
Fenton
  • 241,084
  • 71
  • 387
  • 401
3

Thanks to http://dinosaurswithlaserz.com/2011/07/18/fluid-squares-v2/ for pointing out it can be done with pure CSS and be fluid, like this:

.onesquare {

width: 30%;
margin: 0px 2% 0 0;
padding-bottom: 30%;
background-color: red;

}
aroundtheworld
  • 731
  • 1
  • 5
  • 15
1

Thats unusual

try something like this. It should work

.square {
   width:100px;
   height:100px;
   display:block;
   overflow:hidden;
   float:left;
}

See: http://jsfiddle.net/EyXpC/

Starx
  • 77,474
  • 47
  • 185
  • 261
0

Use display:block together with width and height attributes.

o15a3d4l11s2
  • 3,969
  • 3
  • 29
  • 40