HTML/CSS:当屏幕尺寸低于

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

我有一个动画导航栏,当屏幕尺寸降至某个点以下时,我希望将其从水平更改为垂直。我尝试添加图像,但该帖子一直坚称我的代码格式不正确,所以我只是复制并粘贴了我的代码。第一部分是导航栏 html,第二部分是导航栏 CSS,第三部分是媒体查询 css

我尝试将媒体查询和浮动设置为无。

  nav {
  margin: 27px auto 0;
  position: relative;
  width: 495px;
  height: 50px;
  background-color: #7dabd9;
  border-radius: 8px;
  font-size: 0;
}

nav a {
  line-height: 50px;
  height: 100%;
  font-size: 15px;
  display: inline-block;
  position: relative;
  z-index: 1;
  text-decoration: none;
  text-transform: uppercase;
  text-align: center;
  color: white;
  cursor: pointer;
}

nav .animation {
  position: absolute;
  height: 100%;
  top: 0;
  z-index: 0;
  transition: all .5s ease 0s;
  border-radius: 8px;
}

a:nth-child(1) {
  width: 100px;
}

a:nth-child(2) {
  width: 110px;
}

a:nth-child(3) {
  width: 100px;
}

a:nth-child(4) {
  width: 160px;
}

a:nth-child(5) {
  width: 120px;
}

nav .start-home,
a:nth-child(1):hover~.animation {
  width: 100px;
  left: 0;
  background-color: #1abc9c;
}

nav .start-about,
a:nth-child(2):hover~.animation {
  width: 110px;
  left: 100px;
  background-color: #e74c3c;
}

nav .start-blog,
a:nth-child(3):hover~.animation {
  width: 100px;
  left: 210px;
  background-color: #3498db;
}

nav .start-portefolio,
a:nth-child(4):hover~.animation {
  width: 160px;
  left: 310px;
  background-color: #9b59b6;
}

nav .start-contact,
a:nth-child(5):hover~.animation {
  width: 120px;
  left: 470px;
  background-color: #e67e22;
}

'     
 main {
  box-sizing: border-box;
  color: black;
  background-color: #7dabd9;
  float: none;
  width: 100vw;
  clear: both;
}

aside {
  float: none;
  width: 100vw;
}


}
@media screen and (min-width:500px) and (max-width:799px) {
  header,
  footer {
    width: 100vw;
  }
  
  main {
    box-sizing: border-box;
    color: black;
    background-color: #7dabd9;
    float: none;
    width: 100vw;
    clear: both;
  }
  
  aside {
    display: none;
    float: none;
    width: 100vw;
  }
  
  @media screen and (min-width:50px) and (max-width:499px) {
    header,
    footer {
      width: 100vw;
    }
    main {
      box-sizing: border-box;
      color: black;
      background-color: #7dabd9;
      float: none;
      width: 90vw;
      clear: both;
    }
    aside {
      display: none;
      float: none;
      width: 90vw;
    }
  }
<nav>
  <a href="home.html">Home</a>
  <a href="about.html">About Us</a>
  <a href="products.html">Products</a>
  <a href="contact.html">Contact Us</a>
  <div class="animation start-home"></div>
</nav>

html css media-queries navbar
1个回答
0
投票

使用flexbox

flex-direction
属性允许您在水平和垂直布局之间轻松切换。

Flexbox 有两种风格:

flex
inline-flex
,其行为基本上类似于
block
inline-block
。我在这段代码中使用了
inline-flex
,但请尝试一下并选择您最喜欢的一个。

nav {
  background-color: #7dabd9;
  border-radius: 8px;
  display: inline-flex;
  flex-direction: column;
  gap: 1em;
  padding: 1em;
}

@media (min-width: 500px) {
  nav {
    flex-direction: row;
  }
}

nav a {
  font-size: 15px;
  display: block;
  text-decoration: none;
  text-transform: uppercase;
  color: white;
}
  
<nav>
  <a href="home.html">Home</a>
  <a href="about.html">About Us</a>
  <a href="products.html">Products</a>
  <a href="contact.html">Contact Us</a>
</nav>

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