Forum Moderators: not2easy

Message Too Old, No Replies

align text left and right on same line item

         

greencode

7:42 pm on Jun 4, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



I have a list which contains an item and then a price but I'd like the item aligned left and the price aligned right on the same li - is that possible?

CSS_Kidd

8:09 pm on Jun 4, 2009 (gmt 0)

10+ Year Member



Leave the li the way it is and put a span around the price.

<li>Hot Dogs<span class="price">$2.50</span></li>

CSS for li and span

.price {width:100px; text-align:right;}/*set width to what fits for you*/
li {width:400px}/*set width to what fits for you*/

Just remember to make the width of the li longer than the length of your longest list item and add that extra amount for the .price width. You really don't even have to add any width to the .price class if you don't want.

This way you will have your list perfectly aligned left while all your prices are perfectly aligned on the right.

Hope that makes sense.

greencode

8:33 pm on Jun 4, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Umm, I've done this but it doesn't seem to align right. It just sits next to each other:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<title>test</title>
<style type="text/css">

.item ul {
margin: 10px;
padding: 0;
list-style-type: none;
}

.item ul li
{
width: 500px;
padding-top: 5px;
padding-bottom: 5px;
}

.price {width:100px; text-align:right;}

</style>
</head>
<body>
<div class="item">
<ul>
<li>line Item 1<span class="price">Price 1</span></li>
<li>line Item 2<span class="price">Price 2</span></li>
</ul>
</div>
</body>
</html>

londrum

8:41 pm on Jun 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i'd probably use a <dl> list for this

<dl>

<dt>Item name</dt> <dd>Price</dd>

<dt>Item name</dt> <dd>Price</dd>

float the <dt> and <dd> to the left, give them a width, and do text-align:right on the <dd>

greencode

8:53 pm on Jun 4, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for this. I actually removed the float left from the dd tag because it didn't seem to work... This does though:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
<head>
<title>test</title>
<style type="text/css">

.item dl {
width: 200px;
margin: 10px;
list-style-type: none;
}

.item dl dt
{
float:left;
width: 150px;
}

.item dl dd
{
text-align:right;
}

</style>
</head>
<body>
<div class="item">
<dl>
<dt>line Item 1</dt><dd>Price 1</dd>
<dt>Another line item</dt><dd>Price 2</dd>
</dl>
</div>
</body>
</html>

bedlam

8:55 pm on Jun 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Umm, I've done this but it doesn't seem to align right. It just sits next to each other:

That's because <span> is an inline element, but width [w3.org] "...specifies the content width of boxes generated by block-level and replaced elements." In other words, inline elements such as <span> are unaffected by width declarations unless they are transformed into block level elements by setting their display property to "block" (or by floating them).

-- b

[edited by: swa66 at 2:08 pm (utc) on June 5, 2009]
[edit reason] editing out personal remark [/edit]

CSS_Kidd

1:04 pm on Jun 5, 2009 (gmt 0)

10+ Year Member



Well ... DISPLAY: BLOCK; then...

I use my method all the time.. I have no issues with it.

[edited by: swa66 at 2:08 pm (utc) on June 5, 2009]
[edit reason] editing out personal reply [/edit]

terrybarnes

1:16 pm on Jun 5, 2009 (gmt 0)

10+ Year Member



Thanks for everyone's help with this... Come on let's all be friends - everyone codes differently ;-)

j33dow

2:24 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



Hi, I'm a newbie to CSS... I also have been trying to align text both left and right for a menu. It works great on Safari and IE 8.0 but on FF and IE7 I get an extra line feed on the end. I have reduced a file to the below just to show the problem. ANY IDEAS? HELP!

John from Boston

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>menu</title>
<style type="text/css">
<!--
body {position: absolute; margin: 0; padding: 0;}
p {
font: bold 14px arial, sans-serif;border:1px solid red;
}
</style>
</head>
<body>
<p>Menu Item 1 ……………………… <span style="float:right;">$6.76</span><br>
Menu Item 1 ………………………… <span style="float:right;">$5.55</span><br>
</p>
</body>
</html>

jameshopkins

2:41 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



@j33dow, the inconsistency you're seeing is due to a spec violation in Firefox and IE7 - Safari and IE8 are exhibiting the correct behaviour, in this instance. The issue relates to the relationship between floated elements and preceding anonymous block boxes (https://bugzilla.mozilla.org/show_bug.cgi?id=50630). I know for a fact that this bug has been fixed in FF3.6, although I can't remember the exact revision it was fixed - it may have been earlier than 3.6

To workaround this issue, I suggest that you wrap another span around the text preceding the current floated span, and float it left, and add 'clear:left'. Remember you must clear the floated children; you can do this by adding 'overflow:hidden' to the P element. I seem to vaguely remember that IE6 has a problem with clearing like this, so you may need to check this.

j33dow

2:50 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



Thanks jameshopkins. And I'm sorry to be a dope but can you explain exactly what you mean? Do you mean like this?

<p><span style="float:left;">Menu Item 1 ……………………… <span style="float:right;">$6.76</span></span><br>
<p><span style="float:left;">Menu Item 2 ……………………… <span style="float:right;">$7.76</span></span><br>
</p>

jameshopkins

3:00 pm on Jun 26, 2009 (gmt 0)

10+ Year Member



I mean like this:-

<p><span style="float:left;clear:left;">Menu Item 1 ………………………</span> <span style="float:right;">$6.76</span>
<span style="float:left;clear:left;">Menu Item 2 ………………………</span> <span style="float:right;">$7.76</span>
</p>

Having said that, you could argue that the format that you have laid your elements out in, looks rather like an unordered list, so I would be tempted to go with something like:-
-----------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>menu</title>
<style type="text/css">
<!--
body {margin: 0; padding: 0;}
ul{
border:1px solid red;
padding:0;
float:left /* Emulate the shrink-to-fit behaviour that you achieved by applying 'position:absolute' to BODY */
}
li {
font: bold 14px arial,
sans-serif;
overflow:hidden;
list-style:none;
}
</style>
</head>
<body>
<ul>
<li><span style="float:left;clear:left;">Menu Item 1 ………………………</span> <span

style="float:right;">$6.76</span></li>
<li><span style="float:left;clear:left;">Menu Item 2 ………………………</span> <span

style="float:right;">$7.76</span></li>
</ul>
</body>
</html>
--------------------------------

Note how I removed 'position:absolute' from the BODY, as this is a very bad idea to leave in.