[英]Why is my div image not appearing inside parent div?
I am making an author box. 我正在制作一个作者框。 I want the person's picture at the right of the box while still inside the parent div.
我希望该人的照片在框右侧,而仍在父div内。 Whatever styles I apply, I can't make the picture appear inside the div and on the right smoothly, what is wrong here?
无论使用哪种样式,都无法使图片平滑地显示在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>
Try this solution using flex box. 使用弹性盒尝试此解决方案。
.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>
Are you locating your image properly from your folders? 您是否从文件夹中正确找到了图像? Also, you can try this
另外,您可以尝试一下
.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;
}
First of all a div with no height and width won't be seen, so you div with the background has to have a defined width/height so you can see it. 首先,不会显示没有高度和宽度的div,因此具有背景的div必须具有定义的宽度/高度,这样您才能看到它。
and you put in the right by positioning it absolute and right:0 or a small amount just to push it from the edge. 然后您将其放置在绝对位置,将其放置在绝对位置,并将其放置在正确的位置:: 0或少量,仅从边缘将其推出。
*, *: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'); }
<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>
Your .personImg1
has width:100%;
您的
.personImg1
width:100%;
and height:100%;
height:100%;
, which means it gets the full width and height of its parent container - ie relative settings. ,这意味着它将获取其父容器的完整宽度和高度-即相对设置。
But the parent container's only CSS property is .personContainer { float:right; }
但是父容器的唯一CSS属性是
.personContainer { float:right; }
.personContainer { float:right; }
– there are no width or height settings for it, resulting in zero height both for this container, the .personImg1
DIV and therefore also the background-image of .personImg1
. .personContainer { float:right; }
-有没有它的宽度或高度的设置,导致零高度既此容器中, .personImg1
DIV并因此也的背景图像.personImg1
。 So you need to assign some width and height to .personContainer
因此,您需要为
.personContainer
分配一些宽度和高度
Actually you might want to consider to just use a regular img
tag instead of that .personImg1
DIV and its background-image... 实际上,您可能要考虑只使用常规的
img
标签而不是该.personImg1
DIV及其背景图片...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.