根据孩子的人数调整孩子的大小(Adjust child size based on number of children)

编程入门 行业动态 更新时间:2024-10-23 21:29:47
根据孩子的人数调整孩子的大小(Adjust child size based on number of children)

我想制作一个类似于浏览器中的标签的UI。

为此,我有这个CSS的元素:

width: 20%; max-width: 150px;

好而简单,反应迅速,真棒。

虽然只支持五个标签。 如果我想支持10个选项卡,就像更改为width: 10%一样简单width: 10% 。

所以基本上,我想要这样的东西:

width: calc(100% / number-of-children)

如果这样的事情是可能的,那会很棒。

我目前只是使用JavaScript将宽度设置为上述表达式。 简单,实用,但我想我会问有没有人知道一个纯CSS的解决方案。

I want to make a UI that is similar to tabs in a browser.

To this end, I have elements with this CSS:

width: 20%; max-width: 150px;

Nice and simple, responsive and awesome.

Only supports five tabs, though. If I want to support 10 tabs, it's as easy as changing to width: 10%.

So basically, I want something like:

width: calc(100% / number-of-children)

If such a thing were possible, that'd be amazing.

I'm currently just using JavaScript to set the width to the above expression. Simple, functional, but I figured I'd ask if anyone knows of a CSS-only solution.

最满意答案

用CSS3你可以使用display: flex 。 例如

标记

<section> <div>tab 1</div> <div>tab 2</div> <div>tab 3</div> <div>tab 4</div> </section>

CSS

section { display: flex; } div { flex-grow: 1; max-width: 150px; }

Codepen示例: http ://codepen.io/anon/pen/EGbpH/

with CSS3 you may use display: flex. E.g.

markup

<section> <div>tab 1</div> <div>tab 2</div> <div>tab 3</div> <div>tab 4</div> </section>

Css

section { display: flex; } div { flex-grow: 1; max-width: 150px; }

Codepen example: http://codepen.io/anon/pen/EGbpH/

更多推荐

本文发布于:2023-08-05 08:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428806.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:孩子   大小   人数   Adjust   child

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!