用margin和overflow属性实现div间距的方法

2022-04-15 0 556

用margin和overflow属性实现div间距的方法

通常我们在制作上图的时候,会分别给四个div加上不同的css属性,来实现中间间隔。但我们更希望的是不需要对html标签做标识,直接能通过css实现,今天在论坛上小志提出了一个很不错的解决方案。使用overflow:hidden属性。或许很多人已经是这样用了,我没用过,觉得耳目一新。呵呵。其实道理很简单,我们按照正常的设置li的margin-right、margin-bottom属性,这样的结果自然会比我们期望的要多出来右边和下方的,这个时候我们通过ul的父级来设置width/height属性,然后overflow:hidden把多余的隐藏掉。好方法,呵呵!

解决方法请看图:


用margin和overflow属性实现div间距的方法

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”> <html xmlns=”http://www.w3.org/1999/xhtml” lang=”zh-CN”> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ /> <meta http-equiv=”Content-Language” content=”zh-CN” /> <meta name=”robots” content=”all” /> <meta name=”author” content=”” /> <meta name=”Copyright” content=”CopyRight 2007″ /> <meta name=”keywords” content=”CSS,overflow属性” /> <meta name=”description” content=”巧用overflow:hidden解决中间间隔问题” /> <title>巧用overflow:hidden解决中间间隔问题</title> <style type=”text/css”> *{margin:0;padding:0;} body{text-align:center;background:#CCC;} ul{list-style:none;} .main {background:white;height:800px;width:597px;margin:0 auto;} .box {width:595px;height:205px;margin:20px auto;overflow:hidden;background:white;} .box ul {width:600px;height:210px;} .box li {float:left;margin-right:5px;margin-bottom:5px;} .box div {background:white;background:#f7f7f7;width:293px;height:98px;border:1px solid #CCC} .box h2 {background:#9CC;text-align:left;text-indent:10px;font-size:12px;height:24px;line-height:24px;} </style> </head> <body> <div class=”main”> <h1>巧用overflow:hidden解决中间间隔问题</h1> <div class=”box”> <ul> <li> <div> <h2>Title</h2> <p>Content</p> </div> </li> <li> <div> <h2>Title</h2> <p>Content</p> </div> </li> <li> <div> <h2>Title</h2> <p>Content</p> </div> </li> <li> <div> <h2>Title</h2> <p>Content</p> </div> </li> </ul> </div> </div> </body> </html>

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 CSS/HTML 用margin和overflow属性实现div间距的方法 https://www.niceym.com/16849.html