Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a <ul> to be evenly spaced across the content area it exists in?

I have a List that I'm using as a list of tabs:

<div id="SearchForm">
    <ul class="TabControl">
        <li>
            <a href="/search/Funds">Funds (60)</a>
        </li>
        <li>
            <a href="/search/Companies">Companies (4)</a>
        </li>
        <li>
            <a href="/search/Groups">Groups (1)</a>
        </li>
        <li>
            <a href="/search/Categories">Categories (0)</a>
        </li>
        <li>
            <a href="/search/Categories">Identifiers (60)</a>
        </li>
    </ul>
</div>

where the CSS is defined as follows:

#SearchForm ul {
    list-style: none;
    padding:0;
    margin: 15px 0 5px 0;
}

#SearchForm li {
    display: inline;
    background-color: #F6E9D8;
    margin: 12px 6px 0 0;
    padding: 6px 0 6px 0;   
}

#SearchForm li a {
    padding: 0 20px;
}

This list only takes up about 90% of the width available to it on the page, where everything else in the page takes up 100% of the width because they're laid out in divs. The space avaiable to them is defined in an element supplied by the client at

 width: 62.1em

Basically what I need is to get the tabs to be distributed evenly so they fill the entire width available to them, with the text/link aligned in the middle of each tab? Is there a way for me to do this?

like image 424
DaveDev Avatar asked Aug 17 '10 16:08

DaveDev


People also ask

How do you put a space between Li?

To create space between list bullets and text in HTML, use CSS padding property. Left padding padding-left is to be added to <ul> tag list item i.e. <li> tag. Through this, padding gets added, which will create space between list bullets and text in HTML.

How do I make space even in CSS?

The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.

How do you style the UL list to one line?

The quickest way to display a list on a single line is to give the <li> elements a display property value of inline or inline-block . Doing so places all the <li> elements within a single line, with a single space between each list item.


1 Answers

But of course. I've put together a demo here. The CSS is as follows with explanation as comments:

#SearchForm {
    /* Set width of parent div */
    width: 62.1em;   
}

#SearchForm ul {
    /* Make ul take 100% of its parent's width */
    width: 100%;

    list-style: none;
    padding: 0;
    margin: 15px 0 5px 0;
}

#SearchForm li {
    /* Float the li's left and give them 20% width (20% * 5 = 100%) */
    float: left;
    width: 20%;

    /* Make sure no horizontal padding/margin is applied.  Since we've set an 
       explicit width on the li, horizontal padding/margins would add to this, 
       making the element greater than 20% width and causing float drop */        
    margin: 12px 0 0 0;
    padding: 6px 0;   
}

#SearchForm li a {
    /* Set the nested links to display block and align their text center */
    display: block;
    text-align: center;

    /* Here we can safely apply horizontal padding/margins because we haven't
       explicitly set a width on the element */
    margin: 0 6px 0 0;
    padding: 0 20px;

    background-color: #F6E9D8;
}
like image 127
Pat Avatar answered Sep 27 '22 16:09

Pat