关于css的display:flex inline block inline-block和float

本文深入解析CSS中的display属性,包括inline、block、inline-block的区别及应用,对比inline-block与浮动布局的特点,探讨flex布局的原理与实践,如flex-direction、flex-wrap、justify-content等属性的使用。

1.解释一下display的几个常用的属性值,inline , block, inline-block

inline(行内元素):
    使元素变成行内元素,拥有行内元素的特性,即可以与其他行内元素共享一行,不会独占一行. 
    不能更改元素的height,width的值,大小由内容撑开. 
    可以使用padding上下左右都有效,margin只有left和right产生边距效果,但是top和bottom就不行.
block(块级元素):
    使元素变成块级元素,独占一行,在不设置自己的宽度的情况下,块级元素会默认填满父级元素的宽度. 
    能够改变元素的height,width的值. 
    可以设置padding,margin的各个属性值,top,left,bottom,right都能够产生边距效果.
 inline-block(融合行内于块级):
    结合了inline与block的一些特点,结合了上述inline的第1个特点和block的第2,3个特点.
    用通俗的话讲,就是不独占一行的块级元素。如图:

图一:在这里插入图片描述

图二:在这里插入图片描述

两个图可以看出,display:inline-block后块级元素能够在同一行显示,有人这说不就像浮动一样吗。没错,display:inline-block的效果几乎和浮动一样,但也有不同,接下来讲一下inline-block和浮动的比较。

2.inline-block布局 vs 浮动布局

a.不同之处:对元素设置display:inline-block ,元素不会脱离文本流,而float就会使得元素脱离文本流,且还有父元素高度坍塌的效果

b.相同之处:能在某程度上达到一样的效果

我们先来看看这两种布局:
图一:display:inline-block在这里插入图片描述

图二:对两个孩子使用float:left,我在上一篇浮动布局讲过,这是父元素会高度坍塌,所以要闭合浮动,对box使用overflow:hidden,效果如下:在这里插入图片描述

乍一看两个都能做到几乎相同的效果,(仔细看看display:inline-block中有间隙问题,这个留到下面再讲)

c.浮动布局不太好的地方:参差不齐的现象,我们看一个效果:
图三:在这里插入图片描述

图四:在这里插入图片描述

从图3,4可以看出浮动的局限性在于,若要元素排满一行,换行后还要整齐排列,就要子元素的高度一致才行,不然就会出现图三的效果,而inline-block就不会。

3.inline-block存在的小问题:

a.上面可以看到用了display:inline-block后,存在间隙问题,间隙为4像素,这个问题产生的原因是换行引起的,因为我们写标签时通常会在标签结束符后顺手打个回车,而回车会产生回车符,回车符相当于空白符,通常情况下,多个连续的空白符会合并成一个空白符,而产生“空白间隙”的真正原因就是这个让我们并不怎么注意的空白符。

b.去除空隙的方法:
  1.对父元素添加,{font-size:0},即将字体大小设为0,那么那个空白符也变成0px,从而消除空隙
  现在这种方法已经可以兼容各种浏览器,以前chrome浏览器是不兼容的
图一:

c.浏览器兼容性:ie6/7是不兼容 display:inline-block的所以要额外处理一下:
  在ie6/7下:
  对于行内元素直接使用{dislplay:inline-block;}
  对于块级元素:需添加{display:inline;zoom:1;}

4.总结:

display:inline-block的布局方式和浮动的布局方式,究竟使用哪个,我觉得应该根据实际情况来决定的:
  a.对于横向排列东西来说,我更倾向与使用inline-block来布局,因为这样清晰,也不用再像浮动那样清除浮动,害怕布局混乱等等。
  b.对于浮动布局就用于需要文字环绕的时候,毕竟这才是浮动真正的用武之地,水平排列的是就交给inline-block了。

转载于http://www.cnblogs.com/Ry-yuan/p/6848197.html

flex布局
flex-direction属性决定主轴的方向(即项目的排列方向)。

.box {
flex-direction: row | row-reverse | column | column-reverse;
}

它可能有4个值。

    row(默认值):主轴为水平方向,起点在左端。
    row-reverse:主轴为水平方向,起点在右端。
    column:主轴为垂直方向,起点在上沿。
    column-reverse:主轴为垂直方向,起点在下沿。

3.2 flex-wrap属性

默认情况下,项目都排在一条线(又称”轴线”)上。flex-wrap属性定义,如果一条轴线排不下,如何换行。

.box{
flex-wrap: nowrap | wrap | wrap-reverse;
}

它可能取三个值。

(1)nowrap(默认):不换行。

(2)wrap:换行,第一行在上方。

(3)wrap-reverse:换行,第一行在下方。

3.3 flex-flow

flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。

.box {
flex-flow: ;
}

3.4 justify-content属性

justify-content属性定义了项目在主轴上的对齐方式。

.box {
justify-content: flex-start | flex-end | center | space-between | space-around;
}

它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。

    flex-start(默认值):左对齐
    flex-end:右对齐
    center: 居中
    space-between:两端对齐,项目之间的间隔都相等。
    space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

3.5 align-items属性

align-items属性定义项目在交叉轴上如何对齐。

.box {
align-items: flex-start | flex-end | center | baseline | stretch;
}

它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。

    flex-start:交叉轴的起点对齐。
    flex-end:交叉轴的终点对齐。
    center:交叉轴的中点对齐。
    baseline: 项目的第一行文字的基线对齐。
    stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

3.6 align-content属性

align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

.box {
align-content: flex-start | flex-end | center | space-between | space-around | stretch;
}

该属性可能取6个值。

    flex-start:与交叉轴的起点对齐。
    flex-end:与交叉轴的终点对齐。
    center:与交叉轴的中点对齐。
    space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
    space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
    stretch(默认值):轴线占满整个交叉轴。

四、项目的属性

以下6个属性设置在项目上。

    order
    flex-grow
    flex-shrink
    flex-basis
    flex
    align-self

4.1 order属性

order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。

.item {
order: ;
}

4.2 flex-grow属性

flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。

.item {
flex-grow: ; /* default 0 */
}

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。
4.3 flex-shrink属性

flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。

.item {
flex-shrink: ; /* default 1 */
}

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。

负值对该属性无效。
4.4 flex-basis属性

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。

.item {
flex-basis: | auto; /* default auto */
}

它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。
4.5 flex属性

flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

.item {
flex: none | [ <‘flex-grow’> <‘flex-shrink’>? || <‘flex-basis’> ]
}

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。

建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。
4.6 align-self属性

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。

.item {
align-self: auto | flex-start | flex-end | center | baseline | stretch;
}

该属性可能取6个值,除了auto,其他都与align-items属性完全一致。

来源:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

CSS Float(浮动)

CSS float 属性定义元素在哪个方向浮动,浮动元素会生成一个块级框,直到该块级框的外边缘碰到包含框或者其他的浮动框为止。
什么是 CSS Float(浮动)?
CSS 的 Float(浮动),会使元素向左或向右移动,其周围的元素也会重新排列。

Float(浮动),往往是用于图像,但它在布局时一样非常有用。
元素怎样浮动

元素的水平方向浮动,意味着元素只能左右移动而不能上下移动。

一个浮动元素会尽量向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。

浮动元素之后的元素将围绕它。

浮动元素之前的元素将不会受到影响。

如果图像是右浮动,下面的文本流将环绕在它左边:
实例
img
{
float:right;
}

彼此相邻的浮动元素

如果你把几个浮动的元素放到一起,如果有空间的话,它们将彼此相邻。

在这里,我们对图片廊使用 float 属性:
实例
.thumbnail
{
float:left;
width:110px;
height:90px;
margin:5px;
}

清除浮动 - 使用 clear
元素浮动之后,周围的元素会重新排列,为了避免这种情况,使用 clear 属性。
clear 属性指定元素两侧不能出现浮动元素。
使用 clear 属性往文本中添加图片廊:
实例
.text_line
{
clear:both;
}

<style> /* 保持原有样式不变 */ .cart-page-section { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); margin-bottom: 30px; } .cart-page-section table.cart { width: 100%; border-collapse: collapse; margin-bottom: 20px; } .cart-page-section table.cart th, .cart-page-section table.cart td { padding: 15px; text-align: left; border-bottom: 1px solid #eee; } .cart-page-section table.cart th { background-color: #f8f8f8; font-weight: bold; } .cart-page-section table.cart th.product-select, .cart-page-section table.cart td.product-select { width: 8%; text-align: center; vertical-align: middle; white-space: nowrap; } .cart-page-section table.cart td.product-info { width: 37%; vertical-align: top; } .cart-page-section table.cart .product-info .product-image { float: left; margin-right: 15px; width: 100px; height: 100px; } .cart-page-section table.cart .product-info .product-image img { max-width: 100%; height: auto; display: block; } .cart-page-section table.cart .product-info .product-name { overflow: hidden; } .cart-page-section table.cart td.product-price, .cart-page-section table.cart td.product-quantity, .cart-page-section table.cart td.product-subtotal { width: 15%; vertical-align: middle; } /* 添加到现有样式中 */ .add-to-wishlist { display: inline-block; margin-right: 5px; vertical-align: middle; text-decoration: none; } .remove-wrapper { display: inline-block; margin-left: 5px; } .product-remove .remove { display: inline-block; font-size: 20px; line-height: 1; text-decoration: none; color: #ccc; transition: color 0.3s; } .product-remove .remove:hover { color: #f44336; } /* 确保操作列内的按钮在同一行 */ .product-remove { white-space: nowrap; } .add-to-wishlist .wishlist-icon { font-size: 18px; } .cart-page-section table.cart td.product-remove { width: 5%; text-align: center; vertical-align: middle; } .cart-footer-actions { position: sticky; bottom: 0; background: #fff; padding: 15px; border-top: 1px solid #ddd; box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1); z-index: 1000; display: flex; flex-direction: column; gap: 15px; } .footer-left { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; } .coupon-section { display: flex; align-items: center; gap: 10px; } .selected-summary { font-size: 16px; font-weight: bold; flex: 1; } .cart-footer-actions .button { padding: 10px 20px; background-color: #f44336; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; transition: background-color 0.3s; white-space: nowrap; } .cart-footer-actions .button:hover { background-color: #d32f2f; } #partial-checkout { padding: 12px 24px; background-color: #2196F3; color: white; text-decoration: none; border-radius: 4px; display: inline-block; transition: background-color 0.3s; white-space: nowrap; text-align: center; font-weight: bold; } #partial-checkout:hover { background-color: #0b7dda; } #coupon_code { padding: 8px; width: 200px; border: 1px solid #ddd; border-radius: 4px; transition: border-color 0.3s; } #coupon_code:focus { border-color: #2196F3; outline: none; } /* 响应式样式 */ @media (max-width: 768px) { .cart-page-section table.cart thead { display: none; } .cart-page-section table.cart td { display: block; width: 100% !important; text-align: right; padding: 10px; position: relative; padding-left: 50%; } .cart-page-section table.cart td::before { content: attr(data-title); position: absolute; left: 15px; font-weight: bold; text-align: left; } .cart-page-section table.cart .product-info .product-image { float: none; margin: 0 auto 10px; } .cart-footer-actions { flex-direction: column; align-items: flex-start; } .footer-left { width: 100%; justify-content: space-between; } .coupon-section { width: 100%; margin-top: 10px; } .coupon-section input { flex: 1; } .selected-summary { text-align: center; margin-bottom: 10px; } #partial-checkout { width: 100%; padding: 15px; } .cart-footer-actions .button { padding: 12px 15px; margin: 5px 0; width: 100%; text-align: center; } } @media (max-width: 480px) { .cart-page-section { padding: 15px; } .cart-page-section table.cart td { padding-left: 45%; } .cart-page-section table.cart td::before { font-size: 14px; } .cart-footer-actions { padding: 10px; } #coupon_code { width: 100%; } } /* Change WooCommerce AJAX overlay color to black */ .blockUI.blockOverlay { background-color: #000 !important; opacity: 0.3 !important; } </style> is my style in cart.php correct?
最新发布
09-16
给出注释,不要分隔/**************************************************************************** ** ** This file is part of the LibreCAD project, a 2D CAD program ** ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl) ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file gpl-2.0.txt included in the ** packaging of this file. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ** ** This copyright notice MUST APPEAR in all copies of the script! ** **********************************************************************/ #ifndef QG_BLOCKWIDGET_H #define QG_BLOCKWIDGET_H #include <qwidget.h> #include <q3listbox.h> //Added by qt3to4: #include <QKeyEvent> #include <QContextMenuEvent> #include <QPixmap> #include "rs_blocklist.h" #include "rs_blocklistlistener.h" #include "qg_actionhandler.h" /** * This is the Qt implementation of a widget which can view a * block list. */ class QG_BlockWidget: public QWidget, public RS_BlockListListener { Q_OBJECT public: QG_BlockWidget(QG_ActionHandler* ah, QWidget* parent, const char* name=0, Qt::WFlags f = 0); ~QG_BlockWidget(); void setBlockList(RS_BlockList* blockList) { this->blockList = blockList; update(); } RS_BlockList* getBlockList() { return blockList; } void update(); void highlightBlock(RS_Block* block); //virtual void blockActivated(RS_Block *) {} virtual void blockAdded(RS_Block*) { update(); } virtual void blockEdited(RS_Block*) { update(); } virtual void blockRemoved(RS_Block*) { update(); } virtual void blockToggled(RS_Block*) { update(); } signals: void escape(); public slots: //void slotToggleView(QListBoxItem* item); void slotActivated(const QString& blockName); void slotMouseButtonClicked(int button, Q3ListBoxItem* item, const QPoint& pos); protected: void contextMenuEvent(QContextMenuEvent *e); virtual void keyPressEvent(QKeyEvent* e); private: RS_BlockList* blockList; Q3ListBox* listBox; RS_Block* lastBlock; QPixmap pxmVisible; QPixmap pxmHidden; QPixmap pxmAdd; QPixmap pxmRemove; QPixmap pxmAttributes; QPixmap pxmEdit; QPixmap pxmInsert; QPixmap pxmDefreezeAll; QPixmap pxmFreezeAll; QG_ActionHandler* actionHandler; }; #endif
07-04
以下是你提供的 `QG_BlockWidget.h` 头文件的**完整带注释版本**,使用 Markdown 格式展示: ```cpp /**************************************************************************** ** ** This file is part of the LibreCAD project, a 2D CAD program ** ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl) ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved. ** ** ** This file may be distributed and/or modified under the terms of the ** GNU General Public License version 2 as published by the Free Software ** Foundation and appearing in the file gpl-2.0.txt included in the ** packaging of this file. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ** ** This copyright notice MUST APPEAR in all copies of the script! ** **********************************************************************/ #ifndef QG_BLOCKWIDGET_H #define QG_BLOCKWIDGET_H // Qt 基础类头文件 #include <qwidget.h> // QWidget 基类 #include <q3listbox.h> // Qt3 的列表框控件(兼容性保留) #include <QKeyEvent> // 键盘事件支持 #include <QContextMenuEvent> // 右键菜单事件 #include <QPixmap> // 图像资源类 // LibreCAD 自定义类头文件 #include "rs_blocklist.h" // 包含块列表数据结构 #include "rs_blocklistlistener.h" // 块列表变化监听接口 #include "qg_actionhandler.h" // 动作处理器,用于用户交互响应 /** * 这是图块列表的 Qt 实现。该控件可以显示并操作 CAD 中的图块列表。 * * 它继承自 QWidget,并实现了 RS_BlockListListener 接口, * 能够实时监听图块列表的变化并更新 UI。 */ class QG_BlockWidget : public QWidget, public RS_BlockListListener { Q_OBJECT // 支持信号与槽机制 public: /** * 构造函数 * @param ah 动作处理器 * @param parent 父控件 * @param name 控件名称(Qt 名称机制) * @param f 控件窗口标志 */ QG_BlockWidget(QG_ActionHandler* ah, QWidget* parent, const char* name=0, Qt::WFlags f = 0); /** * 析构函数 */ ~QG_BlockWidget(); /** * 设置当前使用的块列表 * @param blockList 指向新的块列表 */ void setBlockList(RS_BlockList* blockList) { this->blockList = blockList; update(); // 更新界面 } /** * 获取当前使用的块列表 * @return 当前块列表指针 */ RS_BlockList* getBlockList() { return blockList; } /** * 刷新界面显示的图块列表 */ void update(); /** * 高亮指定图块 * @param block 要高亮的图块 */ void highlightBlock(RS_Block* block); // ---------------------- // RS_BlockListListener 接口实现 // 当图块列表发生变化时自动调用 virtual void blockAdded(RS_Block*) { update(); // 添加图块后刷新界面 } virtual void blockEdited(RS_Block*) { update(); // 编辑图块后刷新界面 } virtual void blockRemoved(RS_Block*) { update(); // 删除图块后刷新界面 } virtual void blockToggled(RS_Block*) { update(); // 切换图块状态后刷新界面 } signals: /** * ESC 键按下时发出该信号,通知主窗口重新获取焦点 */ void escape(); public slots: /** * 当用户选择一个图块时触发 * @param blockName 图块名称 */ void slotActivated(const QString& blockName); /** * 鼠标点击处理 * @param button 按下的按钮 * @param item 点击的项 * @param pos 鼠标位置 */ void slotMouseButtonClicked(int button, Q3ListBoxItem* item, const QPoint& pos); protected: /** * 显示上下文菜单(右键菜单) * @param e 上下文菜单事件 */ void contextMenuEvent(QContextMenuEvent *e); /** * 键盘按键事件处理 * @param e 键盘事件对象 */ virtual void keyPressEvent(QKeyEvent* e); private: RS_BlockList* blockList; // 当前管理的图块列表 Q3ListBox* listBox; // Qt3 兼容的列表框控件 RS_Block* lastBlock; // 最近选中的图块 // 各种图块操作相关的图标资源 QPixmap pxmVisible; // 可见图标 QPixmap pxmHidden; // 不可见图标 QPixmap pxmAdd; // 添加图块图标 QPixmap pxmRemove; // 删除图块图标 QPixmap pxmAttributes; // 属性图标 QPixmap pxmEdit; // 编辑图标 QPixmap pxmInsert; // 插入图标 QPixmap pxmDefreezeAll; // 解冻所有图标 QPixmap pxmFreezeAll; // 冻结所有图标 QG_ActionHandler* actionHandler; // 动作处理器,处理用户操作逻辑 }; #endif // QG_BLOCKWIDGET_H ``` --- ## 📚 补充知识点说明(每项不超过 50 字) | 知识点 | 说明 | |--------|------| | **Qt 继承与多重继承** | 类同时继承 QWidget 接口 RS_BlockListListener,实现图形控件业务逻辑解耦 | | **RS_BlockListListener 接口** | 通过接口监听块列表变化,实现视图与模型同步更新 | | **信号与槽 (`escape`) **( | Qt 事件通信机制,用于图块控件通知主窗口返回焦点 | --- 如果你还需要 `.cpp` 文件的详细注释版本或想深入了解某个模块的功能,请继续告诉我!我可以为你定制详细的讲解 😊
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值