css3 cheatsheet,CSS flexbox

本文深入探讨了CSS Flexbox布局的使用技巧与实践案例,包括垂直居中、元素重排及响应式设计等核心概念,并提供了多种场景下的具体实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Simple example

.container {

display: flex;

}

.container > div {

flex: 1 1 auto;

}

Container

.container {

display: flex;

display: inline-flex;

flex-direction: row; /* ltr - default */

flex-direction: row-reverse; /* rtl */

flex-direction: column; /* top-bottom */

flex-direction: column-reverse; /* bottom-top */

flex-wrap: nowrap; /* one-line */

flex-wrap: wrap; /* multi-line */

align-items: flex-start; /* vertical-align to top */

align-items: flex-end; /* vertical-align to bottom */

align-items: center; /* vertical-align to center */

align-items: stretch; /* same height on all (default) */

justify-content: flex-start; /* horizontal alignment - default */

justify-content: flex-end;

justify-content: center;

}

Child

.container > div {

/* This: */

flex: 1 0 auto;

/* Is equivalent to this: */

flex-grow: 1;

flex-shrink: 0;

flex-basis: auto;

order: 1;

align-self: flex-start; /* left */

margin-left: auto; /* right */

}

Tricks

Vertical center

.container {

display: flex;

}

.container > div {

width: 100px;

height: 100px;

margin: auto;

}

Vertical center (2)

.container {

display: flex;

align-items: center; /* vertical */

justify-content: center; /* horizontal */

}

Reordering

.container > .top {

order: 1;

}

.container > .bottom {

order: 2;

}

Mobile layout

.container {

display: flex;

flex-direction: column;

}

.container > .top {

flex: 0 0 100px;

}

.container > .content {

flex: 1 0 auto;

}

A fixed-height top bar and a dynamic-height content area.

Table-like

.container {

display: flex;

}

/* the 'px' values here are just suggested percentages */

.container > .checkbox { flex: 1 0 20px; }

.container > .subject { flex: 1 0 400px; }

.container > .date { flex: 1 0 120px; }

This creates columns that have different widths, but size accordingly according

to the circumstances.

Vertical

.container {

align-items: center;

}

Vertically-center all items.

Left and right

.menu > .left { align-self: flex-start; }

.menu > .right { align-self: flex-end; }

References

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值