如何避免元素重叠

时间:2017-09-14 04:15:09

标签: html css

Demo

enter image description here

我注意到有些时候元素重叠我不知道为什么。我已经从顶部元素(p标签)中删除了边距,所以这不是边缘崩溃。那实际上是什么?

我可以通过添加display:inline-block来解决这个问题。但有没有更好的方法可以避免这种情况?



.ui-btn {
  border: 2px solid #ffffff;
  border-radius: 30px;
  background-color: #18aff4;
  font-weight: bold;
  text-align: center;
  font-size: 18px;
  color: white;
  padding: 18px;
}

p {
  margin: 0;
  padding: 0;
}

<div class="text">
  <p ">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

  <a class="ui-btn ">Sign up</a>
</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

您可以将display: inline-block;添加到.ui-btn以在段落后显示。

<style type="text/css">
.ui-btn {
  border: 2px solid #ffffff;
  border-radius: 30px;
  background-color: #18aff4;
  font-weight: bold;
  text-align: center;
  font-size: 18px;
  color: white;
  padding: 18px;
  display: inline-block;
}
p{
  margin: 0;
  padding:0;
}
</style>

<div class="text">
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

  <a class="ui-btn">sign up</a>
</div>

答案 1 :(得分:2)

您可以使用任何margin-bottomfloatinline-block,但</br>也适用。

.ui-btn {
  border: 2px solid #ffffff;
  border-radius: 30px;
  background-color: #18aff4;
  font-weight: bold;
  text-align: center;
  font-size: 18px;
  color: white;
  padding: 18px;
}

p {
  margin: 0;
  padding: 0;
}
<div class="text">
  <p ">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</br>
  <a class="ui-btn ">Sign up</a>
</div>

答案 2 :(得分:0)

.ui-btn {
  border: 2px solid #ffffff;
  border-radius: 30px;
  background-color: #18aff4;
  font-weight: bold;
  text-align: center;
  font-size: 18px;
  color: white;
  padding: 18px;
}
p{
  margin:0;
  padding:0;
  margin-bottom:40px;
}
<div class="text">
  <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
    has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
    publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

  <a class="ui-btn">sign up</a>
</div>

<a>标记上的填充不算作空格。因此,在p{margin-bottom:40px;}添加<p>会在它之间产生差距。

答案 3 :(得分:0)

锚标记是内联标记,默认情况下为float:left;在css

.ui-btn {
  border: 2px solid #ffffff;
  border-radius: 30px;
  background-color: #18aff4;
  font-weight: bold;
  text-align: center;
  font-size: 18px;
  color: white;
  padding: 18px;
  float: left;
}