为什么我的div图像没有出现在父div中?

时间:2018-06-04 22:01:19

标签: html css css3

我正在制作一个作者框。我希望这个人的照片在盒子的右边,同时仍然在父div中。无论我采用什么样的风格,我都不能让图片在div内部和右边顺利出现,这里有什么问题?



.authorBox {
    background: #222222;
    width: 100%;
    padding:1.5em 2em;
    position: relative;
    border-left:15px solid #d53362;
    box-sizing: border-box;
}

h5.author {
    color: #fff;
    font-weight: 600;
    font-size: 1.5em;
}

h5.authorRole {
    color: #d53362;
    font-weight: 600;
    font-size: 1.3em;
}

p.authorQuote {
    color:#444;
    font-style: italic;
    margin-top: 20px;
    font-size: 1.1em;
    line-height: 1.5em;
}

.personImg1 {
    width:100%;
    height:100%;
    background-size: cover;
    background-image: url(../img/person1.jpeg);
}

.personContainer {
    float:right;
}

<div class="authorBox">
       <h5 class="author">First Last</h5>
       <h5 class="authorRole">Job role goes here</h5>
       <p class="authorQuote">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu erat at nisl laoreet ultrices"</p>
       <div class="personContainer">
            <div class="personImg1"></div>
       </div>  
 </div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

使用弹性盒尝试此解决方案。

.authorBox {
  background: #222222;
  width: 100%;
  padding: 1.5em 2em;
  position: relative;
  border-left: 15px solid #d53362;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

h5.author {
  color: #fff;
  font-weight: 600;
  font-size: 1.5em;
}

h5.authorRole {
  color: #d53362;
  font-weight: 600;
  font-size: 1.3em;
}

p.authorQuote {
  color: #444;
  font-style: italic;
  margin-top: 20px;
  font-size: 1.1em;
  line-height: 1.5em;
}

.personImg1 {
  width: 100px;
  height: 100px;
  background-size: cover;
  background-image: url(../img/person1.jpeg);
}
<div class="authorBox">
  <div class="colContainer">
    <h5 class="author">First Last</h5>
    <h5 class="authorRole">Job role goes here</h5>
    <p class="authorQuote">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu erat
      at nisl laoreet ultrices"</p>
  </div>
  <div class="personContainer">
    <div class="personImg1"></div>
  </div>
</div>

答案 1 :(得分:0)

您是否正确地从文件夹中找到图像? 此外,你可以尝试这个

.personImg1 {
    display:block;
    position:absolute;
    background:url("../img/person1.jpeg");
    background-size: 100% 100%;
    background-repeat:no-repeat;
    width:100%;
    height:100%;
    float:right;
    bottom:0;
    right:0;

}

答案 2 :(得分:0)

首先,不会看到没有高度和宽度的div,所以你必须拥有一个定义的宽度/高度的div才能看到它。

然后你把它放在右边,绝对正确地定位:0或少量只是为了从边缘推动它。

&#13;
&#13;
*,
*:after,
*:before {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.authorBox {
  background: #222222;
  width: 100%;
  padding: 1.5em 2em;
  position: relative;
  border-left: 15px solid #d53362;
  box-sizing: border-box;
}

h5.author {
  color: #fff;
  font-weight: 600;
  font-size: 1.5em;
}

h5.authorRole {
  color: #d53362;
  font-weight: 600;
  font-size: 1.3em;
}

p.authorQuote {
  color: #444;
  font-style: italic;
  margin-top: 20px;
  font-size: 1.1em;
  line-height: 1.5em;
}

.personContainer {
  height: 100px;
  width: 300px;
  position: absolute;
  right: 10px;
  top: 1.5em;
}

.personImg1 {
  width: 100%;
  height: 100%;
  background-size: 100% 100%;
  background-image: url('http://via.placeholder.com/350x150');
}
&#13;
<div class="authorBox">

  <h5 class="author">First Last</h5>
  <h5 class="authorRole">Job role goes here</h5>
  <p class="authorQuote">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eu erat at nisl laoreet ultrices"
  </p>

  <div class="personContainer">
    <div class="personImg1"></div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您的.personImg1width:100%;height:100%;,这意味着它获取其父容器的完整宽度和高度 - 即相对设置。

但是父容器的唯一CSS属性是.personContainer { float:right; } - 它没有宽度或高度设置,导致此容器的{em>零高度,{{ 1}} DIV,因此也是.personImg1的背景图像。因此,您需要为.personImg1

指定一些宽度和高度

实际上,您可能需要考虑使用常规.personContainer标记而不是img DIV及其背景图片......