热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

不尊重边距和盒子尺寸的可伸缩产品:边框-Flexitemsnotrespectingmarginsandbox-sizing:border-box

ImtryingtoachieveasimplethingthatwehaveinstandardCSS.我正在尝试在标准CSS中实现一个简单的东西。Letssay

I'm trying to achieve a simple thing that we have in standard CSS.

我正在尝试在标准CSS中实现一个简单的东西。

Let's say that I have a grid system with 3 columns and box-sizing: border-box.

假设我有一个网格系统,它有3列和盒子大小:边框。

That means that I will fit 3 boxes and with a margin that will shrink the size to fit max 3 boxes.

这就意味着我要装3个盒子,并且有一个边距,可以缩小大小以适合最多3个盒子。

But when I try to do that with FLEXBOX, it's a pain!!

但是当我试着用FLEXBOX做这个的时候,真的很痛苦!!

So if I have div's with flex: 1 1 33.33%; margin: 10px; I was expecting to have 3 boxes per row... but if I use flex-wrap: wrap, that will not shrink to fit 3 boxes .

如果我有div有1 1 33。33%保证金:10 px;我期望每行有3盒……但如果我使用弹性包装:包装,那不会收缩到适合3盒。

Here is a example.. the idea is that the second row would have 3 boxes in a row and fourth box would be in the last row.

这里是一个例子。这个想法是,第二行将有3个方框,而第4个方框将在最后一行。

Thanks

谢谢

https://jsfiddle.net/mariohmol/pbkzj984/14/

https://jsfiddle.net/mariohmol/pbkzj984/14/

.horizontal-layout {
  display: flex;
  flex-direction: row;
  width: 400px;
}

header>span {
  /* flex: 1 1 100%; */
  /* flex: 1 0 100%; */
  flex: 1 1 33.33%;
  margin: 10px;
}

header>.button {
  background-color: grey;
}

header>.app-name {
  background-color: orange;
}

header#with-border-padding {
  flex-wrap: wrap;
}

header#with-border-padding>span {
  flex: 1 1 33.33%;
  box-sizing: border-box;
  /* this is not useful at all */
}

header#with-border-padding>.button {
  border: 1px solid black;
  padding-left: 5px;
}
NO flex-wrap: wrap, so it not respects the flex 33% 
A B C D

WITH flex-wrap: wrap : I expect to have 3 boxes in first row and D box in a down
A B C D

2 个解决方案

#1


3  

Keep in mind that box-sizing: border-box brings padding and borders into the width / height calculation, but not margins. Margins are always calculated separately.

请记住,框级:边框在宽度/高度计算中包含了填充和边框,但不包含边框。边距总是分开计算的。

The box-sizing property takes two values:

box尺寸属性有两个值:

  • content-box
  • 内容框
  • border-box
  • border-box

It does not offer padding-box or margin-box.

它不提供球拍盒或保证金盒。

Consider those terms when referring to the CSS Box Model.

在引用CSS框模型时考虑这些术语。

enter image description here

source: W3C

来源:W3C

3.1. Changing the Box Model: the box-sizing property

3.1。改变盒子模型:盒子尺寸属性

content-box

内容框

The specified width and height apply to the width and height respectively of the content box of the element. The padding and border of the element are laid out and drawn outside the specified width and height.

指定的宽度和高度分别应用于元素内容框的宽度和高度。元素的填充和边框被放在指定的宽度和高度之外并绘制出来。

border-box

border-box

Length and percentages values for width and height on this element determine the border box of the element. That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. The content width and height are calculated by subtracting the border and padding widths of the respective sides from the specified width and height properties.

这个元素的宽度和高度的长度和百分比值决定元素的边框。也就是说,元素上指定的任何填充或边框都在指定的宽度和高度内展开和绘制。内容宽度和高度是通过从指定的宽度和高度属性中减去边界和填充宽度来计算的。


Also, an initial setting of a flex container is flex-shrink: 1. This means that flex items can shrink in order to fit within the container.

另外,flex容器的初始设置是flex-shrink: 1。这意味着flex项目可以收缩以适应容器。

Therefore, a specified width, height or flex-basis will not hold, unless flex-shrink is disabled.

因此,除非禁用了浮动收缩,否则指定的宽度、高度或浮动基础将无法保持。

You can override the default with flex-shrink: 0.

您可以使用flex-shrink: 0覆盖默认值。

Here's a more complete explanation: What are the differences between flex-basis and width?

这里有一个更完整的解释:弹性基底和宽度的区别是什么?


Here's a simple solution:

这里有一个简单的解决方案:

You have four boxes. You want three on row 1 and the last on row 2.

你有四个盒子。你想要第三行是第1行,最后一行是第2行。

This is what you have:

这就是你所拥有的:

flex: 1 1 33.33%;
margin: 10px;

This breaks down to:

这种分解:

  • flex-grow: 1
  • flex-grow:1
  • flex-shrink: 1
  • flex-shrink:1
  • flex-basis: 33.33%
  • flex-basis:33.33%

We know that box-sizing: border-box factors padding and borders into the flex-basis. That's not a problem. But what about the margins?

我们知道盒子尺寸:边界框因素填充和边界到弹性基础。这不是一个问题。但是边际呢?

Well, since you have flex-grow: 1 on each item, there is no need for flex-basis to be 33.33%.

既然每个项目都有1个弹性增长,那么就不需要将弹性增长的基础设为33.33%。

Since flex-grow will consume any free space on the row, flex-basis only needs to be large enough to enforce a wrap.

由于flex-grow将消耗行上的任何空闲空间,所以flex- based只需要足够大就可以强制换行。

Since margins also consume free space, flex-grow will not intrude into the margin space.

由于边际也消耗自由空间,弹性增长不会进入边际空间。

So try this instead:

所以试试这个:

flex: 1 1 26%;
margin: 10px;

* {
  box-sizing: border-box;
}

.horizontal-layout {
  display: flex;
  width: 400px;
}

header > span { 
  flex: 1 0 26%;                    /* ADJUSTED */
  margin: 10px;
}

header#with-border-padding {
  flex-wrap: wrap;
}

header#with-border-padding>span {
  flex: 1 0 26%;                   /* ADJUSTED */
}

header#with-border-padding>.button {
  border: 1px solid black;
  padding-left: 5px;
}

header>.button {
  background-color: grey;
}

header>.app-name {
  background-color: orange;
}
NO flex-wrap: wrap, so it not respects the flex 33% 
A B C D

WITH flex-wrap: wrap : I expect to have 3 boxes in first row and D box in a down
A B C D

#2


0  

As well pointed out in Michael_B's answer margins are not taken into account when calculating flex item width. But you can subtract margins from flex-basis to get desired behaviour.

正如Michael_B在计算flex项目宽度时所指出的那样,答案边距是不考虑的。但是你可以从弹性的基础上减去边际来得到想要的行为。

For current case:

对于目前的情况:

header > span {
  /* 33.33% - margin-left - margin-right */
  flex: 1 1 calc(33.33% - 20px);
  margin: 10px;
}

Also you should set box-sizing: border-box to include padding (and borders) when calculating width.

此外,还应该设置方框大小:边界框,在计算宽度时包含填充(和边框)。

* {
  box-sizing: border-box;
}

.horizontal-layout {
  display: flex;
  width: 400px;
}

header > span { 
  flex: 1 0 calc(33.33% - 20px);
  margin: 10px;
}

header#with-border-padding {
  flex-wrap: wrap;
}

header#with-border-padding > span {
  flex: 1 0 calc(33% - 20px);
}

header#with-border-padding > .button {
  border: 1px solid black;
  padding-left: 5px;
}

header > .button {
  background-color: grey;
}

header > .app-name {
  background-color: orange;
}
NO flex-wrap: wrap, so it not respects the flex 33% 
A B C D

WITH flex-wrap: wrap : I expect to have 3 boxes in first row and D box in a down
A B C D


推荐阅读
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了brain的意思、读音、翻译、用法、发音、词组、同反义词等内容,以及脑新东方在线英语词典的相关信息。还包括了brain的词汇搭配、形容词和名词的用法,以及与brain相关的短语和词组。此外,还介绍了与brain相关的医学术语和智囊团等相关内容。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • jQuery实现简单的动画效果及用法详解
    本文详细介绍了使用jQuery实现简单动画效果的方法,包括显示/隐藏、向上收缩/向下展开、淡入/淡出、自定义动画等。同时提供了具体的用法示例,并解释了参数的含义和使用技巧。通过本文的学习,读者可以掌握如何使用jQuery实现各种动画效果,为网页增添生动和互动性。 ... [详细]
  • C#多线程解决界面卡死问题的完美解决方案
    当界面需要在程序运行中不断更新数据时,使用多线程可以解决界面卡死的问题。一个主线程创建界面,使用一个子线程执行程序并更新主界面,可以避免卡死现象。本文分享了一个例子,供大家参考。 ... [详细]
  • Android源码中的Builder模式及其作用
    本文主要解释了什么是Builder模式以及其作用,并结合Android源码来分析Builder模式的实现。Builder模式是将产品的设计、表示和构建进行分离,通过引入建造者角色,简化了构建复杂产品的流程,并且使得产品的构建可以灵活适应变化。使用Builder模式可以解决开发者需要关注产品表示和构建步骤的问题,并且当构建流程发生变化时,无需修改代码即可适配新的构建流程。 ... [详细]
author-avatar
123sdf87_768
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有