简体   繁体   English

图像未填充容器

[英]Image Does Not Fill Container

This is my first time posting here and I'm entirely self-taught, so bear with me please if I misspeak or say some dumb things.这是我第一次在这里发帖,我完全是自学的,所以如果我说错话或说一些愚蠢的话,请多多包涵。

I have a Wordpress blog.我有一个 Wordpress 博客。 For quite some time, I have had my blog post format set up so that a "header image" displays across the width of the content space.很长一段时间以来,我已经设置了我的博客文章格式,以便在内容空间的宽度上显示“标题图像”。 Across the bottom of that image, I list the title of the post and the date.在该图像的底部,我列出了帖子的标题和日期。

To do this, I created what I believe is called a class -- I named it "container" and the only defining trait of this class is that it has "position: relative;"为此,我创建了我认为称为 class 的东西——我将其命名为“容器”,而这个 class 的唯一定义特征是它具有“位置:相对;” inside it's brackets.在它的括号内。 (It also has a "shadow" border. I tried taking this out and it had no effect.) This was done in the "additional CSS" section of the wordpress site. (它也有一个“阴影”边框。我试着把它去掉,但没有效果。)这是在 wordpress 站点的“附加 CSS”部分完成的。

.container {
  position: relative;
  box-shadow: 1px -1px 7px rgba(0,0,0,0.8);

}

I also made a class called "text-block" that is defined as follows:我还制作了一个名为“文本块”的 class,其定义如下:

.text-block {
  position: absolute;
  bottom: 0px;
  right: 0px;
  left: 0px;
  background-color: rgba(128,128,128,0.6);
  color: white;
    padding-top: 10px;
    padding-left: 10px;
    padding-right: 10px;
    padding-bottom: 0px;
} 

Each post begins with opening a container.每个帖子都从打开一个容器开始。 Then, immediately inside of the container is the image, with the width set to 100%.然后,立即在容器内部是图像,宽度设置为 100%。 Then, still in the container, I put the text-block in, which is oriented across the bottom.然后,仍然在容器中,我将文本块放入,它是面向底部的。 The background color is opaque so that you still see the image behind the text, slightly greyed out.背景颜色是不透明的,因此您仍然可以看到文本后面的图像,略微变灰。

Here is the code that begins the post of which I've attached a screenshot :这是开始我附上屏幕截图的帖子的代码:

<div class="container"><img class="aligncenter wp-image-8056 size-full" style="width: 100%;" src="https://*mysite*/wp-content/uploads/thx-1138-film-confession.png" alt="Robert Duvall as THX 1138" width="831" height="364">
<div class="text-block">
<h2 style="color: white;"><em>THX 1138</em></h2>
<h6 style="color: white;">Review by *me* | November 6, 2020</h6>
</div>
</div>

The crucial part that I need to ask some advice on is the bottom edge of the container.我需要咨询的关键部分是容器的底部边缘。 Until recently, the bottom edges of the image, the text-block, and the container all coincided.直到最近,图像的底部边缘、文本块和容器都重合了。 Now, instead, a small amount of excess "padding" was added to the bottom of the container.现在,取而代之的是,在容器底部添加了少量多余的“填充”。 So now, the text-block overlaps the image and some un-filled space beneath it, as you can see in the image.所以现在,文本块与图像及其下方的一些未填充空间重叠,正如您在图像中看到的那样。 I can't seem to attach a second image of what it should look like, but I think it is clear what I'm trying to achieve.我似乎无法附上它应该是什么样子的第二张图片,但我认为我想要实现的目标很清楚。

I don't know where this excess "padding" came from.我不知道这些多余的“填充”是从哪里来的。 I didn't change anything -- no CSS was changed.我没有改变任何东西——没有改变 CSS。 I was just writing individual posts, copy-pasting the format from an old post to a new one to get the correct formatting.我只是在写个别帖子,将旧帖子中的格式复制粘贴到新帖子中以获得正确的格式。 I was previewing a single new post when I realized the change, and I thought that I had messed something up on that individual post.当我意识到这个变化时,我正在预览一个新帖子,我认为我在那个单独的帖子上搞砸了。 But then I started clicking through my site and realized every single post now showed the error.但后来我开始点击我的网站并意识到现在每一个帖子都显示错误。

In the past, some other weird formatting things have happened that stemmed from a plug-in.过去,由于插件而发生了其他一些奇怪的格式化事情。 I've deactivated all plug-ins and the issue remained.我已停用所有插件,但问题仍然存在。 I've taken the text-block out entirely and the excess "padding" remains.我已经完全去掉了文本块,多余的“填充”仍然存在。

Like I said, I'm self-taught.就像我说的,我是自学的。 I've really only learned things as I needed to know them.我真的只是在需要了解它们时才学到东西。 So it's possible I'm missing something simple or committing some grave error without knowing it.因此,我可能遗漏了一些简单的东西,或者在不知情的情况下犯了一些严重的错误。 But it seems to me that my container lacks the definition that would cause it to be larger than an exact fit to what I put inside of it.但在我看来,我的容器缺乏定义,导致它比我放入其中的东西要大。 Further, the fact that I did not change any of the underlying definitions seems to indicate it's some other kind of issue.此外,我没有更改任何基本定义的事实似乎表明这是其他类型的问题。 But I don't know where it could be stemming from.但我不知道它可能来自哪里。

I would appreciate any help to figure this out!我将不胜感激任何帮助解决这个问题!


UPDATE:更新:

First, Akhilesh, thank you for the reply.首先,Akhilesh,感谢您的回复。 Your suggestions did not directly lead to a solution but they at least got me into the spirit of trial and error.您的建议并没有直接导致解决方案,但它们至少让我进入了反复试验的精神。

I have found two "solutions" to this problem.我找到了解决这个问题的两个“解决方案”。

The first is to set "line-height: 0px;"首先是设置“line-height: 0px;” inside of the container in "Additional CSS." “附加 CSS”中的容器内部。 This isn't ideal because it means that I can't type regular text in this container anymore.这并不理想,因为这意味着我不能再在此容器中键入常规文本。 Not very elegant.不是很优雅。

The second kind of seems like a fluke.第二种似乎是侥幸。 As I showed above, my posts begin with:如上所示,我的帖子开头是:

<div class="container">**here**<img class="aligncenter wp-image-8056 size-full" style="width: 100%;" src="https://*mysite*/wp-content/uploads/thx-1138-film-confession.png" alt="Robert Duvall as THX 1138" width="831" height="364">

That is a single, unbroken line of code.那是一行完整的代码。 By starting a new line where I indicate above, it solves the issue.通过在我上面指出的地方开始一个新行,它解决了这个问题。 As I stated in my initial description, I have not changed anything.正如我在最初的描述中所说,我没有改变任何东西。 All my posts have been like this since I decided on the format.自从我决定格式以来,我所有的帖子都是这样的。 And as Akhilesh stated, it works in the fiddler (which was a new tool for me, so thanks again).正如 Akhilesh 所说,它在 fiddler 中工作(这对我来说是一个新工具,所以再次感谢)。

So, problem solved, but what changed to cause the issue?所以,问题解决了,但是导致问题的原因是什么? Something in the way that Wordpress interprets HTML code? Wordpress 解释 HTML 代码的方式有什么问题吗?

Try adding this to the img element: display:block;尝试将其添加到img元素: display:block;

Also, try to find whether there's any padding/margin set for the h2 and h6 elements.此外,尝试查找是否为h2h6元素设置了任何填充/边距。

Because I don't see any issues when I tried.因为我尝试时没有发现任何问题。 Here's the fiddle: https://jsfiddle.net/vL80u1a2/这是小提琴: https://jsfiddle.net/vL80u1a2/

If that didn't helped, try using the Developer Tools in your browser.如果这没有帮助,请尝试在浏览器中使用开发者工具。 Just right click on the element and choose Inspect Element .只需右键单击元素并选择Inspect Element Then when you select an element on the left side, all CSS would appear on the right pane.然后,当您 select 左侧的元素时,所有 CSS 都会出现在右侧窗格中。 You can try trouble shooting for the padding using that Developer Tools.您可以尝试使用该开发工具对填充进行故障排除。

Have a look at this for more details on how to use the Developer Tools: https://developers.google.com/web/tools/chrome-devtools/css有关如何使用开发人员工具的更多详细信息,请查看此内容: https://developers.google.com/web/tools/chrome-devtools/css

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM