Bootstrap 中的响应式图像与示例

2025年3月17日 | 阅读 3 分钟

在本教程中,我们将通过示例学习如何在 Bootstrap 中使图像具有响应性。

Bootstrap 为图像提供了各种类,以改善其外观并使其具有响应性。尝试使图像具有响应性意味着它需要根据其父元素进行缩放。也就是说,图像的大小不能大于其父元素的大小,并且它需要与父元素的大小成比例地放大和缩小,而不会丢失其宽高比。

Bootstrap 中提供的图像类如下

1. .img-responsive 类: Bootstrap 中的响应式图像是通过将 .img-responsive 类添加到 img 标签来形成的。以下 .img-responsive 类应用于图像:display: block, height: auto 和 max-width: 100%。

HTML 代码

输出

Responsive Images in Bootstrap with Examples

2. .img-fluid 类: 在标签中包含 .img-fluid 类。.img-fluid 类应用于图像,具有以下设置:max-width: 100% | height: auto。

HTML 代码

输出

Responsive Images in Bootstrap with Examples

3. .img-rounded 类: img-rounded 类为图像添加圆角。(IE8 不支持圆角。)

HTML 代码

输出

Responsive Images in Bootstrap with Examples

4. .img-circle 类: img-circle 类将图像的结构转换为圆形。

HTML 代码

输出

Responsive Images in Bootstrap with Examples

5. .img-thumbnail 类: .img-thumbnail 类负责将图像转换为缩略图。

HTML 代码

输出

Responsive Images in Bootstrap with Examples

支持的浏览器

  • Google Chrome
  • Microsoft Internet Explorer
  • Safari
  • 火狐
  • Opera