Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Images

Bootstrap Images

Goals

In this tutorial, we will discuss three Bootstrap 3 classes for styling images. We will also discuss what Bootstrap 3 offers for responsive images.

General Styles

Before we discuss the special classes Bootstrap 3 provides for defining image styles, we will look at the general styles Bootstrap 3 provides for images.



img {

  border: 0;

}

This is the first CSS rule for images found in Bootstrap 3's CSS, used to remove the border when an image is rendered.

Then, within the print media query, these rules are specified.



 img {

    page-break-inside: avoid;

  }

  img {

    max-width: 100% !important;

  }

page-break-inside: avoid; Avoid page breaks inside images.

max-width: 100% !important; The style defined for images is straightforward. Here it is used to ensure that even if the image's width exceeds the container, it is constrained to display within the container. It is used together with !important to override any other styles that break this style. Sometimes developers specifically use these two rules to make styles better support the friendly rendering of images on mobile devices.

Here is another rule for images



img {

  vertical-align: middle;

}

Thanks to this rule, an image will be vertically centered within a div or other element. As shown in the example below.

Example

     Bootstrap 3 Image Rendering Example

    

    

    

    

    

    

    

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.

click here to view the demo online

Note that if you need to vertically center an image depending on the context, additional styles are required.

Special Styles

Bootstrap 3 provides three classes for rendering images with specific styles.

img-rounded

You can use this class to render an image with rounded corners. To achieve this, Bootstrap provides img-rounded class. The styles of this class are defined as follows



.img-rounded {

  border-radius: 6px;

}

So, it sets the border-radius property to a value of 6 pixels, to define the rounded corners of the relevant image. The following example demonstrates two identical images; the first image does not have the img-rounded class, and the second image has the img-rounded class. Note that the second image has rounded corners. You can click here to view the example online。

Example

     Bootstrap 3 Rounded Image Rendering Example

    

    

    

    

    

    

    

image with rounded corners image with rounded corners

img-thumbnail

This is another Bootstrap 3 CSS class that adds a thumbnail to an image. The code for this class is shown below



.img-thumbnail {

  display: inline-block;

  height: auto;

  max-width: 100%;

  padding: 4px;

  line-height: 1.428571429;

  background-color: #ffffff;

  border: 1px solid #dddddd;

  border-radius: 4px;

  -webkit-transition: all 0.2s ease-in-out;

          transition: all 0.2s ease-in-out;

}

The following is an example using this class. You can click here to view the online demo。

Example

     Bootstrap 3 Thumbnail

    

    

    

    

    

    

    

image without thumbnail corners image with thumbnail corners

img-circle

By using the border-radius property, Bootstrap 3 creates an image rendered as a circle.img-circle The CSS code for the class is as follows



.img-circle {

  border-radius: 50%;

}

click here to view the example onlineThe screenshot and code are shown below.

Example

     Bootstrap 3 Circular Image

    

    

    

    

    

    

    

image without circle shape image with circle shape

Responsive images

Bootstrap 3 does not provide ready-to-use responsive imagesYou must add img-responsive class to the image to make it responsive. The CSS code for this class is as follows.



.img-responsive {

  display: block;

  height: auto;

  max-width: 100%;

}

It defines that the image must be displayed as a block-level element, the height will be the same as the image's height, and the maximum width of the image is 100%, to constrain the image to render according to the device it is on.

To make images responsive by default, you can add these CSS codes to img{ }。

An example using this class is as follows. You can click here to view the demo online。

Example

     Bootstrap 3 Responsive Images

    

    

    

    

    

    

    

without responsive image feature with responsive image feature

This method of adding responsiveness to images has limitations.

Load the same large high-resolution images on large-screen devices and mobile devices (with smaller screen sizes, which may causePerformance Issues). Since browsers preload images before CSS and JS load, this can also cause performance issues on a low-speed network connection. Imagine you have a large image with a specific object inside it; when you view the image on a mobile device, the image is scaled down to a smaller version, making the object look very small, referred to as art direction problem.

Developers have come up with solutions to overcome these limitations. We will look at an Marc Grabanski and Christopher Schmitt Use HiSRC example. This is a jQuery plugin that automatically creates low-, medium-, and high-resolution versions of an image, and the version presented depends on the resolution and bandwidth of the device displaying the image.

In our later responsive images tutorial we will discuss all these methods in detail.

click here to view the demo online. The code is as follows:

Example

     Bootstrap 3 Responsive Image Example Using HiSRC

    

    

    

    

    

    

    

Photo courtesy: /http://www.flickr.com/photos/cubagallery/