Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

html - How do I make my dropdown menu links wider?

I have a dropdown menu, with a 100% width, however the links in the dropdown are way too thin, I want the text of each link to be on one line. I feel like it's the '100% width', but without that the links are not directly under the main navigation link that the fall under.

#main-nav a {
  color: #ce2029;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2em;
  padding: 0px 20px;
}

#main-nav ul {
  display: flex;
}

#main-nav li {
  text-align: center;
  position: relative;
}

#main-nav li:hover .sub-menu>li {
  display: flex;
  justify-content: center;
  top: 10px;
}

.sub-menu li {
  display: none;
  position: absolute;
  top: 0px;
}

.sub-menu {
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  background-color: #2b303a;
  box-shadow: 1px 1px 4px 0 rgba(0, 0, 0, 0.2);
  width: 100%;
}
<nav id="main-nav">
  <ul>
    <li><a href="#">Services</a>
      <ul class="sub-menu">
        <li><a href="#">Website & Application Development</a></li>
        <li><a href="#">iOS & Android Development</a></li>
        <li><a href="#">SAAS</a></li>
        <li><a href="#">AI & Data Science</a></li>
        <li><a href="#">Databases</a></li>
      </ul>
    </li>
question from:https://stackoverflow.com/questions/66064376/how-do-i-make-my-dropdown-menu-links-wider

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is this the desired result?

#main-nav a {
  color: #ce2029;
  display: flex;
  justify-content: left;
  font-size: 1.2em;
  padding: 0px 20px;
  width: 100%;
}

#main-nav ul {
  display: flex;
  padding-left: 0;
}

#main-nav li {
  text-align: left;
  position: relative;
  width: 100%;
}

#main-nav li:hover .sub-menu>li {
  display: flex;
  justify-content: center;
  top: 10px;
}

#main-nav li:hover .sub-menu {
  padding-bottom: 15px;
}

.sub-menu li {
  display: none;
  position: absolute;
  top: 0px;
}

.sub-menu {
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  background-color: #2b303a;
  box-shadow: 1px 1px 4px 0 rgba(0, 0, 0, 0.2);
  width: 100%;
}
<nav id="main-nav">
  <ul>
    <li><a href="#">Services</a>
      <ul class="sub-menu">
        <li><a href="#">Website & Application Development</a></li>
        <li><a href="#">iOS & Android Development</a></li>
        <li><a href="#">SAAS</a></li>
        <li><a href="#">AI & Data Science</a></li>
        <li><a href="#">Databases</a></li>
      </ul>
    </li>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...