简体   繁体   English

更好的方法来自动调整图像的纵横比?

[英]Better way to auto resize images mantaning aspect ratio?

I am using this code to auto resize images to the window size on a mobile page: 我正在使用此代码在移动页面上自动将图像调整为窗口大小:

img {
  width:100%;
  max-height : auto;
  max-width : 480px;
}

My intention is to show the image in the right size of the window on small screens and max 480px on bigger screens maintaining the ratio. 我的目的是在小屏幕上以适当大小的窗口显示图像,在更大的屏幕上显示最大480px,保持比例。

But for some reason i dont know when i use that code the text around the image goes behind it. 但由于某些原因,我不知道我什么时候使用该代码,图像周围的文字落后于它。

Theres a way to achieve this result using another method like Java or Jquery and avoid this problem? 有一种方法可以使用Java或Jquery等其他方法来实现这个结果并避免这个问题吗?

If you do this for mobile devices I would recomend server resize to save download size. 如果你为移动设备执行此操作,我会建议服务器调整大小以节省下载大小。

Regarding the text that goes behind, do you have a more comprehensive testcase showing the actual document this CSS applies to? 关于落后的文本,您是否有更全面的测试用例显示此CSS适用的实际文档?

You'll end up with squished images if you do that. 如果你这样做,你最终会得到压扁的图像。 I think this gives the best result you can achieve with CSS: 我认为这会给你用CSS实现最好的结果:

#content img {
    width: 100%;
    height: auto;
    max-width: 480px;
}

I added this code in my page and it's working: 我在我的页面中添加了此代码并且它正在工作:

img {
  width:100%;
  max-height : auto;
  max-width : 480px;
}

Have you tried using different style sheets for different screen sizes? 您是否尝试过针对不同屏幕尺寸使用不同的样式表? Then you would just need to write the code for each situation and then load the needed style. 然后,您只需要为每种情况编写代码,然后加载所需的样式。 It would also come in handy if you have other styles that need to change based on size. 如果您有其他需要根据尺寸更改的样式,它也会派上用场。 Very helpful on mobile sites. 在移动网站上非常有帮助。

<meta content="width=device-width, initial-scale=1.0" name="viewport">

helps to make sure it scales right. 有助于确保它正确缩放。 Not sure how helpful it will be, but hope this link helps. 不确定它会有多大帮助,但希望这个链接有所帮助。

CSS trick for specific style sheet 特定样式表的CSS技巧

I've also played around a little, and it seems to work if you set the image as a percentage. 我也玩过一点,如果你把图像设置为百分比,它似乎也有效。 I floated one to the left of text and at 50% of the screen and it re-sized text and all. 我将文本左侧的一个浮动,屏幕的50%,并重新调整文本大小。 If you need me to post an example, just ask. 如果您需要我发布示例,请询问。

use @media to do manual change by the mobile, tablet or desktop size. 使用@media按手机,平板电脑或桌面尺寸进行手动更改。 by the way mobile and tablet will have landscape and portrait. 顺便说一句,手机和平板电脑将拥有风景和肖像。 if you using google chrome to check you can determine it better. 如果您使用谷歌浏览器检查,您可以更好地确定它。 sample of website : Media Queries: How to target desktop, tablet and mobile? 网站样本: 媒体查询:如何定位桌面,平板电脑和手机?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM