Nic J
Nic J

Reputation: 43

How can I use CSS dot leaders for a "three column" list?

I'm struggling trying to get one portion of a restaurant menu to display properly on a website. I'm using dot leaders that work perfect in a "two-column" list: e.g.

ITEM.......................PRICE
LONGER NAMED ITEM..........PRICE

Using the following:

CSS:

p.menu-item {
    overflow: hidden;
}

span.item {
    float: left;
    padding: 0 .2em 0 0;
    margin: 0;
}

span.price {
    float: right;
    padding: 0 0 0 .2em;
    margin: 0;
}

p.menu-item:after {
    content: "";
    display: block;
    overflow: hidden;
    height: 1em;
    border-bottom: 1px dotted;
}

HTML:

<p class="menu-item"><span class="item">ITEM</span><span class="price">PRICE</span></p>
<p class="menu-item"><span class="item">LONGER NAMED ITEM</span><span class="price">PRICE</span></p>

But I have a few sets of items that have two prices.

e.g.

item..........price 1.....price2
item2.........price 1.....price2

I can only find the help online that let me do the single priced items. I played around with http://jsfiddle.net/vkDgJ/17/ but just can't seem to get it.

Any suggestions?

EDIT: I should also mention that my page has a textured background image so using a background color to hide the dotted border as in Coma's answer below didn't work.

Upvotes: 4

Views: 4932

Answers (2)

Jason Cramer
Jason Cramer

Reputation: 182

Thanks to coma for his awesome work on the "Complex" example he gave. His answer should remain the selected one. I would have simply added this in reply, but I don't have that option.

I've improved his Complex example to allow for a few things.

Firstly, if you have your menu inside a smaller space, you will notice coma's code will start to have some flaws. The divs are set to 50%, therefore if the name of your items goes past that, then you end up with it being cut off (it actually wraps, and is hidden which is intentional). I fixed this with an 80/20 measurement. This is more or less only an issue if you want to take away one of the two prices. What if you want just one? (Granted, I hadn't looked at coma's other examples..maybe he did a better version than mine).

Also, I needed for my work to have a description line in some items...so I added that in there as well.

Small tweaks all around. Sorry if I've convoluted it further...but I couldn't actually fully understand why his example works anyway (well.. I understand 90%).

If it's appreciated, then just add a comment. Coma deserves the win on this one.

You can find my code here: http://jsfiddle.net/tFK68/1/

CSS:

html {
    background: #F4EAEC url(http://colourlovers.com.s3.amazonaws.com/images/patterns/3949/3949452.png?1382901481) 0 0 fixed;
color: black;
}

ul.dotted li, ul.dotted div {
height: inherit;
list-style-type: none;
}

ul.dotted > li div {
float: left;
width: 50%;
position: relative;
overflow: hidden;
}

.dotted div.item {
width: 80%;
}

.dotted div.prices {
width: 20%;
overflow: visible;
}

.dotted div.item, .dotted div.prices {
height: 1em;
font-weight: bold;
}

ul.dotted span {
display: block;
position: absolute;
padding: 0 5px;
}

ul.dotted span:after {
content: "";
display: block;
position: absolute;
top: 67%;
width: 1000px;
border-top: 2px dotted #000;
}

div.item span {
left: 0;
}

div.item span:after {
left: 100%;
}

div.prices span {
right: 0;
width: 80%;
}

div.prices div {
width: 100% !important;
}

div.prices span:after {
right: 100%;
}

ul.dotted p {
padding: 0px 10px 0px;
clear: both;
margin-bottom: 0;
}

Here's the markup:

<ul class="dotted">
<li>
    <div class="item">
        <span>Miso Soup</span>
     </div>
    <div class="prices">
        <div>
            <span>$1.50</span>
         </div>
     </div>
    <p>Descriptive lorem ipsum dolor.</p>
</li>
</ul>

Upvotes: 0

coma
coma

Reputation: 16649

The second idea (http://jsfiddle.net/coma/wrwwn/2/) is better because the dots won't overlap between columns.

HTML

<div class="dotted">
    <div>
        <div>
            <span>item</span>
         </div>
        <div>
            <span>price 1</span>
         </div>
        <div>
            <span>price2</span>
         </div>
     </div>
    <div>
        <div>
            <span>item2</span>
         </div>
        <div>
            <span>price 1</span>
         </div>
        <div>
            <span>price2</span>
         </div>
     </div>
</div>

CSS

div.dotted > div:after {
    content: "";
    display: block;
    clear: both;
}

div.dotted > div > div {
    position: relative;
    float: left;
    width: 33.333333%;
}

div.dotted > div > div:before {
    content: "";
    display: block;
    position: absolute;
    top: 50%;
    right: 0;
    bottom: 0;
    left: 0;
    border-top: 1px dotted #000;
    z-index: -1;
}

div.dotted > div > div:last-child:before {
    display: none;
}

div.dotted > div > div > span {
    padding: 0 5px;
    display: inline-block;
    background-color: #fff;
}

http://jsfiddle.net/coma/wrwwn/

Prices aligned to the right

div.dotted > div {
    position: relative;
}

div.dotted > div:before {
    content: "";
    display: block;
    position: absolute;
    top: 50%;
    right: 0;
    bottom: 0;
    left: 0;
    border-top: 1px dotted #000;
    z-index: -1;
}

div.dotted > div:after {
    content: "";
    display: block;
    clear: both;
}

div.dotted > div > div {
    float: left;
    width: 33.333333%;
}

div.dotted > div > div + div {
    text-align: right;
}

div.dotted > div > div > span {
    padding: 0 5px;
    display: inline-block;
    background-color: #fff;
}

http://jsfiddle.net/coma/wrwwn/2/

Using a fixed background

div.dotted > div > div > span,
html {
    background: #F4EAEC url(http://colourlovers.com.s3.amazonaws.com/images/patterns/3949/3949452.png?1382901481) 0 0 fixed;
}

http://jsfiddle.net/coma/wrwwn/4/

Complex

html {
    background: #F4EAEC url(http://colourlovers.com.s3.amazonaws.com/images/patterns/3949/3949452.png?1382901481) 0 0 fixed;
}

div.dotted div {
    height: 2em;
}

div.dotted > div div {
    float: left;
    width: 50%;
    position: relative;
    overflow: hidden;
}

div.dotted span {
    display: block;
    position: absolute;
    padding: 0 5px;
}

div.dotted span:after {
    content: "";
    display: block;
    position: absolute;
    top: 50%;
    width: 1000px;
    border-top: 1px dotted #000;
}

div.item span {
    left: 0;
}

div.item span:after {
    left: 100%;
}

div.prices span {
    right: 0;
}

div.prices span:after {
    right: 100%;
}

http://jsfiddle.net/coma/wrwwn/6/

Upvotes: 4

Related Questions