在相对div内切断绝对div高度

时间:2018-05-24 03:40:10

标签: html css

我无法正确显示我的一个绝对定位的div。除非我为父(相对)div设置一个高度,否则它会被切断,我无法知道所需的高度。

我最初没有使用任何绝对定位,但comm div的内容包裹在avatar div下面,所以我认为绝对定位会修复它并且除了文字在底部被切断。

这是结构,你可以看到this fiddle中发生了什么。



body {
  background: #212121;
  font-family: arial;
}

.main {
  width: 80%;
  margin: 20px auto;
}

.wrap {
  width: 100%;
  background: white;
  padding: 10px;
  overflow: hidden;
  position: relative;
}

a.avatar {
  width: 45px;
  height: 45px;
  border-radius: 50%;
  overflow: hidden;
  display: inline-block;
  float: left;
  margin-right: 10px;
  position: relative;
}

a.avatar img {
  width: 100%;
}

.top {
  display: block;
  overflow: hidden;
  position: absolute;
  left: 70px;
}

a.name {
  font-weight: bold;
  margin-right: 10px;
  float: left;
}

.posted {
  color: #BBB;
  font-size: 12px;
  padding-top: 3px;
  float: left;
}

p.comm {
  display: block;
  position: absolute;
  left: 70px;
  top: 20px;
  height: 100%;
}

<div class="main">
  <div class="wrap">
    <a href="https://example.com" class="avatar"><img src="http://www.tangoflooring.ca/wp-content/uploads/2015/07/user-avatar-placeholder.png" /></a>
    <div class="top">
      <a href="https://example.com" class="name">John Doe</a>
      <div class="posted">8 mins ago</div>
    </div>
    <p class="comm">Lorem ipsum dolor sit amet.
    </p>
  </div>
  <div class="wrap">
    <a href="https://example.com" class="avatar"><img src="http://www.tangoflooring.ca/wp-content/uploads/2015/07/user-avatar-placeholder.png" /></a>
    <div class="top">
      <a href="https://example.com" class="name">Jane Doe</a>
      <div class="posted">11 mins ago</div>
    </div>
    <p class="comm">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </p>
  </div>
  <div class="wrap">
    <a href="https://example.com" class="avatar"><img src="http://www.tangoflooring.ca/wp-content/uploads/2015/07/user-avatar-placeholder.png" /></a>
    <div class="top">
      <a href="https://example.com" class="name">Jimmy Doe</a>
      <div class="posted">18 mins ago</div>
    </div>
    <p class="comm">Ut enim ad minim veniam.
    </p>
  </div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

尝试使用此修改过的css代码。

body {
  background: #212121;
  font-family: arial;
}

.main {
  width: 80%;
  margin: 20px auto;
}

.wrap {
  width: 100%;
  background: white;
  padding: 10px;
  position: relative;
}

a.avatar {
  width: 45px;
  height: 45px;
  border-radius: 50%;
  overflow: hidden;
  display: inline-block;
  float: left;
  margin-right: 10px;
  position: relative;
}

a.avatar img {
  width: 100%;
}

.top {
  display: flex;
  margin-left: 70px;
}

a.name {
  font-weight: bold;
  margin-right: 10px;
}

.posted {
  color: #BBB;
  font-size: 12px;
  padding-top: 3px;
}

p.comm {
  display: block;
  margin-left: 70px;
  height: 100%;
}

答案 1 :(得分:1)

您需要将.top.comm包裹在div中并使用flex来实现它

&#13;
&#13;
body {
  background: #212121;
  font-family: arial;
}

.main {
  width: 80%;
  margin:20px auto;
}

.wrap {
  width: 100%;
  background: white;
  padding: 10px;
  overflow: hidden;
  position: relative;
  display: flex;
}

a.avatar {
  margin-right: 10px;
}
a.avatar img {
  width: 45px;
  height: 45px;
  border-radius: 50%;
}

.top {
  width: 100%;
  overflow: hidden;
}

a.name {
  font-weight: bold;
  margin-right: 10px;
  float: left;
}

.posted {
  color: #BBB;
  font-size: 12px;
  padding-top: 3px;
  float: left;
}

p.comm {
  margin-top: 5px;
}
.right {
  max-width: 88%;
}
&#13;
<div class="main">

    <div class="wrap">
    <a href="https://example.com" class="avatar"><img src="http://www.tangoflooring.ca/wp-content/uploads/2015/07/user-avatar-placeholder.png"></a>
    <div class="right">
    	<div class="top">
          <a href="https://example.com" class="name">Jane Doe</a>
          <div class="posted">11 mins ago</div>
        </div>
    <p class="comm">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do </p>
    </div>
  </div>

	  <div class="wrap">
    <a href="https://example.com" class="avatar"><img src="http://www.tangoflooring.ca/wp-content/uploads/2015/07/user-avatar-placeholder.png"></a>
    <div class="right">
    	<div class="top">
          <a href="https://example.com" class="name">Jane Doe</a>
          <div class="posted">11 mins ago</div>
        </div>
    <p class="comm">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>
  </div>
    
    <div class="wrap">
    <a href="https://example.com" class="avatar"><img src="http://www.tangoflooring.ca/wp-content/uploads/2015/07/user-avatar-placeholder.png"></a>
    <div class="right">
    	<div class="top">
          <a href="https://example.com" class="name">Jane Doe</a>
          <div class="posted">11 mins ago</div>
        </div>
    <p class="comm">Lorem ipsum dolor sit amet</p>
    </div>
  </div>

</div>
&#13;
&#13;
&#13;

工作小提琴here

答案 2 :(得分:0)

只需修改您的p.comm选择器,如下所示:

p.comm {
    display: block;
    position: relative;
    top: 10px;
    padding-left: 60px;
}