使用css float布局时的高度不等的问题

本文探讨了在使用CSS浮动布局创建表格时遇到的问题,特别是当元素高度不一致导致布局混乱的情况。通过调整代码实现每行的逐行展示,并提供了使元素上下或底部居中对齐的方法。此外,还介绍了在JSP环境中动态生成此类布局的技巧。

在使用css的float 作表格状布局时,如果容器内各元素的高度不一致,将产生如下效果


 
即,不能在换行时从最左边开始,显得看起来比较乱

其代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
    .fl{
        float: left;
        width: 110px;
        margin: 10px 10px;
        background-color: #e92;
        display: block;
    }
   
    .cl{
        clear: left;
    }
</style>
</head>
<body>
<div style="width: 400px;">
    <div class="fl" style="height: 150px;"></div>
    <div class="fl" style="height: 100px;"></div>
    <div class="fl" style="height: 50px;"></div>
   
    <div class="fl" style="height: 50px;"></div>
    <div class="fl" style="height: 150px;"></div>
    <div class="fl" style="height: 50px;"></div>
   
    <div class="fl" style="height: 100px;"></div>
    <div class="fl" style="height: 150px;"></div>
    <div class="fl" style="height: 100px;"></div>

    <div class="fl" style="height: 50px;"></div>
   
    <div class="cl"></div>
</div>
</body>
</html>
 

将代码更改为:

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
    .fl{
        float: left;
        width: 110px;
        margin: 10px 10px;
        background-color: #e92;
        display: block;
    }
   
    .cl{
        clear: left;
    }
</style>
</head>
<body>
<div style="width: 400px;">
    <div class="fl" style="height: 150px;"></div>
    <div class="fl" style="height: 100px;"></div>
    <div class="fl" style="height: 50px;"></div>
    <div class="cl"></div>
    <div class="fl" style="height: 50px;"></div>
    <div class="fl" style="height: 150px;"></div>
    <div class="fl" style="height: 50px;"></div>
    <div class="cl"></div>
    <div class="fl" style="height: 100px;"></div>
    <div class="fl" style="height: 150px;"></div>
    <div class="fl" style="height: 100px;"></div>
    <div class="cl"></div>
    <div class="fl" style="height: 50px;"></div>
   
    <div class="cl"></div>
</div>
</body>
</html>
 


时才能逐行展示,如下图:


 

但目前未找到使其使其每一行 上下居中对其或底部对其的方法。

以上内容若在jsp中动态生成,可使用类似代码:

<div style="width: 400px">
    <c:forEach items="${items}" var="item" varStatus="i">
       
        <div class="fl">${item.content}</div>
       
        <c:if test="${i.count mod 3 eq 0}">
            <div style="clear: both;"></div>
        </c:if>
    </c:forEach>
</div>
 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值