8 ms·
Whenever anybody says that CSS is easy, or "of course you can do that with CSS", or even hints that CSS is somehow well-designed... ...the only thing I really
by crazygringo 13y ago
Whenever anybody says that CSS is easy, or "of course you can do that with CSS", or even hints that CSS is somehow well-designed...
...the only thing I really need to say, is that every couple months, on Hacker News, where a lot of really smart people hang out, a new top-voted story comes up about how to center a div. With tons of comments and discussion too. And it's not even a joke.
What more is there to say?
- adwf 13y agoNext week: The complete guide to sticky footers! It's unbelievable just how annoying CSS can be for seemingly simple things.
- sopooneo 13y agoI may be using words incorrectly, and I'm not positive you can even make the distinction, and I am happy to stand corrected, but it always seemed to me that CSS was fine for style, but terrible for layout. So I think I'm agreeing with you.
- jebblue 13y agoRight, to me plain HTML tables are great for overall master page layout yet it seems everything I've read and the few web geniuses I've met will deride using them over CSS.
- icedchai 13y agoSame here. It's amazing how convoluted "modern" CSS can be compared to simple, reliable tables.
- rimantas 13y agoLayout with CSS is really much simpler than doing that with tables.
- statictype 13y agoGenerally I agree that too many people have a fetish about avoiding tables when it's clearly the right tool for the job. But I've definitely been bitten by using them. Table layouts don't play well with other CSS properties.
- voltagex_ 13y agoDepending on how you do the layout it also may not play well with screenreaders.
- sopooneo 13y agoWhy do people care about screen readers? Is it because they are good people and want to make sure sight-impaired visitors are able to access their content? Or is it because they have contracts that require 508 compliance?
- voltagex_ 13y agoI guess it's a good question. At a recent conference it [website accessibility] was described as the digital equivalent to putting a ramp in at your door. Where I work at the moment I'm required to comply with WCAG2 AA at a minimum.
- jimhart3000 13y agoWhy does it matter why they care? Does doing it for 508 compliance somehow make it less important? It's simply the right thing to do.
- sopooneo 13y agoI ask purely out of curiosity, not because I think the answer should affect my, or anyone else's, actions.
- buttsex 13y agoIf you are using HTML tables for layout then you definitely aren't using the right tool for the job. If it isn't tabular information then it doesn't belong in a table.
- unclebucknasty 13y agoExactly. I mean, I get the part where people are against layout with tables in principle. I just hate the part where they act like CSS is a good alternative in practice.
- robmcm 13y agoEven better is to draw everything in a canvas!
- unclebucknasty 13y agoTo that point, no human should have to work directly with CSS for layout!
- cabalamat 13y ago> but it always seemed to me that CSS was fine for style, but terrible for layout. I think you're right there. When I've set colours, fonts, etc in CSS it's always worked first time, but getting layout right it feels like I'm fighting against the system all the time. I wonder if it wouldn't make sense to build a tool that abstracts out the layout part of CSS? I once built a tool in python that lays out Tk/Tkinter GUIs[1] which I may have a go at adapting to produce CSS/HTML output. 1: https://github.com/cabalamat/parrot https://github.com/cabalamat/parrot
- stormbrew 13y agoI think the issue, in terms of CSS being bad for layout, is that it tries to treat the logical structure of the document as the layout structure. If there were a way to restructure the document and then apply CSS to it a lot of things would get a lot easier and separation of content and presentation could be more realistic. XSLT was interesting in providing some of this kind of restructuring power, but was far too heavyweight for pure layout manipulation. Of course, this is also what flexbox tries to do and I think we'll be groaning about its lack of relative power in this area in the future when it's more established. It feels like a bandaid to me.
- jebblue 13y agoThis is as close as I can get without HTML tables: <html> <head> <title>Testing making a footer with CSS</title> </head> <body> <div id="divpage"> <div id="divcontent" style="overflow: auto; height: 60%;"> This is the content area. </div> <div id="divfooter" style="overflow: auto; height: 30%;"> This is the footer. </div> </div> </body> </html>
- adwf 13y agoI usually go with the footer height 50px, content min-height 100%, bottom-margin -50px. Generally works, but still has some edge cases that can throw it for a loop. Whereas what I instinctively want to do is something more like a conditional absolute positioning. If the content fits above the fold, then have the footer absolute at bottom:0. Else, have it relative to the content div. It can be done in JS of course, but I feel it should be a lot easier to do in CSS.
- nfoz 13y agoHonestly, please don't use sticky footers. I curse CSS not because that's difficult, but because it's possible. :/
- tripzilch 13y agoIn the case of sticky footers I'm all for the adage "if it's annoying to use, it should be annoying to style".
- kmfrk 13y agoI recently went back to a project to convert the CSS to SASS, and shit was over 1,200 LOC. I think I managed to trim something like 200 LOC, but it turns out that there were a truckload of hotfixing for all the evil quirks CSS throws at you at every given opportunity. The LOC just grows exponentially, when you approach 100% non-quirky cross-browser uniformity. I will say this; CSS is a great lesson in humility for every hacker and programmer who try their hand at it. And you can learn a lot about someone by how they describe CSS and the front-end developers who make a living working with it.
- magicalist 13y ago> With tons of comments and discussion too To be fair, the majority of comments tend to either be ones like yours, or ones elaborating on the "CSS is somehow well-designed" front. As others have noted, tools like flexbox make what the article is trying to accomplish easy. The issue is that lots of users run really old browsers, so if you want a single solution across your user population, you're going to have to resort to the same really old hacks. It's silly to say that "nothing has changed", when what you're really saying is "nothing has changed in the browsers that haven't changed". I'm not sure that should be surprising.
- stormbrew 13y agoWell no, the issue is that we're getting close to 20 years since CSS was introduced and they only got around to introducing something that makes this basic simple task 'easy'[1] relatively recently. It's not even like this was some basic part of CSS2 that got ignored by vendors, it became a candidate recommendation for CSS3 in 2012. [1] It makes it 'easy' by introducing yet another weird box modelism that doesn't really fit anything else that's already there. It's really as much a symptom of these problems as a solution, imo.
- magicalist 13y agoI'm not sure what your point is. Web standards have had a turbulent history? Of course that's true. - CSS 2 was originally published as a recommendation in 1998. CSS 2.1 was pushed as a recommendation in 2011. Maybe something was wrong? - "Ignored by vendors" is a bit of an understatement, because your "close to 20 years" includes 5 years of the vendor with 90% marketshare not releasing anything at all. - FWIW, what became flexbox was useable (assuming polyfills aplenty) in Firefox and Webkit browsers 5 years ago[1]. It took some time to standardize, but while frustrating in the present, it's ok to take time to make sure the spec works, because it will be (in theory) around for quite a while. > It makes it 'easy' by introducing yet another weird box modelism that doesn't really fit anything else that's already there What many people don't appreciate about layout in general (and WRT to the web, dynamic layout in particular), is that it turns out to be difficult (and sometimes extremely computationally expensive) to implement an expressive layout system that does what we intuitively think it should. A good example is word wrapping, which is easy to tell when it "looks right" (at least to a trained eye), was done by hand by many when printing was done more manually, but turns out to be difficult to assign a metric that handles all use cases, especially when you're trying to be computationally efficient. Web layout is like a whole set of word wrapping requirements, expected to get along and paint a page in real time. The process that birthed CSS is nowhere near what you'd turn to if you wanted to develop a cohesive styling whole, but it's how history usually plays out. If it's any consolation, I personally don't think there's a comprehensive theory of everything for styling anyways, so there are always going to be aspects that need to be learned, not inferred. In any case, flexbox is pretty straightforward if you spend an afternoon with it, just a constraint-based layout system that maps pretty well to how you would describe a layout like that. [1] http://infrequently.org/2009/08/css-3-progress/ http://infrequently.org/2009/08/css-3-progress/
- zachrose 13y agoSurely we can agree: designing text and images in a fixed frame without alternatives for accessibility is a completely different ball game then designing for screens of immensely variable dimensions with audiences as diverse as completely blind users and search engine bots. Does anyone have an alternative to CSS that tries to take on a problem this big?
- RussianCow 13y agoTo be fair, CSS is hard even if you're only designing for one screen/device.
- cma 13y agoMany tools that handle diverse problems are worse in specific problems: jack of all trades, master of none (not trying to make the case for CSS being good)
- mcantelon 13y agoObligatory: http://4.bp.blogspot.com/-E6ukbgTm5iM/Um0CrEtzXSI/AAAAAAAAG_8/-UoNFePQTcA/s1600/css_is_awesome2.png http://4.bp.blogspot.com/-E6ukbgTm5iM/Um0CrEtzXSI/AAAAAAAAG_...
- twobits 13y agoAgreed. I think everyone will move to canvas eventually (for the whole site), and get over it.
- Lerc 13y agoCSS has two faces, the awesome "You can make amazing things with ease" face and the "It won't do what I want unless I create some cludgy arrangement of elements in order to attach a combination of weird properties" Those two faces are because of two simple facts. 1. CSS is a really good way to set properties. 2. The properties are shit. The part of the properties that is most gloriously broken for layout is that the X and Y dimension behave differently. That's _really_ not what a lot of websites want. I experimented a while back and came up with a model for placement like this http://www.fingswotidun.com/gunk/boxtest.swf http://www.fingswotidun.com/gunk/boxtest.swf If you had properties like these you could center, proportionally center, left/right/top/bottom align and handle growing in the direction the user wants. What CSS needs is a new generation with all new properties, a full modal change so that it doesn't have to be backwards compatible and interact with archaic properties.
- mietek 13y agoHave you tried flexbox? http://html5please.com/#flexbox http://html5please.com/#flexbox
- Lerc 13y agoI have had a look at it. It doesn't fill me with confidence. It looks like it will make some things possible that previously required wrapper elements, but it doesn't seem to be very easy to imagine scenarios. It feels like it is a solution directed at specific problems, whereas I always felt that was much of the problem with CSS properties. They solved their target problem and if your problem was different to their one you might be out of luck. If the properties were defined to be more general I think a lot of those problems would never have appeared.
- bergie 13y agoWith the GSS constraint solver stuff like this becomes very easy... #div[center-x] == #container[center-x]; (note that this is fully source order independent. It doesn't matter where in DOM the elements #div and #container are)
- benihana 13y agoAnytime someone says CSS is well designed, ask them how to vertically center something and look at their face as they go through the five stages of grief. I'm pretty good at CSS I suppose, and it's not something you can learn logically and progressively. You have to learn all the weird nuances and quirks of it as well as the syntax. EDIT: speaking of CSS, if you want to know true pain and nothingness, try making responsive emails that look good at three sizes on several clients in CSS.
- etler 13y agoNot too long ago I saw the best simple solution to vertical centering. It was so obvious: position: absolute; top: 50%; transform: translateY(-50%); Translate is processed at the end, meaning it is based on the final element height. This means it works with any element, even dynamic heights. Of course it only works on relatively new browsers, but translate is well accepted and on the path to being ubiquitous. The style is also easy to understand and isn't hacky. I think the parts of CSS that are designed are well designed (considering how hard the problem space is). However, lots of CSS just wasn't designed at all. It was just everyone throwing in their conflicting specs and implementation without any coordinating. But that was pretty much all of the web. All things considered, I think we're not too bad off considering how crazy things were in the early days. Also, any time anyone complains about CSS, I just show them some pre-css code with `<font>` tags and `bg-color` properties and insane tables. That gets them to shut up pretty quickly. :)
- coldtea 13y ago>Also, any time anyone complains about CSS, I just show them some pre-css code with `<font>` tags and `bg-color` properties and insane tables. That gets them to shut up pretty quickly. :) Nothing is insane about "tables". It behaves as a classical grid based layout system, even though it was not intended as such. That's why it was such a good fit. As for the "font" tags and bg-color properties, nothing strange about them either -- at least nothing worse than a style="" tag. Besides, that's the formatting part, which CSS is quite good add, we're talking layout here.
- 13y ago
- coldtea 13y agoCSS was created for styling. Bold, colors, borders, that stuff. It was not meant as a layout engine. Hence the name, of course. Floats were meant for other stuff (placing pictures in the flow of text and such). They are just as much as an abuse as using tables was for layout. But tables, at least, had one thing going for them: they are an almost feature full layout system for most needs, with sane defaults, and familiar (from all grid layout systems) behavior. So both CSS and tables were abused for layout, but at least with tables they picked something appropriate to abuse. Now, finally, CSS has some extra functionality (flexbox, multi-column etc) that was added later as specifically intended for layout. Which, of course, is not fully supported and implemented yet. Btw, the whole idea of "semantic html" is another cargo-cult -- people conflating the webpage (a presentational output) with some intermediate format, forgetting that REST, JSON, DBs et all are far more appropriate for that repurposing of content. Basically a bunch of designers who knew nothing about computer science, taxonomies, RDF, etc, thought it would be very sophisticated to adopt this "semantic" word. So, they started building pages caring about the naming of elements and such, as if webpages are meant for screen-scrapping (some hand waving always included support of "reading devices", who nobody of course ever tried to actually test with his pages -- they'd found out they're specially adapted to work with regular noise webpages).
- stdbrouw 13y agoYou seem to be very upset about all of this... The nice thing about semantic HTML is that any metadata and structured data is a part of the document itself instead of hidden away somewhere else through an API. This has its disadvantages (I'd rather interact with an API than scrape a website, even if it's all tidy and semantic) but it absolutely also has advantages. The usual alternative to semantic HTML is not a well-designed API, the alternative tends to be nothing.
- etler 13y agoI write my HTML semantically primarily for myself. It's far easier to read, debug, and develop when the structure makes sense and isn't littered with a dozen nested divs with incredibly complicated class names.
- pjmlp 13y agoThis is why, even though I have been doing on and off web development since 2000, I jump of joy when allowed to do native UI coding instead.
- robmcm 13y agoMost native frameworks provide tools to position elements, although at the end of the day they are just positioning things on an x,y,z axis. Using CSS and JS you can do the same thing, respond to events and re-position your elements. The problem is, people get preachy about how you shouldn't use JS for this, because websites should be progressive and degrade gracefully. Native code doesn't have this problem as much as it's not only accepted, but viewed as cool when you only support the latest OS version (iOS7).
- pjmlp 13y agoMy main problem is not only layout, but the whole design stack. I do like UI programming, but like many of us I am a bit lacking in the design area. Native toolkits allow me to ramp up pretty quickly UI thanks to components and layout managers. When something is missing I can drop down to pure graphics programming. With web designs, it doesn't matter how much I try, without the help of a designer guy on the team, I always end up hacking around until something kind of works, repeating it ad nauseum for all required platforms.