CSS 盒模型是理解 CSS 布局的基础,它包括内容(content)、内边距(padding)、边框(border)和外边距(margin)四个部分。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>盒模型案例title>
<style>
.box {
width: 200px; /* 设置内容区域的宽度 */
height: 100px; /* 设置内容区域的高度 */
padding: 20px; /* 内边距,内容与边框之间的距离 */
border: 2px solid #000; /* 边框,实线、宽度2px、黑色 */
margin: 20px; /* 外边距,边框与其他元素之间的距离 */
background-color: #f0f0f0; /* 背景颜色 */
}
style>
head>
<body>
<div class="box">
这是一个盒模型的示例。
div>
body>
html>
浮动(float)是 CSS 中一种常用的布局方式,主要用于实现元素的横向排列。常见的浮动方向有左浮动(float: left)和右浮动(float: right)。浮动元素会脱离正常的文档流,因此需要使用清除浮动(clear: both)来解决父元素高度塌陷的问题。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>浮动布局案例title>
<style>
.container {
border: 1px solid #000;
padding: 10px;
}
.float-left {
float: left; /* 左浮动 */
width: 100px;
height: 100px;
background-color: #f0f0f0;
margin-right: 10px;
}
.float-right {
float: right; /* 右浮动 */
width: 100px;
height: 100px;
background-color: #f0f0f0;
margin-left: 10px;
}
.clearfix::after {
content: "";
display: table;
clear: both; /* 清除浮动 */
}
style>
head>
<body>
<div class="container clearfix">
<div class="float-left">左浮动div>
<div class="float-right">右浮动div>
div>
body>
html>
CSS 定位(position)用于精确控制元素的位置,常见的定位类型有静态定位(position: static,默认值)、相对定位(position: relative)、绝对定位(position: absolute)和固定定位(position: fixed)。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>定位布局案例title>
<style>
body {
margin: 0;
padding: 0;
height: 2000px; /* 设置页面高度,方便测试固定定位 */
}
.relative-box {
position: relative; /* 相对定位 */
width: 200px;
height: 200px;
background-color: #f0f0f0;
margin: 50px auto;
}
.absolute-box {
position: absolute; /* 绝对定位 */
width: 100px;
height: 100px;
background-color: #ff0000;
top: 100px;
left: 100px;
}
.fixed-box {
position: fixed; /* 固定定位 */
width: 100px;
height: 100px;
background-color: #00ff00;
top: 20px;
right: 20px;
}
style>
head>
<body>
<div class="relative-box">
相对定位的盒子
<div class="absolute-box">绝对定位的盒子div>
div>
<div class="fixed-box">固定定位的盒子div>
body>
html>
弹性布局(Flexbox)是一种现代的布局模式,用于简化和增强一维布局(行或列)的设计。主要涉及容器属性和项目属性。
容器属性:
项目属性:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>弹性布局案例title>
<style>
.flex-container {
display: flex; /* 设置为弹性容器 */
justify-content: space-between; /* 主轴对齐方式:两端对齐 */
align-items: center; /* 交叉轴对齐方式:居中 */
height: 100px;
background-color: #f0f0f0;
padding: 10px;
}
.flex-item {
width: 100px;
height: 100px;
background-color: #ff0000;
margin: 0 10px;
}
.flex-item:nth-child(2) {
align-self: flex-start; /* 单个项目交叉轴对齐方式:顶部对齐 */
}
style>
head>
<body>
<div class="flex-container">
<div class="flex-item">项目1div>
<div class="flex-item">项目2div>
<div class="flex-item">项目3div>
div>
body>
html>
网格布局(Grid)是一种更强大的二维布局模式,用于创建复杂的网格结构。主要涉及容器属性和项目属性。
容器属性:
项目属性:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>网格布局案例title>
<style>
.grid-container {
display: grid; /* 设置为网格容器 */
grid-template-columns: 100px 100px 100px; /* 定义三列,每列宽度100px */
grid-template-rows: 100px 100px; /* 定义两行,每行高度100px */
grid-gap: 10px; /* 网格间距 */
background-color: #f0f0f0;
padding: 10px;
}
.grid-item {
background-color: #ff0000;
display: flex;
justify-content: center;
align-items: center;
}
.grid-item:nth-child(1) {
grid-column: 1 / 2; /* 占据第一列 */
grid-row: 1 / 2; /* 占据第一行 */
}
.grid-item:nth-child(2) {
grid-column: 2 / 3; /* 占据第二列 */
grid-row: 1 / 2; /* 占据第一行 */
}
.grid-item:nth-child(3) {
grid-column: 3 / 4; /* 占据第三列 */
grid-row: 1 / 2; /* 占据第一行 */
}
.grid-item:nth-child(4) {
grid-column: 1 / 2; /* 占据第一列 */
grid-row: 2 / 3; /* 占据第二行 */
}
.grid-item:nth-child(5) {
grid-column: 2 / 4; /* 占据第二列和第三列 */
grid-row: 2 / 3; /* 占据第二行 */
}
style>
head>
<body>
<div class="grid-container">
<div class="grid-item">项目1div>
<div class="grid-item">项目2div>
<div class="grid-item">项目3div>
<div class="grid-item">项目4div>
<div class="grid-item">项目5div>
div>
body>
html>
多列布局用于将内容分成多列显示,类似于报纸的排版方式。主要涉及以下属性:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多列布局案例title>
<style>
.multi-column {
column-count: 3; /* 定义三列 */
column-gap: 20px; /* 列间距 */
column-rule: 2px solid #000; /* 列分隔线 */
padding: 10px;
}
.multi-column p {
margin: 0;
padding: 5px 0;
}
style>
head>
<body>
<div class="multi-column">
<p>这是一个多列布局的示例。多列布局可以将内容分成多列显示,类似于报纸的排版方式。这种布局方式在展示大量文本内容时非常有用,可以提高阅读体验。p>
<p>多列布局通过 CSS 的 column-count、column-width、column-gap 和 column-rule 等属性来实现。column-count 定义列的数量,column-width 定义列的宽度,column-gap 定义列之间的间距,column-rule 定义列之间的分隔线。p>
<p>在实际应用中,多列布局常用于文章页面、新闻列表等场景。通过合理的列数和间距设置,可以使页面布局更加美观和实用。p>
div>
body>
html>
以上是 CSS3 布局的一些常见知识点及案例代码,涵盖了盒模型、浮动布局、定位布局、弹性布局、网格布局和多列布局等内容。通过这些案例代码,可以更好地理解和掌握 CSS3 布局的应用。
以下是一些开发中常用的实际具体案例:
nav ul ul {
opacity: 0;
visibility: hidden;
transform: translateY(-20px);
transition: all 0.3s ease;
}
nav ul li:hover > ul {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
.sidebar {
width: 0;
overflow: hidden;
transition: width 0.3s ease;
}
.sidebar.open {
width: 200px;
}
.sidebar ul li {
opacity: 0;
transform: translateX(-20px);
transition: all 0.3s ease 0.1s;
}
.sidebar.open ul li {
opacity: 1;
transform: translateX(0);
}
.slider img {
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 1s ease;
}
.slider img.active {
opacity: 1;
}
.image-popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
opacity: 0;
transition: all 0.3s ease;
z-index: 999;
}
.image-popup.show {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}
.image-popup::before {
content: "";
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
opacity: 0;
transition: opacity 0.3s ease;
z-index: -1;
}
.image-popup.show::before {
opacity: 1;
}
button {
background-color: #3498db;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
transition: all 0.3s ease;
}
button:hover {
background-color: #2980b9;
transform: scale(1.1);
}
button:active {
background-color: #1f618d;
transform: scale(0.9);
}
.loading-button {
position: relative;
}
.loading-button::after {
content: "";
position: absolute;
top: 50%;
left: 50%;
width: 20px;
height: 20px;
border-radius: 50%;
border: 2px solid white;
border-top-color: transparent;
animation: loading 1s linear infinite;
transform: translate(-50%, -50%);
z-index: -1;
}
@keyframes loading {
from {
transform: translate(-50%, -50%) rotate(0deg);
}
to {
transform: translate(-50%, -50%) rotate(360deg);
}
}
.blinking-text {
animation: blink 1s infinite;
}
@keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.scroll-text {
white-space: nowrap;
overflow: hidden;
animation: scroll 5s linear infinite;
}
@keyframes scroll {
0% {
transform: translateX(100%);
}
100% {
transform: translateX(-100%);
}
}
.page {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: all 0.5s ease;
}
.page.active {
opacity: 1;
}
.page-enter {
transform: translateX(100%);
}
.page-enter-active {
transform: translateX(0);
transition: all 0.5s ease;
}
.page-leave {
transform: translateX(0);
}
.page-leave-active {
transform: translateX(-100%);
transition: all 0.5s ease;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
opacity: 0;
transition: all 0.3s ease;
z-index: 9999;
background-color: white;
border-radius: 5px;
padding: 20px;
}
.modal.show {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}