激活时,导航栏切换图标会向左侧翻转。覆盖左对齐的徽标

时间:2018-12-28 00:08:48

标签: html css bootstrap-4

我在Bootstrap 4中使用了可折叠的导航栏。我的菜单项与左对齐的徽标右对齐。当屏幕尺寸减小并且导航栏切换器处于活动状态时,菜单项被隐藏(在右侧),但切换器图标显示在左侧。它与徽标重叠。我希望切换开关保持在右侧。

我尝试使用navbar-toggler-right等方法赋予其绝对位置。我还尝试将按钮放入div并使用之间的justify-content-。

这是带有我编写的代码的Codepen的链接。

https://codepen.io/amandathedev/pen/bOrZZq

所有图像都在本地托管,因为这只是一个练习项目,因此它们不会出现在Codepen预览中。我希望仍然很容易分辨出徽标发生了什么。

谢谢!

HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Modern Calligraphy</title>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />

    <link rel="shortcut icon" type="image/x-icon" href="logo.ico" />

    <!-- Font Awesome -->
    <link
      rel="stylesheet"
      href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"
      integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
      crossorigin="anonymous"
    />

    <!-- Bootstrap CSS -->
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
      integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
      crossorigin="anonymous"
    />

    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css?family=Raleway:300,400,700"
      rel="stylesheet"
    />

    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <div class="grid-container">
      <div class="grid-item" id="header">
        <!-- Navbar -->
        <nav class="navbar bg-transparent navbar-expand-sm">
          <button
            class="navbar-toggler"
            type="button"
            data-toggle="collapse"
            data-target="#collapsibleNavbar">
            <span class="navbar-toggler-icon mr-auto"><i class="fas fa-bars"></i></span>
          </button>
          <img src="logo5.png" alt="Logo" class="navbar-brand mr-auto" />
          <div class="collapse navbar-collapse" id="collapsibleNavbar">
            <ul class="navbar-nav ml-auto">
              <li class="nav-item cool-link">
                <a class="nav-link" href="#">About</a>
              </li>
              <li class="nav-item cool-link">
                <a class="nav-link" href="#">Order</a>
              </li>
              <li class="nav-item cool-link">
                <a class="nav-link" href="#">Learn</a>
              </li>
              <li class="nav-item cool-link">
                <a class="nav-link" href="#">Contact</a>
              </li>
            </ul>
          </div>
        </nav>
      </div>
      <div class="grid-item" id="menu">Menu</div>
      <div class="grid-item" id="main">
        <div class="box">
          <img
            src="personalized.jpg"
            alt="Hand lettered calligraphy quote on paper with fruit"
          />
        </div>
        <div class="box">
          <img
            src="handLettering.jpg"
            alt="Hand lettered quote in open book on map"
          />
        </div>
        <div class="box">
          <img
            src="digital.jpg"
            alt="Digital calligraphy drawn on iPad held by woman with stylus"
          />
        </div>
        <div class="box">
          <img
            src="tutorials.jpg"
            alt="Hand lettered quote on notebook with markers"
          />
        </div>
      </div>
      <div class="grid-item" id="footer">
        <ul>
          <li>
            <a href="#"> <i class="far fa-envelope"></i> Contact</a>
          </li>
          <li><a href="#">Privacy</a></li>
          <li><a href="#">Info</a></li>
        </ul>
        <span>Copyright 2018, Modern Calligraphy Inc.</span>
      </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script
      src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
      integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
      integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
      integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
      crossorigin="anonymous"
    ></script>
  </body>
</html>

CSS:

body {
  background-image: url("concrete-texture.png");
}

.grid-container {
  display: grid;
  font-family: "Raleway", sans-serif;
  grid-template-columns: 10em auto auto;
  grid-template-rows: 1fr 5.5fr 1fr;
  grid-gap: 1px;
  padding: 1px;
  grid-template-areas:
    "header header header header"
    "menu main main main"
    "footer footer footer footer";
}

/* .grid-item {
  border: 1px solid black;
} */

#header {
  grid-area: header;
}

/* Navbar */
.nav-link {
  display: inline-block;
  font-size: 1em;
  letter-spacing: 1px;
  text-decoration: none;
}

.navbar-toggler {
    width: 1em;
    float: right;
    border: none;
}

.navbar-toggler-icon {
    position: fixed;
}

.cool-link {
  display: inline-block;
  color: #000;
  text-decoration: none;
}

.cool-link::after {
  content: "";
  display: block;
  width: 0;
  height: 2px;
  background: black;
  transition: width 0.4s;
}

.cool-link:hover::after {
  width: 95%;
}

@media (max-width: 600px) {

}

#menu {
  grid-area: menu;
}

#main {
  grid-area: main;
  display: grid;
  grid-template-columns: auto auto;
  grid-gap: 5px;
}

img {
  max-width: 100%;
}

.box {
  background-color: burlywood;
  padding: 10px;
}

a {
  color: #292929;
  text-decoration: none;
}

#footer {
  grid-area: footer;
  background-color: #dadada;
  border: none;
  padding: 20px;
}

#footer ul {
  display: flex;
  justify-content: flex-end;
}

#footer li {
  padding: 0 10px;
  list-style: none;
}

#footer a:hover {
  text-decoration: none;
}

#footer span {
  display: flex;
  justify-content: flex-end;
  font-size: 0.9em;
}

2 个答案:

答案 0 :(得分:1)

我将HTML替换为导航按钮和徽标图像,并注释了以下CSS

.navbar-toggler {
    width: 1em;
    float: right;
    border: none;
}

.navbar-toggler-icon {
    position: fixed;
}

https://codepen.io/scheinercc/pen/MZvRaq

body {
  background-image: url("concrete-texture.png");
}

.grid-container {
  display: grid;
  font-family: "Raleway", sans-serif;
  grid-template-columns: 10em auto auto;
  grid-template-rows: 1fr 5.5fr 1fr;
  grid-gap: 1px;
  padding: 1px;
  grid-template-areas:
    "header header header header"
    "menu main main main"
    "footer footer footer footer";
}

/* .grid-item {
  border: 1px solid black;
} */

#header {
  grid-area: header;
}

/* Navbar */
.nav-link {
  display: inline-block;
  font-size: 1em;
  letter-spacing: 1px;
  text-decoration: none;
}

/*.navbar-toggler {
    width: 1em;
    float: right;
    border: none;
}

.navbar-toggler-icon {
    position: fixed;
}*/

.cool-link {
  display: inline-block;
  color: #000;
  text-decoration: none;
}

.cool-link::after {
  content: "";
  display: block;
  width: 0;
  height: 2px;
  background: black;
  transition: width 0.4s;
}

.cool-link:hover::after {
  width: 95%;
}

@media (max-width: 600px) {
  
}

#menu {
  grid-area: menu;
}

#main {
  grid-area: main;
  display: grid;
  grid-template-columns: auto auto;
  grid-gap: 5px;
}

img {
  max-width: 100%;
}

.box {
  background-color: burlywood;
  padding: 10px;
}

a {
  color: #292929;
  text-decoration: none;
}

#footer {
  grid-area: footer;
  background-color: #dadada;
  border: none;
  padding: 20px;
}

#footer ul {
  display: flex;
  justify-content: flex-end;
}

#footer li {
  padding: 0 10px;
  list-style: none;
}

#footer a:hover {
  text-decoration: none;
}

#footer span {
  display: flex;
  justify-content: flex-end;
  font-size: 0.9em;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Modern Calligraphy</title>
    <!-- Required meta tags -->
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, shrink-to-fit=no"
    />

    <link rel="shortcut icon" type="image/x-icon" href="logo.ico" />

    <!-- Font Awesome -->
    <link
      rel="stylesheet"
      href="https://use.fontawesome.com/releases/v5.6.3/css/all.css"
      integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/"
      crossorigin="anonymous"
    />

    <!-- Bootstrap CSS -->
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
      integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO"
      crossorigin="anonymous"
    />

    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css?family=Raleway:300,400,700"
      rel="stylesheet"
    />

    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <div class="grid-container">
      <div class="grid-item" id="header">
        <!-- Navbar -->
        <nav class="navbar bg-transparent navbar-expand-sm">
          <img src="logo5.png" alt="Logo" class="navbar-brand mr-auto" />
          <button
            class="navbar-toggler"
            type="button"
            data-toggle="collapse"
            data-target="#collapsibleNavbar">
            <span class="navbar-toggler-icon mr-auto"><i class="fas fa-bars"></i></span>
          </button>
          <div class="collapse navbar-collapse" id="collapsibleNavbar">
            <ul class="navbar-nav ml-auto">
              <li class="nav-item cool-link">
                <a class="nav-link" href="#">About</a>
              </li>
              <li class="nav-item cool-link">
                <a class="nav-link" href="#">Order</a>
              </li>
              <li class="nav-item cool-link">
                <a class="nav-link" href="#">Learn</a>
              </li>
              <li class="nav-item cool-link">
                <a class="nav-link" href="#">Contact</a>
              </li>
            </ul>
          </div>
        </nav>
      </div>
      <div class="grid-item" id="menu">Menu</div>
      <div class="grid-item" id="main">
        <div class="box">
          <img
            src="personalized.jpg"
            alt="Hand lettered calligraphy quote on paper with fruit"
          />
        </div>
        <div class="box">
          <img
            src="handLettering.jpg"
            alt="Hand lettered quote in open book on map"
          />
        </div>
        <div class="box">
          <img
            src="digital.jpg"
            alt="Digital calligraphy drawn on iPad held by woman with stylus"
          />
        </div>
        <div class="box">
          <img
            src="tutorials.jpg"
            alt="Hand lettered quote on notebook with markers"
          />
        </div>
      </div>
      <div class="grid-item" id="footer">
        <ul>
          <li>
            <a href="#"> <i class="far fa-envelope"></i> Contact</a>
          </li>
          <li><a href="#">Privacy</a></li>
          <li><a href="#">Info</a></li>
        </ul>
        <span>Copyright 2018, Modern Calligraphy Inc.</span>
      </div>
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script
      src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
      integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"
      integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
      crossorigin="anonymous"
    ></script>
    <script
      src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"
      integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
      crossorigin="anonymous"
    ></script>
  </body>
</html>

答案 1 :(得分:1)

仔细看看您的HTML。您将按钮放在HTML中徽标上方。您无需更改Codepen中的CSS即可将切换器向右移动,但是为了更好地对齐,您将需要更多的样式。

将您写的徽标图像如下所示,并将其放在按钮上方:

<!-- Navbar -->
        <nav class="navbar bg-transparent navbar-expand-sm">

          <button
            class="navbar-toggler"
            type="button"
            data-toggle="collapse"
            data-target="#collapsibleNavbar">
            <span class="navbar-toggler-icon mr-auto"><i class="fas fa-bars"></i></span>
          </button>

          <img src="logo5.png" alt="Logo" class="navbar-brand mr-auto" />

切换到此:

<!-- Navbar -->
        <nav class="navbar bg-transparent navbar-expand-sm">

          <img src="logo5.png" alt="Logo" class="navbar-brand mr-auto" />

          <button
            class="navbar-toggler"
            type="button"
            data-toggle="collapse"
            data-target="#collapsibleNavbar">
            <span class="navbar-toggler-icon mr-auto"><i class="fas fa-bars"></i></span>
          </button>