Bootstrap 5学习教程,从入门到精通,Bootstrap 5 图像形状(Image Shapes)语法知识点及案例代码(8)
Bootstrap 5 图像形状(Image Shapes)语法知识点及案例代码
Bootstrap 5 提供了多种工具类来轻松实现图像的不同形状,如圆角、圆形和椭圆形。
目录
- 基本图像类
- 圆角图像
- 圆形图像
- 椭圆形图像
- 响应式图像
- 案例代码
1. 基本图像类
1.1 .img-fluid
使图像在其父容器中自适应大小,保持其原始比例。
<img src="..." class="img-fluid" alt="Responsive image">
1.2 .img-thumbnail
为图像添加一个带有边框的缩略图效果。
<img src="..." class="img-thumbnail" alt="Thumbnail image">
2. 圆角图像
Bootstrap 5 使用 border-radius
类来控制图像的圆角效果。
2.1 .rounded
为图像添加标准的圆角。
<img src="..." class="rounded" alt="Rounded image">
2.2 .rounded-top
仅在图像的顶部添加圆角。
<img src="..." class="rounded-top" alt="Rounded top image">
2.3 .rounded-end
在图像的右侧(对于 LTR 语言)添加圆角。
<img src="..." class="rounded-end" alt="Rounded end image">
2.4 .rounded-bottom
仅在图像的底部添加圆角。
<img src="..." class="rounded-bottom" alt="Rounded bottom image">
2.5 .rounded-start
在图像的左侧(对于 LTR 语言)添加圆角。
<img src="..." class="rounded-start" alt="Rounded start image">
2.6 .rounded-circle
将图像裁剪为圆形。
<img src="..." class="rounded-circle" alt="Rounded circle image">
2.7 .rounded-0
移除图像的所有圆角。
<img src="..." class="rounded-0" alt="No rounded image">
3. 圆形图像
使用 .rounded-circle
类可以将图像裁剪为完美的圆形。
<img src="..." class="rounded-circle" alt="Circular image">
4. 椭圆形图像
Bootstrap 5 没有直接的椭圆形类,但可以通过自定义 CSS 实现。
4.1 使用自定义 CSS 实现椭圆形
<style>.img-ellipse {border-radius: 50% / 25%;}
</style><img src="..." class="img-fluid img-ellipse" alt="Ellipse image">
5. 响应式图像
5.1 .img-fluid
使图像在其父容器中自适应大小,保持其原始比例。
<img src="..." class="img-fluid" alt="Responsive image">
5.2 结合其他类
可以结合圆角、圆形等类,实现响应式且具有特定形状的图像。
<img src="..." class="img-fluid rounded-circle" alt="Responsive circular image">
6. 案例代码
以下是一个综合示例,展示如何使用 Bootstrap 5 的图像形状类。该示例包含:
- 基本的响应式图像
- 圆角图像
- 圆形图像
- 椭圆形图像(通过自定义 CSS)
- 带有缩略图效果的图像
<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>Bootstrap 5 图像形状示例</title><!-- 引入 Bootstrap 5 CSS --><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"><style>/* 自定义椭圆形类 */.img-ellipse {border-radius: 50% / 25%;}</style>
</head>
<body><div class="container my-5"><h1 class="mb-4">Bootstrap 5 图像形状示例</h1><!-- 1. 基本响应式图像 --><div class="row mb-4"><div class="col-md-6"><h3>基本响应式图像</h3><img src="https://via.placeholder.com/300" class="img-fluid" alt="Responsive image"><p>使用 <code>.img-fluid</code> 类使图像在父容器中自适应大小。</p></div></div><!-- 2. 圆角图像 --><div class="row mb-4"><div class="col-md-6"><h3>圆角图像</h3><img src="https://via.placeholder.com/300" class="img-fluid rounded" alt="Rounded image"><p>使用 <code>.rounded</code> 类为图像添加标准圆角。</p></div><div class="col-md-6"><h3>仅顶部圆角</h3><img src="https://via.placeholder.com/300" class="img-fluid rounded-top" alt="Rounded top image"><p>使用 <code>.rounded-top</code> 类仅在图像顶部添加圆角。</p></div></div><!-- 3. 圆形图像 --><div class="row mb-4"><div class="col-md-6"><h3>圆形图像</h3><img src="https://via.placeholder.com/300" class="img-fluid rounded-circle" alt="Circular image"><p>使用 <code>.rounded-circle</code> 类将图像裁剪为圆形。</p></div><div class="col-md-6"><h3>椭圆形图像(自定义)</h3><img src="https://via.placeholder.com/300" class="img-fluid img-ellipse" alt="Ellipse image"><p>使用自定义 CSS 类 <code>.img-ellipse</code> 实现椭圆形图像。</p></div></div><!-- 4. 缩略图图像 --><div class="row mb-4"><div class="col-md-6"><h3>缩略图图像</h3><img src="https://via.placeholder.com/300" class="img-thumbnail" alt="Thumbnail image"><p>使用 <code>.img-thumbnail</code> 类为图像添加缩略图效果。</p></div><div class="col-md-6"><h3>组合使用</h3><img src="https://via.placeholder.com/300" class="img-fluid rounded-circle" alt="Combined image"><p>结合使用 <code>.img-fluid</code> 和 <code>.rounded-circle</code> 类,实现响应式且圆形的图像。</p></div></div><!-- 5. 移除圆角 --><div class="row"><div class="col-md-6"><h3>移除圆角</h3><img src="https://via.placeholder.com/300" class="img-fluid rounded-0" alt="No rounded image"><p>使用 <code>.rounded-0</code> 类移除图像的所有圆角。</p></div></div>
</div><!-- 引入 Bootstrap 5 JS(可选) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script></body>
</html>
代码说明
-
引入 Bootstrap 5 CSS
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
通过 CDN 引入 Bootstrap 5 的 CSS 文件,确保可以使用 Bootstrap 的所有功能。
-
自定义 CSS
.img-ellipse {border-radius: 50% / 25%; }
使用自定义类
.img-ellipse
实现椭圆形图像。通过调整border-radius
的比例,可以实现不同的椭圆形效果。 -
图像示例
-
基本响应式图像
<img src="https://via.placeholder.com/300" class="img-fluid" alt="Responsive image">
使用
.img-fluid
类使图像自适应父容器大小。 -
圆角图像
<img src="https://via.placeholder.com/300" class="img-fluid rounded" alt="Rounded image">
使用
.rounded
类为图像添加标准圆角。 -
圆形图像
<img src="https://via.placeholder.com/300" class="img-fluid rounded-circle" alt="Circular image">
使用
.rounded-circle
类将图像裁剪为圆形。 -
椭圆形图像
<img src="https://via.placeholder.com/300" class="img-fluid img-ellipse" alt="Ellipse image">
使用自定义类
.img-ellipse
实现椭圆形图像。 -
缩略图图像
<img src="https://via.placeholder.com/300" class="img-thumbnail" alt="Thumbnail image">
使用
.img-thumbnail
类为图像添加缩略图效果。 -
移除圆角
<img src="https://via.placeholder.com/300" class="img-fluid rounded-0" alt="No rounded image">
使用
.rounded-0
类移除图像的所有圆角。
-
-
组合使用
<img src="https://via.placeholder.com/300" class="img-fluid rounded-circle" alt="Combined image">
结合使用
.img-fluid
和.rounded-circle
类,实现响应式且圆形的图像。
总结
通过上述语法知识点和案例代码,Bootstrap 5 初学者可以轻松实现各种图像形状效果。掌握这些工具类后,可以根据项目需求灵活应用,提升网页的视觉效果和用户体验。