- Mastering Bootstrap 4(Second Edition)
- Benjamin Jakobus Jason Marah
- 406字
- 2021-06-24 19:25:53
Responsive images
It is as straightforward as you would hope; just apply the img-fluid class to the element:
<div class="col-sm-3">
<img src="images/small.jpg" class="img-fluid">
<h5>Small</h5>
<div class="row">
<div class="col-sm-4">6x5</div>
<div class="col-sm-4">8x10</div>
<div class="col-sm-4">11x17</div>
</div>
</div>
<div class="col-sm-3">
<img src="images/medium.jpg" class="img-fluid">
<h5>Medium</h5>
<div class="row">
<div class="col-sm-4">12x18</div>
<div class="col-sm-4">16x20</div>
<div class="col-sm-4">18x24</div>
</div>
</div>
<div class="col-sm-3">
<img src="images/large.jpg" class="img-fluid">
<h5>Large</h5>
<div class="row">
<div class="col-sm-4">19x27</div>
<div class="col-sm-4">20x30</div>
<div class="col-md-4">22x28</div>
</div>
</div>
<div class="col-sm-3 ">
<img src="images/extra-large.jpg" class="img-fluid">
<h5>Extra Large</h5>
<div class="row">
<div class="col-md-4">24x36</div>
<div class="col-md-4">27x39</div>
<div class="col-md-4">27x40</div>
</div>
</div>
Take a look at Figure 2.15:

This is more like it; img-fluid is exceedingly simple in itself, essentially just adding a max-width: 100% rule to the image element. Now, the img element will respect the boundaries of its parent.
Images in Bootstrap 3 were made responsive using the img-responsive class. The img-fluid class in Bootstrap 4 is, in essence, the equivalent of img-responsive, just with a different name.
However, this simple approach also means that the feature is very basic. The browser still downloads the full resolution image, even though it may be rendered a fraction of the size. There are other libraries and services that help resolve the responsive images problem.
Bootstrap does, however, provide a neat mixin to help mitigate issues with retina displays. The img-retina mixin basically extends the background-image and background-size rules by allowing for two images and two sizes to be defined, one for standard displays and one for retina; img-retina takes the following form:
.img-retina(std-res-img, hi-res-img, standard-width, standard-height)
For standard displays, img-retina will set background-image to std-res-img, with the defined width and height. For retina display, img-retina will set background-image to hi-res-img, with the defined width and height values doubled.
For example, if we wanted to ensure that the Extra Large image is loaded in high resolution on retina displays, we could give it an extra-large-image class and apply that to a div:
<div class="extra-large-image"></div>
Using Sass, we would define extra-large-image as shown:
.extra-large-image {
.img-retina('/images/extra-large_std-res.png', '/images/extra-large_hi-res.png', 700px, 400px)
}
This will result in /images/extra-large_std-res.png being loaded with the 700 x 400 dimensions in standard resolution and /images/extra-large_hi-res.png being loaded in 1400 x 800 on retina displays.
When examining the Bootstrap code, as we have done in this chapter, you may note the repeated use of the z-index CSS property. This property allows one to control the stack order of elements. For example, one the element could be placed in front of another element sharing its x and y coordinates, by increasing the z-index of the element to be placed in front.
- Linux設備驅動開發詳解:基于最新的Linux4.0內核
- PLC控制程序精編108例
- Cybersecurity:Attack and Defense Strategies
- Learning Windows Server Containers
- Linux內核完全注釋(20周年版·第2版)
- 高性能Linux服務器構建實戰:運維監控、性能調優與集群應用
- Extending Bootstrap
- 嵌入式Linux應用開發菜鳥進階
- Fedora 12 Linux應用基礎
- iOS 8開發指南
- Windows 8實戰從入門到精通(超值版)
- Linux應用大全 基礎與管理
- 完美應用Ubuntu(第2版)
- Android應用性能優化最佳實踐
- Website Development with PyroCMS