CSS最小化窗口更改外观

时间:2017-04-11 14:29:56

标签: html css

我的菜单有问题。当我更改浏览器窗口大小时,我的菜单从水平变为垂直。是否有任何选项可以在窗口中使文本静态?即使我更改窗口大小,我希望我的菜单保持相同的状态。这是我的代码:

nav {
  position: absolute;
  margin-top: 288px;
  margin-left: 378px;
  height: 25px;
  z-index: 2;
}

nav>ul>li {
  font-size: 20px;
  color: white;
  padding: 10px 40px;
  display: inline-block;
  z-index: 2;
}

nav>ul>li>a {
  text-decoration: none;
  color: rgba(0, 0, 0, 0.35);
  transition: all linear 0.15s;
}

nav>ul>.current-item>a {
  background-color: rgba(0, 0, 0, 0.35);
  text-align: center;
  color: white;
  padding: 3px 22px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  width: 50px;
  height: 28px;
  z-index: 2;
  text-decoration: none;
}

nav>ul>li:hover>a {
  background-color: rgba(0, 0, 0, 0.35);
  text-align: center;
  color: white;
  padding: 3px 22px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  width: 50px;
  height: 28px;
  z-index: 2;
}
<nav>
  <ul>
    <li class="current-item"><a href="index.php"> HOME </a></li>
    <li><a href="profile.php">PROFILE</a></li>
    <li><a href="contact.php">CONTACT</a></li>
  </ul>
</nav>

4 个答案:

答案 0 :(得分:0)

您可以将white-space: nowrap;添加到nav媒体资源中,如下例所示:

nav {
  position: absolute;
  margin-top: 288px;
  margin-left: 378px;
  height: 25px;
  z-index: 2;
  white-space: nowrap;
}

其他几种方法:

  • display: inline-block添加到nav媒体资源。
  • nav属性中设置导航菜单长度的幻数(定义的数字)(例如width: 650px;min-width:650px;),以便浏览器不会影响它。这个选项通常不被认为是一种好的做法,但根据项目的需要,这可能会被忽视。

答案 1 :(得分:0)

因此,这是一个基于您的代码的可能解决方案。

https://jsfiddle.net/pablodarde/zmh7tyj6/

nav {
  position: absolute;
  margin-top: 288px;
  margin-left: 378px;
  height: 25px;
  z-index: 2;
}

nav>ul {
  display: table;
  margin: 0 auto;
  padding: 0;
  max-width: 650px;
}

nav>ul>li {
  display: table-cell;
  position: relative;
  width: 100%;
  max-width: 100px;
  font-size: 20px;
  color: white;
  padding: 10px 40px;
}

nav>ul>li>a {
  text-decoration: none;
  color: rgba(0, 0, 0, 0.35);
  transition: all linear 0.15s;
}

nav>ul>.current-item>a {
  background-color: rgba(0, 0, 0, 0.35);
  text-align: center;
  color: white;
  padding: 3px 22px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  width: 100%;
  max-width: 50px;
  height: 28px;
  z-index: 2;
  text-decoration: none;
}

nav>ul>li:hover>a {
  background-color: rgba(0, 0, 0, 0.35);
  text-align: center;
  color: white;
  padding: 3px 22px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  width: 100%;
  max-width: 50px;
  height: 28px;
  z-index: 2;
}

但是,我推荐这样的东西,更优雅。

https://jsfiddle.net/pablodarde/tenueqe6/

答案 2 :(得分:0)

在导航中使用min-width,例如min-width:12OOpx;

答案 3 :(得分:0)

nav {
  float:right; // float is cleaner
  width: 650px; // set to your values
  min-width: 650px; // set to your values
  height: 25px;
  z-index: 2;
}

演示:

&#13;
&#13;
nav {
  float:right;
  width: 650px;
  min-width: 650px;
  height: 25px;
  z-index: 2;
}

nav>ul>li {
  font-size: 20px;
  color: white;
  padding: 10px 40px;
  display: inline-block;
  z-index: 2;
}

nav>ul>li>a {
  text-decoration: none;
  color: rgba(0, 0, 0, 0.35);
  transition: all linear 0.15s;
}

nav>ul>.current-item>a {
  background-color: rgba(0, 0, 0, 0.35);
  text-align: center;
  color: white;
  padding: 3px 22px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  width: 50px;
  height: 28px;
  z-index: 2;
  text-decoration: none;
}

nav>ul>li:hover>a {
  background-color: rgba(0, 0, 0, 0.35);
  text-align: center;
  color: white;
  padding: 3px 22px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  width: 50px;
  height: 28px;
  z-index: 2;
}
&#13;
<nav>
  <ul>
    <li class="current-item"><a href="index.php"> HOME </a></li>
    <li><a href="profile.php">PROFILE</a></li>
    <li><a href="contact.php">CONTACT</a></li>
  </ul>
</nav>
&#13;
&#13;
&#13;

有几种方法可以做到这一点;最终,您需要定义width:。此外,position: absolute;通常不需要这样的东西;你也可以margin:漂浮。