html - 如何适应特定子元素的宽度?

标签 html css

问题

我正在尝试创建一个技能表。
(我不能问下一个问题为什么,所以我创建了一个新帐户并询问)

当前状态:

current status

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

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

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


完整图片

整体图

Overall picture

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

picure

代码

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;
 }
 figure > img {
   top: 0;
   bottom: 0;
   }
 figure > .meter {
   height: 4.4rem;
   position: absolute;
   transform: translate(-50%,-50%);
   top: 50%;
   left: 50%;
   }
 figure > figcaption {
   line-height: 1;
   text-align: center;
   }
.meter-t {
 height: 2.3rem;
 display: block;
 position: absolute;
 bottom: 0;
 right: 0;
 transform: translate(-50%,-50%);
 top: 50%;
 left: 50%;
 }
.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>

最佳答案

可以使用 CSS 网格吗?使用起来真的很容易

figure {
  display: grid;
  grid-template: 'meter' auto 'name' min-content / 4.4rem; // set the size of the meter here!
  grid-row-gap: 1.5rem // just to space thing a little
}
figure img {
  grid-area: meter; //put both images on the same area so they overlap
  align-self: center; //center both horizontally and vertically
  justify-self: center;
}
figure .meter {
  width: 100%; // make it 100%, since the size is set on the grid template
}
figure .meter_t {
  height: 50%; // same as before, let the size be dictated by the grid
  width: 50%;
}
figure figcaption {
  grid-area: name; //put the name at the bottom
  justify-self: center; // so it overflows to the sides
}

您甚至可以删除所有定位代码,只保留图像的大小。

https://codepen.io/anon/pen/zbOYPO

编辑:添加了标题的居中

关于html - 如何适应特定子元素的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54832616/

相关文章:

JavaScript小游戏

php - 有人帮助我使用 Css 或在表格中打印此数组

html - 无法使用 CSS 将 div 定位到底部 0px

php - MySQL 查询生成器

javascript - 单击时如何更改按钮的背景?

python - 使用 Flask 在 HTML 文本输入占位符中显示多个单词

css - 用引号包裹的 Angular 函数返回变量显示为文本而不是渲染/使用不破坏 css 的不同方案

HTML/CSS 无序列表居中对齐

html - 如何将图像翻转与链接翻转连接起来?

scroll - 在滚动条上添加文字阴影