“height: 100%” 大于子元素的 Div

huangapple go评论80阅读模式
英文:

Div with "height: 100%" greater than child

问题

I've translated the content you provided:

我正在使用WordPress的Divi主题工作,我已经创建了一个2行4列的网格。我尝试在网格中创建一个之字形图案,其中每个div都包含图像或文本:

图像 文本 图像 文本
文本 图像 文本 图像

我刚刚添加了图像,并注意到每个图像底部有一些多余的空间。这会破坏图案,所以我尝试检查并注意到包含图像的div比图像略微扩展。

我尝试通过使div根据子元素(img)的大小进行缩放来消除每个图像底部的额外空间。

注意:尽管我在WordPress中使用Divi主题,但我发现使用自定义代码最容易,因此我在WordPress外观>自定义>附加CSS中使用了Divi代码模块来编写HTML并编写CSS。

HTML:

<section>
	<div class="grid-container">
    <div class="grid-img-1">
      <img src="/wp-content/uploads/2023/05/PC-grid-grapes.svg">
    </div>
    <div class="grid-img-2">
      <img src="/wp-content/uploads/2023/05/PC-grid-barrels.svg">
    </div>
    <div class="grid-img-3">
      <img src="/wp-content/uploads/2023/05/PC-grid-pouring.svg">
    </div>
    <div class="grid-img-4">
      <img src="/wp-content/uploads/2023/05/PC-grid-grapefield.svg">
    </div>
    <div class="grid-link-1"></div>
    <div class="grid-link-2"></div>
    <div class="grid-link-3"></div>
    <div class="grid-link-4"></div>
  </div>
</section>

CSS:

/* 卡片网格 */

.grid-container {
	display: grid;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(2, 50%);
  grid-auto-flow: row;
}

.grid-img-1 {
    grid-row-start: 1;
    grid-row-end: 2;
    grid-column-start: 1;
    grid-column-end: 2;
}

.grid-img-2 {
    grid-row-start: 2;
    grid-row-end: 3;
    grid-column-start: 2;
    grid-column-end: 3;
}

.grid-img-3 {
    grid-row-start: 1;
    grid-row-end: 2;
    grid-column-start: 3;
    grid-column-end: 4;
}

.grid-img-4 {
    grid-row-start: 2;
    grid-row-end: 3;
    grid-column-start: 4;
    grid-column-end: 5;
}

.grid-link-1 {
    grid-row-start: 2;
    grid-row-end: 3;
    grid-column-start: 1;
    grid-column-end: 2;
}

.grid-link-2 {
    grid-row-start: 1;
    grid-row-end: 2;
    grid-column-start: 2;
    grid-column-end: 3;
}

.grid-link-3 {
    grid-row-start: 2;
    grid-row-end: 3;
    grid-column-start: 3;
    grid-column-end: 4;
}

.grid-link-4 {
    grid-row-start: 1;
    grid-row-end: 2;
    grid-column-start: 4;
    grid-column-end: 5;
}

我已尝试了height: 100%;margin/padding: 0;box-sizing: border-box;等基本方法,都尝试过带有和不带有!important的情况,但都没有奏效。

在检查过程中,我还通过小复选框添加和删除样式,以查看是否能找到引起问题的原因,但也没有找到任何原因。

我认为这个问题可以通过CSS来解决,因为除了我不太熟练的JavaScript之外,我不知道在WordPress/Divi中哪里可以编写JavaScript代码。

英文:

I'm working in WordPress with the Divi theme and I've made a grid of 2 rows and 4 columns. I'm trying to create a zigzag pattern in the grid where each div has either an image or text:

image text image text
text image text image

I've only just added the images and i noticed a bit of leftover space at the bottom of each image. This kind of messes up the pattern so I tried inspecting and noticed the divs which contain the images all extend a little bit further than the images.

I'm trying to get rid of the extra space at the bottom of each image by making the divs scale according to the size of the child (img).

Note: although I work in WordPress with the divi theme, I found it's easiest to make this with custom code so I'm using a Divi code module for the HTML and writing the CSS in WordPress Appearance > Customize > Additional CSS.

Image of the grid. I'm in the inspector here so it properly fits in the picture

HTML:

&lt;section&gt;
	&lt;div class=&quot;grid-container&quot;&gt;
    &lt;div class=&quot;grid-img-1&quot;&gt;
      &lt;img src=&quot;/wp-content/uploads/2023/05/PC-grid-grapes.svg&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;grid-img-2&quot;&gt;
      &lt;img src=&quot;/wp-content/uploads/2023/05/PC-grid-barrels.svg&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;grid-img-3&quot;&gt;
      &lt;img src=&quot;/wp-content/uploads/2023/05/PC-grid-pouring.svg&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;grid-img-4&quot;&gt;
      &lt;img src=&quot;/wp-content/uploads/2023/05/PC-grid-grapefield.svg&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;grid-link-1&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;grid-link-2&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;grid-link-3&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;grid-link-4&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

CSS

/* Card grid */

.grid-container {
	display: grid;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(2, 50%);
  grid-auto-flow: row;
}

.grid-img-1 {
    grid-row-start: 1;
    grid-row-end: 2;
    grid-column-start: 1;
    grid-column-end: 2;
}

.grid-img-2 {
    grid-row-start: 2;
    grid-row-end: 3;
    grid-column-start: 2;
    grid-column-end: 3;
}

.grid-img-3 {
    grid-row-start: 1;
    grid-row-end: 2;
    grid-column-start: 3;
    grid-column-end: 4;
}

.grid-img-4 {
    grid-row-start: 2;
    grid-row-end: 3;
    grid-column-start: 4;
    grid-column-end: 5;
}

.grid-link-1 {
    grid-row-start: 2;
    grid-row-end: 3;
    grid-column-start: 1;
    grid-column-end: 2;
}

.grid-link-2 {
    grid-row-start: 1;
    grid-row-end: 2;
    grid-column-start: 2;
    grid-column-end: 3;
}

.grid-link-3 {
    grid-row-start: 2;
    grid-row-end: 3;
    grid-column-start: 3;
    grid-column-end: 4;
}

.grid-link-4 {
    grid-row-start: 1;
    grid-row-end: 2;
    grid-column-start: 4;
    grid-column-end: 5;
}

I've tried the basics of height: 100%;, margin/padding: 0;, box-sizing: border-box;, each with and without !important just to see what worked, but nothing did.

While inspecting I also removed and added styling with the little checkbox to see if I could find anything that would cause this problem, but I couldn't find anything either.

I thought maybe the image didn't fully load in so the scale of the div would be correct, but the size of the image is as intended so I don't think that's it.

I hope it's a problem that CSS can fix because besides the fact that I'm not that skilled in JavaScript, I don't know where I could write my JavaScript code within WordPress/Divi

答案1

得分: 0

我已尝试重新创建您遇到的错误,但似乎无法工作!
您能分享所涉及图像的尺寸吗?
您可以尝试在图像上使用以下代码:

img {
height: 100%;
width: 100%;
display: block;
}

这是一个关于它的 codepen 的样例。

英文:

I have tried to recreate the error you are having however it doesn't seem to work!
Can you share the sizes of the images involved?
You can try this code on the images:

img {
height: 100%;
width: 100%;
display: block;
}

Here is a codepen on how it looks.

答案2

得分: 0

默认情况下,图像具有display: inline属性(因此它被视为文本),浏览器会在其容器中添加一些空间以修复其他内联元素的基线调整。要去除这个空间,只需将图像的display属性设置为block

代码示例如下:
第一个div有额外的空间,但第二个没有。

div {
  background-color: red;
  margin-bottom: 10px;
}

.image-block {
  display: block;
}
<div>
  <img src="https://placehold.co/600x200" />
</div>

<div>
  <img src="https://placehold.co/600x200" class="image-block" />
</div>

如果底部仍然存在不需要的空间,可能是因为SVG文件的viewBox设置不正确。尝试打开SVG文件并编辑其viewBox属性。第四个值决定了高度,您可以稍微减小它并查看是否有效。

英文:

images by default have display: inline by default (so it is kind of treated like a text) and browsers add a little bit of space in their container to fix the overall baseline adjustment of other inline elements. To remove it, you simply need to add display: block to your image.

Look at this code as an example:
The first div has an extra space but the second one does not.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

div {
  background-color: red;
  margin-bottom: 10px;
}

.image-block {
  display: block;
}

<!-- language: lang-html -->

&lt;div&gt;
  &lt;img src=&quot;https://placehold.co/600x200&quot; /&gt;
&lt;/div&gt;

&lt;div&gt;
  &lt;img src=&quot;https://placehold.co/600x200&quot; class=&quot;image-block&quot; /&gt;
&lt;/div&gt;

<!-- end snippet -->

If you still see unwanted space at the bottom, it might be because of the wrong viewBox for your svg files. Try to open the SVG file and edit its viewBox attribute. The fourth value determines the height, you can reduce it a bit and see if it works.

huangapple
  • 本文由 发表于 2023年5月6日 20:47:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定