Jonathan Ballet
Jonathan Ballet

Reputation: 1003

How to vertically align parts of HTML list item?

I have the following HTML (which I control):

<ul>
    <li>
        <a href="mailto:[email protected]">email and co: [email protected]</a>
    </li>
    <li>
        <a href="xmpp:[email protected]">XMPP: [email protected]</a>
    </li>
</ul>

This renders like this (I exaggerated the "email & co" text for the problem to be more visible):

* email and co: [email protected]
* XMPP: [email protected]

I would like to render this in a slightly prettier way by aligning both email and XMPP address, more or less like this:

* email and co: [email protected]
* XMPP:         [email protected]

(or something similar).

I tried to play with display: table and friends with no luck: it seems the <a> tag blows everything up :(

This is my current Fiddle: http://jsfiddle.net/xL314Lkt/ with a non-working solution. If I remove the <a> tag, it seems to be OK, but I really need it, and I'm looking for a solution (if possible) which:

Upvotes: 0

Views: 86

Answers (5)

Forss
Forss

Reputation: 778

If you are willing to use javascript you could do:

//retrieve all elements you wish to match width
var labelElement = document.getElementsByClassName("label");
var maxWidth = 0;
var elementWidth;

//Loop through the elements to get the maximum width
for (var i = 0; i < labelElement.length; ++i) {
    elementWidth = parseInt(labelElement[i].offsetWidth);
    maxWidth = elementWidth > maxWidth ? elementWidth : maxWidth;
}

//Set the new width for all elements, +1 to avoid linebreak
for (var i = 0; i < labelElement.length; ++i) {
    labelElement[i].style.width = "" + (maxWidth+1) + "px";
}

JSFiddle

Upvotes: 0

tjati
tjati

Reputation: 6079

You should use definition list, this is semantically more correct:

<dl>
    <dt>email and co</dt>
    <dd>[email protected]</dd>
    <dt>XMPP</dt>
    <dd>[email protected]</dd>
</dl>

CSS could look like:

dt
{
    float:left;
    clear:both;
    min-width:38em;
}
dd {
    float:left;
}

Upvotes: 1

im_benton
im_benton

Reputation: 2621

Your best bet will be to just add a min-width.

<ul>
    <li>
        <a href="mailto:[email protected]">
            <span class="label">email and co:</span>
            <span class="value">[email protected]</span>
        </a>
    </li>
    <li>
        <a href="xmpp:[email protected]">
            <span class="label">XMPP:</span>
            <span class="value">[email protected]</span>
        </a>
    </li>
</ul>

CSS

ul {
    display: table;
}

li {
    display: table-row;
}

span.label,
span.value {
    display: table-cell;
}

.label {
    min-width: 100px;
}

Upvotes: 1

PlantTheIdea
PlantTheIdea

Reputation: 16359

If you are willing to make the label a fixed width:

<ul>
    <li>
        <a href="mailto:[email protected]">
            <label class="listLabel">email and co:</label>
            <span class="listSpan">[email protected]
        </a>
    </li>
    <li>
        <a href="xmpp:[email protected]">
            <label class="listLabel">XMPP:</label>
            <span class="listSpan">[email protected]</span>
        </a>
    </li>
</ul>

Then set your CSS:

.listLabel,
.listSpan {
    display:inline-block;
    vertical-align:top;
}

.listLabel {
    width:100px;
}

This should give the appearance you're looking for.

Upvotes: 1

Shawn Erquhart
Shawn Erquhart

Reputation: 1858

You need to change the anchor tag's display property to block:

Codepen: http://codepen.io/erquhart/pen/wJcCK

<ul>
    <li>
      <a href="mailto:[email protected]">email and co: <span>[email protected]</span></a>
    </li>
    <li>
      <a href="xmpp:[email protected]">XMPP: <span>[email protected]</span></a>
    </li>
</ul>

a {
  display: block;
  width: 300px;
}

span {
  float: right;
}

Upvotes: 1

Related Questions