html前端学习七:浮动 定位的详细示例

浮动定位细节讲解

示例一

盒子的特点(div) :

  1. 自适应的宽度,默认占据一行,固定宽度没有margin-left,没有宽度自适应的盒子是可以使用右边距
  2. 默认高度为0时,没有设定高度的时候,可以由子元素撑起来
  3. 浮动和定位(absolute、fixed)使用之后元素变为块

元素类型

  1. 块元素:

    1. 前后元素会被换行,允许设置宽高度
  2. 内联元素:

    1. 在一行的元素,不允许设置宽高度
    2. 不允许设置垂直方向外边距,只能设置水平方向的外边距
  3. 内联块:

    1. 内联元素的块,但是拥有了块的特质,除了占据一行(允许设置宽高度)

display样式

block 块级元素
inline 内联元素
inline-block 内联块
none 隐藏元素 不代表任何类型

text-align

可以调整文字、内联元素、内联块居中

文字居中示例

.text-inlineblock{
    		display:inline-block;
    		width:200px;
    		height:50px;
    		background: pink;
    		margin-top:100px;
    		text-align: center;
    	}

<b class="text-inlineblock">this is inline-block</b>

元素居中,需要在外面加个盒子

.text-box{
    text-align:center;
}


<div class="text-box">
<b class="text-inlineblock">this is inline-block</b>
</div>

示例二

vertical-align

特点:
用于改变文字基线
baseline: 把当前盒的基线与父级盒的基线对齐。如果该盒没有基线,就将底部外边距的边界和父级的基线对齐
sub: 把当前盒的基线降低到合适的位置作为父级盒的下标(该值不影响该元素文本的字体大小)
super: 把当前盒的基线提升到合适的位置作为父级盒的上标(该值不影响该元素文本的字体大小)
text-top: 把当前盒的top和父级的内容区的top对齐
text-bottom: 把当前盒的bottom和父级的内容区的bottom对齐
middle: 把当前盒的垂直中心和父级盒的基线加上父级的半x-height对齐
top: 把当前盒的top与行盒的top对齐
bottom: 把当前盒的bottom与行盒的bottom对齐

<style type="text/css">
    	.img-box{
    		background: pink;
    	}
    </style>
    
    <div class="img-box">
    	<img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1516083124296&di=04374b6832d9103c9afea2966d870937&imgtype=0&src=http%3A%2F%2Fpic29.photophoto.cn%2F20131204%2F0034034499213463_b.jpg" alt="" height="105" width="156" />
    	<span>baseline span test</span>
    </div> 
    
    
    

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JRkbz9vJ-1577065892532)(http://img2.ph.126.net/MPuV5ywO5FX6iYJtxNJsXQ==/1035827914315027703.png)]

添加样式 基线消失

    	.img-box img{
			vertical-align: middle;
    	}

top
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UB445iHx-1577065892533)(http://img1.ph.126.net/PYnawkHutFC41DMFaA2E9A==/3083276894908339510.png)]

middle
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-j6OUBR4B-1577065892534)(http://img1.ph.126.net/Yvwo42uldI0C9jOx7tESgQ==/3079336245234383785.png)]

bottom

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uzIGBxMQ-1577065892535)(http://img0.ph.126.net/Iwy4sNS4tMdTfn22Eh27Lw==/6632748918979192350.png)]

谁的基线高,调整谁的基线

overflow样式

visible: 对溢出内容不做处理,内容可能会超出容器。
hidden: 超出容器部分内容隐藏
scroll: 无论超不超出都有滚动条
auto: 超出部分有滚动条,没超出没有滚动条

示例代码:

<style type="text/css">
		* {
			margin:0;
			padding:0;
		}
		.box{
			width:300px;
			height: 150px;
			background: pink;
			overflow: hidden;
		}

	</style>
	
	    <div class="box">
    	玳瑁(学名:Eretmochelys imbricata)属爬行纲,海龟科的海洋动物。一般长约0.6米,大者可达1.6米。头顶有两对前额鳞,吻部侧扁,上颚前端钩曲呈鹰嘴状;前额鳞2对;背甲盾片呈覆瓦状排列;背面的角质板覆瓦状排列,表面光滑,具褐色和淡黄色相间的花纹。四肢呈鳍足状。前肢具2爪。尾短小,通常不露出甲外
    </div>
	

hidden效果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EaTOrQzI-1577065892536)(http://img0.ph.126.net/pZvdy48QXf9cdEG7-G5jKA==/1036109389291738758.png)]

auto效果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-E8IZ8ZPT-1577065892537)(http://img1.ph.126.net/ZVIalHMLNyScLjE45FcRQw==/1284370318750528936.png)]

scroll效果:把盒子高度改大点
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B6Qm1Gzn-1577065892538)(http://img2.ph.126.net/-KLfEi4dfVl6LBrEA3RSAw==/2594636335338632966.png)]

opacity透明样式

opacity:0-1

transparent:完全透明 只能用在颜色上面

filter透明样式

filter: alpha(opacity=50);
0-100 100是不透明
支持IE9以下的情况

RGBA

background-color: rgba(0,0,0,0.7) 透明30%;
只能作用于颜色color属性

.bg{
			width:500px;
			height: 200px;
			background: pink;
			/*opacity: 0.5;*/
			/*filter: alpha(opacity=50);*/
			background-color: rgba(0,0,0,0.5)
		}
		
		
		<div class="bg">
    	<img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1516097186069&di=8bfda8e71286b842474ebdb66c50f8c5&imgtype=0&src=http%3A%2F%2Fwww.chaojupai.com%2Fwp-content%2Fuploads%2F2014%2F11%2Fshenghuojia3.jpg" alt="" width="318" height="199">
    </div>

效果展示

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qu0Xh94w-1577065892538)(http://img1.ph.126.net/r42zDy8PNPHdM_dTL50GbQ==/6632731326793149301.png)]

CSS3新特性

first-child
last-child
nth-child(n) n表示要选择的第几个元素

    	div:first-child{
    		width: 150px;
            height: 150px;
            background: green;
    	}
    	div:last-child {
                width: 150px;
                height: 150px;
                background: red;
            }
        div:nth-child(2){
        	width:300px;
        	height: 150px;
        	background: blue;
        }
        
        <body>
    <div>111</div>
    <div>222</div>
    <div>333</div>
    <div>444</div>
    
</body>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IkiVFH3h-1577065892540)(http://img2.ph.126.net/M5Crz26S8MZz3CPsA2WR-Q==/6632564201028293222.png)]

这种div:first-child 第一个元素必须是div 否则样式将丢失
last-child后面不能加/

not

p适应样式并排除p1 p2

	.box1 p:not(.p1):not(.p2){
			width:150px;
			height: 150px;
			background: pink;
    	}
    	
    	
    	<div class="box1">
    	<p class="p1">111</p>
    	<p class="p2">222</p>
    	<p class="p3">333</p>
    	<p class="p4">444</p>
    </div>
     <div class="box1">
    	<p class="p">111</p>
    	<p class="p2">222</p>
    	<p class="p3">333</p>
    	<p class="p4">444</p>
    </div> 

示例2:使用最后一个div,但不适用第一个元素
如果只保留box2,是不会应用这个内容的

	<div class="box1">
		<div class="box2">222</div>

	</div>

hover样式

鼠标一定上去,元素发生样式变化

样式写法:

   .p-box{
        	width:150px;
        	height: 150px;
        	background: skyblue;
        }
        .p-box:hover{
        	background: black;
        }
        
        
         <div class="box1">
    	<p class="p1">111</p>
    	<p class="p2">222</p>
    	<p class="p3">333</p>
    	<p class="p4">444</p>
    </div>
     <div class="box1">
    	<p class="p">111</p>
    	<p class="p2">222</p>
    	<p class="p3">333</p>
    	<p class="p4">444</p>
    </div>

导航栏的写法

   <ul class="list">
    	<li><a href="#">秒杀</a></li>
    	<li><a href="#">优惠券</a></li>
    	<li><a href="#">闪购</a></li>
    	<li><a href="#">拍卖</a></li>
    </ul>
    
    #样式:
    
    		li {
			list-style: none;    #去除圆点
		}
		.list li{
			float:left;
			height:60px;
		}
		.list li a{
			padding: 6px 10px;
			color:#fff;
			font-size:22px;
			text-decoration: none;
			background: #f10180;
		}
		.list li a:hover{
			color:skyblue;
			background: #bd1067;
		}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-09PpMJQz-1577065892541)(http://img2.ph.126.net/nSQu5lz_-3wJGBkcuvOCmA==/6632671953165256778.png)]

input元素的属性大全

属性描述
placeholdtext规定帮助用户填写输入字段的提示。
readonlyreadonly规定输入字段为只读。
maxlengthnumber规定输入字段中的字符的最大长度。
<input type="text" placeholder="请输入内容" value="学习写代码的艰难困苦" maxlength="10">

border-radius 画圆

border-radius: 75px 75px 75px 75px;

等价于:

border-top-left-radius: 75px;
border-bottom-right-radius: 75px;
border-top-right-radius: 75px;
border-bottom-left-radius: 75px;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PnWcH3bP-1577065892543)(http://img1.ph.126.net/cJu1E9Sg-aMkdz7aj6raYw==/1034139064454764939.png)]

页面布局

clear用于两个框,让浮动的下来,父级盒子框住所有子盒子

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="keyword" content="" />
    <meta name="description" content="" />
    <title>layout</title>
    <style type="text/css">
    	* {
    		margin: 0;
    		padding: 0;
    	}
    	.header{
    		height: 80px;
    		background: green;
    	}
    	.main{
    		height: 2000px;
    	}
    	.footer{
    		height: 120px;
    		background: orange;
    	}
    	.header-nav{
    		width: 1000px;
    		height: 80px;
    		margin-left: auto;
    		margin-right: auto;
    		background: red;
    	}
    	.left{
    		width: 730px;
			height: 2000px;
			background: blue;
			float: left ;
    	}
    	.right{
    		width: 250px;
    		height: 2000px;
    		background: pink;
    		float: right;
    	}
    	.main-wrap{
    		width: 1000px;
    		margin-left: auto;
    		margin-right: auto;
    	}
		.clear{
			clear: both;
		}
		.footer-wrap{
			width: 1000px;
			height: 120px;
			margin-left: auto;
			margin-right: auto;
			background: yellow;
		}
    </style>
</head>

<body>
    <div class="header">
    	<div class="header-nav"></div>
    </div>
    <div class="main">
    	<div class="main-wrap">
    	<div class="left"></div>
    	<div class="right"></div>
    	<div class="clear"></div>
    	</div>
    </div>
    <div class="footer">
    	<div class="footer-wrap"></div>
    </div>
    
    <div class=""></div>
</body>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值