带有下拉菜单的响应式导航栏

问题描述 投票:0回答:1

如何使该导航栏同时适应中小屏幕。我不断得到不好的结果。下面是目前为止编写的 html 代码和 CSS 代码。

nav {
  background: black;
  height: 5rem;
  width: 100%;
  display: grid;
  place-items: center;
}

.nav_container {
  display: flex;
  align-items: center;
  position: relative;
}

.menu-left {
  display: flex;
  align-items: center;
  margin-left: 4rem;
  gap: 6rem;
  position: relative;
}

.menu-right {
  display: flex;
  align-items: center;
  margin-left: auto;
  gap: 3rem;
}

menu-item .dropdown-menu {
  /* display: flex;
    flex-direction: column;  */
  gap: 0.5rem;
  position: absolute;
  margin-top: 2rem;
  background: var(--White);
  width: 200px;
  border-radius: 0.5rem;
  display: none;
}

.dropdown-menu li a {
  color: var(--Very-dark-blue);
}
   <ul class="menu-left">
      <li class="menu-item"><a href="#">Product <img src="images/icon-arrow-light.svg"   alt=""></a>
        <ul class="dropdown-menu">
          <li><a href="#">Overview</a></li>
          <li><a href="#">Pricing</a></li>
          <li><a href="#">Marketplace</a></li>
          <li><a href="#">Features</a></li>
          <li><a href="#">Integrations</a></li>
        </ul>
      </li>

      <li class="menu-item"><a href="#">Company <img src="images/icon-arrow-light.svg" alt=""></a>
        <ul class="dropdown-menu ">
          <li><a href="#">About</a></li>
          <li><a href="#">Team</a></li>
          <li><a href="#">Blog</a></li>
          <li><a href="#">Careers</a></li>
        </ul>
      </li>

      <li class="menu-item"><a href="#">Connect<img src="images/icon-arrow-light.svg" alt=""></a>
        <ul class="dropdown-menu">
          <li><a href="#">Contact</a></li>
          <li><a href="#">Newsletter</a></li>
          <li><a href="#">LinkedIn</a></li>
        </ul>
      </li>
    </ul>
    <ul class="menu-right">
      <li><a href="#">Login</a></li>
      <li><a href="#" class="btn-color">Sign Up</a></li>
    </ul>
    <button class="toggle-btn"><img src="images/icon-hamburger.svg" alt=""></button>
    <button class="toggle-btn"><img src="images/icon-close.svg" alt=""></button>
  </div>
</nav>

javascript html css navbar responsive-nav
1个回答
0
投票

考虑使用 CSS Reset 来完全重置你的 CSS。这将允许您从头开始创建自己的设计,并防止默认样式干扰。

响应能力所需的只是断点。听起来您的目标是拥有一个移动响应网站。通常,您首先启动移动设备,然后再升级到桌面设备。这意味着首先要设计您的网站以支持移动设备。然后,完成后,您可以转向平板电脑、笔记本电脑,然后是台式机。

我对你的代码所做的是:

  1. 添加 CSS 重置
  2. 删除现有的 CSS 并将其设计为在移动设备中显示所有菜单项 查看
  3. 插入笔记本电脑断点(~769px),在断点中我更新了导航以具有悬停效果。

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}


/* HTML5 display-role reset for older browsers */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}

body {
  line-height: 1;
}

ol,
ul {
  list-style: none;
}

blockquote,
q {
  quotes: none;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}


/* END CSS RESET */

nav {
  display: grid;
  grid-template: 1fr auto / 1fr;
  gap: 10px;
  font-size: 1.2rem;
  justify-content: center;
}

.menu-left,
.menu-right {
  display: flex;
  flex-flow: column nowrap;
  text-align: center;
  gap: 15px;
}

.dropdown-menu {
  display: flex;
  flex-flow: column nowrap;
  text-align: center;
  gap: 3px;
}

.dropdown-menu li {
  border: 1px solid #00000042;
  padding: 2px;
}

.dropdown-menu li a {
  text-decoration: none;
  color: black;
  display: block;
  width: 100%;
  height: 100%;
}

.dropdown-menu li:hover {
  border: 1px solid black;
}


/* Extra small devices (phones, 600px and down) */

@media only screen and (min-width: 769px) {
  nav {
    display: grid;
    grid-template: 1fr / 1fr auto;
    font-size: 1;
  }
  .menu-left,
  .menu-right {
    flex-flow: row nowrap;
    gap: 10px;
  }
  .dropdown-menu {
    display: none;
    padding: 0;
  }
  .menu-item:hover .dropdown-menu {
    display: flex;
    position: absolute;
    gap: 1px;
  }
}
<nav>
  <ul class="menu-left">
    <li class="menu-item"><a href="#">Product</a>
      <ul class="dropdown-menu">
        <li><a href="#">Overview</a></li>
        <li><a href="#">Pricing</a></li>
        <li><a href="#">Marketplace</a></li>
        <li><a href="#">Features</a></li>
        <li><a href="#">Integrations</a></li>
      </ul>
    </li>

    <li class="menu-item"><a href="#">Company</a>
      <ul class="dropdown-menu ">
        <li><a href="#">About</a></li>
        <li><a href="#">Team</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="#">Careers</a></li>
      </ul>
    </li>

    <li class="menu-item"><a href="#">Connect</a>
      <ul class="dropdown-menu">
        <li><a href="#">Contact</a></li>
        <li><a href="#">Newsletter</a></li>
        <li><a href="#">LinkedIn</a></li>
      </ul>
    </li>
  </ul>
  <ul class="menu-right">
    <li><a href="#">Login</a></li>
    <li><a href="#" class="btn-color">Sign Up</a></li>
  </ul>
</nav>

© www.soinside.com 2019 - 2024. All rights reserved.