html - 导航栏布局使用 flexbox : How to position link or paragraph inline/next to menu icon

标签 html css flexbox

我正在处理页面顶部的导航栏。我正在使用 flexbox用于定位导航栏的元素;在左侧我有一个动画汉堡包菜单图标以及页面的标题,在右侧我有几个链接。图标和页面标题包含在 <div> 中, 链接包含在另一个 <div> 中.使用 flexbox , flex-direction: rowjustify-content: space-between , 我将每个位置放在页面的相对两侧。

在左侧,我希望页面标题位于菜单图标旁边。不知何故,我不知道该怎么做,在我的代码中,页面标题总是位于图标下方。使用我的代码的当前版本查看这个 fiddle (由于某种原因,图标的动画在 fiddle 中不起作用,但这对于演示我的问题并不重要):

function changeicon(x) {
  x.classList.toggle("change");
}
.flex {
  display: flex;
}

.flex-menu {
  flex-direction: row;
  justify-content: space-between;
}

.nav {
  left: 0;
  padding: 5px;
  box-sizing: border-box;
  width: 100%;
  position: fixed;
  top: 0.4em;
}


/* ----- Nav/Menu Icon  --------------------------------------------- */

.icon-container {
  display: inline;
  cursor: pointer;
}

.bar1,
.bar2,
.bar3 {
  width: 35px;
  height: 3px;
  background-color: black;
  margin: 6px 0;
  transition: all 400ms ease;
}


/*--- Rotation of the first bar */

.change .bar1 {
  transform: rotate(-45deg) translate(-6px, 6px);
}


/* Fade out the second bar */

.change .bar2 {
  opacity: 0;
}


/* Rotate last bar */

.change .bar3 {
  transform: rotate(45deg) translate(-6px, -8px);
}
<div class="nav flex flex-menu">

  <div class="icon-container" onclick="changeicon(this)">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>

    <a href="">Page Title</a>
  </div>

  <div>
    <a href="" target="blank">Link1</a>
    <a href="" target="blank">Link2</a>
    <a href="" target="blank">Link3</a>
  </div>

</div>

我如何设置 <div> 中的元素?在左侧,以便它们彼此相邻?

最佳答案

将标题放在单独的 div 中,并使用 margin-right: auto 将其向左推

function changeicon(x) {
  x.classList.toggle("change");
}
.flex {
  display: flex;
}

.flex-menu {
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.nav {
  left: 0;
  padding: 5px;
  box-sizing: border-box;
  width: 100%;
  position: fixed;
  top: 0.4em;
}


/* ----- Nav/Menu Icon  --------------------------------------------- */

.icon-container {
  display: inline;
  cursor: pointer;
}

.page-title {
  margin-right: auto;
  padding: 5px;
}

.bar1,
.bar2,
.bar3 {
  width: 35px;
  height: 3px;
  background-color: black;
  margin: 6px 0;
  transition: all 400ms ease;
}


/*--- Rotation of the first bar */

.change .bar1 {
  transform: rotate(-45deg) translate(-6px, 6px);
}


/* Fade out the second bar */

.change .bar2 {
  opacity: 0;
}


/* Rotate last bar */

.change .bar3 {
  transform: rotate(45deg) translate(-6px, -8px);
}
<div class="nav flex flex-menu">

  <div class="icon-container" onclick="changeicon(this)">
    <div class="bar1"></div>
    <div class="bar2"></div>
    <div class="bar3"></div>
  </div>
  <div class="page-title">
    <a href="">Page Title</a>
  </div>
  <div>
    <a href="" target="blank">Link1</a>
    <a href="" target="blank">Link2</a>
    <a href="" target="blank">Link3</a>
  </div>

</div>

关于html - 导航栏布局使用 flexbox : How to position link or paragraph inline/next to menu icon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45264671/

相关文章:

HTML -- margin-left & -right 不适用于 flexbox

jquery - 如何在 HTML/jquery 中为 Linux 终端生成 256 色调色板

html - 如何提取该网页中的所有章节标题?

javascript - 如何在 HTML 中将 div 高度滚动到底部?

javascript - 第一次点击动画效果很好,第二次它跳过了一些步骤

html - 设置 Flex 列表项之间的间距

html - 使用 flexbox 和伪元素构建 line-through 样式

php - 拆分长 php 生成的 HTML 表?

javascript - 如何转义 JavaScript 中的所有单引号和双引号

javascript - 你能(应该)使用 Twitter Bootstrap 的 "half"吗?