简体   繁体   English

使导航菜单响应

[英]make navigation menu responsive

When viewing my website from different screen sizes the menu going out of the screen. 从不同屏幕尺寸查看我的网站时,菜单会从屏幕上移出。 how to make it responsive? 如何使其响应? here is the link to the site I have added the html and css for your reference. 这是我添加了html和css供您参考的网站链接。 Thanks. 谢谢。

html html

<div class="wrapper">
  <p class="menu">
    <a href="#" class="animBtn themeB hastip" title="<h2>Who We Are</h2>">Who We Are</a>

    <a href="#" class="animBtn themeB hastip" title="<h2>What We Do</h2>"Loree Do</a>

    <a href="/forum" class="animBtn themeB hastip" title="<h2>Step Into Our Community</h2>">Forum</a>

    <a href="#" class="animBtn themeB">Contact US</a>
  </p>
</div>

css 的CSS

.menu {
  position: absolute;
  top: 50%;
  height: 300px;
  margin-left: 950px;
  margin-top: 10px;
}

a.animBtn:link, a.animBtn:visited {
  position: relative;
  display: block;
  width: 220px;
  margin: 10px auto 0;
  border-radius: 10px;
  font-size: 21px !important;
  padding: 14px 15px;
  border: 2px solid #608B82;
  background: rgba(255, 255, 255, 0.15);
  color: rgba(159,192,181,1);
  text-align: center;
  text-decoration: none;
  text-transform: note;
  overflow: hidden;
  letter-spacing: .08em;
  text-shadow: 0 0 1px rgba(0, 0, 0, 0.2), 0 1px 0 rgba(0, 0, 0, 0.2);
  -webkit-transition: all .3s ease;
  -moz-transition: all .3s ease;
  -o-transition: all .3s ease;
  transition: all .3s ease;
}
a.animBtn:link:after, a.animBtn:visited:after {
  content: "";
  position: absolute;
  background: none repeat scroll 0 0 rgba(255, 255, 255, 0.1);
  height: 0%;
  left: 50%;
  top: 50%;
  width: 100%;
  z-index: -1;
  -webkit-transition: all .3s ease 0s;
  -moz-transition: all .3s ease 0s;
  -o-transition: all .3s ease 0s;
  transition: all .3s ease 0s;
}
a.animBtn:link:hover, a.animBtn:visited:hover {
  color: rgba(159,192,181,1);
  text-shadow: none;
}
a.animBtn:link:hover:after, a.animBtn:visited:hover:after {
  height: 420%;
}

a.animBtn.themeB:after {
  -moz-transform: translateX(-50%) translateY(-50%) rotate(45deg);
  -ms-transform: translateX(-50%) translateY(-50%) rotate(45deg);
  -webkit-transform: translateX(-50%) translateY(-50%) rotate(45deg);
  transform: translateX(-50%) translateY(-50%) rotate(45deg);
}

There is more than one solution to this problem. 解决此问题的方法不止一种。 In your case you might want to try using CSS3 media rules . 您可能需要尝试使用CSS3媒体规则

If we look at your CSS for the menu: 如果我们在菜单中查看CSS:

.menu {
  position: absolute;
  top: 50%;
  height: 300px;
  margin-left: 950px;
  margin-top: 10px;
}

The margin-left is pushing your menu out of smaller screens. 左边距将菜单推出较小的屏幕。 So for smaller screens you want a smaller margin. 因此,对于较小的屏幕,您需要较小的边距。 You can set this by appending the following rule to your CSS code: 您可以通过将以下规则附加到CSS代码来进行设置:

@media all and (max-width: 600px) {
    .menu {
        margin-left: 250px;
    }
}

This will override your menu styling if the browser detects a screensize smaller than 600px. 如果浏览器检测到小于600像素的屏幕尺寸,则它将覆盖菜单样式。

I saw W3schools is also offering some other solutions over here . 我看到W3schools还在这里提供其他一些解决方案。

Okay. 好的。 Here we go. 开始了。 The below code is for just for mobile and screen larger than mobile. 以下代码仅适用于移动设备,且屏幕大于移动设备。 But for fluid layout which work in every standard device, you may need to apply media screen for all standard devices. 但是,对于在每个标准设备中都能使用的流畅布局,您可能需要为所有标准设备应用媒体屏幕。

Here is the HTML 这是HTML

<p class="menu">
<a href="#" class="animBtn themeB hastip" title="">Who We Are</a>

<a href="#" class="animBtn themeB hastip" title="">What We Do</a>

<a href="/forum" class="animBtn themeB hastip" title="">Forum</a>

<a href="#" class="animBtn themeB">Contact US</a>
</p>

CSS 的CSS

@media screen and (max-width:320px){ /*For Mobile*/
.menu {
position: absolute;
top: 50%;
height: 300px;
margin:auto;
margin-top: 10px;
}

@media screen and (min-width:321px){ /* for screen larger than mobile*/
.menu {
 position: absolute;
top: 50%;
height: 300px;
margin-left: 70%;
margin-top: 10px;
}
}

Customize the position for each screen as per your need. 根据需要自定义每个屏幕的位置。 Here is the DEMO Do come back if any issue. 这里是DEMO待办事项回来,如果任何问题。 Hope this help you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM