如何将图像定位在四个角落,以共同形成一个圆形?

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

How can I position images in four corners to collectively form a circle?

问题

我尝试用CSS制作这个特定的设计,但我无法找到实现它的方法。

如何将图像定位在四个角落,以共同形成一个圆形?

我尝试的代码没有给我想要的结果,我不知道我做错了什么。

我得到的图像已经是圆角的,所以我只需要将它们放在矩形的角落里,以便以后添加一些文本。

.container {
  display: flex;
  background-color: red;
}

.rectangle {
  position: relative;
  flex-basis: calc(50% - 20px);
  height: 150px;
  margin: 10px;
  background-color: blue;
}

.rectangle img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.rectangle:nth-child(1) img {
  position: absolute;
  bottom: 0;
  right: 0;
}

.rectangle:nth-child(2) img {
  position: absolute;
  bottom: 0;
  left: 0;
}

.rectangle:nth-child(3) img {
  position: absolute;
  top: 0;
  right: 0;
}

.rectangle:nth-child(4) img {
  position: absolute;
  top: 0;
  left: 0;
}
<div class="container">
  <div class="row">
    <div class="rectangle">
      <img src="image1.jpg" alt="Image 1">
    </div>
    <div class="rectangle">
      <img src="image2.jpg" alt="Image 2">
    </div>
  </div>
  <div class="row">
    <div class="rectangle">
      <img src="image3.jpg" alt="Image 3">
    </div>
    <div class="rectangle">
      <img src="image4.jpg" alt="Image 4">
    </div>
  </div>
</div>
英文:

I was trying to make this specific design in CSS but I couldn't figure out a way to do it .

如何将图像定位在四个角落,以共同形成一个圆形?

The code I tried didn't give me the result and I don't know what I'm doing wrong .

The images I got are already rounded corners so I just need to place them in the corners of rectangles so I can add some text later on.

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

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

.container {
  display: flex;
  background-color: red;
}

.rectangle {
  position: relative;
  flex-basis: calc(50% - 20px);
  height: 150px;
  margin: 10px;
  background-color: blue;
}

.rectangle img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.rectangle:nth-child(1) img {
  position: absolute;
  bottom: 0;
  right: 0;
}

.rectangle:nth-child(2) img {
  position: absolute;
  bottom: 0;
  left: 0;
}

.rectangle:nth-child(3) img {
  position: absolute;
  top: 0;
  right: 0;
}

.rectangle:nth-child(4) img {
  position: absolute;
  top: 0;
  left: 0;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;rectangle&quot;&gt;
      &lt;img src=&quot;image1.jpg&quot; alt=&quot;Image 1&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;rectangle&quot;&gt;
      &lt;img src=&quot;image2.jpg&quot; alt=&quot;Image 2&quot;&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;rectangle&quot;&gt;
      &lt;img src=&quot;image3.jpg&quot; alt=&quot;Image 3&quot;&gt;
    &lt;/div&gt;
    &lt;div class=&quot;rectangle&quot;&gt;
      &lt;img src=&quot;image4.jpg&quot; alt=&quot;Image 4&quot;&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

以下是翻译好的内容:

  • &lt;div class=&quot;container&quot;&gt; 使用了 flexbox,所以两个子元素 &lt;div class=&quot;row&quot;&gt; 水平并排放置。&lt;div class=&quot;rectangle&quot;&gt; 是块级元素,垂直地在彼此的上方/下方放置。因此,您打算的行实际上是列。

  • 您的 :nth-child(3):nth-child(4) 选择器没有匹配任何内容,因为每个 &lt;div class=&quot;row&quot;&gt; 仅有两个 &lt;div class=&quot;rectangle&quot;&gt; 子元素(您可以使用 :nth-of-type()

  • .rectangle {flex-basis: calc(50% - 20px)} 没有效果,因为 &lt;div class=&quot;row&quot;&gt; 不是一个 flex 容器

  • 删除 &lt;div class=&quot;row&quot;&gt; 容器后,另一个明显的问题是图像覆盖整个 div,应删除以下声明:

.rectangle img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

在进行这些更正后,我们可以得到所期望的定位:

<!-- 开始代码片段: js 隐藏: true 控制台: true Babel: false -->

<!-- 语言:lang-css -->
.container {
  display: flex;
  flex-wrap: wrap;
  background-color: red;
}

.rectangle {
  position: relative;
  flex-basis: calc(50% - 20px);
  height: 150px;
  margin: 10px;
  background-color: blue;
}

.rectangle img {
  position: absolute;
}

.rectangle:nth-child(1) img {
  bottom: 0;
  right: 0;
}

.rectangle:nth-child(2) img {
  bottom: 0;
  left: 0;
}

.rectangle:nth-child(3) img {
  top: 0;
  right: 0;
}

.rectangle:nth-child(4) img {
  top: 0;
  left: 0;
}

<!-- 语言:lang-html -->
<div class="container">
  <div class="rectangle">
    <h2>Lorem Ipsum</h2>
    <p><cite>Lorem ipsum</cite> is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p>
    <img src="//picsum.photos/100?1" alt="Image 1">
  </div>
  <div class="rectangle">
    <h2>Lorem Ipsum</h2>
    <p><cite>Lorem ipsum</cite> is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p>
    <img src="//picsum.photos/100?2" alt="Image 2">
  </div>
  <div class="rectangle">
    <h2>Lorem Ipsum</h2>
    <p><cite>Lorem ipsum</cite> is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p>
    <img src="//picsum.photos/100?3" alt="Image 3">
  </div>
  <div class="rectangle">
    <h2>Lorem Ipsum</h2>
    <p><cite>Lorem ipsum</cite> is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.</p>
    <img src="//picsum.photos/100?4" alt="Image 4">
  </div>
</div>

<!-- 结束代码片段 -->

然而,现在您会发现图像放置在其他内容的上面。这些图像应该用作背景图像以实现此效果。

.rectangle {
  background-repeat: no-repeat;
}

.rectangle:nth-child(1) {
  background-image: url(//picsum.photos/100?1);
  background-position: bottom 0 right 0;
}

.rectangle:nth-child(2) {
  background-image: url(//picsum.photos/100?2);
  background-position: bottom 0 left 0;
}

.rectangle:nth-child(3) {
  background-image: url(//picsum.photos/100?3);
  background-position: top 0 right 0;
}

.rectangle:nth-child(4) {
  background-image: url(//picsum.photos/100?4);
  background-position: top 0 left 0;
}

要修复 flex 项周围的双边距,声明容器上的 gappadding 并调整 flex-basis 计算。

.container {
  gap: 10px;
  padding: 10px;
}

.rectangle {
  flex-basis: calc(50% - 5px);
}
英文:

Reasons why you are not getting the intended result:

  • &lt;div class=&quot;container&quot;&gt; uses flexbox so the two child &lt;div class=&quot;row&quot;&gt; are positioned horizontally side-by-side. &lt;div class=&quot;rectangle&quot;&gt; is block level and is positioned vertically above/below each other. Therefore, what you intended to be rows are actually columns.

  • Your :nth-child(3) and :nth-child(4) selectors don't match anything because there are only two &lt;div class=&quot;rectangle&quot;&gt; child elements of each &lt;div class=&quot;row&quot;&gt; (you could have used :nth-of-type())

  • .rectangle {flex-basis: calc(50% - 20px)} has no effect because &lt;div class=&quot;row&quot;&gt; is not a flex container

  • After removing the &lt;div class=&quot;row&quot;&gt; containers, another apparent issue is the images cover the entire div due to these declarations that should be removed:

.rectangle img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

We get the desired positioning after making those corrections:

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

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

.container {
  display: flex;
  flex-wrap: wrap;
  background-color: red;
}

.rectangle {
  position: relative;
  flex-basis: calc(50% - 20px);
  height: 150px;
  margin: 10px;
  background-color: blue;
}

.rectangle img {
  position: absolute;
}

.rectangle:nth-child(1) img {
  bottom: 0;
  right: 0;
}

.rectangle:nth-child(2) img {
  bottom: 0;
  left: 0;
}

.rectangle:nth-child(3) img {
  top: 0;
  right: 0;
}

.rectangle:nth-child(4) img {
  top: 0;
  left: 0;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
    &lt;img src=&quot;//picsum.photos/100?1&quot; alt=&quot;Image 1&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
    &lt;img src=&quot;//picsum.photos/100?2&quot; alt=&quot;Image 2&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
    &lt;img src=&quot;//picsum.photos/100?3&quot; alt=&quot;Image 3&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
    &lt;img src=&quot;//picsum.photos/100?4&quot; alt=&quot;Image 4&quot;&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

<br>

However, now you'll discover that the images are placed on top of other content. The images should be used as the background image for this effect.

.rectangle {
  background-repeat: no-repeat;
}

.rectangle:nth-child(1) {
  background-image: url(//picsum.photos/100?1);
  background-position: bottom 0 right 0;
}

.rectangle:nth-child(2) {
  background-image: url(//picsum.photos/100?2);
  background-position: bottom 0 left 0;
}

.rectangle:nth-child(3) {
  background-image: url(//picsum.photos/100?3);
  background-position: top 0 right 0;
}

.rectangle:nth-child(4) {
  background-image: url(//picsum.photos/100?4);
  background-position: top 0 left 0;
}

<br>

To fix the double margin around the flex items, declare gap and padding on the container and adjust the flex-basis calculation.

.container {
  gap: 10px;
  padding: 10px;
}

.rectangle {
  flex-basis: calc(50% - 5px);
}

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

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

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px;
  background-color: red;
}

.rectangle {
  flex-basis: calc(50% - 5px);
  height: 150px;
  background-color: blue;
  background-repeat: no-repeat;
}

.rectangle:nth-child(1) {
  background-image: url(//picsum.photos/100?1);
  background-position: bottom 0 right 0;
}

.rectangle:nth-child(2) {
  background-image: url(//picsum.photos/100?2);
  background-position: bottom 0 left 0;
}

.rectangle:nth-child(3) {
  background-image: url(//picsum.photos/100?3);
  background-position: top 0 right 0;
}

.rectangle:nth-child(4) {
  background-image: url(//picsum.photos/100?4);
  background-position: top 0 left 0;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

<br>

Regarding your comment about using components: I don't have any knowledge of using components. Would it be possible to declare the value for background-image: url() in a &lt;style&gt; block on the page?

&lt;style&gt;
  .rectangle:nth-child(1) {
    background-image: url(//picsum.photos/100?1);
  }

  .rectangle:nth-child(2) {
    background-image: url(//picsum.photos/100?2);
  }

  .rectangle:nth-child(3) {
    background-image: url(//picsum.photos/100?3);
  }

  .rectangle:nth-child(4) {
    background-image: url(//picsum.photos/100?4);
  }
&lt;/style&gt;

<br>

If you must use &lt;img&gt;, this can be done using the z-index property. (This will require all text in &lt;div class=&quot;rectangle&quot;&gt; to be inside child elements.) Using z-index for this result can potentially conflict with other elements on the page (present or future) that also use z-index.

.rectangle &gt; * {
  position: relative;
  z-index: 1;
}

.rectangle img {
  position: absolute;
  z-index: 0;
}

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

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

.container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px;
  background-color: red;
}

.rectangle {
  position: relative;
  flex-basis: calc(50% - 5px);
  height: 150px;
  background-color: blue;
}

.rectangle &gt; * {
  position: relative;
  z-index: 1;
}

.rectangle img {
  position: absolute;
  z-index: 0;
}

.rectangle:nth-child(1) img {
  bottom: 0;
  right: 0;
}

.rectangle:nth-child(2) img {
  bottom: 0;
  left: 0;
}

.rectangle:nth-child(3) img {
  top: 0;
  right: 0;
}

.rectangle:nth-child(4) img {
  top: 0;
  left: 0;
}

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

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
    &lt;img src=&quot;//picsum.photos/100?1&quot; alt=&quot;Image 1&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
    &lt;img src=&quot;//picsum.photos/100?2&quot; alt=&quot;Image 2&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
    &lt;img src=&quot;//picsum.photos/100?3&quot; alt=&quot;Image 3&quot;&gt;
  &lt;/div&gt;
  &lt;div class=&quot;rectangle&quot;&gt;
    &lt;h2&gt;Lorem Ipsum&lt;/h2&gt;
    &lt;p&gt;&lt;cite&gt;Lorem ipsum&lt;/cite&gt; is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.&lt;/p&gt;
    &lt;img src=&quot;//picsum.photos/100?4&quot; alt=&quot;Image 4&quot;&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: -1

你可以使用以下的 CSS 属性:

border-top-left-radius: 100%; /* 用于顶部左边的图像 */
border-top-right-radius: 100%; /* 用于顶部右边的图像 */
border-bottom-right-radius: 100%; /* 用于底部右边的图像 */
border-bottom-left-radius: 100%; /* 用于底部左边的图像 */
英文:

You could use these css properties:

border-top-left-radius: 100%; /* for the top left image */
border-top-right-radius: 100%; /* for the top right image */
border-bottom-right-radius: 100%; /*for the bottom right image */
border-bottom-left-radius: 100%; /* for the bottom left image*/

huangapple
  • 本文由 发表于 2023年8月4日 07:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832082.html
匿名

发表评论

匿名网友

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

确定