Align size according to larger text

Asked

Viewed 333 times

0

I have a menu and some submenus, I need the submenu to be aligned width from it according to the larger text, because there are texts that can contain only 5 words but texts in the submenu that can contain 15, so it needs to be aligned according to the 15 so there is not that line break. I got the following CSS

nav > ul > li{display:inline;margin-left:57px;position:relative;}
nav > ul > li > a.active{color:#e6007e;border-bottom:3px #e6007e solid;padding-bottom:5px;}
nav > ul > li > a:hover{color:#e6007e;border-bottom:3px #e6007e solid;padding-bottom:5px;}
nav > ul > li > a{text-decoration: none;color:#a1a3a5;font-family: 'Quicksand', sans-serif;}

nav > ul > li > ul{
visibility: hidden;
position: absolute;
left: 0;
top: 23px;
padding: 4px 7px;
-webkit-transition: all 0.7s ease;
-moz-transition: all 0.7s ease;
-o-transition: all 0.7s ease;
-moz-opacity: 0.00;
opacity: 0.00;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha"(Opacity=0);
font-family: 'Quicksand', sans-serif;
}

nav > ul > li:hover > ul{
visibility: visible;
text-align: left;
background: #e6007e;
color:#fff;
font-family: 'Quicksand', sans-serif;
-moz-opacity: 1;
opacity: 1;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha"(Opacity=100);
font-family: 'Quicksand', sans-serif;
}

nav > ul > li > ul > li > a{color:#fff;text-decoration:none;font-family: 'Quicksand', sans-serif;}
nav > ul > li > ul > li > a:hover{text-decoration: underline;}

nav > ul > li > ul > li{
display: block;
font-size: 12px;
margin-bottom: 5px;
width: 200px;
}
  • Also add your HTML to the question that makes it easier to help you.

1 answer

4


Just make use of the property white-space: nowrap; that prevents line breaks unless there is a <br> in your code, you can read more about this article from Maujor here.

Then just apply the property min-width instead of width as you had done not to cut the text if it exceeds the defined element size getting min-width: 200px;.

Example (Based on the stylesheet cited in the question, I only applied the SOPT tool code organization):

nav > ul > li {
  display: inline;
  margin-left: 57px;
  position: relative;
}
nav > ul > li > a.active {
  color: #e6007e;
  border-bottom: 3px #e6007e solid;
  padding-bottom: 5px;
}
nav > ul > li > a:hover {
  color: #e6007e;
  border-bottom: 3px #e6007e solid;
  padding-bottom: 5px;
}
nav > ul > li > a {
  text-decoration: none;
  color: #a1a3a5;
  font-family: 'Quicksand', sans-serif;
  display: inline-block;
}
nav > ul > li > ul {
  visibility: hidden;
  position: absolute;
  left: 0;
  top: 23px;
  padding: 4px 7px;
  -webkit-transition: all 0.7s ease;
  -moz-transition: all 0.7s ease;
  -o-transition: all 0.7s ease;
  -moz-opacity: 0.00;
  opacity: 0.00;
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha"(Opacity=0);
  font-family: 'Quicksand', sans-serif;
}
nav > ul > li:hover > ul {
  visibility: visible;
  text-align: left;
  background: #e6007e;
  color: #fff;
  font-family: 'Quicksand', sans-serif;
  -moz-opacity: 1;
  opacity: 1;
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha"(Opacity=100);
  font-family: 'Quicksand', sans-serif;
}
nav > ul > li > ul > li > a {
  color: #fff;
  text-decoration: none;
  font-family: 'Quicksand', sans-serif;
}
nav > ul > li > ul > li > a:hover {
  text-decoration: underline;
}
nav > ul > li > ul > li {
  display: block;
  font-size: 12px;
  margin-bottom: 5px;
  min-width: 200px; /* Para o texto não cortar o texto */
  white-space: nowrap; /* Para não haver quebra de linha */
}
<nav>
  <ul>
    <li><a href="#">Lorem</a>
      <ul>
        <li><a href="#">Sub Lorem Sub Lorem Sub Lorem Sub Lorem Sub Lorem Sub Lorem Sub Lorem</a>
        </li>
        <li><a href="#">Sub Lorem</a>
        </li>
        <li><a href="#">Sub Lorem</a>
        </li>
      </ul>
    </li>
    <li><a href="#">Lorem</a>
    </li>
    <li><a href="#">Lorem</a>
    </li>
    <li><a href="#">Lorem</a>
    </li>
  </ul>
</nav>

OBS: I applied comments to the excerpts where I applied the properties cited for easy visualization.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.