带有响应式图像的 2 列 CSS 响应式布局 [英] 2-column CSS responsive layout with a responsive image

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

问题描述

我浏览了尽可能多的关于这个主题的帖子,但没有一个能解决这个难题.是否有可能有一个带有文本的左列和一个带有图像的右列,该图像在调整大小时会流入单列,并带有自动调整大小的图像?

在 img 上使用 100% 的最大宽度将使图像具有响应性并自动调整大小.但是,自动调整大小在表格中不起作用,或者在其周围的 div 上应用了百分比或浮点数.因此,任何包含浮点数或图像百分比的 CSS 2 列布局都将无法调整图像大小.

除了使用网格之外,有没有人对此有解决方案?

解决方案

如果您浮动图像的父 div,它不应该影响图像的响应宽度.

HTML

<div class="left"><p>Lorem ipsum dolor sat amet, consectetur adipisicing 精英.Minima corporis voluptates repelt ullam laboure qui voluptatum error nesciunt ratione dolorem fugiat veritatis ipsum nobis eius dicta est obcaecati ab animi illum molestias accusamus a cumlaboriosam magni recusandae earum unde fugiat veritatis ipsum nobis eius dictaexeritationem voluptatem facilis eos quasi.Mollitia sequi 假设和腐败排斥,ex amet reprehenderit animi illum ducimus totam unde quia distinctio quam velit magnam.Voluptatibus dolores natus sint enim fugiat.Sapiente voluptates enim officiis.Iste repudiandae illo nulla sed nam a ratione iure?</p>

<div class="right"><img src="http://lorempixel.com/640/480/" alt=""/>

CSS

.left {向左飘浮;宽度:50%;}.对 {浮动:对;宽度:50%;}.group:在{之后内容:"";显示:表;清楚:两者;}图片{最大宽度:100%;高度:自动;}@media 屏幕和(最大宽度:480 像素){.剩下,.对 {浮动:无;宽度:自动;}}

演示

I've looked through as many posts on this subject that I can find, but none of them solve this puzzle. Is it possible to have a left column with text and a right column with an image that will flow into a single column when resized, with the an auto resizing image?

Using a max-width of 100% on img will make the image responsive and auto-resize. However, the auto-resize doesn't work in a table or with a percentage or a float applied to the div around it. So any CSS 2 column layout that contains either a float, or a percentage to the image will defeat the image resizing.

Other than using a grid, does anyone have a solution for this?

解决方案

If you float the parent div of the image it shouldn't effect the responsive width of the image.

HTML

<div class="group">
    <div class="left">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima corporis voluptates repellat ullam labore qui voluptatum error nesciunt ratione dolorem fugiat veritatis ipsum nobis eius dicta est obcaecati ab animi illum molestias accusamus cum laboriosam magni recusandae earum unde fuga deserunt laudantium facere ducimus rerum tempora pariatur consectetur iste nulla a aut ea sit nam autem doloremque iusto exercitationem voluptatem facilis eos quasi. Mollitia sequi assumenda corrupti repellendus ex amet reprehenderit animi illum ducimus totam unde quia distinctio quam velit magnam. Voluptatibus dolores natus sint enim fugiat. Sapiente voluptates enim officiis. Iste repudiandae illo nulla sed nam a ratione iure?</p>
    </div>
    <div class="right">
        <img src="http://lorempixel.com/640/480/" alt="" />
    </div>
</div>

CSS

.left {
    float: left;
    width: 50%;
}
.right {
    float: right;
    width: 50%;
}
.group:after {
    content:"";
    display: table;
    clear: both;
}
img {
    max-width: 100%;
    height: auto;
}
@media screen and (max-width: 480px) {
    .left, 
    .right {
        float: none;
        width: auto;
    }
}

Demo

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

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆