英文:
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 -->
<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>
<!-- end snippet -->
答案1
得分: 0
以下是翻译好的内容:
-
<div class="container">
使用了 flexbox,所以两个子元素<div class="row">
水平并排放置。<div class="rectangle">
是块级元素,垂直地在彼此的上方/下方放置。因此,您打算的行实际上是列。 -
您的
:nth-child(3)
和:nth-child(4)
选择器没有匹配任何内容,因为每个<div class="row">
仅有两个<div class="rectangle">
子元素(您可以使用:nth-of-type()
) -
.rectangle {flex-basis: calc(50% - 20px)}
没有效果,因为<div class="row">
不是一个 flex 容器 -
删除
<div class="row">
容器后,另一个明显的问题是图像覆盖整个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 项周围的双边距,声明容器上的 gap
和 padding
并调整 flex-basis
计算。
.container {
gap: 10px;
padding: 10px;
}
.rectangle {
flex-basis: calc(50% - 5px);
}
英文:
Reasons why you are not getting the intended result:
-
<div class="container">
uses flexbox so the two child<div class="row">
are positioned horizontally side-by-side.<div class="rectangle">
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<div class="rectangle">
child elements of each<div class="row">
(you could have used:nth-of-type()
) -
.rectangle {flex-basis: calc(50% - 20px)}
has no effect because<div class="row">
is not a flex container -
After removing the
<div class="row">
containers, another apparent issue is the images cover the entirediv
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 -->
<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>
<!-- 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 -->
<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>
</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>
</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>
</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>
</div>
</div>
<!-- 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 <style>
block on the page?
<style>
.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);
}
</style>
<br>
If you must use <img>
, this can be done using the z-index
property. (This will require all text in <div class="rectangle">
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 > * {
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 > * {
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 -->
<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>
<!-- 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*/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论