【问题标题】:How to make a responsive button menu如何制作响应式按钮菜单
【发布时间】:2020-07-17 15:08:14
【问题描述】:

我有几个页面具有以下配置: div 中的背景图像(居中)和垂直菜单(按钮)。 => https://ibb.co/3Cmf0dt

div-menu,将被放置在右侧,但取决于一个页面到另一个页面,一次居中,一次稍高。

我希望得到您对 div 菜单的 CSS 的帮助。 与背景图像相比,它如何定位以便缩小(使用按钮)并始终保持在同一位置?

这是我的代码(但按钮不缩小,div 位置指的是页面而不是图像背景):

=> https://ibb.co/9cQL1yD

提前致谢!!

【问题讨论】:

  • 请不要将您的代码添加为图像,否则我们无法复制和调试它。试着做一个片段
  • 对不起,我不知道如何在正文中添加消息 :-( 但我不搜索如何做一个菜单栏,而是一个带有 3 个垂直按钮的 div 响应和定位在页面中的特殊位置

标签: html css button menu position


【解决方案1】:

在选项卡中打开它,然后调整窗口大小。

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #4CAF50;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {display: none;}
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>

<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    <i class="fa fa-bars">&times;</i>
  </a>
</div>

<div style="padding-left:16px">
  <h2>Responsive Topnav Example</h2>
  <p>Resize the browser window to see how it works.</p>
</div>


</body>
</html>

这是链接:https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_topnav

【讨论】:

    猜你喜欢
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多