*{margin: 0;padding: 0;font-size: 18px}
h1{margin: 20px 0 0 0;font-weight:100;clear: both;font-size: 24px}
span{float: left;padding:0 10px;width: 250px}
input{display: block}
section{height: 50px;width: 50px;border: 1px solid #333;line-height: 50px;text-align: center}
#display{border: 1px solid #222;display: flex;padding: 5px;}
#flex-direction{border: 1px solid #222;padding: 5px;display: flex;flex-direction: row}
#flex-wrap{border: 1px solid #222;padding: 5px;display: flex;flex-wrap:nowrap}
#justify-content{height: 100px;;border: 1px solid #222;padding: 5px;display: flex;justify-content: flex-start}
<h1>display</h1>
<span>
flex<input type="radio" name="display" />
inline-flex<input type="radio" name="display" />
</span>
<div id="display">
<section>1</section>
<section>2</section>
<section>3</section>
<section>4</section>
</div>
<h1>flex-direction</h1>
<span>
row<input type="radio" name="flex-direction" />
row-reverse<input type="radio" name="flex-direction"/>
column<input type="radio" name="flex-direction"/>
column-reverse<input type="radio" name="flex-direction"/>
</span>
<div id="flex-direction">
<section>1</section>
<section>2</section>
<section>3</section>
<section>4</section>
</div>
<h1>flex-wrap(设置伸缩子元素在缩小窗口时是否换行)</h1>
<span>
nowrap<input type="radio" name="flex-wrap" />
wrap<input type="radio" name="flex-wrap"/>
wrap-reverse<input type="radio" name="flex-wrap"/>
</span>
<div id="flex-wrap">
<section>1</section>
<section>2</section>
<section>3</section>
<section>4</section>
<section>5</section>
<section>6</section>
<section>7</section>
<section>8</section>
</div>
<h1>justify-content</h1>
<span>
flex-start<input type="radio" name="justify-content" />
flex-end<input type="radio" name="justify-content"/>
center<input type="radio" name="justify-content"/>
space-between<input type="radio" name="justify-content"/>
space-around<input type="radio" name="justify-content"/>
</span>
<div id="justify-content">
<section>1</section>
<section>2</section>
<section>3</section>
<section>4</section>
</div>
var btnFlex = document.getElementsByTagName("input");
var oDiv = document.getElementsByTagName("div");
/* display */
btnFlex[0].onclick = function (){
if(this.checked){
oDiv[0].style.display = "flex"; }}
btnFlex[1].onclick = function (){
if(this.checked){
oDiv[0].style.display = "inline-flex";}}
/* flex-direction */
btnFlex[2].onclick = function (){
if(this.checked){
oDiv[1].style.cssText = "flex-direction: row";}}
btnFlex[3].onclick = function (){
if(this.checked){
oDiv[1].style.cssText = "flex-direction: row-reverse";}}
btnFlex[4].onclick = function (){
if(btnFlex[4].checked){
oDiv[1].style.cssText = "flex-direction: column";}}
btnFlex[5].onclick = function (){
if(this.checked){
oDiv[1].style.cssText = "flex-direction: column-reverse";}}
/* flex-wrap */
btnFlex[6].onclick = function (){
if(this.checked){
oDiv[2].style.cssText = "flex-wrap: nowrap";}}
btnFlex[7].onclick = function (){
if(this.checked){
oDiv[2].style.cssText = "flex-wrap: wrap";}}
btnFlex[8].onclick = function (){
if(this.checked){
oDiv[2].style.cssText = "flex-wrap: wrap-reverse";}}
/* justify-content */
btnFlex[9].onclick = function (){
if(this.checked){
oDiv[3].style.cssText = "justify-content: flex-start";}}
btnFlex[10].onclick = function (){
if(this.checked){
oDiv[3].style.cssText = "justify-content: flex-end";}}
btnFlex[11].onclick = function (){
if(this.checked){
oDiv[3].style.cssText = "justify-content: center";}}
btnFlex[12].onclick = function (){
if(this.checked){
oDiv[3].style.cssText = "justify-content: space-between";}}
btnFlex[13].onclick = function (){
if(this.checked){
oDiv[3].style.cssText = "justify-content: space-around";}}
.flex-start{height: 100px;display: flex;border: 1px solid #333;padding: 5px;align-items:flex-start}
.flex-end{height: 100px;display: flex;border: 1px solid #333;padding: 5px;align-items:flex-end}
.center{height: 100px;display: flex;padding: 5px;border: 1px solid #333;align-items:center}
.baseline{height: 100px;padding: 5px;display: flex;border: 1px solid #333;align-items:baseline}
.stretch{height: 100px;padding: 5px;display: flex;border: 1px solid #333;align-items:stretch}
#align-items p{width: 50px;border: 1px solid #333}
<h1>align-items侧轴</h1>
<span>
stretch<input type="radio" name="align-items"/>
flex-start<input type="radio" name="align-items" />
flex-end<input type="radio" name="align-items"/>
center<input type="radio" name="align-items"/>
base-line<input type="radio" name="align-items"/>
</span>
<div id="align-items" class="stretch">
<p style="font-size: 12px">1</p>
<p>2</p>
<p style="font-size: 28px">3</p>
<p style="font-size: 40px">4</p>
</div>
.cflex-start{height: 300px;display: flex;border: 1px solid #333;padding: 5px;align-content:flex-start;flex-wrap:wrap}
.cflex-end{height: 300px;display: flex;border: 1px solid #333;padding: 5px;align-content:flex-end;flex-wrap:wrap}
.ccenter{height: 300px;display: flex;border: 1px solid #333;padding: 5px;align-content:center;flex-wrap:wrap}
.cspace-between{height: 300px;display: flex;border: 1px solid #333;padding: 5px;align-content:space-between;flex-wrap:wrap}
.cspace-around{height: 300px;display: flex;border: 1px solid #333;padding: 5px;align-content:space-around;flex-wrap:wrap}
.cstretch{height: 300px;display: flex;border: 1px solid #333;padding: 5px;align-content:stretch;flex-wrap:wrap}
<h1>align-content(设置伸缩子元素在缩小窗口时是否换行)</h1>
<span>
stretch<input type="radio" name="align-content"/>
flex-start<input type="radio" name="align-content" />
flex-end<input type="radio" name="align-content"/>
center<input type="radio" name="align-content"/>
space-between<input type="radio" name="align-content"/>
space-around<input type="radio" name="align-content"/>
</span>
<div id="align-content" class="cstretch">
<section>1</section>
<section>2</section>
<section>3</section>
<section>4</section>
<section>5</section>
<section>6</section>
<section>7</section>
<section>8</section>
</div>
btnFlex[19].onclick = function (){
if(this.checked){
oDiv[5].setAttribute("class","cstretch");}}
btnFlex[20].onclick = function (){
if(this.checked){
oDiv[5].setAttribute("class","cflex-start");}}
btnFlex[21].onclick = function (){
if(this.checked){
oDiv[5].setAttribute("class","cflex-end");}}
btnFlex[22].onclick = function (){
if(this.checked){
oDiv[5].setAttribute("class","ccenter");}}
btnFlex[23].onclick = function (){
if(this.checked){
oDiv[5].setAttribute("class","cspace-between");}}
btnFlex[24].onclick = function (){
if(this.checked){
oDiv[5].setAttribute("class","cspace-around");}}
.box{width: 210px;height: 30px;;border: 1px solid #333;display: flex}
.box1{width: 50px;background: #f00;flex-grow: 1}
.box2{width: 100px;background:#0f0;flex-grow: 2}
<div class="box">
<div class="box1">是非得失</div><div class="box2">送豆腐豆腐</div>
</div>
.box3{width: 220px;height: 30px;border: 1px solid #333;display: flex}
.box4{width: 150px;background: #f00;flex-shrink: 1}
.box5{width: 100px;background:#0f0;flex-shrink: 2}
<div class="box3">
<div class="box4">是非得失</div><div class="box5">送豆腐豆腐</div>
</div>
.ul2{width:600px;height: 200px;background: #999;display: flex;list-style: none;}
.ul2 li{height: 100px;width: 100px;background: #444}
.ul2 li:nth-of-type(2){flex-basis: 600px;background: #555}
<h1>flex-basis</h1>
<ul class="ul2">
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
.ul1{height: 100px;width: 600px;background: #999;display: flex;list-style: none;}
.ul1 li{width: 100px;margin-right: 20px;background: #333}
.ul1 li:nth-child(3),.ul1 li:nth-child(5){order: 1;}
<ul class="ul1"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul>
.ul3{height: 100px;width: 800px;padding: 20px; background: #999;display: flex;list-style: none;}
.ul3 li{width: 100px;margin-right: 20px;background: #333}
.ul3 li:nth-child(1){align-self: auto;}
.ul3 li:nth-child(2){align-self: flex-start;}
.ul3 li:nth-child(3){align-self: flex-end;}
.ul3 li:nth-child(4){align-self: center;}
.ul3 li:nth-child(5){align-self: baseline;padding: 20px}
.ul3 li:nth-child(6){align-self: baseline;}
.ul3 li:nth-child(7){align-self: stretch;}
@media screen and (min-width:1000px) {
body {background:#0f0;}
}
@media screen and (min-width:800px) and (max-width:1000px) {
body {background:#f00;}
}
@media screen and (max-width:800px) {
body{background:#00f;}
}
/* rem(web app使用)用根元素的font-size作为单位去匹配所有的单位 */
@media screen and (min-width:1000px) {
html{font-size: 48px;}
}
@media screen and (max-width: 800px) and (min-width:1000px) {
html{font-size: 36px;}
}
@media screen and (min-width:800px) {
html{font-size: 24px;}
}
.input2{width: 3rem;height: 2rem;background: #0ff;border: none;color: #fff;font-size: 1rem}
<h1>rem(web app使用)</h1>
<input class="input2" value="确定" type="submit">
.div{display:flex;flex-wrap: nowrap;background: #999;padding: 10px;width: 600px}
.div div{transition: all 0.5s;height: 100px;width: 200px;border:solid 1px #111;}
.div div:nth-child(1){background: url("../img/1.jpg")}
.div div:nth-child(2){background: url("../img/2.jpg")}
.div div:nth-child(3){background: url("../img/3.jpg")}
.div div:nth-child(4){background: url("../img/4.jpg")}
.div div:nth-child(5){background: url("../img/3.jpg")}
.div div:hover{width:400px}
<h1>练习</h1>
<div class="div">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
原文:https://www.cnblogs.com/solaris-wwf/p/11617435.html