Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make all <li> same width as the widest

I've got this menu wich is setup inline and has dropdowns.
The inner ul has a background.
Each dropdown li has a :hover that changes the background of the li:

<div id="navMain">
    <ul>
        <li><a href="#nogo">Forside</a>
            <ul>
                <li><a href="#nogo">1111111111111</a></li>
                <li><a href="#nogo">Link 1-2</a></li>
                <!-- etc. -->
            </ul>
        </li>
        <li><a href="#nogo">Om Os</a>
            <ul>
                <li><a href="#nogo">Link 2-1</a></li>
                <li><a href="#nogo">Link 2-2</a></li>
                <!-- etc. -->
            </ul>
        </li>
        <li><a href="#nogo">Link 3</a>
            <ul>
                <li><a href="#nogo">Link 3-1</a></li>
                <li><a href="#nogo">Link 3-2</a></li>
                <!-- etc. -->
            </ul>
        </li>
    </ul>
</div>

Problem is, that when one of the submenu li is longer than the others, it will only expand itself, and not the other li ofcourse.
This results in the :hover effect having different lengths.

So how would i make all li in each inner ul the same size as the widest one?

Here you can find the CSS if needed.

like image 656
Christian Bekker Avatar asked Jan 11 '12 17:01

Christian Bekker


People also ask

How do I keep my DIV the same size?

if you set width:500px; and height:300px; (or whatever size you want) the div should stay the same size. Also, you can set overflow:hidden; so that if whatever is in the div goes beyond those bounds, it is not shown, and does not resize the div. Save this answer. Show activity on this post.

How do you make a full width element in HTML?

If you set the width to 100% on the body element you will have a full page width. This is essentially equivalent to not setting a width value and allowing the default. If you want to use the body element as a smaller container and let the HTML element fill the page, you could set a max-width value on the body.

How do you make a div full width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

Which five styles are associated with box model?

The CSS Box Model It consists of: margins, borders, padding, and the actual content.


2 Answers

Here. Notice I added a class to your menu li's and that I added a body background to your css, because I couldn't notice your menus. Finally the trick is done by making the li elements 100% width

body {
  background-color: green;
}

.menu li {
  width: 100%
}

#navMain {}

#navMain ul {
  padding: 0;
  margin: 0;
  z-index: 2;
}

#navMain ul li {
  margin-right: 5px;
  text-align: center;
}

#navMain li a {
  display: block;
  text-decoration: none;
  color: white;
  padding-left: 10px;
  padding-right: 10px;
}

#navMain li a:hover {
  background-color: black;
}

#navMain ul li:last-child {
  margin-right: 0px;
}

#navMain li {
  position: relative;
  float: left;
  list-style: none;
  margin: 0;
  padding: 0;
  font-size: 17px;
  font-weight: bold;
  color: #fff;
}

#navMain ul ul {
  position: absolute;
  top: 20px;
  visibility: hidden;
  background-image: url(img/alphaBg.png);
}

#navMain ul li ul li {
  font-size: 12px;
  margin-right: 0px;
  text-align: left;
}

#navMain ul li ul li:first-child {
  padding-top: 5px;
}

#navMain ul li:hover ul {
  visibility: visible;
}
<html>

<head>
</head>

<body>
  <div id="navMain">
    <ul>
      <li><a href="#nogo">Forside</a>
        <ul class="menu">
          <li><a href="#nogo">1111111111111</a></li>
          <li><a href="#nogo">Link 1-2</a></li>
          <li><a href="#nogo">Link 1-3</a></li>
          <li><a href="#nogo">Link 1-3</a></li>
          <li><a href="#nogo">Link 1-3</a></li>
          <li><a href="#nogo">Link 1-3</a></li>
        </ul>
      </li>
      <li><a href="#nogo">Om Os</a>
        <ul class="menu">
          <li><a href="#nogo">Link 2-1</a></li>
          <li><a href="#nogo">Link 2-2</a></li>
          <li><a href="#nogo">Link 2-3</a></li>
        </ul>
      </li>
      <li><a href="#nogo">Link 3</a>
        <ul class="menu">
          <li><a href="#nogo">Link 3-1</a></li>
          <li><a href="#nogo">Link 3-2</a></li>
          <li><a href="#nogo">Link 3-3</a></li>
        </ul>
      </li>
    </ul>
  </div>
</body>

</html>
like image 98
Darkade Avatar answered Oct 14 '22 16:10

Darkade


li {display:block} will make the list items as wide as the widest item in that parent container

like image 3
David Nguyen Avatar answered Oct 14 '22 16:10

David Nguyen