简体   繁体   English

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

[英]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及其背景图片...

为什么背景图片没有出现在我的内部<div> ?</div><div id="text_translate"><p> 我试图为我的 web 页面的这一部分放置一个背景图像,我将我的图像作为一个 id(水果),但它没有出现。 这是我的代码:</p><pre> &lt;div class="p-1 text-center bg-light" id="fruit"&gt; &lt;div class="col-md-6 mx-auto my-5"&gt; &lt;h1 class="display-4 font-weight-normal"&gt;TrendyEats&lt;/h1&gt; &lt;p class="lead font-weight-normal"&gt;The Latest Food Trends&lt;/p&gt; &lt;a class="btn btn-outline-secondary" href="#"&gt;Login&lt;/a&gt; &lt;p class="small pt-4"&gt;blablabla &lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 水果 id 的 css 是:</p><pre> #fruit { background-image: url('/Pictures/fruits.jpg'); alt:"woops"; }</pre><p> “alt”也没有出现。</p><p> 任何帮助表示赞赏,谢谢!</p><p> 编辑:已修复,看来我必须写“Pictures/fruits.jpg”而不是“/Pictures/fruits.jpg”</p></div> - Why isn't the background image appearing inside my <div>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么背景图片没有出现在我的内部<div> ?</div><div id="text_translate"><p> 我试图为我的 web 页面的这一部分放置一个背景图像,我将我的图像作为一个 id(水果),但它没有出现。 这是我的代码:</p><pre> &lt;div class="p-1 text-center bg-light" id="fruit"&gt; &lt;div class="col-md-6 mx-auto my-5"&gt; &lt;h1 class="display-4 font-weight-normal"&gt;TrendyEats&lt;/h1&gt; &lt;p class="lead font-weight-normal"&gt;The Latest Food Trends&lt;/p&gt; &lt;a class="btn btn-outline-secondary" href="#"&gt;Login&lt;/a&gt; &lt;p class="small pt-4"&gt;blablabla &lt;/p&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 水果 id 的 css 是:</p><pre> #fruit { background-image: url('/Pictures/fruits.jpg'); alt:"woops"; }</pre><p> “alt”也没有出现。</p><p> 任何帮助表示赞赏,谢谢!</p><p> 编辑:已修复,看来我必须写“Pictures/fruits.jpg”而不是“/Pictures/fruits.jpg”</p></div> - Why isn't the background image appearing inside my <div>? 图像没有出现在 div 内 - image not appearing inside of div 为什么我的孩子div不会留在父div中? - Why won't my child div stay inside the parent div? div内的div没有出现? - Div inside a div not appearing? 为什么我的背景CSS图像没有出现在DIV中? - Why isn't my background CSS image appearing in DIV? DIV位于父级DIV内部的图像之上 - DIV on top of image, both inside of parent DIV 图片没有出现在div中? 是因为CSS吗? - Image not appearing inside a div? Is it because of the CSS? 父div不将图像包装在其中 - Parent div not wrapping image inside it 为什么我的div不脱离父级div? - Why is my div not breaking out of parent div? 为什么我的DIV右侧会出现微小的边缘? - Why is there a tiny margin appearing to the right of my DIV?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM