【问题标题】:Navigation buttons not on the side of logo导航按钮不在徽标的一侧
【发布时间】:2020-09-06 18:23:17
【问题描述】:

我真的很想制作一个好看的导航菜单,但我无法让导航按钮与徽标see picture below 保持在同一水平上,并且也在中间。 我的代码是纯 HTML/CSS 没有 javascript 任何帮助将不胜感激。 (代码笔版本:https://codepen.io/amrouhab/pen/jObXxOy

我的代码:

<nav>
    <div href="#default" id="logo"><img src="logo2.png"></div>
    <label for="drop" class="toggle">Menu</label>
    <input type="checkbox" id="drop" />
    <ul class="menu">
      <li><a href="#">Accueil</a></li>
      <li>
        <label for="drop-1" class="toggle">Connaitre la CFTC +</label>
        <a href="#">Connaitre la CFTC</a>
        <input type="checkbox" id="drop-1" />
        <ul>
          <li><a href="#!">Histoire</a></li>
          <li><a href="#!">Valeurs</a></li>
          <li><a href="#!">Identité</a></li>
        </ul>
      </li>

      <li>
        <label for="drop-1" class="toggle">Syndicat CFTC-FPT 34 +</label>
        <a href="#">Syndicat CFTC-FPT 34</a>
        <input type="checkbox" id="drop-2" />
        <ul>
          <li><a href="#!">Organisation</a></li>
          <li><a href="#!">Accueil</a></li>
          <li><a href="#!">Rôle et définition</a></li>
        </ul>
      </li>

      <li>
        <label for="drop-1" class="toggle">Vie Professionnelle +</label>
        <a href="#">Vie Professionnelle</a>
        <input type="checkbox" id="drop-1" />
        <ul>
          <li><a href="#!">Formation</a></li>
          <li><a href="#!">Statut</a></li>
          <li><a href="#!">Carrière</a></li>
          <li><a href="#!">Temps de Travail</a></li>
          <li><a href="#!">Congés</a></li>
        </ul>
      </li>

      <li>
        <label for="drop-1" class="toggle">Vie Pratique +</label>
        <a href="#">Connaitre la CFTC</a>
        <input type="checkbox" id="drop-1" />
        <ul>
          <li><a href="#!">Action Logement</a></li>
          <li><a href="#!">Pensions Alimentaires impayées</a></li>
          <li><a href="#!">Prime d'activité</a></li>
          <li><a href="#!">Apprentissage</a></li>
        </ul>
      </li>

      <li>
        <label for="drop-1" class="toggle">Activités +</label>
        <a href="#">Activités</a>
        <input type="checkbox" id="drop-1" />
        <ul>
          <li><a href="#!">Actualités</a></li>
          <li><a href="#!">Presse</a></li>
        </ul>
      </li>

      <li>
        <label for="drop-1" class="toggle">Adhésion +</label>
        <a href="#">Adhésion</a>
        <input type="checkbox" id="drop-1" />
        <ul>
          <li><a href="#!">Les + adhérents</a></li>
          <li><a href="#!">Espace adhérents</a></li>
          <li><a href="#!">Nous rejoindre</a></li>
        </ul>
      </li>

      <li>
        <label for="drop-1" class="toggle">Coordonnées +</label>
        <a href="#">Coordonnées</a>
        <input type="checkbox" id="drop-1" />
        <ul>
          <li><a href="#!">Syndicat CFTC-FPT 34</a></li>
          <li><a href="#!">UD34</a></li>
          <li><a href="#!">Fédération CFTC FPT</a></li>
        </ul>
      </li>

    </ul>
  </nav>

我的 CSS:

/* CSS Document */
@font-face {
  font-family: "oswald";
  src: url("fonts\oswald.regular.ttf");
  font-family: "lato";
  src: url("fonts\Lato-Regular.ttf");
  font-family: "playfairdisplay";
  src: url("fonts\PlayfairDisplaySC-Regular.otf");
}

body {
  background: #d2d2d2;
  /* font-size: 30px; */
  /* line-height: 32px; */
  color: #ffffff;
  margin: 0;
  /* padding: 0; */
  word-wrap: break-word !important;
  font-family: oswald;
}

/* h1 {
  font-size: 60px;
  text-align: center;
  color: #FFF;
}

h3 {
  font-size: 30px;
  line-height: 34px;
  text-align: center;
  color: #FFF;
}

h3 a {
  color: #FFF;
} */

a {
  color: #FFF;
}

/* h1 {
  margin-top: 100px;
  text-align: center;
  font-size: 60px;
  line-height: 70px;
  font-family: 'Bree Serif', 'serif';
} */

#container {
  margin: 0 auto;
  max-width: 890px;
}

/* p {
  text-align: center;
} */

.toggle,
[id^=drop] {
  display: none;
}

/* Giving a background-color to the nav container. */
nav {
  margin: 0px;
  padding: 15px 10px 10px 10px;
  background-color: #302878;
}

#logo {
  display: block;
  padding: 0px 30px 0px 15px;
  float: left;
  /* font-size: 20px; */
  /* line-height: 60px; */
}

/* Since we'll have the "ul li" "float:left"
 * we need to add a clear after the container. */
nav:after {
  content: "";
  display: table;
  clear: both;
}

/* Removing padding, margin and "list-style" from the "ul",
 * and adding "position:reltive" */
nav ul {
  float: none;
  padding: 0;
  margin: 0;
  list-style: none;
}

/* Positioning the navigation items inline */
nav ul li {
  margin: 0px;
  display: inline-block;
  float: left;
  background-color: #302878;
}

/* Styling the links */
nav a {
  display: block;
  padding: 10px 10px;
  color: #FFF;
  font-size: 22px;
  text-decoration: none;
}

nav ul li ul li:hover {
  background: #28244d;
}

/* Background color change on Hover */
nav a:hover {
  background-color: #28244d;
}

/* Hide Dropdowns by Default
 * and giving it a position of absolute */
nav ul ul {
  display: none;
  position: absolute;
  /* has to be the same number as the "line-height" of "nav a" */
  top: 60px;
}

/* Display Dropdowns on Hover */
nav ul li:hover>ul {
  display: inherit;
}

/* Fisrt Tier Dropdown */
nav ul ul li {
  width: 170px;
  float: none;
  display: list-item;
  position: relative;
}

/* Second, Third and more Tiers
 * We move the 2nd and 3rd etc tier dropdowns to the left
 * by the amount of the width of the first tier.
*/
nav ul ul ul li {
  position: relative;
  top: -60px;
  /* has to be the same number as the "width" of "nav ul ul li" */
  left: 170px;
}

/* Change ' +' in order to change the Dropdown symbol */
li>a:after {
  content: ' +';
}

li>a:only-child:after {
  content: '';
}

/* Media Queries
--------------------------------------------- */
@media all and (max-width : 768px) {
  #logo {
    display: block;
    padding: 0;
    width: 100%;
    text-align: center;
    float: none;
  }

  nav {
    margin: 0;
  }

  /* Hide the navigation menu by default */
  /* Also hide the  */
  .toggle+a,
  .menu {
    display: none;
  }

  /* Stylinf the toggle lable */
  .toggle {
    display: block;
    background-color: #254441;
    padding: 14px 20px;
    color: #FFF;
    font-size: 17px;
    text-decoration: none;
    border: none;
  }

  .toggle:hover {
    background-color: #000000;
  }

  /* Display Dropdown when clicked on Parent Lable */
  [id^=drop]:checked+ul {
    display: block;
  }

  /* Change menu item's width to 100% */
  nav ul li {
    display: block;
    width: 100%;
  }

  nav ul ul .toggle,
  nav ul ul a {
    padding: 0 40px;
  }

  nav ul ul ul a {
    padding: 0 80px;
  }

  nav a:hover,
  nav ul ul ul a {
    background-color: #000000;
  }

  nav ul li ul li .toggle,
  nav ul ul a,
  nav ul ul ul a {
    padding: 14px 20px;
    color: #FFF;
    font-size: 17px;
  }

  nav ul li ul li .toggle,
  nav ul ul a {
    background-color: #212121;
  }

  /* Hide Dropdowns by Default */
  nav ul ul {
    float: none;
    position: static;
    color: #ffffff;
    /* has to be the same number as the "line-height" of "nav a" */
  }

  /* Hide menus on hover */
  nav ul ul li:hover>ul,
  nav ul li:hover>ul {
    display: none;
  }

  /* Fisrt Tier Dropdown */
  nav ul ul li {
    display: block;
    width: 100%;
  }

  nav ul ul ul li {
    position: static;
    /* has to be the same number as the "width" of "nav ul ul li" */
  }
}

@media all and (max-width : 330px) {
  nav ul li {
    display: block;
    width: 94%;
  }
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    好的,我想我明白了。将 position: relative; 添加到您的 .nav CSS 中,然后添加

    position: absolute;
    bottom: 0;
    left: 180px;
    

    在 CSS 中发送到您的 nav ul。这是结果: 注意:运行代码 sn -p 然后查看全屏查看。

    编辑:我对其进行了更多修改:我将left: 10px; 添加到nav ul ul

    /* CSS Document */
    @font-face {
      font-family: "oswald";
      src: url("fonts\oswald.regular.ttf");
      font-family: "lato";
      src: url("fonts\Lato-Regular.ttf");
      font-family: "playfairdisplay";
      src: url("fonts\PlayfairDisplaySC-Regular.otf");
    }
    
    body {
      background: #d2d2d2;
      /* font-size: 30px; */
      /* line-height: 32px; */
      color: #ffffff;
      margin: 0;
      /* padding: 0; */
      word-wrap: break-word !important;
      font-family: oswald;
    }
    
    /* h1 {
      font-size: 60px;
      text-align: center;
      color: #FFF;
    }
    
    h3 {
      font-size: 30px;
      line-height: 34px;
      text-align: center;
      color: #FFF;
    }
    
    h3 a {
      color: #FFF;
    } */
    
    a {
      color: #FFF;
    }
    
    /* h1 {
      margin-top: 100px;
      text-align: center;
      font-size: 60px;
      line-height: 70px;
      font-family: 'Bree Serif', 'serif';
    } */
    
    #container {
      margin: 0 auto;
      max-width: 890px;
    }
    
    /* p {
      text-align: center;
    } */
    
    .toggle,
    [id^=drop] {
      display: none;
    }
    
    /* Giving a background-color to the nav container. */
    nav {
      margin: 0px;
      padding: 15px 10px 10px 10px;
      background-color: #302878;
      position: relative;
    }
    
    #logo {
      display: block;
      padding: 0px 30px 0px 15px;
      float: left;
      /* font-size: 20px; */
      /* line-height: 60px; */
    }
    
    /* Since we'll have the "ul li" "float:left"
     * we need to add a clear after the container. */
    nav:after {
      content: "";
      display: table;
      clear: both;
    }
    
    /* Removing padding, margin and "list-style" from the "ul",
     * and adding "position:reltive" */
    nav ul {
      float: none;
      padding: 0;
      margin: 0;
      list-style: none;
      position: absolute;
      bottom: 0;
      left: 180px;
      
    }
    
    /* Positioning the navigation items inline */
    nav ul li {
      margin: 0px;
      display: inline-block;
      background-color: #302878;
      position: relative;
    
    }
    
    /* Styling the links */
    nav a {
      display: block;
      padding: 10px 10px;
      color: #FFF;
      font-size: 22px;
      text-decoration: none;
      position: relative;
    }
    
    nav ul li ul li:hover {
      background: #28244d;
    }
    
    /* Background color change on Hover */
    nav a:hover {
      background-color: #28244d;
    }
    
    /* Hide Dropdowns by Default
     * and giving it a position of absolute */
    nav ul ul {
      display: none;
      position: absolute;
      /* has to be the same number as the "line-height" of "nav a" */
      top: 60px;
      left: 10px;
    }
    
    /* Display Dropdowns on Hover */
    nav ul li:hover>ul {
      display: inherit;
    }
    
    /* Fisrt Tier Dropdown */
    nav ul ul li {
      width: 170px;
      float: none;
      display: list-item;
      position: relative;
    }
    
    /* Second, Third and more Tiers
     * We move the 2nd and 3rd etc tier dropdowns to the left
     * by the amount of the width of the first tier.
    */
    nav ul ul ul li {
      position: relative;
      top: -60px;
      /* has to be the same number as the "width" of "nav ul ul li" */
      left: 170px;
    }
    
    /* Change ' +' in order to change the Dropdown symbol */
    li>a:after {
      content: ' +';
    }
    
    li>a:only-child:after {
      content: '';
    }
    
    /* Media Queries
    --------------------------------------------- */
    @media all and (max-width : 768px) {
      #logo {
        display: block;
        padding: 0;
        width: 100%;
        text-align: center;
        float: none;
      }
    
      nav {
        margin: 0;
      }
    
      /* Hide the navigation menu by default */
      /* Also hide the  */
      .toggle+a,
      .menu {
        display: none;
      }
    
      /* Stylinf the toggle lable */
      .toggle {
        display: block;
        background-color: #254441;
        padding: 14px 20px;
        color: #FFF;
        font-size: 17px;
        text-decoration: none;
        border: none;
      }
    
      .toggle:hover {
        background-color: #000000;
      }
    
      /* Display Dropdown when clicked on Parent Lable */
      [id^=drop]:checked+ul {
        display: block;
      }
    
      /* Change menu item's width to 100% */
      nav ul li {
        display: block;
        width: 100%;
      }
    
      nav ul ul .toggle,
      nav ul ul a {
        padding: 0 40px;
      }
    
      nav ul ul ul a {
        padding: 0 80px;
      }
    
      nav a:hover,
      nav ul ul ul a {
        background-color: #000000;
      }
    
      nav ul li ul li .toggle,
      nav ul ul a,
      nav ul ul ul a {
        padding: 14px 20px;
        color: #FFF;
        font-size: 17px;
      }
    
      nav ul li ul li .toggle,
      nav ul ul a {
        background-color: #212121;
      }
    
      /* Hide Dropdowns by Default */
      nav ul ul {
        float: none;
        position: static;
        color: #ffffff;
        /* has to be the same number as the "line-height" of "nav a" */
      }
    
      /* Hide menus on hover */
      nav ul ul li:hover>ul,
      nav ul li:hover>ul {
        display: none;
      }
    
      /* Fisrt Tier Dropdown */
      nav ul ul li {
        display: block;
        width: 100%;
      }
    
      nav ul ul ul li {
        position: static;
        /* has to be the same number as the "width" of "nav ul ul li" */
      }
    }
    
    @media all and (max-width : 330px) {
      nav ul li {
        display: block;
        width: 94%;
      }
    }
    <nav>
        <div href="#default" id="logo"><img src="https://i.imgur.com/hxVf3lw.png"></div>
        <label for="drop" class="toggle">Menu</label>
        <input type="checkbox" id="drop" />
        <ul class="menu">
          <li><a href="#">Accueil</a></li>
          <li>
            <label for="drop-1" class="toggle">Connaitre la CFTC +</label>
            <a href="#">Connaitre la CFTC</a>
            <input type="checkbox" id="drop-1" />
            <ul>
              <li><a href="#!">Histoire</a></li>
              <li><a href="#!">Valeurs</a></li>
              <li><a href="#!">Identité</a></li>
            </ul>
          </li>
    
          <li>
            <label for="drop-1" class="toggle">Syndicat CFTC-FPT 34 +</label>
            <a href="#">Syndicat CFTC-FPT 34</a>
            <input type="checkbox" id="drop-2" />
            <ul>
              <li><a href="#!">Organisation</a></li>
              <li><a href="#!">Accueil</a></li>
              <li><a href="#!">Rôle et définition</a></li>
            </ul>
          </li>
    
          <li>
            <label for="drop-1" class="toggle">Vie Professionnelle +</label>
            <a href="#">Vie Professionnelle</a>
            <input type="checkbox" id="drop-1" />
            <ul>
              <li><a href="#!">Formation</a></li>
              <li><a href="#!">Statut</a></li>
              <li><a href="#!">Carrière</a></li>
              <li><a href="#!">Temps de Travail</a></li>
              <li><a href="#!">Congés</a></li>
            </ul>
          </li>
    
          <li>
            <label for="drop-1" class="toggle">Vie Pratique +</label>
            <a href="#">Connaitre la CFTC</a>
            <input type="checkbox" id="drop-1" />
            <ul>
              <li><a href="#!">Action Logement</a></li>
              <li><a href="#!">Pensions Alimentaires impayées</a></li>
              <li><a href="#!">Prime d'activité</a></li>
              <li><a href="#!">Apprentissage</a></li>
            </ul>
          </li>
    
          <li>
            <label for="drop-1" class="toggle">Activités +</label>
            <a href="#">Activités</a>
            <input type="checkbox" id="drop-1" />
            <ul>
              <li><a href="#!">Actualités</a></li>
              <li><a href="#!">Presse</a></li>
            </ul>
          </li>
    
          <li>
            <label for="drop-1" class="toggle">Adhésion +</label>
            <a href="#">Adhésion</a>
            <input type="checkbox" id="drop-1" />
            <ul>
              <li><a href="#!">Les + adhérents</a></li>
              <li><a href="#!">Espace adhérents</a></li>
              <li><a href="#!">Nous rejoindre</a></li>
            </ul>
          </li>
    
          <li>
            <label for="drop-1" class="toggle">Coordonnées +</label>
            <a href="#">Coordonnées</a>
            <input type="checkbox" id="drop-1" />
            <ul>
              <li><a href="#!">Syndicat CFTC-FPT 34</a></li>
              <li><a href="#!">UD34</a></li>
              <li><a href="#!">Fédération CFTC FPT</a></li>
            </ul>
          </li>
    
        </ul>
      </nav>

    【讨论】:

    • 感谢您花时间帮助我,我尝试了您的解决方案,但现在我无法进入下拉菜单
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    相关资源
    最近更新 更多