繁体   English   中英

React/CSS:将 ul 的第一个元素向左推,并将 rest 保持在右侧

[英]React/CSS: Pushing the first element of ul to left and keeping the rest to the right

我正在尝试创建一个导航栏,左侧带有徽标,右侧带有 rest 元素。 由于某种原因, :first-child不断将 ul 中的所有元素推向左侧。

JS:

function Navigation() {
    return (

    <Navbar>
        <NavItem href="#" icon={<Logo />} />
        <NavItem href="#" icon={<About />} />
        <NavItem href="#" icon={<Idea />}>
            <DropdownMenu></DropdownMenu>
        </NavItem>
    </Navbar>
    );
}

function Navbar(props) {
    return (
        <nav className="navbar">
            <ul className="navbar-nav">{props.children}</ul>
        </nav>
    );
}

function NavItem(props) {
    const [open, setOpen] = useState(false);

    return (
    <li className="nav-item">
        <a href={props.href} className="icon-button" onClick={() => setOpen(!open)}>
        {props.icon}
        </a>
        {open && props.children}
    </li>
    );
}

CSS:

.navbar {
    height: var(--nav-size);
    background-color: var(--bg);
    padding: 0 1rem;
    border-bottom: var(--border);
    box-shadow: 0 4px 10px -2px #252424;
}

/* <ul> */
.navbar-nav{
    max-width: 100%;
    height: 100%;
    display: flex;
    justify-content: flex-end;
    margin-right: 20%;
}

.navbar-nav:first-child{
    float:left;
}

您可以将徽标与导航栏列表元素分开吗? 如果是,则某些 flexbox 配置可以按照您想要的方式对齐。

 .navbar { padding: 0 1rem; box-shadow: 0 4px 10px -2px #252424; display: flex; align-items:center; justify-content: space-between; } /* <ul> */.navbar-nav{ max-width: 100%; list-style:none; display: flex; } /*li*/.nav-item{ padding:12px; }
 <nav class="navbar"> <div>Logo</div> <ul class="navbar-nav"> <li class="nav-item"> Menu </li> <li class="nav-item"> Menu </li> <li class="nav-item"> Menu </li> <li class="nav-item"> Menu </li> </ul> </nav>

实现您想要的效果的最简单方法是使用margin left auto 将弹性项目向右推。 考虑这个标记:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <header>
    <h1>My sample website</h1>
  </header>
  <nav>
    <ul class="nav">
      <li><a href="#">Home</a></li>
      <li class="nav-right"><a href="#">Company</a></li>
      <li><a href="#">Privacy</a></li>
      <li><a href="#">About</a></li>
    </ul>
  </nav>
</body>
</html>

这是 style.css 文件:

:root {
  box-sizing: border-box;
}

*, ::before, ::after {
  box-sizing: inherit;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

.nav {
  list-style: none;
  display: flex;
  padding-left: 0;
  background-color: blue;
  padding: 0.5em;
  border-radius: 0.2em;
}

.nav > li + li {
  margin-left: 1.5em;
}

.nav > .nav-right {
  margin-left: auto;
}

.nav > li > a {
  text-decoration: none;
  color: white;
  background-color: red;
  display: block;
  padding: 0.5em 1em;
}

所有的技巧都是由导航右 class 完成的。 通过在弹性项目上设置margin-left auto,左边距将尽可能地增长,最终结果是将弹性项目一直推到右边。

在这里您可以找到运行示例: https://codepen.io/EnricoMassone/pen/PoNqwxa

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM