html - 使用 float 元素垂直对齐内容+响应式布局

标签 html css css-float css-position vertical-alignment

我有一个组件框布局,图像位于左/右+内容需要垂直居中对齐。它需要以顶部图像+底部内容的布局来响应移动设备。 Click Here for Jsfiddle Attempt: 1 Code Link

我尝试过使用.wrapper显示表格.img-wrap、.content-wrap显示表格单元格 在桌面上工作正常,但在移动设备上,每个组件框必须在顶部有图像,在下面有内容。 Click here for Jsfiddle Attempt: 2 code Link

我想要的结果是获得尝试 2(针对桌面)和尝试 1(针对移动设备)的结果。如果可能的话,最好使用相同的 html 布局 - 尝试 1 HTML。

Update:

I've tried with a different approach (link as below) that i've found from CSS Tricks at fist i thought it does center vertically but then after i start adding more text content it doesn't works anymore. Does anyone know why?

Click here for Jsfiddle Attempt: 3 code Link

HTML - 尝试 3

<div class="wrapper">
    <div class="img-wrap left">
        <img src="http://lorempixel.com/400/400/" alt="" />
    </div>
    <div class="content-wrap">
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. ... Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
    </div></p>
    </div></p>
    </div>
</div>
<div class="wrapper">
    <div class="img-wrap right">
        <img src="http://lorempixel.com/400/400/" alt="" />
    </div>
    <div class="content-wrap">
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
    </div>
</div>

CSS-尝试 3

* {
    box-sizing: border-box;
}
.wrapper:before, .wrapper:after {
    display: table;
    content:'';
}
.wrapper:after {
    clear:both;
}
.wrapper {
    width: 100%;
    height:auto;
    position :relative;
    max-width: 800px;
    display:block;
    margin: 30px auto;
    background: #f3f3f3;
    *zoom:1;
}
.wrapper:before {
    content:'';
    display: inline-block;
    vertical-align: middle;
}
.img-wrap {
    width:30%;
}
.left {
    float:left;
}
.right {
    float: right;
}
.img-wrap img {
    display:block;
    width: 100%;
}
.content-wrap {
    display: inline-block;
    width: 70%;
    height: 100%;
    padding:20px;
    vertical-align:middle;
    position: relative;
}
.content-wrap:before {
    content:'';
    display: inline-block;
    vertical-align: middle;
}
@media screen and (max-width:480px) {
    .right, .left {
        float:none;
    }
    .wrapper:before {
        display: none;
    }
    .img-wrap {
        width: 100%;
    }
    .content-wrap {
        width:100%;
    }
}

最佳答案

我建议使用display:table来实现vertical-align:

CSS

* {
    box-sizing: border-box;
}
.wrapper:before, .wrapper:after {
    display: table;
    content:'';
}
.wrapper:after {
    clear:both;
}
.wrapper {
    width: 100%;
    height:auto;
    position :relative;
    max-width: 800px;
    display:table;
    margin: 30px auto;
    background: #f3f3f3;
    vertical-align:middle;
    *zoom:1;
}
.img-wrap {
    display: table-cell;
    text-align: left;
}
.left {
    float:left;
}
.right {
    float: right;
}
.img-wrap img {
    display:block;
    width: 100%;
}
.content-wrap {
    display: table-cell;
    width: 70%;
    height: 100%;
    padding:20px;
    vertical-align:middle;
    position:relative;
}
@media screen and (max-width:750px) {
    .right, .left {
        float:none;
    }
    .img-wrap {
        width: 100%;
    }
    .content-wrap {
        width:100%;
    }
}

fiddle

关于html - 使用 float 元素垂直对齐内容+响应式布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24611847/

相关文章:

javascript - 在 keydown 事件中,当按下键(40)、(38)、(13)中的任何一个时,其他功能将立即停止。当前功能将启动

html - 响应间距的 Bootstrap 问题

CSS LESS 输出问题

css - 为什么css float不会改变后面div的宽度?

css - float :left div's 的垂直对齐

javascript - 为什么我的 javascript 文件是唯一出现 404 错误的文件?

javascript - 如何在 dragover/dragenter HTML 5 拖放期间更改图标

javascript - 带有 imagemapster 的 ie8 - 位置 :absolute not working

css - float 段落宽度未定义

html - Bootstrap 4 输入显示 'invalid-feedback' 时出现跳转问题