浮动 div - 可变高度和可变列 - 无间隙
我已经使用 FIDDLES 进行了更新,请参见底部
我正在开发一个 wordpress 主题,可以在 iphone 和 ipad 以及其他 webkit 设备上使用。
我正在使用媒体查询根据设备的大小和方向更改 div 的宽度。
然而,这些 div 向左浮动,以填充设备屏幕。
我的问题是浮动 div 的高度是可变的。而且 div 的高度高于某些,会导致浮动元素出现间隙问题。
我需要有关修复的可能建议,这样就不会出现任何间隙,并且元素总是浮动,即使其路径中有更高的 div 也是如此。
有没有可能的方法使 div 高度与最高 div 相等,唯一的问题是它需要适应 iPad 上的方向变化。在 iPhone 上,情况还不错,因为只有一列。但在 iPad 上,纵向有 2 列,横向有 3 列。
请参阅下图解释我的问题。
< br>
当前 CSS
/* GLOBAL STYLE ( which iPhone uses first - 1 column ) ----------- */
.module {
display: block;
overflow: hidden;
width: 100%;
float: left;
}
/* IPADS (PORTRAIT - 2 column ) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
.module {
width: 50%;
}
}
/* IPADS (LANDSCAPE - 3 Column ) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
.module {
width: 33.33%;
}
}
基本标记
<div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
</div>
更新 - 我已经创建了纯 CSS FIDDLES 来模拟我的问题
下面的小提琴模拟智能手机(纵向和横向) - 一栏
查看样式.module
- width: 100%
模拟此 @media 查询的外观
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px)
http://jsfiddle.net/motocomdigital/kdgnP/1/
下面的小提琴模拟IPADS (肖像) - 两列
请参阅样式 .module
- width: 50%
模拟外观此 @media 查询
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
http://jsfiddle.net/motocomdigital/kdgnP/2/
< br>
下面的小提琴模拟 IPADS (LANDSCAPE) - 三栏
查看样式 .module
- 宽度: 33.33%
模拟此 @media 查询的外观
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
http://jsfiddle.net/ motocomdigital/kdgnP/3/
I HAVE UPDATED WITH FIDDLES, SEE BOTTOM
I am developing a wordpress theme to work on iphone and ipads and other webkit devices.
I am using media queries to change the width of divs depending on the size and orientation of the device.
These divs however float left, to fill the device screen.
My problem is that the floated div's height are variable. And the div's which are higher than some, are causing gap issues on the floated elements.
I need possible advice on a fix so there are never any gaps, and elements always float, even if there is a higher div in its path.
Is there a possible way of equalizing the div heights to the highest div, the only problem with this is that it need to work with orientation changes on iPad. On iPhone it's not so bad because there is only one column. But on iPad, portrait has 2 columns and landscape has 3 columns.
Please see diagram below explaining my issue.
CURRENT CSS
/* GLOBAL STYLE ( which iPhone uses first - 1 column ) ----------- */
.module {
display: block;
overflow: hidden;
width: 100%;
float: left;
}
/* IPADS (PORTRAIT - 2 column ) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
.module {
width: 50%;
}
}
/* IPADS (LANDSCAPE - 3 Column ) ----------- */
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
.module {
width: 33.33%;
}
}
BASIC MARKUP
<div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
<div class="module"></div>
</div>
UPDATE - I HAVE CREATED PURE CSS FIDDLES TO SIMULATE MY PROBLEM
The fiddle below simulates SMARTPHONES (PORTRAIT AND LANDSCAPE) - ONE COLUMN
See style .module
- width: 100%
which simulates the appearance of this @media query
@media only screen
and (min-device-width : 320px)
and (max-device-width : 480px)
http://jsfiddle.net/motocomdigital/kdgnP/1/
The fiddle below simulates IPADS (PORTRAIT) - TWO COLUMN
See style .module
- width: 50%
which simulates the appearance of this @media query
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : portrait) {
http://jsfiddle.net/motocomdigital/kdgnP/2/
The fiddle below simulates IPADS (LANDSCAPE) - THREE COLUMN
See style .module
- width: 33.33%
which simulates the appearance of this @media query
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px)
and (orientation : landscape) {
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像我在评论中所说,你需要在 javascript 中计算它并使用绝对定位。这是一个示例:
http://jsfiddle.net/rJxtm/4/
Like I said in a comment, you need to calculate this in javascript and use absolute positioning. Here is an example:
http://jsfiddle.net/rJxtm/4/
这不能简单地通过浮动来解决 - 唯一的选择是使所有这些模块大小相同(或为较小的模块添加一些边距)
This cannot be sloved simply by float - only option is to make all this modules same sized ( or add some margin for smaller ones )
您可以使用 JavaScript 将它们设置为相等的高度。
这是缩小的 jQuery 插件:
你可以这样使用:
另一个好的解决方案是 CSS 显示表。看看这篇文章:http://css-tricks.com/fluid -宽度等于高度列/
You can use JavaScript to set equal height to all of them.
This is the minified jQuery plugin:
And you can use like that:
Another good solution would be CSS display table. Take a look at this post: http://css-tricks.com/fluid-width-equal-height-columns/