CSS之float vs position:absolute
补充:ul 应该设置下 list-style: none;
题外话:看了张鑫旭的视频,这家伙把简单的css玩出了新花样,绝对大神级的存在。膜拜下先~
float的作用前面一章已经说过了,但没考虑过的是 position:absolute 作用与float极其类似。
二者都是将自身浮空(垂直于屏幕) -- 其实是将其容器高度塌陷。
不同点在于:float会改变位置,但 position:absolute 则会保存其原有位置!!!
利用 position:absolute 的这个特性,我们可以更好的实现一个搜索框的功能:
代码如下,
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>POSITION:ABSOLUTE vs FLOAT</title> <style>
ul,
li,
input,
button {
padding: 0;
margin: 0;
} .box,
section {
width: 254px;
height: 32px;
border: 1px solid #e6e8e9;
box-sizing: content-box;
overflow: hidden;
} input {
width: 200px;
height: 30px;
border: 1px solid #009527;
box-sizing: content-box;
/*padding-left: 5px;*/
text-indent: 5px;
text-overflow: ellipsis;
} button {
width: 50px;
height: 30px;
border: 1px solid #009527;
box-sizing: content-box;
}
ul{
color: #333;
border: 1px solid #999;
margin-top: -1px;
margin-left: 1px;
/*transition: all 1.25s;*/
}
li{
width: 252px;
background-color: #d5d7d9;
text-indent: 10px;
}
</style>
</head> <body>
<h3>使用absolute!!!</h3>
<div class="box">
<section>
<input type="text" name="key" placeholder="Anything you wanna know..."><button type="button">Go!</button>
</section>
<div>
<ul>
<li>AAA</li>
<li>BBB</li>
<li>CCC</li>
<li>DDD</li>
<li>EEE</li>
</ul>
</div>
</div>
</body>
<script>
var count=0;
var btn = document.getElementsByTagName('button')[0];
btn.onclick = function () {
count++;
var ul = document.getElementsByTagName('ul')[0];
if(count%2)
ul.style.position = 'absolute';
else
ul.style.position = 'static';
}
</script> </html>
其效果如下:

CSS之float vs position:absolute的更多相关文章
- float和position:absolute脱离文本流的区别
float和position:absolute脱离文本流的区别原创 paediatrician 最后发布于2016-09-19 10:43:05 阅读数 5375 收藏展开 文档流:将窗体自上而下分成 ...
- 脱离文档流两操作,float和position:absolute的区别
文档流:将窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,块状元素独占一行,内联元素不独占一行: CSS中脱离文档流,也就是将元素从普通的布局排版中拿走,其他盒子在定位的时候,会当做脱离 ...
- CSS绝对定位和相对定位 position: absolute/relative
absolute(绝对定位): 会把对象拖离HTML文档流,并通过top, left, right, bottom确定对象的具体位置,这个四个位置属性至少要设置一个,否则无法激活对象的absolute ...
- css的float和position属性
(1)float的简单用法 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- 关于css float 属性以及position:absolute 的区别。
1.float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕在图像周围,不过在 CSS 中,任何元素都可以浮动.浮动元素会生成一个块级框,而不论它本身是何种元素.div一个典型的块 ...
- 【总结】我所整理的float, inline-block还有position:absolute
这篇主要写了一下几个知识点: 浮动和inline-block的概念和选择 浮动和position:absolute对于脱离文档流的区别 这篇文章参考了一下几个链接: https://www.zhihu ...
- 解决CSS的position:absolute中left效果
有时候css中设置了position:absolute,left:100px,但是如果我们不想要这个left怎么办呢,当然你直接删除就可以,但是如果是框架的css,你直接删除就有问题了,这时候该怎么办 ...
- 【CSS】 布局之浮动float和绝对定位absolute的选择
浮动float: 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 由于浮动框不在文档的普通流中,所以文档的普通流中的块框表现得就像浮动框不存在一样.(W3C) 绝对定位 ...
- 浅谈position: absolute和position:relative
一.在此先说一下文档流的概念: 1,文档流定义: 百度百科定义:文档流是文档中可显示对象在排列时所占用的位置. 大多网友的理解:元素的位置由元素在 (X)HTML 中的位置决定.将窗体自上而下分成一行 ...
随机推荐
- 定期批量修改远程服务器root密码
一.背景 很多时候运维或安全工作人员需要维护大量的服务器,其中就包括判断是否存在root弱口令, 如果服务器数量很多一一修改的话的确是要好花费不少时间精力的.如果通过脚本来实现密码更改, 再做一个定期 ...
- [Windows Azure] Building the web role for the Windows Azure Email Service application - 3 of 5
Building the web role for the Windows Azure Email Service application - 3 of 5. This is the third tu ...
- 每日英语:Tencent Fights for China's Online Shoppers
In the war for the Chinese Internet, messaging giant Tencent is taking the battle to rival Alibaba's ...
- 每日英语:5 Things to Know About Missing Malaysia Airlines Flight and Air Safety
Malaysia Airlines Flight MH370, with 239 people aboard, lost contact early Saturday with the airline ...
- std::bind()图解
参考:http://blog.think-async.com/2010/04/bind-illustrated.html 避免链接失效,就把文中图转过来了,这几张就清楚的说明了bind的用法和原理.
- 9个绚丽多彩的HTML5进度条动画赏析
进度条在网页应用中越来越广泛了,特别是现在的页面异步局部刷新时代,进度条可以让用户更好的等待操作结果.本文要分享9款绚丽多彩的HTML5进度条动画,有很多还是挺实用的,效果也非常不错. 1.CSS3发 ...
- C#如何删除数组中的一个元素
C#如何删除数组中的一个元素,剩余的元素组成新数组,数组名不变double[] arr = new double[n];需要删除的是第m+1个数据arr[m]求新数组arr.(新数组arr包含n-1个 ...
- docker之创建MariaDB镜像的方法
一.基于commit命令方式创建 docker的安装 ? 1 2 3 [root@test01 ~]# yum install docker [root@test01 ~]# systemctl en ...
- php类库安装xml
问题 报错:Call to undefined function dom_import_simplexml() yum install php-dom service restart httpd 参考 ...
- python 中文编码(一)
我在学python的过程中,遇到的第二个问题,就是中文乱码,如今也算勉强入门了,在这里给大家说说我的经验,也算个新人引导吧. 在文章里,我会重点提到一个概念:有来有去. 即数据从哪里来,到哪里 ...