慕课学习-->前端开发-->HTML/CSS-->CSS深入理解之float浮动
Float
1、float设计初衷是:文字环绕效果
2、浮动具有破坏性,会让父元素高度塌陷!
3、清除浮动带来的影响
(1)在底部插入具有clear:both;声明的元素
与外部可以直接接触,例如可以发生margin重叠效果
(2)父元素BFC(IE8+)或haslayout(IE6/IE7)
形成封闭的结构,里面的任何生命都不会对外部产生影响
.clearfix:after{content:'';display:block;height:0;overflow:hidden;clear:both;} -->IE8+
.clearfix{*zoom:1;} -->IE6/IE7
.clearfix:after{content:'';display:table;clear:both;} -->IE8+
Clearfix应用在包含浮动子元素的父级元素上
4、float浮动的滥用
(1)元素block块状化
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>浮动与display:block化</title>
<style>
.ovh { overflow: hidden; }
.red { color: #cd0000; }
[hidden] { display: none; }
</style>
</head>
<body>
<p id="first">这是一个没有设置<code>float</code>属性的按钮:</p>
<p class="ovh"><input type="button" id="btnShow" value="点击我显示display属性值"></p>
<p hidden="">此时,按钮的display属性值是:<span id="result" class="red"></span></p>
<p>点击下面的按钮让上面的按钮添加<code>float: left</code>的声明:</p>
<p><input type="button" id="btnAdd" value="上面的按钮添加float:left"></p>
<script>
var btnShow = document.getElementById("btnShow"),
btnAdd = document.getElementById("btnAdd"),
result = document.getElementById("result"),
first = document.getElementById("first");
if (btnShow && btnAdd && result) {
btnShow.onclick = function() {
// 获得该按钮的display值
var display = this.currentStyle? this.currentStyle.display: window.getComputedStyle(this, null).display;
// 显示结果
result.innerHTML = display;
result.parentNode.removeAttribute("hidden");
// repain fix IE7/IE8 bug
document.body.className = "any";
};
// 设置浮动按钮的点击事件
btnAdd.onclick = function() {
btnShow.style["cssFloat" in this.style? "cssFloat": "styleFloat"] = "left";
// 文字描述的变化
this.value = "上面的按钮已经设置了float:left";
btnShow.value = "再次点击我确认display属性值";
first.innerHTML = first.innerHTML.replace("没有", '<del>没有</del>');
// 结果隐藏
result.parentNode.setAttribute("hidden", "");
// 按钮禁用
this.setAttribute("disabled", "");
};
}
</script>
</body>
</html>
(2)破坏性造成的紧密排列特性
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>浮动去空格</title>
<style>
button { margin: 0; }
p { clear: both; }
</style>
</head>
<body>
<button>按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
<button>按钮4</button>
<p><input type="button" id="trigger" value="点击按钮浮动"></p>
<script>
var trigger = document.getElementById("trigger"),
buttons = document.getElementsByTagName("button");
var length = buttons.length;
if (trigger && length > 0) {
trigger.onclick = function() {
for (var index = 0; index < length; index += 1) {
buttons[index].style["cssFloat" in trigger.style? "cssFloat": "styleFloat"] = "left";
}
};
}
</script>
</body>
</html>
5、布局问题
(1)容错性比较糟糕,容易出问题
(2)这种布局需要元素固定尺寸,很难重复使用
(3)低版本的IE下有很多问题
6、浮动与流体布局
(1)文字环绕衍生--单侧固定:固定布局写法和流体布局写法
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>浮动与单侧尺寸固定的流体布局</title>
<style>
body { font-size: 14px; background-color: #DDF3F7; color: #333; }
a { color: #0082CB; text-decoration: none; }
p { margin: 0; } img { border: 0; }
.mib_body { width: 600px; margin-left: auto; margin-right: auto; -webkit-transition: width .35s; transition: width .35s; }
.mib_x { margin-bottom: 10px; background-color: #fff; }
.mib_list { padding: 20px; overflow: hidden; _zoom: 1; }
.mib_vip { display: inline-block; width: 11px; height: 10px; margin-left: 1px; background: url(data:image/gif;base64,R0lGODlhCwAKAJEDAPyZCveDBuJmBP///yH5BAEAAAMALAAAAAALAAoAAAIdhD1zopgTXgMpsBdylVCPK2UCKI0j95hoRa0NVwAAOw==); }
.mib_head_a { width: 56px; float: left; }
/* 下面这个是固定布局写法 */
.mib_feed_fixed { width: 484px; float: right; }
/* 下面这个是流体布局写法 */
.mib_feed_flow { margin-left: 76px; }
.mib_sms { line-height: 22px; padding-bottom: 6px; font-size: 14px; }
.mib_input { width: 70px; padding: 4px; font-size: 100%; }
.mib_btn { width: 70px; padding: 5px; font-size: 100%; }
</style>
</head>
<body>
<div id="mibBody" class="mib_body">
<div class="mib_x">
<div class="mib_list">
<a href="http://t.sina.com.cn/xuruoxuan" class="mib_head_a">
<img title="徐若瑄VIVIAN" src="http://img.mukewang.com/53e2e9470001dfd200500050.jpg">
</a>
<div class="mib_feed_fixed">
<p class="mib_sms"><a title="徐若瑄VIVIAN" href="#">徐若瑄VIVIAN<i title="新浪认证" class="mib_vip"></i></a>:一個人的晚餐!茶泡飯!飯、飯、飯… 今日不減肥,先把病治好再說! 我認真吃完這,燒就會退了吧?! 開動啦~~~~~~~~~~~~~~~~~~</p>
<div class="feed_img"><img src="http://img.mukewang.com/53e2e9b10001948000890120.jpg" height="120"></div>
</div>
</div>
</div>
<div class="mib_x">
<div class="mib_list">
<a href="http://t.sina.com.cn/xuruoxuan" class="mib_head_a">
<img title="徐若瑄VIVIAN" src="http://img.mukewang.com/53e2e9470001dfd200500050.jpg">
</a>
<div class="mib_feed_flow">
<p class="mib_sms"><a title="徐若瑄VIVIAN" href="#">徐若瑄VIVIAN<i title="新浪认证" class="mib_vip"></i></a>:一個人的晚餐!茶泡飯!飯、飯、飯… 今日不減肥,先把病治好再說! 我認真吃完這,燒就會退了吧?! 開動啦~~~~~~~~~~~~~~~~~~</p>
<div class="feed_img"><img src="http://img.mukewang.com/53e2e9b10001948000890120.jpg" height="120"></div>
</div>
</div>
</div>
<div class="mib_x">
<div class="mib_list">
<p class="mib_sms">
修改外框的宽度:<input type="number" id="mibInput" step="100" class="mib_input" value="600"> 像素
</p>
<button type="button" id="mibBtn" class="mib_btn">确定</button>
</div>
</div>
</div>
<script>
var ele_mibBody = document.getElementById("mibBody"),
ele_mibInput = document.getElementById("mibInput"),
ele_mibBtn = document.getElementById("mibBtn");
if ( ele_mibBody && ele_mibInput && ele_mibBtn ) {
ele_mibBtn.onclick = function() {
var value = ele_mibInput.value;
if (!/^\d+$/.test(value)) {
value = 600;
ele_mibInput.value = value;
}
// 改变最外面容器的尺寸
ele_mibBody.style.width = value + "px";
};
if (ele_mibInput.type == "number") {
ele_mibInput.onchange = function() {
ele_mibBtn.click();
};
}
}
</script>
</body>
</html>
(2)浮动与右侧尺寸固定:改变DOM位置的流体布局写法和不改变DOM位置的流体布局写法
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>浮动与右侧尺寸固定的流体布局</title>
<style>
body { font-size: 14px; background-color: #DDF3F7; color: #333; }
a { color: #0082CB; text-decoration: none; }
p { margin: 0; } img { border: 0; }
.mib_body { width: 600px; margin-left: auto; margin-right: auto; }
.mib_x { margin-bottom: 10px; background-color: #fff; }
.mib_list { padding: 20px; overflow: hidden; _zoom: 1; resize: none; }
.mib_resize { overflow: auto; resize: both; }
.mib_vip { display: inline-block; width: 11px; height: 10px; margin-left: 1px; background: url(data:image/gif;base64,R0lGODlhCwAKAJEDAPyZCveDBuJmBP///yH5BAEAAAMALAAAAAALAAoAAAIdhD1zopgTXgMpsBdylVCPK2UCKI0j95hoRa0NVwAAOw==); }
/* 下面这个是右浮动,改变DOM位置的流体布局写法 */
.mib_head_r { width: 56px; float: right; }
.mib_feed_flow { margin-right: 76px; }
/* 下面这个是左浮动,不改变DOM位置的流体布局写法 */
.mib_full_float { width: 100%; float: left; }
.mib_head_l { width: 56px; float: left; margin-left: -56px;}
.mib_sms { line-height: 22px; padding-bottom: 6px; font-size: 14px; }
.mib_input { width: 70px; padding: 4px; font-size: 100%; }
.mib_btn { width: 70px; padding: 5px; font-size: 100%; }
</style>
</head>
<body>
<div id="mibBody" class="mib_body">
<div class="mib_x mib_resize">
<div class="mib_list">
<a href="http://t.sina.com.cn/xuruoxuan" class="mib_head_r">
<img title="徐若瑄VIVIAN" src="http://img.mukewang.com/53e2e9470001dfd200500050.jpg">
</a>
<div class="mib_feed_flow">
<p class="mib_sms"><a title="徐若瑄VIVIAN" href="#">徐若瑄VIVIAN<i title="新浪认证" class="mib_vip"></i></a>:一個人的晚餐!茶泡飯!飯、飯、飯… 今日不減肥,先把病治好再說! 我認真吃完這,燒就會退了吧?! 開動啦~~~~~~~~~~~~~~~~~~</p>
<div class="feed_img"><img src="http://img.mukewang.com/53e2e9b10001948000890120.jpg" height="120"></div>
</div>
</div>
</div>
<div class="mib_x mib_resize">
<div class="mib_list">
<div class="mib_full_float">
<div class="mib_feed_flow">
<p class="mib_sms"><a title="徐若瑄VIVIAN" href="#">徐若瑄VIVIAN<i title="新浪认证" class="mib_vip"></i></a>:一個人的晚餐!茶泡飯!飯、飯、飯… 今日不減肥,先把病治好再說! 我認真吃完這,燒就會退了吧?! 開動啦~~~~~~~~~~~~~~~~~~</p>
<div class="feed_img"><img src="http://img.mukewang.com/53e2e9b10001948000890120.jpg" height="120"></div>
</div>
</div>
<a href="http://t.sina.com.cn/xuruoxuan" class="mib_head_l">
<img title="徐若瑄VIVIAN" src="http://img.mukewang.com/53e2e9470001dfd200500050.jpg">
</a>
</div>
</div>
</div>
</body>
</html>
(3)智能自适应尺寸:浮动与两侧皆自适应的流体布局。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>浮动与两侧皆自适应的流体布局</title>
<style>
body { font-size: 14px; background-color: #DDF3F7; color: #333; }
a { color: #0082CB; text-decoration: none; }
p { margin: 0; } img { border: 0; }
.mib_body { width: 600px; margin-left: auto; margin-right: auto; }
.mib_x { margin-bottom: 10px; background-color: #fff; }
.mib_list { padding: 20px; }
.mib_resize { overflow: auto; resize: both; }
.mib_vip { display: inline-block; width: 11px; height: 10px; margin-left: 1px; background: url(data:image/gif;base64,R0lGODlhCwAKAJEDAPyZCveDBuJmBP///yH5BAEAAAMALAAAAAALAAoAAAIdhD1zopgTXgMpsBdylVCPK2UCKI0j95hoRa0NVwAAOw==); }
.mib_head_a { float: left; margin-right: 20px; }
/* 下面这个是固定布局写法 */
.mib_cell { display: table-cell; *display: inline-block; width: 2000px; *width: auto; }
.mib_sms { line-height: 22px; padding-bottom: 6px; font-size: 14px; }
.mib_select { width: 80px; padding: 5px; font-size: 100%; }
</style>
</head>
<body>
<div id="mibBody" class="mib_body">
<div class="mib_x mib_resize">
<div class="mib_list">
<a href="http://t.sina.com.cn/xuruoxuan" class="mib_head_a">
<img id="mibHeadImg" title="徐若瑄VIVIAN" src="http://img.mukewang.com/53e2e9470001dfd200500050.jpg">
</a>
<div class="mib_cell">
<p class="mib_sms"><a title="徐若瑄VIVIAN" href="#">徐若瑄VIVIAN<i title="新浪认证" class="mib_vip"></i></a>:一個人的晚餐!茶泡飯!飯、飯、飯… 今日不減肥,先把病治好再說! 我認真吃完這,燒就會退了吧?! 開動啦~~~~~~~~~~~~~~~~~~</p>
<div class="feed_img"><img src="http://img.mukewang.com/53e2e9b10001948000890120.jpg" height="120"></div>
</div>
</div>
</div>
<div class="mib_x">
<div class="mib_list">
<p class="mib_sms">
选择头像的宽度:<select id="minSelect" class="mib_select">
<option value="56px">56px</option>
<option value="70px">70px</option>
<option value="84px">84px</option>
<option value="100px">100px</option>
</select>
</p>
</div>
</div>
</div>
<script>
var ele_mibSelect = document.getElementById("minSelect"),
ele_mibHeadImg = document.getElementById("mibHeadImg");
if (ele_mibSelect && ele_mibHeadImg) {
ele_mibSelect.onchange = function() {
ele_mibHeadImg.style.width = this.value;
};
}
</script>
</body>
</html>
一侧浮动:float,
另一侧:display:table-cell(IE8+);display:inline-block(IE7)
7、让IE7飙泪的浮动问题
(1)含clear的浮动元素包裹不正确的问题;
(2)浮动元素倒数2个莫名垂直间距问题;
(3)浮动元素最后一个字符重复问题;
(4)浮动元素楼梯排列问题;
(5)浮动元素和文本不在同一行的问题;