Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS ID variations

Hey, I am guessing that this is probably fairly trivial, but I am having difficulty finding an answer or figuring it out nonetheless.

I'm trying to create a grid of colored squares with arbitrary spacing between them. This in itself is easy to do, especially because I need only nine squares. But while I look at my completed code, I cannot help but feel there is a far more simple and efficient way to accomplish this.

At the moment, I have nine different IDs declared in my css, one for each square.

div.container{
    position:relative;
    left:50px;
    top:50px;
    background-color:rgb(45,45,45);
    height:300px;
    width:300px;
}

#square{
    position:absolute;
    background-color:rgb(52,82,222);
    left:20px;
    top:20px;
    height:80px
    width:80px

#square2{
    position:absolute;
    background-color:rgb(58,82,22);
    left:110px;
    top:20px;
    height:80px;
    width:80px;


etc etc etc

What I would like to do is find a more efficient way to do this.

Thanks for the help!

like image 877
danem Avatar asked Apr 10 '26 12:04

danem


1 Answers

You can use a class for the squares that share a property:

.square {
    position: absolute;
    width: 80px;
    height: 80px;
}

Is there a specific reason you're absolutely positioning them though? Sounds like a job better suited for floats.

div.container {
    width: 240px;
}

.square {
    float: left;
    width: 80px;
    height: 80px;
}
like image 191
dtbarne Avatar answered Apr 13 '26 01:04

dtbarne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!