gpt4 book ai didi

html - 如何在圆的边缘放置图标

转载 作者:行者123 更新时间:2023-12-03 14:36:54 27 4
gpt4 key购买 nike

我想将图标放置在其父级(圆圈)的边缘,如下所示:
enter image description here
我尝试了一些方法,但默认情况下绝对子级放置在其父级的起始位置,如下所示:
enter image description here
甚至 parent 也有一个圆形( border-radius: 50%; )
我不想用 marginposition对于每个 child ,因为他们的人数是动态的。
有没有办法将 child 放在 parent 的边缘?

.circle {
position: relative;
width: 300px;
height: 300px;
background: rgba(0, 0, 0, 0.2);
display: flex;
align-items: center;
border-radius: 50%;
}

.menu {
list-style-type: none;
display: flex;
flex-direction: column;
justify-content: center;
padding: 0;
margin: 0;
position: absolute;
right: 5px;
}

.menu li {
margin: 5px 0;
}

.menu li a {
display: block;
}

.menu li a img {
width: 50px;
}
<div class="circle">
<ul class="menu">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
</ul>
</div>

最佳答案

考虑使用转换。您将所有元素放在中心,然后旋转/平移它们以将它们放置在边缘。我正在使用 CSS 变量来简化代码,但这不是强制性的

.circle {
width: 300px;
height: 300px;
background: rgba(0, 0, 0, 0.2);
display: flex;
border-radius: 50%;
}

.menu {
list-style-type: none;
padding: 0;
display:grid; /* use grid */
margin:auto; /* center the menu with all the items */
}

.menu li {
--d:120px; /* distance from the center */
grid-area:1/1; /* all of them at the same position (they overlap) */
/* we rotate then translate to the edge then rotate again using the opposite rotation */
transform:rotate(var(--r)) translateX(var(--d)) rotate(calc(-1*var(--r)));
}

/* adjust the angle for each icon */
.menu li:nth-child(1) { --r: 0deg}
.menu li:nth-child(2) { --r: 40deg}
.menu li:nth-child(3) { --r:-40deg}

.menu li a {
display: block;
}
.menu li a img {
width: 50px;
}
<div class="circle">
<ul class="menu">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
</ul>
</div>

使用 shape-outside 的另一个想法:

.circle {
width: 300px;
height: 300px;
background: rgba(0, 0, 0, 0.2);
display: inline-flex;
border-radius: 50%;
}

.menu {
list-style-type: none;
padding: 0;
margin:0 0 0 auto;
width:50%;
text-align:right;
word-spacing: 150px;
line-height:0;
}
.menu::before {
content:"";
float:right;
height:100%;
width: 100%;
shape-outside:radial-gradient(100% 50% at left,transparent 98%,#fff)
}

.menu li {
display:inline-block;
}

.menu li a {
display: block;
}
.menu li a img {
width: 50px;
}
<div class="circle">
<ul class="menu">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
</ul>
</div>

<div class="circle">
<ul class="menu">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
</ul>
</div>


如果元素数量有限,您可以优化第一个,如下所示:

.circle {
width: 300px;
height: 300px;
background: rgba(0, 0, 0, 0.2);
display: inline-flex;
border-radius: 50%;
}

.menu {
--d:120px; /* distance from the center */
--i:30deg; /* the increment */
list-style-type: none;
padding: 0;
display:grid; /* use grid */
margin:auto; /* center the menu with all the items */
}

.menu li {
grid-area:1/1; /* all of them at the same position (they overlap) */
/* we rotate then translate to the edge then rotate again using the opposite rotation */
transform:rotate(var(--r)) translateX(var(--d)) rotate(calc(-1*var(--r)));
}

/* adjust the angle for each icon */
.menu li:nth-child(1) { --r: calc( 0*var(--i))}
.menu li:nth-child(2) { --r: calc( 1*var(--i))}
.menu li:nth-child(3) { --r: calc(-1*var(--i))}
.menu li:nth-child(4) { --r: calc( 2*var(--i))}
.menu li:nth-child(5) { --r: calc(-2*var(--i))}
.menu li:nth-child(6) { --r: calc( 3*var(--i))}
.menu li:nth-child(7) { --r: calc(-3*var(--i))}
.menu li:nth-child(8) { --r: calc( 4*var(--i))}
.menu li:nth-child(9) { --r: calc(-4*var(--i))}
.menu li:nth-child(10){ --r: calc( 5*var(--i))}
.menu li:nth-child(11){ --r: calc(-5*var(--i))}
.menu li:nth-child(12){ --r: calc(-6*var(--i))}

.menu li a {
display: block;
}
.menu li a img {
width: 50px;
}
<div class="circle">
<ul class="menu" style="--i:50deg">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
</ul>
</div>

<div class="circle">
<ul class="menu" style="--i:20deg;--d:130px">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
</ul>
</div>

<div class="circle">
<ul class="menu" style="--d:100px">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li> <li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
</ul>
</div>

以上涵盖了多达 12 个图标,您可以轻松扩展到代码以涵盖更多。那么你要做的就是调整变量 id另一种配置:

.circle {
width: 300px;
height: 300px;
background: rgba(0, 0, 0, 0.2);
display: inline-flex;
border-radius: 50%;
}

.menu {
--d:120px; /* distance from the center */
--i:30deg; /* the increment */
list-style-type: none;
padding: 0;
display:grid; /* use grid */
margin:auto; /* center the menu with all the items */
}

.menu li {
grid-area:1/1; /* all of them at the same position (they overlap) */
/* we rotate then translate to the edge then rotate again using the opposite rotation */
transform:rotate(var(--r)) translateX(var(--d)) rotate(calc(-1*var(--r)));
}

/* adjust the angle for each icon */
.menu li:nth-child(1) { --r: calc( 0*var(--i))}
.menu li:nth-child(2) { --r: calc( 1*var(--i))}
.menu li:nth-child(3) { --r: calc( 2*var(--i))}
.menu li:nth-child(4) { --r: calc( 3*var(--i))}
.menu li:nth-child(5) { --r: calc( 4*var(--i))}
.menu li:nth-child(6) { --r: calc( 5*var(--i))}
.menu li:nth-child(7) { --r: calc( 6*var(--i))}
.menu li:nth-child(8) { --r: calc( 7*var(--i))}
.menu li:nth-child(9) { --r: calc( 8*var(--i))}
.menu li:nth-child(10){ --r: calc( 9*var(--i))}
.menu li:nth-child(11){ --r: calc(10*var(--i))}
.menu li:nth-child(12){ --r: calc(11*var(--i))}

.menu li a {
display: block;
}
.menu li a img {
width: 50px;
}
<div class="circle">
<ul class="menu" style="--i:50deg">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
</ul>
</div>

<div class="circle">
<ul class="menu" style="--i:20deg;--d:130px">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
</ul>
</div>

<div class="circle">
<ul class="menu" style="--d:100px">
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li> <li>
<a href="#instagram">
<img src="https://img.icons8.com/cotton/344/instagram-new.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
<li>
<a href="#youtube">
<img src="https://img.icons8.com/cotton/344/youtube.png" />
</a>
</li>
<li>
<a href="#chrome">
<img src="https://img.icons8.com/cotton/344/chrome.png" />
</a>
</li>
</ul>
</div>

通过调整 nth-child您控制元素放置方式的逻辑。

关于html - 如何在圆的边缘放置图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66930181/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com