如何在没有额外 div 的情况下实现浮动/对齐?

发布于 2024-10-03 00:26:34 字数 1081 浏览 0 评论 0原文

我试图获得一个导航栏,其中包含一个导航栏,其中有一个无序列表,以与页面宽度的右侧对齐。导航本身应该跨越整个宽度(它是横跨整个页面的黑条),但链接本身应该与内容宽度的右侧对齐,如下所示:

|                                        home | about | etc.               |
|                                                                          |
|            This is content with a fixed with in the center               |
|            of the page.                                                  |

我有的 HTML:

<nav>
 <ul id="navlist">
  <li><a href="#">Home</a></li>
  <li><a href="#">about</a></li>
  <li><a href="#">etc,</a></li>
 </ul>
</nav>

CSS:

nav {
  display:block;
  background:#000;
 }

  nav ul {
   margin: 0 auto;
   width: 777px;
  }
   nav ul li {
    float:left;

   }

但当然这是在左侧页面的一侧,如果不添加额外的 div 元素,我似乎无法将其移至右侧...唯一的选择是将

  • 的右侧浮动,但那这意味着我的导航是相反的顺序,我真的不希望这样...这是在 WordPress 安装中自动生成的页面列表,因此我可以在管理面板中反向排序页面,但必须更改后端内容这并不是处理设计的好方法。
  • 有人吗?

    I'm trying to get a navigation bar that consists of a nav with an unordered list in it to align to the right side of the page's width. The nav itself should span the entire width (it's a black bar across the entire page) but the links itself should be aligned to the right of the contents width, like so:

    |                                        home | about | etc.               |
    |                                                                          |
    |            This is content with a fixed with in the center               |
    |            of the page.                                                  |
    

    The HTML I have:

    <nav>
     <ul id="navlist">
      <li><a href="#">Home</a></li>
      <li><a href="#">about</a></li>
      <li><a href="#">etc,</a></li>
     </ul>
    </nav>
    

    CSS:

    nav {
      display:block;
      background:#000;
     }
    
      nav ul {
       margin: 0 auto;
       width: 777px;
      }
       nav ul li {
        float:left;
    
       }
    

    But of course that's on the left side of the page, and I can't seem to get it to the right without adding an extra div element... The only option is to float the <li>'s right, but that would mean my navigation is in reverse order and I really don't want that... It's an auto generated list of pages in a wordpress install, so I could then order the pages reversely in the admin panel but having to alter backend stuff is not really a good way to handle a design.

    Anyone?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    我偏爱纯白色 2024-10-10 00:26:34
    nav {
      display:block;
      background:#000;
      text-align:right;      
     }
    
      nav ul {
       margin: 0 auto;
       width: 777px;
      }
       nav ul li {
        display: inline;
    
       }
    
    nav {
      display:block;
      background:#000;
      text-align:right;      
     }
    
      nav ul {
       margin: 0 auto;
       width: 777px;
      }
       nav ul li {
        display: inline;
    
       }
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文