Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded corner tables with LESS

Tags:

css

less

After some digging around on SO I found this as the best response for my need of having rounded corners for tables.

Which lead me to the following CSS snippet:

.greytable tr:first-child th:first-child {
    -moz-border-radius-topleft: 5px;
    -webkit-border-top-left-radius: 5px;
    border-top-left-radius: 5px;
}

.greytable tr:first-child th:last-child {
    -moz-border-radius-topright: 5px;
    -webkit-border-top-right-radius: 5px;
    border-top-right-radius: 5px;
}

.greytable tr:last-child td:first-child {
    -moz-border-radius-bottomleft: 5px;
    -webkit-border-bottom-left-radius: 5px;
    border-bottom-left-radius: 5px;
}

.greytable tr:last-child td:last-child {
    -moz-border-radius-bottomright: 5px;
    -webkit-border-bottom-right-radius: 5px;
    border-bottom-right-radius: 5px;
}

Now I'd like to know how I could simplify all these with LESS. I tried the following LESS code:

.border-radius (@v, @h, @radius: 5px) {
    -moz-border-radius-@v@h: @radius;
    -webkit-border-@v-@h: @radius;
    border-@v-@h: @radius;
}

And then invoke it (for the top left corner):

.greytable tr:first-child th:first-child {
    .border-radius('top', 'left')
}

But it doesn't work (error on the second line of the LESS snippet).

Thanks in advance!

like image 228
janosrusiczki Avatar asked Mar 02 '12 08:03

janosrusiczki


People also ask

How do you make a table with rounded corners?

Click the Insert > Shapes button and choose the Rounded Rectangle tool.

Why rounded corners are better?

Rounded corners are more effective for maps and diagrams because they allow our eyes to easily follow lines “as it suits better to the natural movement of the head and eyes respectively” [5]. Sharp corners throw your eyes off the path of the line so you end up experiencing abrupt pauses when the line changes direction.

What is a curved corner called?

bevel. (redirected from Rounded corner)


1 Answers

You might need to use the string interpolation syntax, try this:

.border-radius (@v, @h, @radius: 5px) {
    -moz-border-radius-@{v}@{h}: @radius;
    -webkit-border-@{v}-@{h}-radius: @radius;
    border-@{v}-@{h}-radius: @radius;
}

I would also add that webkit and mozilla are largely up to speed with the standard border-radius property, and the vendor prefixes are becoming outdated for it.


EDIT: It seems that string interpolation isn't working out for this task (the above code won't compile), so here's a workaround mixin that will actually be easier to use:

.rounded-table(@radius) {
    tr:first-child th:first-child {
        -moz-border-radius-topleft: @radius;
        -webkit-border-top-left-radius: @radius;
        border-top-left-radius: @radius;
    }
    tr:first-child th:last-child {
        -moz-border-radius-topright: @radius;
        -webkit-border-top-right-radius: @radius;
        border-top-right-radius: @radius;
    }
    tr:last-child td:first-child {
        -moz-border-radius-bottomleft: @radius;
        -webkit-border-bottom-left-radius: @radius;
        border-bottom-left-radius: @radius;
    }
    tr:last-child td:last-child {
        -moz-border-radius-bottomright: @radius;
        -webkit-border-bottom-right-radius: @radius;
        border-bottom-right-radius: @radius;
    }
}

Usage:

.greytable {
    .rounded-table(10px)
}

Output:

.greytable tr:first-child th:first-child {
  -moz-border-radius-topleft: 10px;
  -webkit-border-top-left-radius: 10px;
  border-top-left-radius: 10px;
}
.greytable tr:first-child th:last-child {
  -moz-border-radius-topright: 10px;
  -webkit-border-top-right-radius: 10px;
  border-top-right-radius: 10px;
}
.greytable tr:last-child td:first-child {
  -moz-border-radius-bottomleft: 10px;
  -webkit-border-bottom-left-radius: 10px;
  border-bottom-left-radius: 10px;
}
.greytable tr:last-child td:last-child {
  -moz-border-radius-bottomright: 10px;
  -webkit-border-bottom-right-radius: 10px;
  border-bottom-right-radius: 10px;
}
like image 142
Wesley Murch Avatar answered Oct 21 '22 07:10

Wesley Murch