Bootstrap4 图像形状

图像在现代网页设计中非常普遍。因此,对图像进行样式设置并将其正确放置在网页上对于改善用户体验非常重要。

使用Bootstrap内置类,您可以轻松设置图像的样式,例如制作圆角或圆形图像,或赋予它们类似缩略图的效果。

圆角图片

.rounded 类可以让图片显示圆角效果:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap 示例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>圆角图片</h2>
  <p>.rounded 类可以让图片显示圆角效果:</p>            
  <img src="https://www.nhooo.com/run/images/cinqueterre.jpg" class="rounded" alt="Cinque Terre" width="304" height="236"> 
</div>
</body>
</html>
测试看看 ‹/›

椭圆图片

.rounded-circle 类可以设置椭圆形图片:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap 示例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>椭圆图片</h2>
  <p>.rounded-circle 类可以设置椭圆形图片:</p>            
  <img src="https://www.nhooo.com/run/images/cinqueterre.jpg" class="rounded-circle" alt="Cinque Terre"> 
</div>
</body>
</html>
测试看看 ‹/›

缩略图

.img-thumbnail 类用于设置图片缩略图(图片有边框):

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap 示例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>缩略图</h2>
  <p>.img-thumbnail 类用于设置图片缩略图(图片有边框):</p>            
  <img src="https://www.nhooo.com/run/images/cinqueterre.jpg" class="img-thumbnail" alt="Cinque Terre">
</div>
</body>
</html>
测试看看 ‹/›

图片对齐方式

使用 .float-right 类来设置图片右对齐,使用 .float-left 类设置图片左对齐:

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap 示例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>图片对齐方式</h2>
  <p>使用 .float-right 类来设置图片右对齐,使用 .float-left 类设置图片左对齐:</p>
  <img src="https://www.nhooo.com/run/images/paris.jpg" class="float-left"> 
  <img src="https://www.nhooo.com/run/images/cinqueterre.jpg" class="float-right">
</div>
</body>
</html>
测试看看 ‹/›

响应式图片

图像有各种各样的尺寸,我们需要根据屏幕的大小自动适应。

我们可以通过在 <img>  标签中添加 .img-fluid 类来设置响应式图片。

.img-fluid 类设置了 max-width: 100%; 、 height: auto; :

<!DOCTYPE html>
<html>
<head>
  <title>Bootstrap 示例</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>响应式图片</h2>
  <p>.img-fluid 类可以设置响应式图片,重置浏览器大小查看效果:</p>
  <img src="https://www.nhooo.com/run/images/paris.jpg" class="img-fluid">
</div>
</body>
</html>
测试看看 ‹/›