Thứ Tư, Tháng Hai 12, 2025
spot_img
HomeStudyLesson 7: Images in Bootstrap 5

Lesson 7: Images in Bootstrap 5

What is img-fluid? It is part of the Bootstrap image. Here's everything you need to know about images in Bootstrap 5.

Bootstrap is an open source CSS framework, primarily for building responsive web and mobile applications. This framework includes general CSS settings; a responsive grid system, pre-built elements, slides, forms, navigation bars and optional JavaScript plugins. All help you speed up web and application development.

You can create components by adding the Bootstrap class to the elements. Those classes also style the images and optimize them for responsive behavior, which means scaling along with the image container when the browser window is resized.

An important factor in flexible layouts is responsive images. They should be resized, oriented, and resolution based on the end user device, allowing websites to detect and respond to each visitor's screen conditions. And Bootstrap will help you do that easily.

Adaptive photo

In Bootstrap, you would use class .img-fluid on all images you want it to be responsive. This class will “tell” the browser not to expand the image larger than its original size by using it max-widthand shrink the image if the browser window is narrower than the width of the image (in pixels).

To make images responsive, you add a class .img-fluid to the card . This class will apply max-width: 100%; and height: auto; for photos.

Ảnh thích ứng

Quantrimang.com icon

If you want to fix the fixed size of the image, just add width=”kich_thuoc_px” and height=”kich_thuoc_px” after the element alt of card như trên, nếu để ảnh theo đúng kích thước gốc, bạn bỏ width và height đi là được. Chạy code trên ta có kết quả sau:

Post responsive images on Bootstrap

Rounded corners for photos

Class .rounded will round the corners of the image (equivalent to border-radius:6px;) like the example below:

Bo góc ảnh

Quantrimang.com icon

Then the 4 corners of the image will be very slightly rounded as follows:

Xem thêm  Data types in C programming

Round image corners in Bootstrap

Round the image

Class .rounded-circle will crop the image into a circle like below:

Bo tròn ảnh

Quantrimang.com

The uploaded photo has a pink background, but when using class .rounded-circle then you will only see the following:

Round images in Bootstrap

Thumbnails, thumbnails in Bootstrap

Class .img-thumbnail will convert the photo to a thumbnail, with a border. And just like above, if you need to fix the size, you only need to declare the width and height of the image.

Thumbnail

Quantrimang.com icon

When running the above file you will receive the following results:

Convert photos to thumbnails

Align image left/right

You align the image to the right using class .float-rightalign to the left with .float-left:

Căn lề cho ảnh

BootstrapBootstrap

The files on the image are aligned left and right at the same time for your convenience, the results are as follows:

Align images in Bootstrap

Align image to center on Bootstrap

To center the image is a bit more complicated, you will have to add a class .mx-auto (margin:auto) and .d-block (display:block) for photos, like the example below:

Căn giữa ảnh

Bootstrap

As a result, the image is centered as shown below:

Center the image in Bootstrap

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments