Poetry is one of the most expressive and creative writing formats. It allows plenty of artistic freedom and creativity. Historically, layout and style have been of great importance in poetry. In poetry, line and stanza breaks are extremely important as apposed to prose where line breaks can be moved with very little impact upon the writing. When taking poetry to the web, I (and others) feel that this styling and layout needs to be preserved. There are a number of ways to do this using XHTML and CSS. We will use XHTMLto create the semantic structure of our poem and CSS so it can keep up with the latest style. Just like in WordPress, let’s make sure that our code is poetry.
First off, we need to semantically structure the XHTML source of our poetry. This could be achieved using <p>, <ol>, or <pre>. All of these methods have their advantages which we can see. However, some are more semantic than others.
Paragraphs
You could structure each stanza into a <p> tag and add line breaks by using a <br /> tag. While this looks fine, it isn’t really entirely semantic since lines don’t really carry enough importance. You are really only structuring the line breaks, not lines, in this approach. Also, screen readers generally do not place enough emphasis on a line break created by <br /> so entire stanzas may be read as one sentence. Still, this is probably the easiest approach and would look something like this:
- <div class=“poetry”>
- CSS and XHTML intertwine,<br />
- to create a semantic web,<br />
- of unsurpassed beauty and usability.<br />
- Hail the semantic web!
- </p>
- With a great roar,<br />
- the web exploded<br />
- paving roads and connecting people<br />
- But it destroyed many houses and left behind poetry
- </p>
- <p class=“poetry”>
- Hail the semantic web!<br />
- But do not be controlled by it
- </p>
- </div>
Listed
In some ways, poetry could be though of as an ordered list of lines. Using a list emphasizes the importance of each line greatly. However, poetry isn’t really a list - this is not the semantically correct way to do it. However, if you are really set on the importance of each line you might want to put the poem in an ordered list. This method might be useful if you were teaching an English class and wanted to easily reference each line. If you do want to reference the lines easily you might do it like this:
- <ol class=“poetry”>
- <li>CSS and XHTML intertwine,</li>
- <li>to create a semantic web,</li>
- <li>of unsurpassed beauty and usability.</li>
- <li>Hail the semantic web!</li>
- </p>
- <li>With a great roar,</li>
- <li>the web exploded</li>
- <li>paving roads and connecting people</li>
- <li>But it destroyed many houses and left behind poetry</li>
- </p>
- <p class=“important”>
- <li>Hail the semantic web!</li>
- <li>But do not be controlled by it</li>
- </p>
- </ol>
Formats Away!
Code is poetry
-#. And since code is made with <pre><code> it makes sense that poetry should use <pre> too. To create the easiest and most semantic solution I think it would be best to enclose each stanza in a <pre> tag. The <pre> tag designates preformatted text. A poem certainly falls into this category since we are preformatting the line breaks and stanzas rather than leaving it up to the user agent to decide how it should be formatted. Most screen readers will interpret line breaks enclosed within <pre> correctly by pausing and making it clear that each line is separate. Besides being the best way to do it semantically, a <pre> also makes it the easiest to maintain and change poetry - there are no messy <br /> tags to move. Using <pre> we can create a semantic solution:
- <div class=“poetry”>
- CSS and XHTML intertwine,
- to create a semantic web,
- of unsurpassed beauty and usability.
- Hail the semantic web!
- </pre>
- With a great roar,
- the web exploded
- paving roads and connecting people
- But it destroyed many houses and left behind poetry
- </pre>
- <pre class=“important”>
- Hail the semantic web!
- But do not be controlled by it
- </pre>
- </div>
In the future, we could use the line element to provide greater control over each line and add another level of importance to the layout of poetry. However, this will not be supported until XHTML 2.0.
The Latest Style
Bad writing lacks style and voice. For poetry, you’ll have to provide the voice but we can add some style to it. Now that we have our semantic XHTML, we turn to CSS to make our poetry look great. To start with, we add a style to the containing div. In order for the poem to appear separated and have a slight shadow you might use something like this:
- div.poetry {
- background:#FFF6BF;
- color:#333333;
- border-top:1px solid #555555;
- border-left:1px solid #555555;
- border-bottom:3px solid #333333;
- border-right:3px solid #333333;
- padding:10px 20px;
- }
Once the container is styled, we can make the stanzas appear the way we want. This is where we specify a font since many browsers will set a special font for the
<pre> tag. We also ensure that white space is interpreted correctly. We should add this style to our <pre> tags.
- div.poetry pre {
- white-space:pre;
- font-family:“Comic Sans MS”, comic sans, “brush Script MT”, brush script, “Zapfino”, “Marker Felt”, cursive;
- margin:0px 0px 10px 0px;
- }
Things should be looking pretty rosy now. You may want to add some additional fanciness by styling the first letter or line of each stanza or placing special emphasis on a stanza. With all our sweet styles added on we will end up with this beautiful poem.
- div.poetry pre:first-line {
- color:#222222;
- }
- div.poetry pre:first-letter {
- font-weight:bold;
- font-size:120%;
- }
- div.poetry pre.important {
- font-size:105%;
- }
By using <pre> tags to semantically structure our stanzas and using CSS to make it look pretty we have ensured that none of the authenticity of our poetry will be lost when it is translated to code. This is the way that I believe poetry should be created although other methods certainly have valid arguments. If you have another idea or think that poetry should be created using some other method, please speak up. Remember, just as code is poetry, your poetry must act like code.

















Hey there…none of the links in this tutorial seem to work. Do you have the pages indexed elsewhere?
Sorry, I lost the pages when I moved my site for server to server… I’ll try to get some new ones up tonight. In the meantime, I use this technique in my writing section.
Hello, I enjoyed reading your article because I too am interested in marking up poetry (song lyrics) (and in addition, accompanying them with their translations).
First of all, it must be said that not all poetry is the same. For those poems that place a lot of meaning into their presentation, e.g. poems whose presentation on a page creates a certain shape, I think that the pre solution is the best.
However, many poems/lyrics are composed of stanzas, each stanza being made up of a number of lines. Most of the time, if the lines in a stanza are strung up together, they make up a single sentence. In this case, I find using the use of br-tags to separate the lines semantically correct. I believe that what is important is that if the page is read unstyled (just basic HTML, no CSS), it is loyal to the printed poem, even if it doesn’t look so nice. What you say about the br-tag not placing emphasis on the lines is true, but it can be resolved by further enclosing the lines in span-tags of class ‘line’, in addition to using the line-breaks.
Thanks!
@dwardu: I agree that not all poems/songs are the same. It really comes down to what kind of poem/song it is: where the format is the most important thing, pre tags are best. Meanwhile, where the lines are meant to form a single sentence I agree that a br tag is best. Either way, it should look fine when unstyled.
Hopefully, the future should bring even more options with the introduction of the line element in XHTML 2.0