CSS lets picture width adaptive screen Example

Source: Internet
Author: User

Mobile Browsing Adaptive Web pages, Safari browser can often automatically identify and layout, picture width is filled with the screen, then how to do without the reader when the effect of this? As shown here, let the picture width fill up the screen.

Because the picture is inside a P tag and is markdown into HTML format, you need to add the style to the P tag dynamically using jquery.

CSS code:

Css

@media screen and (max-width:768px) {
. img-width{
margin:auto-15px;
}
}


Here CSS 3 Media query Adaptive phone screen.

JS Code:


<script type= "Text/javascript" >
$image = $ (". post-content img");
$image. Parent (' P '). addclass (' img-width ');
$image. addclass (' img-responsive center-block ');
</script>

This effect is achieved, run the Gulp command to compress CSS and JS files.


The 1px in the Web page is not the same as the 1px in the physical pixel, so the results are different on different phones, and the following settings are found to fit the current Web page to adapt to different mobile phones, different browsers, the code is as follows:

[HTML] View plain copy on the code to see a piece of a snippet derived from my Code

<meta name= "viewport" content= "width=620px,initial-scale=1,target-densitydpi=device-dpi,minimum-scale=1, Maximum-scale=1,user-scalable=1 "/>

The width=620px is the minimum width required for the content area of the Web page, the need for a full screen on different mobile phones, target-densitydpi=device-dpi settings, the 1px in the CSS will be equal to the physical pixel 1px.

Add: Because Safari browser does not support target-densitydpi=device-dpi, so add JS code automatically adjust the scale, adjust the code:

[HTML] View plain copy on the code to see a piece of a snippet derived from my Code

<style type= "Text/css" >
@viewport
{
zoom:1.0;
width:620px;
}
@-ms-viewport
{
width:620px;
zoom:1.0;
}</style>
<meta name= "viewport" id= webviewport "content=" width=620px,initial-scale=1,target-densitydpi=device-dpi, Minimum-scale=0.5,maximum-scale=1,user-scalable=1 "/>
<meta name= "apple-mobile-web-app-capable" content= "yes" >
<meta name= "Apple-mobile-web-app-status-bar-style" content= "Black" >
<meta name= "format-detection" content= "Telephone=no" >
<script language= "JavaScript" >
if (screen.width<620)
{
document.getElementById (' Webviewport '). setattribute (' content ', ' width=620px,initial-scale= ' + screen.width/620 + ', Target-densitydpi=device-dpi,minimum-scale=0.5,maximum-scale=1,user-scalable=1 ');
}
</script>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.