Posts Tagged ‘css3’

Styling table columns with nth-child()

Tables in HTML are dead. Well, mostly. Sometimes you actually have tabular data that you wish to show on a website, and in that case the <table> element is obviously still the best choice.
One problem that I often encounter is that while you can style table rows quite easily with a single class per row, styling columns is harder.

Here's a random example table from a site I've been working on.

Month Credits Earned Dollars Earned
June 2012 2,054 $133
July 2012 1,580 $89
June 2012 3240 $187

Classes

Now say you want to align the columns with numbers to the right and give the final column a different background color. One of the ways to do this is to use classes on each of the table cells.

This isn't a bad solution per se, especially if you use semantic classes and not just class="align-right", but on big tables you end up with lots and lots of classes horizontally (for every cell that you need to style) and vertically (for every row, repeat the cells and their classes). I don't like that.

The <col /> element

Another solution is to use the <col /> element. By defining the columns in your table you can add CSS hooks to it in this way:

<table class="sales">
  <colgroup>
    <col class="month" />
    <col class="credits" />
    <col class="dollars" />
  </colgroup>
  <thead>
    <tr>
      <th>Month</th>
      <th>Credits Earned</th>
      <th>Dollars Earned</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>June 2012</td>
      <td>2,054</td>
      <td>$133</td>
    </tr>
    <tr>
      <td>July 2012</td>
      <td>1,580</td>
      <td>$89</td>
    </tr>
  </tbody>
</table>

(I left out a row for brevity)

Now you can use .month to style the first column, or if you only want to style the contents and not the header cells you can use .month td.

The problem? You can only style the border, background, width and visibility. Not even the text-alignment. Why? No idea, it shouldn't be too hard to implement other styles there as well, right? (Fun fact: IE6 does support all styles this way)

nth-child()

So that leaves us with one more solution: using CSS3's pseudo-classes. The nth-child() selector allows you to choose specific siblings of elements without the need for classes. For example, if you want to style the "month" column you only have to count to one:

.sales td:nth-child(1) {
  color: red;
}

Or if you want to align both the heading and content cells that contain numbers to the right:

.sales tr :nth-child(2),
.sales tr :nth-child(3) {
  text-align: right;
}

There's a lot more to the nth-child than just this. You can use counters to select every nth element: :nth-child(2n+1) selects every second element, starting with the first, :nth-child(-n+3) styles only the first three elements, and if that's getting too confusing don't visit nthmaster.com, because I've only barely scratched the surface.

The downsides

Of course there's some minor downsides to this method. First of all, it's not supported by IE8 and lower. I know, that's still a problem, but Microsoft has been getting better at pushing their browser updates so I expect it won't even be too long before IE8 won't have to be supported anymore. Or you can just use Selectivizr.

Another downside is that it gets a little harder to maintain. When you have to add a column to a table, you'll have to re-count instead of just adding a <td> with a class. If you know your tables are going to change a lot, maybe stick to classes for now.

All in all though, for projects where I know the HTML doesn't change much and I'm not too bound by supporting older browsers, I tend to use these pseudo-selectors more and more.

If you feel like playing around a bit more with these rules, see this awesome Pseudo-class selector tester and the aforementioned nthmaster.

The difference between pseudo-classes and pseudo-elements

Today I stumbled upon an article by ImpressiveWebs about  the difference between :before and ::before in CSS3. I had wondered about this before, so the article was actually an interesting read.

In summary, the difference is that in CSS 2.1 pseudo-elements and pseudo-classes are both targeted using the single colon (:before). In CSS3 however, a distinction is made between the both, pseudo-elements now being prefixed with two colons (::before).

Why? Not sure, but as Louis points out the rule may have come a little too late anyway. Because a lot of pseudo-elements were already introduced in CSS 2.1, backwards compatibility will have to be ensured by supporting the single-colon version as well. Anyway, more about that in the original post: What’s the Difference Between “:before” and “::before”?

Not surprisingly, one of the comments implied (unless I misunderstood the speaker) not being able to actually tell the difference between a pseudo-class and a pseudo-element. This too is something I’ve been wondering about, but which I found the answer to a couple of weeks back. (more…)

Bruce Lawson: HTML5 is not CSS3!

Bruce Lawson is one of a handful of prominent names in the world of HTML. Working for Opera, member of the Accessibility Task Force of the Web Standards Project, evangelising open web standards, etc. He’s cool.

Anyway, here’s a rant he did a while back about the differences between HTML5 and CSS3, which has become very relevant again as the W3C just released an HTML5 logo that according to them is “is a general-purpose visual identity for a broad set of open web technologies, including HTML5, CSS, SVG, WOFF, and others.” Just thought it’d be fun to share:

Captioning test with Bruce Lawson's HTML5 != CSS3 rant

For some more rants about the new logo and why it’s complete bullcrap to file CSS, SVG and other technologies under the HTML5 moniker I refer you to Jeremy Keith. Also, CSSsquirrel did a comic about it.

Oh, and I really need to get me some ranting headgear.

Update:

t Tantek Çelik

We did it. @adactio, friends, comrades, we did it. @W3C fixed the FAQ. http://www.w3.org/html/logo/faq #html5logo http://ttk.me/t49v1

Selectivizr: CSS3 features in IE6-8

As I’ve just started at my new job yesterday I won’t have much time to write extensive articles for a while. Not during working hours that is, and as I’m in the middle of moving back to my hometown I don’t see myself writing anything from home either.

So here’s just a short one that I felt the urge to share: Selectivizr. As tweeted by @zeldman:

selectivizr is a JavaScript utility that emulates CSS3 pseudo-classes and attribute selectors in IE 6-8.

From the looks of it, by just including a script and using one of the main seven javascript libraries (among which are jQuery, MooTools and ProtoType) you can now safely use CSS3′s :first-child, ::first-letter and all the other cool pseudo-selectors. Also the advanced attribute selectors like [attr=], [attr~=] and [attr^=] are supported, making it even easier to style your websites without using all those redundant classes and id’s on all your document’s elements.

Sounds awesome, right? I’ll post an update when I’ve found the time to give it a try.

CSS3: Indicate active menu item with a triangle

Today on StackOverflow a user asked the following question:

In the following picture, I have mocked up a CSS active menu styling I would like to find examples of, and hopefully replicate.

Navigational menu with a triangle indicating active item

You will notice there is a triangle highlighting the active menu, and it can appear bold too.

Question 1. What is this “active menu triangle style” actually called? So I can find CSS examples of it.
Question 2. Where can I find examples of this?

Thanks.

Source: What is this CSS active menu style with triangle called?

While I don’t know what this kind of menu would exactly be called, it did get me thinking about how to solve this using only CSS, so preferrably without background images.

My first thought was to use a css border triangle, but those only come in one solid color per triangle. This one needed to be a white triangle with a border, which can’t be achieved with the border slopes method. So I figured I’d just have to append an element looking like a triangle, using the :after pseudo-element. I’ll explain how I did that.

(more…)

CSS3 rotate makes elements overlap

The problem

As I was working on my portfolio page I noticed an interesting bug in my theme: the thumbnail images in my posts are surrounded by an <a> element (they link to the original image) but hovering them didn’t always give the hand cursor. Clicking on them didn’t work either when the hand wasn’t appearing. At first I thought my links were broken, but the problem seemed to occur only on some areas of the images. After playing around I made the following observations:

  • All the linked images on the page produced the same bug
  • All the linked images were floated to the right. Not the <a> itself, but just the <img> was floating.
  • Making the <a>‘s float in stead of the <img>‘s didn’t fix the problem
  • Removing the floats did fix the problem.
  • Other websites didn’t seem to have this problem with floating links or images
  • Other websites don’t usually use the transform: rotate() CSS3 property

The investigation

I went on to investigate the structure of the HTML, which can be boiled down to the following simplified example: (more…)

Durango theme for WordPress

Durango is the name for my newly created WordPress theme, the one you’re probably seeing right now. I’m not a graphical designer, so most of the visual aspects I borrowed from a collection of other websites, but I tried to give it my own twist and included some cool new HTML/CSS stuff.

I’ll give a brief overview of how and why it was made, and things I think I should still improve. If you don’t care about all that and just want a copy of this theme, jump to the download button. (more…)