使用 CSS 的响应式图像 [英] Responsive Images with CSS

查看:17
本文介绍了使用 CSS 的响应式图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现调整图像大小以使其具有响应性很棘手.

我正在开发一个 php 应用程序来自动将网站转换为响应式版本.我有点被图片困住了.

我已经成功地为网站上的每张图片添加了一个包装类,并且可以很好地调整图片的大小.

我的问题在于自然小于窗口的图像,例如徽标和图标.我不想调整这些大小.

我的代码目前正在转换:

进入:

<img src="[src]"/>

我使用以下 CSS 的地方:

.erb-image-wrapper{最大宽度:90%;高度:自动;位置:相对;显示:块;保证金:0 自动;}.erb 图像包装器 img{宽度:100%!重要;高度:100%!重要;显示:块;}

这会调整所有图像的大小,但我只希望它调整超过页面宽度的图像的大小.我可以通过 CSS 实现这一点吗?

解决方案

.erb-image-wrapper img{最大宽度:100%!重要;高度:自动;显示:块;}

为我工作.
感谢 MrMisterMan 的帮助.

I'm finding it tricky to resize images to make them responsive.

I'm developing a php application to automatically convert a website to a responsive version. I'm a little stuck on the images.

I've successfully added a wrapper class to every image on a website and can re-size the images quite well.

My issue lies with images that are naturally smaller than the the window, such as logos and icons. I don't want to resize these.

My code currently converts:

<img src="[src]" />

into:

<div class="erb-image-wrapper">
    <img src="[src]" />
</div>

Where I use the following CSS:

.erb-image-wrapper{
    max-width:90%;
    height:auto;
    position: relative;
    display:block;
    margin:0 auto;
}
.erb-image-wrapper img{
    width:100% !important;
    height:100% !important;
    display:block;
}

This resizes all images, but I only want it to resize images that are over the width of the page. Is the a way I can achieve this via CSS?

解决方案

.erb-image-wrapper img{
    max-width:100% !important;
    height:auto;
    display:block;
}

Worked for me.
Thanks for MrMisterMan for his assistance.

这篇关于使用 CSS 的响应式图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆