Is it possible to use SVG images as CSS background?

XHTML/CSS

  • I want to believe in XHTML/CSS. I want my structure entirely separate from my layout. But I also think it might all be a big lie, and not because of browser difficulties. Please help clear my misunderstanding. Further explanation - your people call it "more" - is inside. I'm redesigning an old site as a personal project, so I have no restrictions. I figured I'd take the opportunity to make a truly pure XHTML/CSS creation, no table layout, etc. So I head over to http://www.csszengarden.com where I remembered a beautiful demonstration of what could be done with one HTML page and many different style sheets. Here's where they lost me. When I looked at the example HTML file, I saw this: <!-- These extra divs/spans may be used as catch-alls to add extra imagery. --> <!-- Add a background image to each and use width and height to control sizing, place with absolute positioning --> <!-- There's a rather nasty colour shift bug with transparent GIFs in Netscape 6/7 and Mozilla v1.0 up to v1.3 (in which it's fixed), so make sure to test your work in these browsers. --> <div id="extraDiv1"><span></span></div><div id="extraDiv2"><span></span></div><div id="extraDiv3"><span></span></div> <div id="extraDiv4"><span></span></div><div id="extraDiv5"><span></span></div><div id="extraDiv6"><span></span></div> Ok. They've stuck six extra divs because they may need them to add images in the future. Now, in the programming world, that's what we call a dirty hack. You can't just say "six oughtta do it" and move on. You need a way to incorporate as many images as a future design may require, or else you haven't really separated display from structure. What if my next design needs seven divs to get the job done? Whether that's unlikely or not is besides the point. I'd never declare a static array in code and write a program around "there probably won't be more than X elements...". You just can't do that. It's the definition of sloppy. Looking at it that way, the idea of a "pure" XHTML/CSS site seems like a farce, since honestly all it seems like CSS is doing is giving us control over how individual HTML tags are styled, NOT over how a page in general is layed out and designed. CSS Zen Garden does it by using all sorts of strange tricks with DIVs to get the design they want. But I don't see how that's much different than strange tricks with tables, aside from keeping different files. And again, ultimately, if the CSS file can't dictate page layout by itself, structure and style were never truly separated, because you've got style sitting in your structure in the form of those extra DIVs. I know a lot of very brilliant information architects and web gurus put this stuff together, so I figure I'm the one missing something major, not the other way around. Can someone walk me through this?

  • Answer:

    You've nailed it. It's just that css is a more useful dirty hack than html.

tirade at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

CSS is perfect. Browsers are imperfect. I disagree with the idea that adding in things like redundant DIVs to acheive effects across browsers is equivalent to a lazy hack in programming. A lazy hack, to me, is something you do because the right fix is relatively expensive. Extraneous markup is introduced into XHTML because different browsers make different mistakes but users shouldn't have to know about it.

yerfatma

And the literary moose really pushes the envelope for what is do-able with CSS right now. The problem isn't just what's in the realm of capability, though. Yes, you can use acrobatics to create pure CSS rounded rectangles. By the time you're done, in terms of time and markup and CSS that were necessary, you may as well have used a table and a few images to do that trick (with the exception that it gums up table semantics for that page, but like I said, that semanticity problem has as much to do with limited definitions as anything else). Don't get me wrong: I understand that proof of concept tricks are useful. I've tried a few myself. It's just that pushing the envelope is probably less than half of the equation -- the real questions are if the common case is easy/powerful.

weston

adamrice: What about selectors like "blockquote>p", "div#content *", or "h1+h2"?I mean, you can't say "this block should be adjacent to that block" ... the ability for which tables are still in use.You could do this using Z-indexes and absolutely-placed images.Yes, but then the images must be elements on the page. Some images are part of the content and are okay as elements. Others are purely presentational, like fancy borders or rounded corners. These should be in the presentation layer, but background-image: is usually not good enough.

Khalad

You can't specify that elements should be adjacent, or otherwise have their properties be dependant on one another.Really? What about selectors like "blockquote>p", "div#content *", or "h1+h2"? Those give you descendant and adjacent elements (although there is no reverse-adjacent, which could be handy. Simple calculations would be nice.For example, it would be nice to be able to add images all over the page (background-image: does not suffice)You could do this using Z-indexes and absolutely-placed images. And http://www.literarymoose.info/=/css.xhtml really pushes the envelope for what is do-able with CSS right now.

adamrice

I'll agree on #1, but I think #2 has been bandied about for CSS3. As for #3, doesn't the ill-supported "content:" property promise to do what you want? And CSS3 has much deeper border options, thankfully.

yerfatma

DrJohnEvans, I think that's the entire point, that it's a complete hack since you can't make really sophisticated designs without slashing and burning the markup. If CSS were more powerful, you wouldn't need to add lots of extraneous markup to create Zen-like designs. CSS has a few major deficiencies as I see it:You can't specify that elements should be adjacent, or otherwise have their properties be dependant on one another.You can't do simple arithmetic. Imagine if you could do something like:#left, #right { width: 200px; }#content { width: 100% - 400px; }.That would make multi-column layouts much easier, eliminating 90% of the cases where people rely on tables.You can't add presentational elements, leaving you with limited styling options. For example, it would be nice to be able to add images all over the page (background-image: does not suffice), or surround an existing element with multiple <div/>s to give it a multi-colored border.Any one of those would make CSS an order of magnitude more powerful. They could all be used to enable grid-based layouts without the use of tables. Personally, I would be happy with #2, and it would appear a straightforward addition to CSS.

Khalad

Just echoing what jragon said: the redundant <div>s in the Zen Garden XHTML file are pretty specific to the Zen Garden's design. It's not a hack; it's Dave Shea trying to give hundreds of designers as much freedom as possible when working with a single, static XHTML file. You'd never have this situation on a site in which the design team actually had access to the XHTML.

DrJohnEvans

To me, xhtml/css validation is shenannigans, but the philosophy is an important and userful one. In a nutshell: you don't know what browser, screen size, disability, or much of anything else about the user. So make websites that look nice under your ideal conditions, and usable under your least ideal conditions. It's very zen. It will never be exactly the way you want it, so it's best not to want it to be very exact.

4easypayments

Honestly, in most HTML/table based sites with a good CMS, the structure and presentation are already separatedThis was JWZ's complaint when he tried his hand at CSS. Here's the thing: it's separated at the back-end, but it is not separated as the rest of the web sees it. So in terms of, say, search engines extracting useful information, non-semantic markup puts them at a disadvantage. The way those pages live on the web is significant.

adamrice

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.