Abhishek Pandey Abhishek Pandey - 2 months ago 5
CSS Question

Absolute positioned div not auto adjusting according with content

I'm making a navigation with hover dropdown, everything is working fine, but the problem is with dropdown with, dropdown with is not increasing according to content/text. I want dropdown takes width according it's content.



.navigation {
width: 100%;
float: left;
background: #008271;
min-height: 50px;
font-family: segoe ui;
padding: 0 30px;
}
.navigation,
.navigation * {
box-sizing: border-box;
}
.navigation ul {
padding: 0;
margin: 0;
list-style: none;
}
.navigation .nav-right {
float: right;
}
.navigation > ul > li {
display: block;
float: left;
position: relative;
}
.navigation > ul > li > a {
display: block;
color: #fff;
height: 50px;
line-height: 50px;
text-align: center;
padding: 0 15px;
font-size: 15px;
}
.navigation > ul > li.drop-menu:hover > a,
.navigation > ul > li.drop-menu > a:hover {
background: #01675a;
}
.navigation .sub-menu {
display: none
}
.navigation > ul > li:hover .sub-menu {
width: 100%;
display: block;
position: absolute;
top: 50px;
background: #fff;
min-width: 160px;
right: 0;
border: 1px solid rgba(0, 0, 0, .15);
box-shadow: 0 6px 12px rgba(0, 0, 0, .17);
padding: 5px 0;
-webkit-background-clip: padding-box;
background-clip: padding-box;
}
.sub-menu > li > a {
display: block;
padding: 5px 15px;
color: #333;
white-space: nowrap;
font-size: 14px;
}
.sub-menu > li > a:hover {
background: #f5f5f5;
}
.caret {
display: inline-block;
width: 0;
height: 0;
margin-left: 2px;
vertical-align: middle;
border-top: 4px dashed;
border-top: 4px solid\9;
border-right: 4px solid transparent;
border-left: 4px solid transparent;
}
.nav-left > li > a {
font-size: 24px!important;
font-weight: 300;
}

<div class="navigation">
<ul class="nav-right">
<li class="drop-menu">
<a href="#">Teams <span class="caret"></span></a>
<ul class="sub-menu">
<li><a href="#">Team 1</a>
</li>
</ul>
</li>
<li class="drop-menu">
<a href="#">Communities <span class="caret"></span></a>
<ul class="sub-menu">
<li><a href="#">Communities 1</a>
</li>
<li><a href="#">Communities 2 and much more content</a>
</li>
</ul>
</li>
</ul>

</div>




Answer

I've changed the width to auto
.navigation > ul > li:hover .sub-menu { width: auto; }

.navigation {
  width: 100%;
  float: left;
  background: #008271;
  min-height: 50px;
  font-family: segoe ui;
  padding: 0 30px;
}
.navigation,
.navigation * {
  box-sizing: border-box;
}
.navigation ul {
  padding: 0;
  margin: 0;
  list-style: none;
}
.navigation .nav-right {
  float: right;
}
.navigation > ul > li {
  display: block;
  float: left;
  position: relative;
}
.navigation > ul > li > a {
  display: block;
  color: #fff;
  height: 50px;
  line-height: 50px;
  text-align: center;
  padding: 0 15px;
  font-size: 15px;
}
.navigation > ul > li.drop-menu:hover > a,
.navigation > ul > li.drop-menu > a:hover {
  background: #01675a;
}
.navigation .sub-menu {
  display: none
}
.navigation > ul > li:hover .sub-menu {
  width: auto;
  display: block;
  position: absolute;
  top: 50px;
  background: #fff;
  min-width: 160px;
  right: 0;
  border: 1px solid rgba(0, 0, 0, .15);
  box-shadow: 0 6px 12px rgba(0, 0, 0, .17);
  padding: 5px 0;
  -webkit-background-clip: padding-box;
  background-clip: padding-box;
}
.sub-menu > li > a {
  display: block;
  padding: 5px 15px;
  color: #333;
  white-space: nowrap;
  font-size: 14px;
}
.sub-menu > li > a:hover {
  background: #f5f5f5;
}
.caret {
  display: inline-block;
  width: 0;
  height: 0;
  margin-left: 2px;
  vertical-align: middle;
  border-top: 4px dashed;
  border-top: 4px solid\9;
  border-right: 4px solid transparent;
  border-left: 4px solid transparent;
}
.nav-left > li > a {
  font-size: 24px!important;
  font-weight: 300;
}
<div class="navigation">
  <ul class="nav-right">
    <li class="drop-menu">
      <a href="#">Teams <span class="caret"></span></a>
      <ul class="sub-menu">
        <li><a href="#">Team 1</a>
        </li>
      </ul>
    </li>
    <li class="drop-menu">
      <a href="#">Communities <span class="caret"></span></a>
      <ul class="sub-menu">
        <li><a href="#">Communities 1</a>
        </li>
        <li><a href="#">Communities 2 and much more content</a>
        </li>
      </ul>
    </li>
  </ul>

</div>

Comments