CSS3 自适应的Flex
响应式的Flexbox
您从CSS媒体查询一章中了解到,可以使用媒体查询为不同的屏幕大小和设备创建不同的布局。
笔记本或台式电脑:
1
2
3
移动设备:
1
2
3
实例
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
.flex-container {
display: flex;
flex-direction: row;
font-size: 30px;
text-align: center;
}
.flex-item-left {
background-color: #f1f1f1;
padding: 10px;
flex: 50%;
}
.flex-item-right {
background-color: dodgerblue;
padding: 10px;
flex: 50%;
}
/* Responsive layout - makes a one column-layout instead of two-column layout */
@media (max-width: 800px) {
.flex-container {
flex-direction: column;
}
}
</style>
</head>
<body>
<h1>响应式的Flexbox</h1>
<p>"flex-direction: row;" 水平(从左到右)堆叠 flex 项。</p>
<p>"flex-direction: column;" 垂直(从上到下)堆叠 flex 项。</p>
<p><b>调整浏览器窗口的大小,以查看屏幕尺寸为800px或更小。</b></p>
<div class="flex-container">
<div class="flex-item-left">1</div>
<div class="flex-item-right">2</div>
</div>
</body>
</html>
使用 Flexbox 的响应式图库
使用 flexbox 创建响应式图像库,该图像库根据屏幕大小在四幅、两幅或全宽图像之间变化:
实例
<!DOCTYPE html>
<html>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial;
}
.header {
text-align: center;
padding: 32px;
}
.row {
display: flex;
flex-wrap: wrap;
padding: 0 4px;
}
/* Create four equal columns that sits next to each other */
.column {
flex: 25%;
max-width: 25%;
padding: 0 4px;
}
.column img {
margin-top: 8px;
vertical-align: middle;
}
/* Responsive layout - makes a two column-layout instead of four columns */
@media screen and (max-width: 800px) {
.column {
flex: 50%;
max-width: 50%;
}
}
/* Responsive layout - makes the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 600px) {
.column {
flex: 100%;
max-width: 100%;
}
}
</style>
<body>
<!-- Header -->
<div class="header">
<h2>响应式图片画廊</h2>
<p>调整浏览器窗口的大小以查看响应效果。</p>
</div>
<!-- Photo Grid -->
<div class="row">
<div class="column">
<img src="/images/wedding.jpg" style="width:100%">
<img src="/images/rocks.jpg" style="width:100%">
<img src="/images/falls2.jpg" style="width:100%">
<img src="/images/paris.jpg" style="width:100%">
<img src="/images/nature.jpg" style="width:100%">
<img src="/images/mist.jpg" style="width:100%">
<img src="/images/paris.jpg" style="width:100%">
</div>
<div class="column">
<img src="/images/underwater.jpg" style="width:100%">
<img src="/images/ocean.jpg" style="width:100%">
<img src="/images/wedding.jpg" style="width:100%">
<img src="/images/mountainskies.jpg" style="width:100%">
<img src="/images/rocks.jpg" style="width:100%">
<img src="/images/underwater.jpg" style="width:100%">
</div>
<div class="column">
<img src="/images/wedding.jpg" style="width:100%">
<img src="/images/rocks.jpg" style="width:100%">
<img src="/images/falls2.jpg" style="width:100%">
<img src="/images/paris.jpg" style="width:100%">
<img src="/images/nature.jpg" style="width:100%">
<img src="/images/mist.jpg" style="width:100%">
<img src="/images/paris.jpg" style="width:100%">
</div>
<div class="column">
<img src="/images/underwater.jpg" style="width:100%">
<img src="/images/ocean.jpg" style="width:100%">
<img src="/images/wedding.jpg" style="width:100%">
<img src="/images/mountainskies.jpg" style="width:100%">
<img src="/images/rocks.jpg" style="width:100%">
<img src="/images/underwater.jpg" style="width:100%">
</div>
</div>
</body>
</html>
使用 Flexbox 的响应式网站
使用 flexbox 创建响应式网站,其中包含弹性导航栏和弹性内容:
代码如下:
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
box-sizing: border-box;
}
/* Style the body */
body {
font-family: Arial;
margin: 0;
}
/* Header/logo Title */
.header {
padding: 60px;
text-align: center;
background: #1abc9c;
color: white;
}
/* Style the top navigation bar */
.navbar {
display: -ms-flexbox; /* IE10 */
display: flex;
background-color: #333;
}
/* Style the navigation bar links */
.navbar a {
color: white;
padding: 14px 20px;
text-decoration: none;
text-align: center;
}
/* Change color on hover */
.navbar a:hover {
background-color: #ddd;
color: black;
}
/* Column container */
.row {
display: -ms-flexbox; /* IE10 */
display: flex;
-ms-flex-wrap: wrap; /* IE10 */
flex-wrap: wrap;
}
/* Create two unequal columns that sits next to each other */
/* Sidebar/left column */
.side {
-ms-flex: 30%; /* IE10 */
flex: 30%;
background-color: #f1f1f1;
padding: 20px;
}
/* Main column */
.main {
-ms-flex: 70%; /* IE10 */
flex: 70%;
background-color: white;
padding: 20px;
}
/* Fake image, just for this example */
.fakeimg {
background-color: #aaa;
width: 100%;
padding: 20px;
}
/* Footer */
.footer {
padding: 20px;
text-align: center;
background: #ddd;
}
/* 响应式布局-当屏幕宽度小于700px时,使两列堆叠在一起,而不是相邻 */
@media screen and (max-width: 700px) {
.row, .navbar {
flex-direction: column;
}
}
</style>
</head>
<body>
<!-- Header -->
<div class="header">
<h2>我的网站</h2>
<p>使用了弹性布局。</p>
</div>
<!-- Navigation Bar -->
<div class="navbar">
<a href="#">链接</a>
<a href="#">链接</a>
<a href="#">链接</a>
<a href="#">链接</a>
</div>
<!-- The flexible grid (content) -->
<div class="row">
<div class="side">
<h2>关于我</h2>
<h5>我的照片:</h5>
<div class="fakeimg" style="height:200px;">图片</div>
<p>关于我的一些文本...</p>
<h3>更多文本</h3>
<p>文本文本文本文本...</p>
<div class="fakeimg" style="height:60px;">图片</div><br>
<div class="fakeimg" style="height:60px;">图片</div><br>
<div class="fakeimg" style="height:60px;">图片</div>
</div>
<div class="main">
<h2>标题</h2>
<h5>标题描述,2021 年 7 月 7 日</h5>
<div class="fakeimg" style="height:200px;">Image</div>
<p>一些文本...</p>
<p>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本...</p>
<br>
<h2>TITLE HEADING</h2>
<h5>标题描述,2021 年 107月 7 日</h5>
<div class="fakeimg" style="height:200px;">图片</div>
<p>一些文本...</p>
<p>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本...</p>
</div>
</div>
<!-- Footer -->
<div class="footer">
<h2>页脚</h2>
</div>
</body>
</html>