简体   繁体   中英

evenly distribute li items

hope this is self explanatory:

HTML:

<ul class="steps">
<li class="step1 first">
    <div class="icon basket"></div>
    1.Warenkorb
</li>
<li class="step2">
    <div class="icon registration"></div>
    2.Adresse
</li>
<li class="step3">
    <div class="icon payment"></div>
    3.Zahlungsart
</li>
<li class="step4">
    <div class="icon order"></div>
    4.Bestätigen
</li>
<li class="step5 last">
    <div class="icon thankyou last"></div>
    5.Danke
</li>
<div style="clear:both"></div>

CSS:

.steps {
width:100%;
list-style-type: none;
padding:0;
margin:0 auto;
background:url(http://tnsdev.cloudapp.net/dev/steps_slice.png) repeat-x;
 }

 .steps li {
width:20%;
float:left;

}

.steps li .icon {
background:url(http://tnsdev.cloudapp.net/dev/steps_icon.png) no-repeat;
height:44px;
width:44px;
}

http://jsfiddle.net/HYYwn/1/

how can i achieve that the distances between the bubbles are all the same and the bubble of step5 is on the far right? i am constraint to have 5 different li's and to use % so it stays responsive.

can't get around this myself at the moment playing with it for a while already!

EDIT:

the result should look like this

 O--O--O--O--O 

and not like

--O--O--O--O--O  

or

O--O--O--O--O--  

or

--O--O--O--O--O--

Here is one way of doing it using text-align: justify .

The advantage of this approach is that the circle/bubble motifs are evenly spaced and you can also control the justification of the labels beneath then.

You first need to wrap the labels in a container, I used a <p> tag, and add a terminating <li> element, equivalent to the clearing element.

<ul class="steps">
    <li class="step1 first">
        <div class="icon basket"></div>
        <p>1.Warenkorb</p>
    </li>
    <li class="step2">
        <div class="icon registration"></div>
        <p>2.Adresse</p>
    </li>
    <li class="step3">
        <div class="icon payment"></div>
        <p>3.Zahlungsart</p>
    </li>
    <li class="step4">
        <div class="icon order"></div>
        <p>4.Bestätigen</p>
    </li>
    <li class="step5 last">
        <div class="icon thankyou last"></div>
        <p>5.Danke</p>
    </li>
    <li class="filler"></li>
</ul>

For the CSS :

.steps {
    width:100%;
    list-style-type: none;
    padding:0;
    margin:0 auto;
    background:url(http://tnsdev.cloudapp.net/dev/steps_slice.png) repeat-x;
    text-align: justify;
    line-height: 0;
}
.steps li {
    width: auto;
    display: inline-block;
    margin: 0;
    padding: 0;
    line-height: 1.5;
    position: relative;
    text-align: center;
}
.steps li .icon {
    background:url(http://tnsdev.cloudapp.net/dev/steps_icon.png) top center no-repeat;
    height:44px;
    width:44px;
}
.steps li p {
    position: absolute;
    width: 100px;
    top: 50px;
    left: -22px;
    margin: 0;
}
.steps li.first p {
    text-align: left;
    left: 0;
}
.steps li.last p {
    text-align: right;
    left: auto;
    right: 0;
}
.steps li.filler {
    width: 100%;
    height: 0;
    font-size: 0;
    vertical-align: top;
}

See demo at jsFiddle

First, I used text-align: justify on the parent container to evenly distribute the li elements which are inline-blocks that shrink to fit the square .icon elements.

The .filler line forces a new 100% width line that allows the text-justify to work. I set the vertical-align: top (and line-height: 0 in the parent) to get rid of a orphan with space that is created by the filler element.

I then take the labels out of the flow using absolute positioning, and the adjust the text-alignment for the first and last list items and position them using a negative margin to center the labels.

The one limitation is that there the width is specified for the labels, so you should add a min-width to the parent container as you see fit.

You have plenty of room here to adjust things as needed.

See this Fiddle

I'm using the calc function for the width of the 4 first li s.

this is working like this.

the 4 first will look like O------ and the 5'th will look like O .

width: calc((100% - 44px)/4);

Explanation: the fifth li takes exactly 44px, so the 4 first li s divide the rest between them equally.

CSS:

ul{
    text-align: justify;
}

ul::after {
    content: '';
    width: 100%; 
    display: inline-block;
}

li{
    display:inline-block;
}

Try:

.steps {
width:100%;
list-style-type: none;
padding:0;
margin:0 auto;
background:url(http://tnsdev.cloudapp.net/dev/steps_slice.png) repeat-x;
 }

 .steps li {
width:20%;
float:left;
left: 200px;
right: 200px;
}

.steps li .icon {
background:url(http://tnsdev.cloudapp.net/dev/steps_icon.png) no-repeat;
height:44px;
width:44px;
}

http://jsfiddle.net/HYYwn/2/

I loved this question! Real brain teaser...

Here's what I've got: http://jsfiddle.net/HYYwn/10/

I've simplified the HTML and swapped your icon <div /> for <img /> s because these are easier to maintain the square ratio that's required. Can be done with a <div /> but we need to use a bit of JS to make it responsive.

[Simplified] HTML

<div class="steps">
    <ul>
        <li>
            <img  src="http://tnsdev.cloudapp.net/dev/steps_icon.png" />
        </li>
        <li>
            <img  src="http://tnsdev.cloudapp.net/dev/steps_icon.png" />
        </li>
        <li>
            <img  src="http://tnsdev.cloudapp.net/dev/steps_icon.png" />
        </li>
        <li>
            <img  src="http://tnsdev.cloudapp.net/dev/steps_icon.png" />
        </li>
        <li>
            <img  src="http://tnsdev.cloudapp.net/dev/steps_icon.png" />
        </li>
    </ul>
</div>

CSS

* {
    margin: 0;
    padding: 0;
    border: 0;
}
.steps * {
    position: relative;
}
.steps {
    width: 100%;
    overflow: hidden;
    background-image: url(http://tnsdev.cloudapp.net/dev/steps_slice.png);
    background-repeat: repeat-x;
    background-position: 50%;
}
.steps ul {
    width: 119%;
    list-style-type: none;
}
.steps li {
    width: 20%;   /* 1/5 of ul.width */
    float: left;
}
.steps img {
    width: 20%;   /* 1/5 of li.width */
    height: auto; /* Always "square" */
}

Note: there's a magic number being used here... 119% . I can't tell you why that works, but it does. Hopefully someone smarter than me can explain the algebra behind that value (note that if we change the <img /> width then we must change this value).

I have achieved this with :before and it works quite nicely, the only thing that you might be worried about is last-child but you can add a .last-child class to the last item that will make it cross browser.

Demo: http://jsfiddle.net/F2Kh6/

CSS

li{
    float: left;
    list-style: none;
    width: 20%;
    position: relative;
}

li .icon{
    background: url('http://tnsdev.cloudapp.net/dev/steps_icon.png') no-repeat;
    width: 44px;
    height: 44px;
}

li:last-child:before{
    display: none;
}

li:before{
    content: '';
    position: absolute;
    top:0; left:0; right:0; bottom:0;
    background: url('http://tnsdev.cloudapp.net/dev/steps_slice.png') repeat-x;
    z-index: -1;
}

So we just but the steps slice in the :before and hide the :before on the :last-child

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM