分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

css清除浮动

发布时间:2023-09-06 01:44责任编辑:胡小海关键词:暂无标签

1 给浮动元素的父元素设置高度,只适合高度固定的布局,要给出精确的高度,如果高度和父级div不一样时,会产生问题。

<style type="text/css"> ????.div1{background:#000080;border:1px solid red;/*解决代码*/height:200px;} ????.div2{background:#800080;border:1px solid red;height:100px;margin-top:10px} ????.left{float:left;width:20%;height:200px;background:#DDD} ????.right{float:right;width:30%;height:80px;background:#DDD} </style> <div class="div1"> ????<div class="left">Left</div> ????<div class="right">Right</div> </div> <div class="div2"> ????div2 </div> 

2 在浮动元素后加一个空div,给div设置css属性:clear: both; 让父级div能自动获取到高度,浏览器支持好,如果页面浮动布局多,就要增加很多空div,让人感觉很不好。

<html>, ???<head> ???????<script type="text/javascript" src="jquery.js"></script> ???????<style type="text/css"> ???????????.div1{ ???????????????background:#000080; ???????????????border:1px solid red; ???????????} ????????????.div2{ ???????????????background:#800080; ???????????????border:1px solid red; ???????????????height:100px; ???????????????margin-top:10px ???????????} ????????????.left{ ???????????????float:left; ???????????????width:20%; ???????????????height:200px; ???????????????background:#DDD ???????????} ????????????.right{ ???????????????float:right; ???????????????width:30%; ???????????????height:80px; ???????????????background:#DDD ???????????} ?????????????/*解决代码*/ ???????????.clearfloat{clear:both} ????????</style> ???</head> ???<body> ???????<div class="div1"> ????????????<div class="left">Left</div> ????????????<div class="right">Right</div> ?????????????<!-- 解决代码 --> ???????????<div class="clearfloat"></div> ???????</div> ????????<div class="div2"> ????????????div2 ????????</div> ????</body></html>

3 IE8以上和非IE浏览器才支持:after,zoom(IE转有属性)可解决ie6,ie7浮动问题

<html> ???<head> ???????<script type="text/javascript" src="jquery.js"></script> ???????<style type="text/css"> ???????????.div1{ ???????????????background:#000080; ???????????????border:1px solid red; ???????????} ????????????.div2{ ???????????????background:#800080; ???????????????border:1px solid red; ???????????????height:100px; ???????????????margin-top:10px ???????????} ????????????.left{ ???????????????float:left; ???????????????width:20%; ???????????????height:200px; ???????????????background:#DDD ???????????} ????????????.right{ ???????????????float:right; ???????????????width:30%; ???????????????height:80px; ???????????????background:#DDD ???????????} ????????????/*清除浮动代码*/ ????????????.clearfloat:after{ ???????????????display:block; ???????????????clear:both; ???????????????content:""; ???????????????visibility:hidden; ???????????????height:0 ???????????} ????????????.clearfloat{ ???????????????zoom:1 ???????????} ????????</style> ???</head> ???<body> ???????<div class="div1 clearfloat"> ????????????<div class="left">Left</div> ????????????<div class="right">Right</div> ????????</div> ????????<div class="div2"> ????????????div2 ????????</div> ????</body></html>

4 给父元素添加overflow:hidden

<html> ???<head> ???????<script type="text/javascript" src="jquery.js"></script> ???????<style type="text/css"> ???????????.div1{ ???????????????background:#000080; ???????????????border:1px solid red; ???????????????/*解决代码*/ ???????????????overflow: hidden; ???????????} ????????????.div2{ ???????????????background:#800080; ???????????????border:1px solid red; ???????????????height:100px; ???????????????margin-top:10px ???????????} ????????????.left{ ???????????????float:left; ???????????????width:20%; ???????????????height:200px; ???????????????background:#DDD ???????????} ????????????.right{ ???????????????float:right; ???????????????width:30%; ???????????????height:80px; ???????????????background:#DDD ???????????} ????????</style> ???</head> ???<body> ???????<div class="div1"> ????????????<div class="left">Left</div> ????????????<div class="right">Right</div> ????????</div> ????????<div class="div2"> ????????????div2 ????????</div> ????</body></html>

css清除浮动

原文地址:https://www.cnblogs.com/xjy20170907/p/8492155.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved