简体   繁体   English

CSS网格和内联块设计问题

[英]CSS grid and inline-block design issue

I have made a blog design using CSS grid, having used inline-block to pack DIVs together. 我使用CSS网格进行了博客设计,使用了内联块将DIV打包在一起。

In my blog I have 2 picture-DIVS of height 60 that I want to show next to a text-DIV of height 120. Only the first picture is shown next to the text. 在我的博客中,我要在高度为120的文本DIV旁边显示2个高度为60的图片DIVS。在文本旁边仅显示第一张图片。

Why is the second picture shown below the text, and please get some pointers on how I can fix this. 为什么第二张图片显示在文本下方,请获得一些有关如何解决此问题的指导。

 .GridCont { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-template-rows: auto auto auto; grid-template-areas: "content content content content" "content content content content" "content content content content"; } .PostContent { grid-area: content; background: #B8E986; } .Content { background: #000000; width: 35%; color: white; display: inline-block; } .box1 { height: 120vh; } .PicContent { background: blue; color: white; display: inline-block; } .pic1 { height: 60vh; width: 50%; } .pic2 { height: 60vh; width: 45%; } .cTextP { padding: 20px; } 
 <div class="GridCont"> <div class="PostContent"> <div class="PicContent pic1"> <div class="cTextP">Picture #1</div> </div> <div class="Content box1"> <div class="cTextP">Content #1</div> </div> <div class="PicContent pic2"> <div class="cTextP">Picture #2</div> </div> </div> </div> 

Code is at this JS-fiddle 代码在这个JS小提琴上

Why would the second image show right beneath the first? 为什么第二个图像显示在第一个图像的正下方? There is no reason for that. 没有任何理由。

The second image is on the second row. 第二张图片在第二行上。

The second row goes right beneath the first row. 第二行位于第一行的正下方。

More specifically, the first row is occupied by two elements: image #1 and the content box. 更具体地说,第一行由两个元素占据:图像#1和内容框。 The height of the first row is defined by the tallest element. 第一行的高度由最高的元素定义。 In this case, that would the content box. 在这种情况下,将显示内容框。

So, because image #1 doesn't extend the full height of row #1, there will be a gap between images. 因此,由于图像#1并未扩展行#1的整个高度,因此图像之间会存在间隙。

Here's an even more detailed explanation of the problem: 这是该问题的更详细说明:

(It's a flexbox-related post, but the logic applies here, as well.) (这是与flexbox相关的文章,但逻辑也适用于此。)

Instead of inline-block , use Grid properties to get the content box to span both rows: 可以使用Grid属性而不是inline-block来使内容框跨越两行:

 .PostContent { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 60vh 60vh; grid-gap: 1em; grid-template-areas: " pic1 box1 " " pic2 box1 "; } .box1 { grid-area: box1; } .pic1 { grid-area: pic1; } .pic2 { grid-area: pic2; } .PostContent { background: #B8E986; } .PicContent { background: blue; color: white; } .Content { background: #000000; color: white; } .cTextP { padding: 20px;} 
 <div class="GridCont"> <div class="PostContent"> <div class="PicContent pic1"> <div class="cTextP">Picture #1</div> </div> <div class="Content box1"> <div class="cTextP">Content #1</div> </div> <div class="PicContent pic2"> <div class="cTextP">Picture #2</div> </div> </div> </div> 

revised jsfiddle 修改后的jsfiddle

Also note that grid properties work only between parent and child elements. 另请注意,网格属性仅在父元素和子元素之间起作用。

This will fix your problem: 这将解决您的问题:

<div class="grid-container">
  <div class="image1"></div>
  <div class="image2"></div>
  <div class="text"></div>
</div>


.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-template-areas: "image1 image1 text text" "image2 image2 text text";
}

.image1 { grid-area: image1; }

.image2 { grid-area: image2; }

.text { grid-area: text; }

You can see the working example over here: https://codepen.io/dennisperremans/pen/NeqNJp 您可以在此处查看工作示例: https : //codepen.io/dennisperremans/pen/NeqNJp

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

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