Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm surprised at the lack of semantic meaning in most popular grid frameworks.

Years ago, we moved away from layout with tables because <table><tr> is a horrible way to separate presentation from content but <div class="two-thirds columns"> is just as bad.



What exactly do you mean by "semantic meaning"? Meaning to who? For what purpose?

Classes on elements exist for the purpose of styling via CSS, not for users to see or screen readers to read or googlebots to classify. So if you want your css classes to "mean something" (which is what "being semantic" is), you want them to mean something to the STYLE of your page! Hence, using class="two-thirds columns" is perfectly semantic towards the purpose of visually styling the page.

Did you have some other interpretation of "semantic" that you think should apply instead? Perhaps you mean "the structure of the content"? That's not what classes are for -- that's what the elements themselves are for. Or perhaps you mean for visually-impaired folks to be able to easily navigate the content? That's not what classes are for either -- that's what ARIA roles are for. SEO perhaps? It's possible that google uses class names for this, but that's just black magic voodoo that us mere mortals can have no knowledge about (and even if we did, it could change tomorrow).

Here's the thing about using tables for layout... the table element actually has a defined meaning in HTML... it is for tabular data. The div element however does not imply any meaning (other than "arbitrary division of the page"). So using a div here with the classes that imply meaning to the CSS is quite "semantic" in this case.


"Semantic" in the sense he's complaining about means, roughly, "what the thing is" rather than "how the thing looks". So instead of 'class="one-third column left"' you would write 'class="left-navigation"' or, if you want to be more pure 'class="category-navigation"', which acknowledges that being on the left or right is itself a presentation choice that should be controlled in CSS. Behind the scenes, of course, you can use SASS or similar to apply the styles of left, one-third width, etc, to your semantically-named class, without changing the name "category-navigation" or your markup at all.

Markup written this way is, imo, indeed more readable, maintainable, and less likely to change. Whether you agree with that or not, that's the meaning of "semantic" in CSS arguments.

The argument here is similar to the argument for writing declarative code, or, in OO, for ensuring that your objects, and their names, match natural domain concepts. The rates of change of such concepts are generally much slower than the rates of change of their concrete manifestations in views.


I respectfully / slightly disagree. The "thing" we're referring to here is a div whose sole purpose is for the grid layout.

The way I see it, that thing is a boundary for the grid. In fact, I bet if you narrow your screen, that "thing" changes the way it looks entirely and is no longer a "column" but rather stacks above or below its counterparts within the same grid "row". If you used the class "left-navigation", to me that's describing how it looks even more than saying it's a "grid column" (because the navigation won't be on the "left" on narrow screens)!

Of course your example of class="category-navigation" does not suffer from this problem, but again my point is that sure it's more "pure" from the perspective of describing the element's place in the overall structure of the page, but this is not the purpose of the 'class' attribute! The HTML5 elements are for structuring the content... but classes are hooks for styling the content. As long as the styles themselves are not intermingled within the HTML, I think it's no more or less "semantic" to use words that imply meaning to the css versus the content itself.

Finally, in my experience building tons and tons of CMS-based sites over the years, the "meaning" of the content that exists in a particular location of the page changes a lot more frequently than the visual layout of the page. So I'd argue that it's more likely (in my experience, for the kinds of sites I tend to build) that using "column" as a class name will keep its intended meaning for much longer than using something like "category-navigation" (because one day someone in marketing is going to want to put a CTA button or more social links in that spot that you originally intended to be for "category navigation").


class="two-thirds columns" enforces a strong coupling between the HTML and how it is presented.

To change how wide a column is, you change its class. To change how wide an image is, you go into the CSS. To change how wide a column's border is, you go to the CSS. Doesn't changing the class seem a little out of place?

In theoretical terms, the class is semantically the category to which the component belongs. a <section class="cart"> is perfectly reasonable, whereas <div class="two-thirds columns"> is not. The <div> and it's children are not tied to the notion of how wide the div is. The fact that a section has class "cart" is important in understanding it's children.


I don't see how class="two-thirds columns" enforces coupling any stronger than using a different class name... what if that div only exists for the purposes of the grid layout? As long as the styles themselves are in CSS, how is it any different? I mean, of course if you just arbitrarily named the class with gibberish, that's no good -- but the point I'm trying to make is that the CSS classes exist to impart meaning to the designer (or the developer), not the end-user and not the screen readers and not search engine crawlers... so "two-thirds columns" is perfectly semantic because it is giving meaning to that div whose entire purpose in life is to be a two-thirds column in wide-screen view :)

I totally understand your point, and I'm kind of being obtuse for the sake of argument... but I do think the term "semantic" is thrown around without people really thinking about "semantic for whom" or "towards what purpose".


> what if that div only exists for the purposes of the grid layout?

Those things aren't supposed to exist. HTML is supposed to look like:

    <div class="blogpost><h1> ...
    <div id="mainmenu"> ...
We never got enough power in CSS to actually do that, and current frameworks are a return to the table based layout of the 90s where structure of the layout (not structure of the content) is stored in the HTML.

If semantic in terms of HTML is ever thrown around meaning anything but description of the content, it is used wrong.

The question about "semantic for what purpose" is moot when talking about HTML. It is semantic towards what information the content conveys.

"Semantic HTML is the use of HTML markup to reinforce the semantics, or meaning, of the information in webpages rather than merely to define its presentation or look."

https://en.wikipedia.org/wiki/Semantic_HTML

The question of whether that is feasible to do with current technology is completely different, but it is too late to try and redefine semantic HTML.


Okay, let's say instead of calling those classes "columns", we instead call them "item". (I do this with my own "semantic grids" -- instead of "row" and "column", I use the terms "group" and "item", because on narrow screens the columns aren't actually columns). It seems to me that I could use a div to group content together in this way and it then is used to describe the structure of the content (which things are grouped together).

Everything is so loosey-goosey with HTML. HTML5 sectioning elements are a huge clusterfuck, no browsers utilize the document outline and screen readers primarily rely on ARIA roles. Google uses black magic to figure out what documents mean... so the "html can only contain information that defines the content" ship has sailed. You need to have divs in your html to achieve certain layouts. As long as those divs need to be there to serve that purpose, why not use class names that are related to the purpose you're using them for?

This is very different from table-based layouts. The <table> element does have a defined meaning! But div's do not... so using divs and styling them with CSS achieves proper separation of concerns. The label you use to describe them only has meaning to you (the designer/developer).


> You need to have divs in your html to achieve certain layouts.

I'm not saying that semantic HTML is working today. But just because it isn't working doesn't mean we should start calling what is working for semantic HTML.


Reminds me of @hsivonen's wonderful "HOWTO Spot a Wannabe Web Standards Advocate": https://hsivonen.fi/wannabe/

Edit: Curious why this is being downvoted. Is humor (intelligent humor in this case IMO) really that frowned upon at HN? Or did someone think I was calling the parent commenter a "Wannabe Web Standards Advocate"? (I certainly wasn't. Quite the opposite.)


Nicolas Gallagher wrote a pretty thorough rebuttal to this popular idea (at least popular in comment sections, as you note none of the frameworks that have taken the web by storm recently pay any attention to it).

http://nicolasgallagher.com/about-html-semantics-front-end-a...

Conclusion:

"When you choose to author HTML and CSS in a way that seeks to reduce the amount of time you spend writing and editing CSS, it involves accepting that you must instead spend more time changing HTML classes on elements if you want to change their styles. This turns out to be fairly practical, both for front-end and back-end developers – anyone can rearrange pre-built “lego blocks”; it turns out that no one can perform CSS-alchemy."


I share your frustration, but a general purpose css framework can't really avoid using this approach to support layout. What possible alternative semantic classes could they use that would provide the level of flexibility they're catering for?


It's very doable, but Sass or similar is necessary.

For example: http://neat.bourbon.io/


This is just absurd:

    div#alpha {
      @include span-columns(1);
    }

    div#beta {
      @include span-columns(11);
    }

    div#gamma {
      @include span-columns(2);
    }

    ...
How is this any better than col-xs-2, col-xs-4, etc.? I see absolutely no reason to obsess over semantic markup anymore. We all have work to do and grid frameworks help us actually get things done.


In this way the HTML is static and when you have to change a presentation detail (like the width of the columns) you just have to edit the CSS - as it's meant to be.


Until you have a ton of automated tests. Then a markup (class) change breaks all the selectors used in the tests - not fun. I prefer to abstract the styling so I don't have to fix the tests if the styling needs to change.


An HTML element can have both a class and an ID or multiple classes. Your tests should not have any knowledge of your framework's classes.


So your saying that tests should always rely only on the id attribute? Tell me how you target elements that have id attributes auto-generated by a framework either on the front end, Ember for instance, or the back end?

It's a nice idea, but falls over pretty spectacularly in the real world when you have a complicated web application.


Read my comment again. You can select elements using whatever CSS/XPATH selectors you want. Whether those elements have additional presentational CSS classes on them is completely irrelevant since the selectors will still work.


Thanks for the link. I know that Bootstrap can be used in a similar way (in theory, I haven't tried it myself) but this just seems much lighter and focused on that specific use-case rather than "also you can use it like this" in Bootstrap.


AFAIK you shouldn't get this problem if you use the sass/less sources of these frameworks directly.

But I didn't find any good tutorials on this.

Also, I don't know how this should change the html-elements needed by stuff like the bootstrap components.


No tutorial needed.

1. Create your HTML using only semantics. If you're tempting to create a class called, say, "column" or "left-container", stop yourself. If you have multiple items that will be presented the same way, give them the same class. For a shopping site, for example, a class might be "product".

2. Use a CSS preprocessor to inherit non-semantic classes from your framework of choice into the classes you created in step 1. You might use the framework's classes as they are or modify them a little.


(didn't down-vote)

This may work for simple cases. But what about stuff like the Bootstrap components, which consist of many HTML-elements?


You have two options.

A. You can compose components the same way Bootstrap does, but using semantic classes. For example, if Bootstrap is using "thumbnail-container", you could call your class "product-thumb-container". Because of the way HTML works, container classes are often necessary, and they can be semantic. Think about a legal document: at the end of them, you have appendices. An appendix is like a container: it's factored into the overall structure, but it also has a semantic value. You can't move an appendix somewhere else and still call it an appendix.

B. You can use only the parts of Bootstrap that you want, so you could leave components out altogether. As mentioned above, this might be a better option for a larger project with fine-grained, custom designs.


If you're going this route, then you wouldn't use Bootstrap. You'd dreg up your own semantic/non-semantic styles from scratch.

This works really well on projects that have a dedicated design team.


This sounds interesting, but I definitely need a tutorial for step 2.


Just look up the syntax for extending classes for the preprocessor of your choice. This is such an important part of the preprocessors that it'll usually be on the landing page for the documentation (sometimes above the fold).

I'd recommend SCSS if you're just getting started.


I got tired of using CSS frameworks where classes are used to define widths. Now, I much prefer frameworks that support semantic style like Susy.

http://susy.oddbird.net/


If you use SCSS or LESS, you can inherit the widths into semantic classes. So Bootstrap can be as semantic as you want it to be.


Just be sure to use @include and not @extend, otherwise you'll wind up blowing through your allowed selector ration and wind up wonky css bugs that you will never be able to resolve without re-writing everything.


Its not as bad because table had another meaning that was being abused because there was nothing else. That's not the case for grids. Also grids are presentation, not semantic.


Agree, I really like this one http://semantic.gs/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: