栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Html/CSS > CSS教程

新闻列表中标题和日期的左右分别对齐的几种处理方法

CSS教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

新闻列表中标题和日期的左右分别对齐的几种处理方法

前言

在新闻列表中,有标题和日期,然后分别对齐,这种应用场景非常广泛。而在前端实践中,其也有很多中布局方式。很多前端新手在入门时,可能会稍微有点迷茫。

今天我列举几个常用的布局方法,便于新手学习。

这里只是为了实现功能效果,所以不额外添加美化类的样式。只列出核心参数,详细使用时,请根据自己的情况灵活使用。

效果演示

方法一:日期定位法

这种方法是使用定位,将日期设定到right:0;top:0的位置。

DOM结构

    
  • this is a news title 12015-12-15
  • this is a news title 22015-12-15
  • this is a news title 32015-12-15
  • this is a news title 42015-12-15
css代码
.news_box {width: 400px;margin: 100px auto;}
.news_box li {position: relative;height: 24px;line-height: 24px;overflow: hidden;}
.news_box li span {position: absolute;right: 0;top: 0;}
小结

优点:不能说没有,但实在想不起来。
缺点:如果标题文字比较长,会和日期叠在一起。

总之,不推荐使用。

方法二:日期浮动法

这种方法的dom结构和上面的不一样,它把日期给提前了。(上面的方法用这个DOM结构也是可以的。但是,上面的那种DOM结构更加符合HTML语义)。

DOM结构

    
  • 2015-12-15this is a news title 1
  • 2015-12-15this is a news title 2
  • 2015-12-15this is a news title 3
  • 2015-12-15this is a news title 4
css代码
.news_box {width: 400px;margin: 100px auto;}
.news_box li {height: 24px;line-height: 24px;overflow: hidden;}
.news_box li span {float: right;padding-left: 10px;}
小结

优点: 显示效果合理,也便于处理标题过长溢出的问题,隐藏溢出即可。兼容IE6+所有浏览器。
缺点: DOM结构不合理。

这个方法我常用。虽然DOM结构不合理,但是css没有使用任何hack。

方法三:日期浮动法hack版

上面的方法虽然解决了问题,但是毕竟dom结构不是我们希望的。那么,可以不可以在DOM结构为先标题后日期的情况下,实现想要的效果呢?

可以。

DOM结构

    
  • this is a news title 12015-12-15
  • this is a news title 22015-12-15
  • this is a news title 32015-12-15
  • this is a news title 42015-12-15
css代码
.news_box {width: 400px;margin: 100px auto;}
.news_box li {height: 24px;line-height: 24px;overflow: hidden;}
.news_box li span {float: right;*margin-top: -24px;}
小结

优点: dom结构正常,兼容性强,hack是为了处理ie67,效果优秀。
缺点: 使用了hack。当然,现在不考虑IE67的项目,两个浮动法,都是OK的。

方法四:模拟表格法

这种方式完全不推荐。但是,可以作为知识点进行学习,在某些场合,这种方法是很有用的。但是在本帖的例子中,这个方法是不合适的。

DOM结构

    
  • this is a news title 12015-12-15
  • this is a news title 22015-12-15
  • this is a news title 32015-12-15
  • this is a news title 42015-12-15
css代码
.news_box {width: 400px;margin: 100px auto;}
.news_box li {line-height: 24px;display: table;width: 100%;}
.news_box li span,.news_box li a {display: table-cell;}
.news_box li span {text-align: right;}
小结

优点: 当学新东西咯
缺点: 兼容性差,效果扯淡,不能隐藏标题溢出

总结

浮动法优于定位法优于表格法,至于用不用hack,完全根据项目需要。


首发地址:http://blog.csdn.net/fungleo/article/details/50315437

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号