为什么li元素与`block`显示重叠?

时间:2016-08-26 10:52:18

标签: html css responsive-design

我正在尝试创建一个响应式菜单,如下所示。

.menu {
  margin: auto;
  text-align: center;
  margin-top: 10px;
  padding: 10px;
}
.menu ul {
  list-style-type: none;
  padding-left: 0;
}
.menu li {
  display: inline;
}
.menu li a {
  border: 1px solid #eee;
  padding: 10px 15px;
  text-transform: uppercase;
  transition: background 0.5s ease;
}
.menu li a:focus {
  text-decoration: none;
}
.menu li a:hover {
  text-decoration: none;
  background: #ddd;
}
@media (max-width: 620px) {
  .menu li {
    display: block;
    width: 100%;
  }
}
<div class="menu">
  <ul>
    <li><a href="#/home">Home</a>
    </li>
    <li><a href="#/experience">Experience</a>
    </li>
    <li><a href="#/education">Education</a>
    </li>
    <li><a href="#/opensource">Open Source</a>
    </li>
    <li><a href="#/contact">Contact</a>
    </li>
  </ul>
</div>

但菜单条目在小屏幕上重叠。为什么会出现这个问题?此外,我已将宽度设置为100%,但条目不会按预期在小屏幕中更改大小。

2 个答案:

答案 0 :(得分:2)

您需要为锚标记

设置display:inline-block
.menu li a {
  display: inline-block;
}

&#13;
&#13;
.menu {
  margin: auto;
  text-align: center;
  margin-top: 10px;
  padding: 10px;
}
.menu ul {
  list-style-type: none;
  padding-left: 0;
}
.menu li {
  display: inline;
}
.menu li a {
  border: 1px solid #eee;
  padding: 10px 15px;
  text-transform: uppercase;
  transition: background 0.5s ease;
  display: inline-block;
}
.menu li a:focus {
  text-decoration: none;
}
.menu li a:hover {
  text-decoration: none;
  background: #ddd;
}
@media (max-width: 620px) {
  .menu li {
    display: block;
  }
}
&#13;
<div class="menu">
  <ul>
    <li><a href="#/home">Home</a>
    </li>
    <li><a href="#/experience">Experience</a>
    </li>
    <li><a href="#/education">Education</a>
    </li>
    <li><a href="#/opensource">Open Source</a>
    </li>
    <li><a href="#/contact">Contact</a>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
.menu {
  margin: auto;
  text-align: center;
  margin-top: 10px;
  padding: 10px;
}
.menu ul {
  list-style-type: none;
  padding-left: 0;
}
.menu li {
  display: inline;
}
.menu li a {
  border: 1px solid #eee;
  padding: 10px 15px;
  text-transform: uppercase;
  transition: background 0.5s ease;
  display:block;
}
.menu li a:focus {
  text-decoration: none;
}
.menu li a:hover {
  text-decoration: none;
  background: #ddd;
}
@media (max-width: 620px) {
  .menu li {
    display: block;
    width: 100%;
  }
}
&#13;
<div class="menu">
  <ul>
    <li><a href="#/home">Home</a>
    </li>
    <li><a href="#/experience">Experience</a>
    </li>
    <li><a href="#/education">Education</a>
    </li>
    <li><a href="#/opensource">Open Source</a>
    </li>
    <li><a href="#/contact">Contact</a>
    </li>
  </ul>
</div>
&#13;
&#13;
&#13;

默认情况下,锚具有display: inline属性。如果您需要全宽度,则需要将其设置为display:block,或者将其设置为display:inline-block以根据文本获取大小。