Russel Riehle Russel Riehle - 2 months ago 7
CSS Question

How can I make the exceeding submenu area open based on the right edge of the parent items?

I'm building a simple horizontal menu with some submenus.

But I'm stuck trying to make the sub-items respect the width of the parent items.

Here some images what I'm talking about:

Here's what I get:

enter image description here

enter image description here

Fiddle

Check it out:



* {
margin: 0;
padding: 0;
}
body {
font-family: arial, helvetica, sans-serif;
font-size: 12px;
}
.menu {
list-style: none;
border: 1px solid #c0c0c0;
float: left;
}
.menu li {
position: relative;
float: left;
border-right: 1px solid #c0c0c0;
}
.menu li a {
color: #333;
text-decoration: none;
padding: 5px 10px;
display: block;
}
.menu li a:hover {
background: #333;
color: #fff;
-moz-box-shadow: 0 3px 10px 0 #CCC;
-webkit-box-shadow: 0 3px 10px 0 #ccc;
text-shadow: 0px 0px 5px #fff;
}
.menu li ul {
position: absolute;
top: 25px;
left: 0;
background-color: #fff;
display: none;
}
.menu li:hover ul,
.menu li.over ul {
display: block;
}
.menu li ul li {
border: 1px solid #c0c0c0;
display: block;
width: 150px;
}

<nav>

<ul class="menu">

<li><a href="#">Home</a>
</li>

<li><a href="#">About</a>
</li>

<li><a href="#">What we do?</a>

<ul>

<li><a href="#">What we do? 01</a>
</li>
<li><a href="#">What we do? 02</a>
</li>
<li><a href="#">What we do? 03</a>
</li>
</ul>

</li>

<li><a href="#">Links</a>

<ul>

<li><a href="#">Myotherwebsiteiscool</a>
</li>

</ul>

</li>

<li><a href="#">Contact</a>

<ul>

<li><a href="#">Form Contact</a>
</li>
<li><a href="#">Find us</a>
</li>

</ul>

</li>

</ul>

</nav>




Answer

Change the css here- note that I've change left: 0 to right: 0

.menu li ul {
    position: absolute;
    top: 25px;
    right: 0;
    background-color: #fff;
    display: none;
}

check it out:

* {
  margin: 0;
  padding: 0;
}
body {
  font-family: arial, helvetica, sans-serif;
  font-size: 12px;
}
.menu {
  list-style: none;
  border: 1px solid #c0c0c0;
  float: left;
}
.menu li {
  position: relative;
  float: left;
  border-right: 1px solid #c0c0c0;
}
.menu li a {
  color: #333;
  text-decoration: none;
  padding: 5px 10px;
  display: block;
}
.menu li a:hover {
  background: #333;
  color: #fff;
  -moz-box-shadow: 0 3px 10px 0 #CCC;
  -webkit-box-shadow: 0 3px 10px 0 #ccc;
  text-shadow: 0px 0px 5px #fff;
}
.menu li ul {
  position: absolute;
  top: 25px;
  right: 0;
  background-color: #fff;
  display: none;
}
.menu li:hover ul,
.menu li.over ul {
  display: block;
}
.menu li ul li {
  border: 1px solid #c0c0c0;
  display: block;
  width: 150px;
}
<nav>

  <ul class="menu">

    <li><a href="#">Home</a>
    </li>

    <li><a href="#">About</a>
    </li>

    <li><a href="#">What we do?</a>

      <ul>

        <li><a href="#">What we do? 01</a>
        </li>
        <li><a href="#">What we do? 02</a>
        </li>
        <li><a href="#">What we do? 03</a>
        </li>
      </ul>

    </li>

    <li><a href="#">Links</a>

      <ul>

        <li><a href="#">Myotherwebsiteiscool</a>
        </li>

      </ul>

    </li>

    <li><a href="#">Contact</a>

      <ul>

        <li><a href="#">Form Contact</a>
        </li>
        <li><a href="#">Find us</a>
        </li>

      </ul>

    </li>

  </ul>

</nav>

Comments