我的CSS行为异常,将两个div合并到一个稍大的框中

时间:2018-10-09 03:02:48

标签: html css

我编写的代码有什么问题?以下是代码段。

html {
  background-image: url(back.png)
}
#main {
  text-align: center;
}
#visible {
  display: inline-block;
  background-color: #000c21;
    width: 80%;
    color: #c4c5c6;
    font-family: sans-serif;
    font-size:20;
}
html,body,#main,#visible { height:100%; }
.topnav {
  overflow: hidden;
  background-color: #182b3a;
  position: fixed;
  top: 0;
  width: 100%;
}

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

.topnav a:hover,.dropdown:hover .dropbtn {
  background-color: #1a3c56;
  color: white;
}

.topnav a.active {
  background-color: #1a3c56;
  color: white;
}
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #182b3a;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}
.dropdown {
    float: left;
    overflow: hidden;
}

.dropdown .dropbtn {
    font-size: 16px;    
    border: none;
    outline: none;
    color: white;
    padding: 14px 16px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;
}

.dropdown:hover .dropdown-content {
    display: block;
}
<html>
<head>
<link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#pfft">herpiderp</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
</div>
    <div id=main>
      <div id=visible>
          herpiderp
      </div>
    </div>

</body>
</html>

我的下拉菜单元素窃取了我的主div,为什么? 我需要将其作为适当的下拉元素,但是如果找到解决方法,我将永远不知道为什么会首先出现这种情况。
我正在建立一个机器人技术网站,并希望创建一个下拉菜单,但是我得到了这种奇怪的效果。谁能帮我吗?

3 个答案:

答案 0 :(得分:1)

消除溢出:隐藏; .topnav类中的属性

答案 1 :(得分:1)

是我还是您在这里有一个露天办公室?我认为您忘记了关闭div。试试这个

    <div class="topnav">
        <a class="active" href="#home">Home</a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
        <a href="#pfft">herpiderp</a>
        <div class="dropdown">
           <button class="dropbtn">Dropdown 
              <i class="fa fa-caret-down"></i>
           </button>
           <div class="dropdown-content">
              <a href="#">Link 1</a>
              <a href="#">Link 2</a>
              <a href="#">Link 3</a>
           </div>
       </div>
   </div>
   <div id="main">
       <div id="visible">
          herpiderp
       </div>
   </div>

@Romel Indemne也建议您忽略双引号。

答案 2 :(得分:-2)

html {
  background-image: url(back.png)
}

#main {
  text-align: center;
}

#visible {
  display: inline-block;
  background-color: #000c21;
  width: 80%;
  color: #c4c5c6;
  font-family: sans-serif;
  font-size: 20;
}

html,
body {
  min-height: 100vh;
}
#main,
#visible {
  height: 100%;
}

.topnav {
  background-color: #182b3a;
  position: fixed;
  top: 0;
  width: 100%;
}

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

.topnav a:hover,
.dropdown:hover .dropbtn {
  background-color: #1a3c56;
  color: white;
}

.topnav a.active {
  background-color: #1a3c56;
  color: white;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #182b3a;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 3;
}

.dropdown {
  float: left;
  position: relative;
}

.dropdown .dropbtn {
  font-size: 16px;
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#pfft">herpiderp</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
    <div id="main">
      <div id="visible">
        herpiderp
      </div>
    </div>
  </div>
</div>