简体   繁体   English

创建相等宽度的弹性项目

[英]creating equal width flex items

I'm using flex for layout purposes, but the browser does not spread the width equally between items. 我使用flex进行布局,但浏览器不会在项目之间平均分配宽度。

 .parent { display: flex; width: 200px; } .btn1 { flex: 1 1 auto; } 
 <div class="parent"> <div class="btn1"> <button align="left" style="width:100%">Ok</button> </div> <button align="left" class="btn1">Cancel</button> <div> 

Now, I want the buttons to split the container length 50% / 50%. 现在,我希望按钮将容器长度分成50%/ 50%。

But that's not what's happening. 但那不是正在发生的事情。 I tried using flex: 1 1 auto and flex: 1 1 0 but with no success. 我尝试使用flex: 1 1 autoflex: 1 1 0但没有成功。

I know that I can use the OK button directly and it will solve my problem, but in my particular scenario it's important to wrap it with a div. 我知道我可以直接使用OK按钮,它将解决我的问题,但在我的特定情况下,用div包装它是很重要的。

Now, as I understand it, flex should be able to spread the width equally and that's my goal here. 现在,据我所知,flex应该能够均匀地扩展宽度,这就是我的目标。

One more thing though, I noticed that the button content seems to have an effect on the width and I want to ignore this effect somehow. 还有一件事,我注意到按钮内容似乎对宽度有影响,我想以某种方式忽略这种效果。

Thanks! 谢谢!

JSFiddle example: https://jsfiddle.net/edismutko/cvytLkyp/3/ JSFiddle示例: https ://jsfiddle.net/edismutko/cvytLkyp/3/

flex-basis: auto vs flex-basis: 0 flex-basis: auto vs flex-basis: 0

You're sizing your flex items with flex: 1 1 auto . 您正在使用flex: 1 1 auto调整您的Flex项目flex: 1 1 auto

However, if you want to distribute space evenly among items, you need to use flex: 1 1 0 . 但是,如果要在项目之间均匀分配空间,则需要使用flex: 1 1 0

The difference is the flex-basis component. 不同之处在于基于flex-basis组件。

With flex-basis: 0 , every item is considered to have a zero width and flex-grow distributes container space equally among them. 使用flex-basis: 0 ,每个项目被认为具有零宽度, flex-grow在它们之间平均分配容器空间。 This results in all items having the same length. 这导致所有项目具有相同的长度。

With flex-basis: auto , the size of the item is factored into the flex-grow calculation and container space is distributed proportionally among items. 使用flex-basis: auto ,项目的大小被计入flex-grow计算中,容器空间按比例分配在项目中。

So when you want equal length items use flex: 1 1 0 , which is the same as flex: 1 . 所以当你想要相等长度的项目时,使用flex: 1 1 0 ,这与flex: 1相同。

Here's a more detailed explanation: Make flex-grow expand items based on their original size 以下是更详细的说明: 根据原始大小灵活扩展项目


Default rules on button elements button元素的默认规则

Browsers apply styles to elements by default. 默认情况下,浏览器将样式应用于元素。 For instance, Chrome adds padding and border widths to button elements. 例如,Chrome会向button元素添加填充和边框宽度。

在此输入图像描述

Reset those defaults. 重置这些默认值。

Now you have two equal width flex items. 现在您有两个相等宽度的弹性项目。 (Additional styling is up to you.) (额外的造型取决于你。)

 .parent { display: flex; width: 200px; } .btn1 { flex: 1; } button { padding: 1px 0; border-left-width: 0; border-right-width: 0; } 
 <div class="parent"> <div class="btn1"> <button align="left" style="width:100%">Ok</button> </div> <button align="left" class="btn1">Cancel</button> <div> 


box-sizing: border-box

Something else to consider is including the padding and border lengths in the width / flex-basis calculation. 要考虑的其他因素包括width / flex-basis计算中的填充和边框长度。 Why are borders causing div to overflow container? 为什么边框导致div溢出容器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM