如何通过CSS网格实现块形状的显示

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

How to achieve a block shape display by css grid

问题

如果我使用CSS网格,我可以创建一个内容区域,类似于以下方式:

grid-template-areas:
"b b a a"
"b b a a"
"b b a a"
"b b a a"
"c c c c";

但我想要实现类似下面的效果,网格的限制不允许这样做,但是否有其他方法来实现这个效果?

grid-template-areas:
"b b a a"
"b b a a"
"a a a a"
"a a a a"
"c c c c";

这是一个简单的UI卡片,我正在使用:

  • a 用于描述区域
  • b 用于图像区域
  • c 用于页脚区域
英文:

If I use a css grid, I have have a content area like

grid-template-areas:             
 "b b a a"            
 "b b a a"            
 "b b a a"	
 "b b a a"			
 "c c c c";

But I want to have something like below, grid limitations doesn’t aloow this, but any other way to implement this ?

grid-template-areas:             
 "b b a a"            
 "b b a a"            
 “a a a a"	
 “a a a a"			
 "c c c c";

This is a simple card on UI where I’m using

  • a is for description area
  • b is for image area
  • c is used for footer
    area

答案1

得分: 1

以下是您要翻译的内容:

这确实是使用 float 的目的。这里有一个简单的演示:

* {margin: 0; padding: 0; box-sizing: border-box;}

section {
  display: grid;
  grid-template: 3fr 1fr / 500px;
  grid-template-areas:
   "a"
   "c";
  gap: 20px;
  margin: 40px;
}

article {
  grid-template-areas: "a";
}

img {
  float: left;
  padding: 0 20px 20px 0;
  width: 50%;
}

footer {
  padding: 20px; 
  background: #e7e7e7;
  grid-template-areas: "c";
}
<section>
  <article>
    <p><img src="http://placehold.it/200x200">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil voluptatibus, beatae in saepe ullam laborum atque nulla quam commodi vitae ab rerum itaque quaerat deleniti ut eligendi veniam optio officiis, ipsum dolor sit amet consectetur adipisicing elit. Nihil voluptatibus, beatae in saepe ullam laborum atque nulla quam commodi vitae ab rerum itaque quaerat deleniti ut eligendi veniam optio officiis, ipsum dolor sit amet consectetur adipisicing elit. Nihil voluptatibus, beatae in saepe ullam laborum atque nulla quam commodi vitae ab rerum itaque quaerat deleniti ut eligendi veniam optio officiis.</p>
  </article>
  <footer>This is the footer.</footer>
</section>

我已完成翻译,没有其他内容。

英文:

This is really the purpose of float. Here's a simple demo:

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

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

* {margin: 0; padding: 0; box-sizing: border-box;}

section {
  display: grid;
  grid-template: 3fr 1fr / 500px;
  grid-template-areas:
   &quot;a&quot;
   &quot;c&quot;;
  gap: 20px;
  margin: 40px;
}

article {
  grid-template-areas: a;
}

img {
  float: left;
  padding: 0 20px 20px 0;
  width: 50%;
}

footer {
  padding: 20px; 
  background: #e7e7e7;
  grid-template-areas: c;
}

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

&lt;section&gt;
  &lt;article&gt;
    &lt;p&gt;&lt;img src=&quot;http://placehold.it/200x200&quot; &gt;Lorem, ipsum dolor sit amet consectetur adipisicing elit. Nihil voluptatibus, beatae in saepe ullam laborum atque nulla quam commodi vitae ab rerum itaque quaerat deleniti ut eligendi veniam optio officiis, ipsum dolor sit amet consectetur adipisicing elit. Nihil voluptatibus, beatae in saepe ullam laborum atque nulla quam commodi vitae ab rerum itaque quaerat deleniti ut eligendi veniam optio officiis, ipsum dolor sit amet consectetur adipisicing elit. Nihil voluptatibus, beatae in saepe ullam laborum atque nulla quam commodi vitae ab rerum itaque quaerat deleniti ut eligendi veniam optio officiis.&lt;/p&gt;
  &lt;/article&gt;
  &lt;footer&gt;This is the footer.&lt;/footer&gt;
&lt;/section&gt;

<!-- end snippet -->

I wouldn't use template areas for this, but just used them to get closer to your original suggestion.

huangapple
  • 本文由 发表于 2023年6月12日 11:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453427.html
匿名

发表评论

匿名网友

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

确定