views:

205

answers:

9

I'm wondering what options one has in xhtml 1.0 strict to create a line on both sides of text like-so:

Section one
----------------------- Next section -----------------------
Section two

I've thought of doing some fancy things like this:

<div style="float:left; width: 44%;"><hr/></div>
<div style="float:right; width: 44%;"><hr/></div>
Next section

Or alternatively, because the above has problems with alignment (both vertical and horizontal):

<table><tr>
<td style="width:47%"><hr/></td>
<td style="vertical-align:middle; text-align: center">Next section</td>
<td style="width:47%"><hr/></td>
</tr></table>

This also has alignment problems, which I solve with this mess:

<table><tr>
<td style="border-bottom: 1px solid gray; width: 47%">&nbsp;</td>
<td style="vertical-align:middle;text-align:center" rowspan="2">Next section</td>
<td style="border-bottom: 1px solid gray; width: 47%">&nbsp;</td>
</tr><tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr></table>

In addition to the alignment problems, both options feel 'fudgy', and I'd be much obliged if you happened to have seen this before and know of an elegant solution.

Thank you for reading.

Brian

+5  A: 

I used line-height:0 to create the effect in the header of my site guerilla-alumnus.com

<div class="description">
   <span>Text</span>
</div>

.description {
   border-top:1px dotted #AAAAAA;
}

.description span {
   background:white none repeat scroll 0 0;
   line-height:0;
   padding:0.1em 1.5em;
   position:relative;
}

Another good method is on http://robots.thoughtbot.com/

He uses a background image and floats to achieve a cool effect

willoller
@Willoller - I like this, it looks great on your site, but I couldn't get it to work (I suspect interference from jQuery css). :( But it is really cool.
Brian M. Hunt
+1  A: 

Try this:

CSS:

.divider {
    width:500px;
    text-align:center;
}

.divider hr {
    margin-left:auto;
    margin-right:auto;
    width:40%;

}

.left {
    float:left;
}

.right {
    float:right;
}

HTML:

<div class="divider">
<hr class="left"/>TEXT<hr class="right" />
</div>
Diodeus
A: 

You can create a wrapper block with some appropriate background image (and also set some background color for your centered text block).

scaryzet
+1  A: 

You can accomplish this without <hr />. Semantically, design should be done with the means of CSS, in this case it is quite possible.

div.header
{
  position: relative;
  text-align: center;
  padding: 0 10px;
  background: #ffffff;
}

div.line
{
  position: absolute;
  top: 50%;
  border-top: 1px dashed;
  z-index: -1;
}

<div class="header">
  Next section
  <div class="line">&nbsp;</div>
</div>
Developer Art
A: 

You could try doing a fieldset, and aligning the "legend" element (your "next section" text) to the middle of the field with only border-top set. I'm not sure about how a legend is positioned in accordance with the fieldset element. I imagine it might just be a simple margin: 0px auto to do the trick, though.

dclowd9901
+3  A: 

If you can use CSS and are willing to use the deprecated align attribute, a styled fieldset/legend will work:

<style type="text/css">
fieldset { 
    border-width: 1px 0 0 0;
}
</style>

<fieldset>
<legend align="center">First Section</legend>
Section 1 Stuff
</fieldset>

<fieldset>
<legend align="center">Second Section</legend>
Section 2 Stuff
</fieldset>

The intended purpose of a fieldset is to logically group form fields. As willoler pointed out, a text-align: center style for will not work for legend elements. align="center" is deprecated HTML but it should center the text properly in all browsers.

Trey
A more detailed explanation of what I was going for.
dclowd9901
+1 semantic -1 doesn't actually center legend
willoller
I'm pretty sure `<legend>` takes on the display traits of a `block` or `inline-block` element, as it can be padded *and* margined, which means `text-align:center` shouldn't work...
dclowd9901
yeah legends are great for semantics - i use them for wrapping radio groups but they are notoriously stubborn
willoller
I corrected my answer. Unfortunately due to the stubbornness of certain browsers in styling the `legend` element, `align="center"` is the only solution I could find that reliably centers a `legend`.
Trey
+5  A: 

How about:

<div style="height: 2px; background-color: black; text-align: center">
  <span style="background-color: white; position: relative; top: -0.5em;">
    Section Title
  </span>
</div>

position: relative; top: -0.5em; is the key to this method. It lets you vertically center the text. Unfortunately this method requires you to know the background color of your site so that the text doesn't end up looking like it has a line through it.

As an alternative to using a background color to create the line, you could use border-top. With that you could bring in border effects like dotted lines.

Fletcher Moore
@Brian fixed the width problem.
Fletcher Moore
+2  A: 
<fieldset style="border:0px; border-top:1px solid black">
    <legend>Test</legend>
</fieldset>

Evil hack ...

Dänu
I wouldn't call it a hack, but I would test that it works in all browsers you intend to support.
NickLarsen
fieldset doesn't want to be used like that, it's been hacked into place ;-)
Dänu
+2  A: 
<div style="text-align: center; border-top: 1px solid black">
  <div style="display: inline-block; position: relative; top: -10px; background-color: white; padding: 0px 10px">text</div>
</div>
Aleksejs