gpt4 book ai didi

html - 如何将子元素放在特定子元素的中心?

转载 作者:行者123 更新时间:2023-11-28 00:22:19 25 4
gpt4 key购买 nike

全部解决


问题

我正在尝试创建一个技能表。

当前状态:

enter image description here

我想将元素的宽度与 .meter 相匹配。
也就是说,如何在.meter(子元素)处设置block的标准?
(在上面的gif中,img.meter的地方)

如果我的英语不好,我很抱歉。如果感到困惑,请问我:)


完整图片

整体图

enter image description here

一张“因为我要做这个,所以我想基于这个宽度的绿色”的图片(我觉得很容易成像)

enter image description here


更新状态

now

多谢指教!
现在是这样的。
我想要基于 .meter.. 的边距(现在 figcaption 是标准)
由于 figcaption 上升了,我试图更正它,但没有成功。
多一点,请大家多多赐教!

(我不能问下一个问题为什么,所以我创建了一个新帐户并询问)
-> How to fit the width of a specific child element?

edit:而且,有了这个问题的答案 (▲) 一切都解决了!非常感谢。 】


代码

解决了标题问题ver。 (谢谢!):

html {
font-size: 62.5%;
}
.data {
padding-top: 1.7rem;
padding-bottom: 1.7rem;
}
.skill li {
display: flex;
flex-wrap: wrap;
align-content: space-between;
}
.skill li :last-child {
margin-right: 0;
}
.code {
margin-top: 1.7rem;
}
figure {
margin-right: .9rem;
font-size: 1rem;
position: relative;
height: 4.4rem; /* add */
}
figure > img {
top: 0;
bottom: 0;
}
figure > .meter {
height: 4.4rem;
/* add */
position: absolute;
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
/* /add */
}
figure > figcaption {
line-height: 1;
text-align: center;
}
.meter-t {
height: 2.3rem;
display: block;
position: absolute;
/* top: 0; (delete) */
bottom: 0;
right: 0;
/* left: 0; (delete) */
/* margin: auto; (delete) */
/* add */
transform: translate(-50%,-50%);
top: 50%;
left: 50%;
/* /add */
}
.PHP > .meter-t {
width: 2.4rem;
}
<section class="skill">
<p class="title">skill</p>
<ul class="data">
<li class="tool">
<figure class="Illustrator">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221627.png" alt="advanced" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221624.png" alt="Illustrator" />
<figcaption>Illustrator</figcaption>
</figure>
<figure class="Photoshop">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221629.png" alt="intermediate" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221753.png" alt="Photoshop" />
<figcaption>Photoshop</figcaption>
</figure>
<figure class="Indesign">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221632.png" alt="elementary" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221719.png" alt="Indesign" />
<figcaption>Indesign</figcaption>
</figure>
<figure class="Vectorworks">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221629.png" alt="intermediate" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221747.png" alt="Vectorworks" />
<figcaption>Vectorworks</figcaption>
</figure>
<figure class="Shade">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221629.png" alt="intermediate" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221750.png" alt="Shade" />
<figcaption>Shade</figcaption>
</figure>
</li>
<li class="code">
<figure class="HTML">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221632.png" alt="elementary" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221722.png" alt="HTML" />
<figcaption>HTML</figcaption>
</figure>
<figure class="CSS">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221632.png" alt="elementary" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221725.png" alt="CSS" />
<figcaption>CSS</figcaption>
</figure>
<figure class="Javascript">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221634.png" alt="beginner" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221717.png" alt="Javascript" />
<figcaption>Javascript</figcaption>
</figure>
<figure class="PHP">
<img class="meter" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221634.png" alt="beginner" />
<img class="meter-t" src="https://cdn-ak.f.st-hatena.com/images/fotolife/O/O2_milk/20190224/20190224221714.png" alt="PHP" />
<figcaption>PHP</figcaption>
</figure>
</li>
</ul>
</section>

最佳答案

据我所知,您需要内部 img 以外部 img 为中心。

请使用下面的示例。

figure{
position: relative;
width: 100%;
/* height equal to the height of outer image*/
height: 400px;
}
.meter{
border: 1px solid red;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
}
.meter-t{
border: 1px solid red;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
}
<figure class="Photoshop">
<img class="meter" src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/400px-Cat_silhouette.svg.png" alt="intermediate" />
<img class="meter-t" src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/60/Cat_silhouette.svg/40px-Cat_silhouette.svg.png" alt="Photoshop" />
<figcaption>Photoshop</figcaption>
</figure>

如有任何问题,请告诉我。

关于html - 如何将子元素放在特定子元素的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54828092/

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