javascript - 如何垂直对齐 float 的、流动的 div 中的内容?

标签 javascript jquery html css responsive-design

我使用 1140px 网格系统在流动 div 中的标题旁边 float 图像,如下所示:

<div class='row'>
  <div class='sixcol'>
    <img src='img/someImage.jpg'/>
  </div>
  <div class='sixcol last'>
    <h2>Large Header Text</h2>
  </div>
</div>

.row {
    width: 100%;
max-width: 1140px;
min-width: 755px;
margin: 0 auto;
overflow: hidden;
}
.sixcol {
    margin-right: 3.8%;
    float: left;
    min-height: 1px;
}
.row .sixcol {
    48%;
}
.sixcol h2 {
       text-align: center;
 }
.last {
    margin-right: 0px;
}
img {
    max-width: 100%;
    height: auto;
}
@media handheld, only screen and (max-width: 767px) {
    .row {
       width: 100%; min-width: 0; margin-left: 0px; margin-right: 0px;
   }
   .row .sixcol {
       width: auto; float: none; margin-left: 0px; margin-right: 0px;
   }
   .sixcol h2 {
       text-align: left;
    }
}

对于智能手机,标题被推到图像下方并与容器的左侧对齐,而在桌面上,所需的效果是标题始终位于图像右侧并垂直居中于展开/收缩图像高度.有没有办法只使用 CSS 和 HTML 来做到这一点?有没有简单的 JS/jQuery 解决方案?

最佳答案

在 div 上使用 display:table-cell;。 div 会像表格单元格一样 react ,因此您可以使用垂直对齐方式。

关于javascript - 如何垂直对齐 float 的、流动的 div 中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13610716/

相关文章:

javascript - 动态扩展动态加载的 DIV 的宽度

javascript - 通过 REST API 在 BlockBlob 上设置 Azure 元数据

javascript - 使用 Jquery 更改文本字符串

javascript - 多个html5音频元素Jquery错误

html - 如何防止样式 `a span` 跳到下一行?

jquery - 悬停时的 CSS 变换覆盖 jQuery onclick addclass 变换

javascript - 删除 X 和 Y 轴上的标签并删除 X 轴上的最后一条网格线

javascript - 确定调用了哪些 AngularJS 函数

javascript - 来自 jquery 插件的 angularjs 指令

Javascript 过滤列表并从中返回匹配的项目