picture


Responsive Web Design -Pictures


Use the width attribute

If the width attribute is set to 100%, the picture will be implemented according to the upper and lower ranges Responsive function:

Instance

     php 中文网  

调整浏览器窗口查看图像是如何扩展的。


Run instance»

Click the "Run instance" button to view the online instance

Note that in the above example, the image will be larger than its original image. We can use themax-widthattribute to solve this problem very well.


Use the max-width attribute

If the max-width attribute is set to 100%, the image will never be larger than its original size:

Example

     php 中文网  

调整浏览器大小,在宽度小于 460px 时查看图片比例变化。


Run instance»

Click the "Run instance" button to view the online instance


Add pictures to the webpage

Instance

     php中文网(php.cn)  

Chania

The City

Chania is the capital of the Chania region on the island of Crete. The city can be divided in two parts, the old town and the modern city.

What?

Chania is a city on the island of Crete.

Where?

Crete is a Greek island in the Mediterranean Sea.

How?

You can reach Chania airport from all over Europe.


Run instance?

Click the "Run Instance" button to view the online instance


Background Image

Background image can be resized or scaled responsively.

The following are three different methods:

1. If the background-size property is set to "contain", the background image will adapt proportionally to the content area. The image keeps its proportions:


Here is the CSS code:

Example

     php 中文网  

调整浏览器大小查看效果。


Run instance»

Click the "Run instance" button to view the online instance

2. If the background-size attribute is set to "100% 100%", the background The image will stretch to cover the entire area:


Instance

     php 中文网  

调整浏览器大小查看效果。


Run Instance»

Click the "Run Instance" button to view the online instance

3. If the background-size property is set to "cover", the background image will be expanded to be large enough so that the background image completely covers the background area. Note that this property maintains the proportions of the image so Some parts of the background image cannot be displayed in the background anchor area.


This is the CSS code:

Example

     php 中文网  

调整浏览器大小查看效果。


Run Example»

Click the "Run Instance" button to view the online instance


Different devices display different pictures

Large size pictures can be displayed on large screens, but on small screens It really doesn't display well on the screen. There is no need for us to load large images on a small screen, as this will affect the loading speed. So we can use media queries to display different images based on different devices.

The following large and small pictures will be displayed on different devices:

Instance

     php 中文网  

调整浏览器宽度,背景图片在小于 400 px 时将改变。


Run instance»

Click the "Run instance" button to view the online instance

You can use the min-device-width of the media query instead of the min-width attribute , it will detect the device width instead of the browser width. Image size does not change when the browser is resized.

Instance

     php 中文网  

Run instance »

Click the "Run instance" button to view the online instance


HTML5 element

Theelement of HTML5 can set multiple pictures.

Browser support

##:visited Not supported 38.0 38.0 Not supported 25.0
Function 1546322463316818.gif 1546322467179911.gif 3.gif 4.gif 5.gif
##

Elements similar toandelements. Different resources can be configured. The first resource set is the preferred one:

Instance

     php 中文网 
Flowers

调整浏览器宽度和高度,背景在宽度小于 400px 时将改变。

Running Instance»
Click the "Run Instance" button to view the online instance

srcset

The attribute is required and defines the image resource.

media

The attribute is optional and can be viewed for details in theCSS @media ruleof the media query.For browsers that do not support the

element, you can also define theelement instead.