Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6624787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:41:44+00:00 2026-05-25T21:41:44+00:00

So I have a horizontal unordered list with a set width. Inside, I have

  • 0

So I have a horizontal unordered list with a set width. Inside, I have a dynamic number of list items. It could be 2 items, or 6 – it depends on several factors.

I’d like to be able to set the width of each list item so that they fill the entire width of the ul, no matter how many items are inside. So if there was 1 list item, it’s width would be 100%; 2, and they would both be 50%, and so on.

Here’s my HTML and CSS:

ul
{
    width: 300px;
    height: 50px;
    background-color: #000000;
    list-style: none;
    margin: 0;
    padding: 0;
}

li
{
    float: left;
    height: 50px;
    background-color: #4265CE;
    margin: 0;
    padding: 0;
}

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
</ul>

Here it is in jFiddle: http://jsfiddle.net/vDVvm/3/

Is there a way to do this with CSS, or will I have to use jQuery?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:41:45+00:00Added an answer on May 25, 2026 at 9:41 pm

    No way with pure CSS, I’m afraid.

    Edit: Most proper jQuery solution i can think of: http://jsfiddle.net/vDVvm/8/

    (function( $ ){
      $.fn.autowidth = function() {
        return this.each(function() {        
            $('li', this).css({'width' : (100 / $('li', this).length) + '%'})
        });
      };
    })( jQuery );
    
    $(document).ready(function(){
        $('.fill-me').autowidth();    
    });
    

    Keep in mind though, that whenever (100 % number_of_lis !== 0), you’re gonna be running into some nasty problems with rounding errors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of <li> elements in an unordered list. The items are
I have a ul list that I display as a horizontal menu. I'd like
I currently have a drop-line horizontal menu using an unordered list with the following
I have created a navigation bar with a horizontal unordered list. This displays fine
I'm working on 'timeline' project, where I have content inside of list items, in
I have a script that turns an unordered list of images into a horizontal
I have a horizontal unordered list of 2 images. Intent is to make the
i have an unordered list of horizontal/vertical segments of length 1, which build one
I have a horizontal nav bar that is an unordered list with anchor tags.
I have an unordered list and I want to have the list items have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.