
本教程将详细介绍如何使用css flexbox实现一个动态的两列布局,确保每行最多容纳两个子元素,并使其在空间不足时自动换行。特别地,我们将解决当子元素数量为奇数时,如何使最后一行的单个元素水平居中显示的问题,全程无需javascript介入。
在网页设计中,我们经常需要创建能够根据内容数量或屏幕尺寸动态调整的布局。本教程的目标是构建一个容器,其内部的子元素按照以下规则排列:
核心挑战在于如何在纯CSS环境下,既实现两列布局与自动换行,又能优雅地处理奇数项的居中问题。Flexbox是解决此类问题的理想工具。
首先,我们需要一个包含多个子元素的父容器。重要的是,子元素应使用类(class)而非ID(id),因为ID在HTML文档中必须是唯一的。
我们将使用一个父div作为Flex容器,内部包含任意数量的子div作为Flex项目。
立即学习“前端免费学习笔记(深入)”;
<div id="container">
<div class="smallerCon">
<div class="smallerLeftBoxText">Lorem ipsum dolor sit, amet consectetur adipisicing elit.</div>
</div>
<div class="smallerCon">
<div class="smallerLeftBoxText">Lorem ipsum dolor sit, amet consectetur adipisicing elit.</div>
</div>
<div class="smallerCon">
<div class="smallerLeftBoxText">Lorem ipsum dolor sit, amet consectetur adipisicing elit.</div>
</div>
<div class="smallerCon">
<div class="smallerLeftBoxText">Lorem ipsum dolor sit, amet consectetur adipisicing elit.</div>
</div>
<div class="smallerCon">
<div class="smallerLeftBoxText">Lorem ipsum dolor sit, amet consectetur adipisicing elit.</div>
</div>
<!-- 可以根据需要添加更多 .smallerCon 元素 -->
</div>为了确保元素的宽度计算符合预期,尤其是在设置边框或内边距时,建议全局设置box-sizing: border-box;。
/* 确保边框和内边距包含在元素的总宽度内 */
* {
box-sizing: border-box;
}我们将分别配置父容器(#container)和子元素(.smallerCon)的Flexbox属性。
父容器是Flexbox的起点,它定义了子元素的排列方式、换行行为以及水平对齐方式。
#container {
width: 90vw; /* 容器宽度,可根据需求调整 */
margin: auto; /* 容器自身水平居中 */
/* Flexbox 属性 */
display: flex; /* 启用 Flexbox 布局 */
flex-flow: row wrap; /* 定义主轴方向为行,并允许子元素换行 */
justify-content: center; /* 子元素在主轴(水平方向)上居中对齐 */
gap: 1rem; /* 子元素之间的间距 */
}子元素需要被配置为在Flex容器中占据合适的宽度,以确保每行显示两个。
.smallerCon {
height: 20vh; /* 子元素高度,可根据需求调整 */
min-width: 18rem; /* [可选] 设置最小宽度,防止在小屏幕上过度收缩 */
/* Flexbox 属性 */
/* flex: <flex-grow> <flex-shrink> <flex-basis>; */
flex: 0 1 calc(50% - 0.5rem); /* 关键属性,控制子元素宽度 */
/* 子元素内部如果还有内容,也可以作为Flex容器 */
display: flex;
flex-flow: row wrap; /* 内部内容也支持换行 */
/* 样式美化 */
background-image: linear-gradient(
to bottom right,
rgba(255, 0, 128, 0.577),
rgba(0, 204, 255, 0.49)
),
url("../img/26March.gif");
object-fit: cover;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
border: 2px solid rebeccapurple;
}.smallerLeftBoxText {
width: 40vw; /* 文本宽度 */
margin: auto; /* 文本自身在 .smallerCon 内部居中 */
font-weight: 400;
color: rgb(245, 223, 223);
}将上述HTML和CSS整合,即可得到一个功能完整的动态两列布局。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexbox动态两列布局与奇数项居中</title>
<style>
/* 全局盒模型设置 */
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
/* 父容器样式 */
#container {
width: 90vw; /* 容器宽度 */
margin: 20px auto; /* 容器自身水平居中,并设置上下边距 */
border: 2px dashed #ccc; /* 方便观察容器边界 */
padding: 1rem; /* 容器内边距 */
background-color: #fff;
/* Flexbox 属性 */
display: flex; /* 启用 Flexbox */
flex-flow: row wrap; /* 主轴方向为行,允许换行 */
justify-content: center; /* 子元素在主轴上居中对齐 */
gap: 1rem; /* 子元素之间的间距 */
}
/* 子元素样式 */
.smallerCon {
height: 20vh; /* 子元素高度 */
min-width: 18rem; /* 可选:设置最小宽度,防止小屏幕下过度收缩 */
border: 2px solid rebeccapurple; /* 边框 */
background-image: linear-gradient(
to bottom right,
rgba(255, 0, 128, 0.577),
rgba(0, 204, 255, 0.49)
),
url("https://via.placeholder.com/400x200/FF00FF/FFFFFF?text=Background+Image"); /* 示例背景图 */
background-size: cover;
background-repeat: no-repeat;
background-position: center;
color: rgb(245, 223, 223);
display: flex; /* 使子元素内部内容也能Flex布局 */
align-items: center; /* 垂直居中内部文本 */
justify-content: center; /* 水平居中内部文本 */
text-align: center; /* 文本自身居中 */
/* Flexbox 属性 */
/* flex-grow: 0, flex-shrink: 1, flex-basis: calc(50% - 0.5rem) */
flex: 0 1 calc(50% - 0.5rem); /* 关键:确保每行两列并考虑间距 */
}
.smallerLeftBoxText {
width: 90%; /* 文本占据 .smallerCon 的宽度 */
font-weight: 400;
padding: 10px;
background-color: rgba(0, 0, 0, 0.3); /* 文本背景,增加可读性 */
border-radius: 5px;
}
/* 响应式调整 */
@media (max-width: 768px) {
#container {
width: 95vw;
gap: 0.5rem;
}
.smallerCon {
flex: 0 1 calc(100% - 0.5rem); /* 小屏幕下每行一个 */
min-width: unset; /* 取消最小宽度限制 */
}
}
</style>
</head>
<body>
<div id="container">
<div class="smallerCon">
<div class="smallerLeftBoxText">这是第一个内容块。Flexbox布局实现了动态两列排版。</div>
</div>
<div class="smallerCon">
<div class="smallerLeftBoxText">这是第二个内容块。当空间不足时,元素会自动换行。</div>
</div>
<div class="smallerCon">
<div class="smallerLeftBoxText">这是第三个内容块。如果总数为奇数,最后一个元素会居中。</div>
</div>
<div class="smallerCon">
<div class="smallerLeftBoxText">这是第四个内容块。无需JavaScript,纯CSS即可实现。</div>
</div>
<div class="smallerCon">
<div class="smallerLeftBoxText">这是第五个内容块。因为是奇数,我被居中了!</div>
</div>
<!-- 可以根据需要添加更多 .smallerCon 元素 -->
</div>
</body>
</html>通过上述Flexbox技术,我们能够以纯CSS的方式,高效且灵活地构建出符合动态两列布局需求,并能智能处理奇数项居中显示的UI组件,极大地提升了前端开发的效率和代码的可维护性。
以上就是实现动态两列布局并居中奇数项的CSS Flexbox教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号