【问题标题】:CSS dropdown navigation align-rightCSS 下拉导航右对齐
【发布时间】:2019-07-28 16:08:12
【问题描述】:

我刚刚开始学习 HTML 和 CSS,我正在尝试创建一个下拉导航,其中下拉菜单的边缘在右侧与该下拉菜单的导航项对齐。除了对齐之外,这一切都完美无缺。

我已将下拉列表移至“主页”导航项下方,以便查看对齐方式。我还看到一些页面建议使用right:0 和left:auto,我尝试将其放置在很多地方,它要么什么都不做,要么将整个菜单移动到浏览器的右边缘。

这是 jsfiddle:https://jsfiddle.net/StePeters17/qm4Lkevs/36/

【问题讨论】:

    标签: html css navigation alignment dropdown


    【解决方案1】:

    您可以使用right: #px 设置与右侧的距离,您不需要包含left: auto。只需将 # 替换为您需要的像素数。你已经包含了大量的代码,所以我只会告诉你我添加了什么来解决这个问题,你可以运行 sn-p 来看看它是如何工作的。

    如果您将right: 220px 添加到.dropdown-content 类,这将使其向右对齐。文本仍将左对齐,但如果您还希望该文本右对齐,您可以通过将 text-align: right 添加到同一类来更改它。您想将其添加到此类,因为这是所有下拉内容的容器。

    * {
      /*global settings */
      box-sizing: border-box;
      padding: 0;
      margin: 0;
      font-family: 'Roboto', sans-serif;
    }
    
    body {
      background-color: whitesmoke;
      padding-top: 70px;
      padding-left: 20px;
    }
    
    /* ---------------------------------------------------------------- Navigation bar styling------------------------------------------------------------------------*/
    
    /*------------- Main Nav Bar-------------- */
    
    .nav-main {
      background-color: #333333;
      color: white;
      font-size: 18px;
      width: 100%;
      position: fixed;
      top: 0;
      left: 0;
      height: 60px;
      /*Set the height so that it doesn't auto change and fill screen when dropdown opens*/
      line-height: 36px;
      /* 36 (line height) + 4 (border-bottom) + 2*10 (padding top & bottom) = 60 (height of nav)
    }
    .nav-main .logo{
    	/* insert logo stlying e.g. float left, height etc. */
    }
    
    .nav-main ul {
      float: right;
      /*Float the list itself to the right of the header */
      list-style-type: none;
      margin: 0;
      padding: 0;
    }
    
    .nav-main ul li {
      float: left;
      /* Float the list items (menu items) left within the list which is floated on the right */
    }
    .nav-main ul li a {
      right:0;
    }
    
    #nav-active {
      border-bottom: 4px solid red;
    }
    
    .nav-item {
      /* This is the header item links, i.e. the top items which produce dropdowns */
      display: inline-block;
      padding: 10px;
      color: white;
      text-decoration: none;
    }
    
    .nav-item:hover {
      background-color: dimgrey;
      border-bottom: 4px solid red;
    }
    
    /*Next 2 statements will hide and unhide the dropdown on hover of nav item*/
    .nav-item:hover ~.dropdown-content{
      max-height:400px;
      -webkit-transition:max-height 0.25s ease-in;
      -moz-transition:max-height 0.25s ease-in;
      transition:max-height 0.25s ease-in;
    }
    .dropdown-content:hover {
      max-height:400px;
    }
    
    /*------------- Dropdown styling-------------- */
    
    .dropdown-content {
      position: absolute;
      top: 60px;
      /*This is the height of the header */
      overflow: hidden;
      background-color: #333333;
      color: white;
      font-size: 16px;
      max-height:0;
      right: 220px;
    }
    
    .dropdown-content ul {
      margin: 0;
      padding: 0;
    }
    
    .dropdown-content a {
      color: white;
      text-decoration: none;
    }
    
    .dropdown-sub {
      /* This is where we can add things like padding without messing things up */
      padding: 0;
      margin: 0;
    }
    
    .dropdown-sub ul {
      margin: 0;
      padding: 0;
      list-style-type: none;
    }
    
    .dropdown-sub ul li {
      float: none;
    }
    
    .dropdown-sub ul li a {
      width: 100%;
      white-space:nowrap;
      border-bottom: 4px solid transparent;
      display: inline-block;
      padding: 3px 10px;
      /* 5px padding top&bottom, 10 on l&r */
    }
    
    .dropdown-sub ul li a:hover {
      background-color: dimgrey;
      border-bottom: 4px solid red;
    }
    
    /* ---------------------------------------------------------------- End of navigation bar styling----------------------------------------------------------------*/
    <!DOCTYPE html>
    <html>
    
      <head>
        <meta charset="utf-8">
        <meta name="description" content="This is a meta description, this is what often shows up under the search results">
        <meta name=viewport content="width=device-width, initial-scale=1">
        <title></title>
        <!-- Link to stylesheet -->
        <link rel="stylesheet" href="Stylesheet2.css">
        <!-- Add Roboto font -->
        <link href="https://fonts.googleapis.com/css?family=Roboto:300,400" rel="stylesheet">
        <!-- Add icon library -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.css">
      </head>
    
      <body>
        <!-- This is the top part to all the page docs -->
        <!-- Need to add the header code that appears in all docs -->
    
        <header>
          <!-- Navigation Bar -->
    
          <nav class="nav-main">
            <!--This the container for the header -->
            <div class="logo">
              <!-- Insert logo in this container then use css to style, e.g. float left -->
            </div>
            <ul>
              <!-- This is the list of nav headers -->
              <li>
                <!-- This is the actual item for the nav header-->
                <a href="#" class="nav-item" id="nav-active">Home</a>
                <!-- This is the header item link -->
    <div class="dropdown-content">
                  <!-- Create a couple of divs for the dropdown content... placed at same level as the nav-item itself-->
                  <div class="dropdown-sub">
                    <ul>
                      <!-- Dropdown list -->
                      <li><a href="#">User Profile</a></li>
                      <li><a href="#">Settings</a></li>
                      <li><a href="#">Log out</a></li>
                    </ul>
                  </div>
                </div>
              </li>
              <li><a href="#" class="nav-item">About</a></li>
              <!-- Can sit on one line if no dropdown-->
              <li><a href="#" class="nav-item">News</a></li>
              <li>
                <a href="#" class="nav-item">Account</a>
                
              </li>
            </ul>
    
          </nav>
    
        </header>
    
        <!-- Since this gets called using php into all page docs, we don't close the <body> and <html> tags here-->
        <!-- Close the <body> and <html> tags in the footer document, which gets called at the end of each page doc -->
    
        <p>Top Line</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
        <p>Blah</p>
    
    
      </body>
    
    </html>

    【讨论】:

    • 有没有办法将其作为自动设置,而不是为每个下拉菜单指定像素?就像我说的,HTML 和 CSS 的新手——从 YouTube 视频中获得了这个导航布局(尽管他们让它左对齐)......如果你知道使用 HTML 和 CSS 实现与 jsfiddle 中相同的结果的更好方法,那么请告诉我!
    • 并非如此。大多数下拉导航栏是单独的导航(或在您的情况下是单独的 div),默认情况下隐藏,直到悬停或鼠标悬停事件导致它们可见,因此它实际上不是原始导航栏的一部分。您可以尝试使用 Bootstrap 或 Semantic UI 之类的框架,但作为初学者,您可能会在自定义组件方面不知所措。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 2014-10-02
    • 2023-02-20
    • 2013-05-16
    • 1970-01-01
    相关资源
    最近更新 更多