首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >汉堡包响应导航定位: CSS Flexbox

汉堡包响应导航定位: CSS Flexbox
EN

Stack Overflow用户
提问于 2021-11-25 09:54:47
回答 1查看 168关注 0票数 1

所以这是一个非常集中的问题

  • 首先,我希望您在全屏上执行代码。
  • 请检查代码并将其设置为移动宽度(这是响应性问题)
  • 现在,打开汉堡包菜单
  • 正如您在这个图像中所看到的,导航条的链接不是以y轴为中心的。
  • 尽管我已经在下面的代码中清楚地包含了合理的内容和对齐项

代码:

代码语言:javascript
复制
/* CSS Setup And Reset */

@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap");
@font-face {
  font-family: AstroSpace;
  src: url(/fonts/AstroSpace.ttf);
}

body {
  margin: 0;
  padding: 0;
  font-family: "Montserrat", sans-serif;
  background-color: #265eac;
  color: white;
}

header {
  background-color: #2c304b;
  position: sticky;
  top: 0;
  right: 0;
  width: 100%;
  margin-bottom: 20px;
}


/* Navbar */

.main-nav {
  height: 90px;
}

.logo {
  color: white;
  line-height: 90px;
  font-size: 30px;
  font-weight: 900;
  text-decoration: none;
  margin-left: 30px;
  font-family: "Roboto  ", sans-serif;
}

.navlinks {
  list-style: none;
  float: right;
  line-height: 90px;
  margin: 0;
  padding: 0;
}

.navlinks li {
  display: inline-block;
  margin: 0px 20px;
}

.navlinks li a {
  color: white;
  text-decoration: none;
  font-size: 18px;
  transition: all 0.3s linear 0s;
  text-transform: uppercase;
}

.navlinks li a:hover {
  color: #7ebcb9;
  padding-bottom: 7px;
  border-bottom: 2px solid #7ebcb9;
}

li a.contact {
  background-color: #00adb5;
  padding: 9px 20px;
  border-radius: 50px;
  transition: all 0.3s ease 0s;
  border-bottom: none;
}

li a.contact:hover {
  background-color: #047e85;
  color: white;
  border-bottom: none;
}

#check {
  display: none;
}

.menu-btn {
  font-size: 25px;
  color: white;
  float: right;
  line-height: 90px;
  margin-right: 40px;
  display: none;
  cursor: pointer;
}


/* Responsive Design */


/* As you can see, justify content and align items are included, but the links are not centere */

@media (max-width: 800px) {
  .navlinks {
    position: fixed;
    top: 90px;
    ;
    width: 100%;
    height: 100vh;
    transition: all 0.5s;
    right: -100%;
    background: #222831;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
  }
  .navlinks li {
    display: block;
  }
  .navlinks li a {
    font-size: 20px;
  }
  .navlinks li a:hover {
    border-bottom: none;
  }
  .menu-btn {
    display: block;
  }
  #check:checked~.navlinks {
    right: 0;
  }
  #check:checked~header {
    margin: 0;
  }
}

@media (max-width: 360px) {
  .logo {
    margin-left: 10px;
    font-size: 25px;
  }
  .menu-btn {
    margin-right: 10px;
    font-size: 25px;
  }
  .menu-btn:focus {
    color: blue;
  }
}
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Responsive Navbar</title>
  <link rel="stylesheet" href="styles/styles.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" />

  <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;800&family=Quicksand:wght@300&family=Roboto:wght@900&display=swap" rel="stylesheet">
  <style>

  </style>
</head>

<body>
  <header>
    <nav class="main-nav">
      <input type="checkbox" id="check" />
      <label for="check" class="menu-btn">
              <i class="fas fa-bars"></i>
            </label>
      <a href="index.html" class="logo">Nikita Gada</a>
      <ul class="navlinks">
        <li><a href="#" id="link1" onmousedown="myfun()" onmouseup="myfun2()">About Me</a></li>
        <li><a href="#" id="link3" onmousedown="myfun5()" onmouseup="myfun6()">Work</a></li>
        <li><a href="#" id="link2" onmousedown="myfun3()" onmouseup="myfun4()">Services</a></li>
        <li><a href="#" class="contact">Contact</a></li>
      </ul>
    </nav>
  </header>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-25 10:33:42

在本例中,我将在我的示例下面使用height动态设置卡尔克并进行更改。

代码语言:javascript
复制
/* CSS Setup And Reset */

@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap");
@font-face {
  font-family: AstroSpace;
  src: url(/fonts/AstroSpace.ttf);
}

body {
  margin: 0;
  padding: 0;
  font-family: "Montserrat", sans-serif;
  background-color: #265eac;
  color: white;
}

header {
  background-color: #2c304b;
  position: sticky;
  top: 0;
  right: 0;
  width: 100%;
  margin-bottom: 20px;
}


/* Navbar */

.main-nav {
  height: 90px;
}

.logo {
  color: white;
  line-height: 90px;
  font-size: 30px;
  font-weight: 900;
  text-decoration: none;
  margin-left: 30px;
  font-family: "Roboto  ", sans-serif;
}

.navlinks {
  list-style: none;
  float: right;
  line-height: 90px;
  margin: 0;
  padding: 0;
}

.navlinks li {
  display: inline-block;
  margin: 0px 20px;
}

.navlinks li a {
  color: white;
  text-decoration: none;
  font-size: 18px;
  transition: all 0.3s linear 0s;
  text-transform: uppercase;
}

.navlinks li a:hover {
  color: #7ebcb9;
  padding-bottom: 7px;
  border-bottom: 2px solid #7ebcb9;
}

li a.contact {
  background-color: #00adb5;
  padding: 9px 20px;
  border-radius: 50px;
  transition: all 0.3s ease 0s;
  border-bottom: none;
}

li a.contact:hover {
  background-color: #047e85;
  color: white;
  border-bottom: none;
}

#check {
  display: none;
}

.menu-btn {
  font-size: 25px;
  color: white;
  float: right;
  line-height: 90px;
  margin-right: 40px;
  display: none;
  cursor: pointer;
}


/* Responsive Design */


/* As you can see, justify content and align items are included, but the links are not centere */

@media (max-width: 800px) {
  .navlinks {
    position: fixed;
    top: 90px;
    ;
    width: 100%;
    height: calc(100vh - 90px);
    /*     height: 100vh; */
    transition: all 0.5s;
    right: -100%;
    background: #222831;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
  }
  .navlinks li {
    display: block;
  }
  .navlinks li a {
    font-size: 20px;
  }
  .navlinks li a:hover {
    border-bottom: none;
  }
  .menu-btn {
    display: block;
  }
  #check:checked~.navlinks {
    right: 0;
  }
  #check:checked~header {
    margin: 0;
  }
}

@media (max-width: 360px) {
  .logo {
    margin-left: 10px;
    font-size: 25px;
  }
  .menu-btn {
    margin-right: 10px;
    font-size: 25px;
  }
  .menu-btn:focus {
    color: blue;
  }
}
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Responsive Navbar</title>
  <link rel="stylesheet" href="styles/styles.css" />
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" />

  <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;800&family=Quicksand:wght@300&family=Roboto:wght@900&display=swap" rel="stylesheet">
  <style>

  </style>
</head>

<body>
  <header>
    <nav class="main-nav">
      <input type="checkbox" id="check" />
      <label for="check" class="menu-btn">
        <i class="fas fa-bars"></i>
      </label>
      <a href="index.html" class="logo">Nikita Gada</a>
      <ul class="navlinks">
        <li><a href="#" id="link1" onmousedown="myfun()" onmouseup="myfun2()">About Me</a></li>
        <li><a href="#" id="link3" onmousedown="myfun5()" onmouseup="myfun6()">Work</a></li>
        <li><a href="#" id="link2" onmousedown="myfun3()" onmouseup="myfun4()">Services</a></li>
        <li><a href="#" class="contact">Contact</a></li>
      </ul>
    </nav>
  </header>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70109023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档