1.positon:fixed
可以实现网页浏览器上的返回顶部的功能。
positon:fixed 表示将当前div块固定在页面的某一个位置(默认为左上角)。

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body style="margin:0 auto;">
<div onclick='GoTop();' style="height:50px;width:50px;background-color: black;color:white;
position:fixed;
bottom:20px;
right:20px;
">返回顶部</div>
<div style="height: 5000px;background-color: #dddddd;">
</div>
<script>
function GoTop(){
document.body.scrollTop=0;
}
</script> </body>
</html>

2.positon实现 网页头部在网页上面固定

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.pg-header{
height:48px;
background-color: black;
color:#dddddd;
positon:fixed;
top:0;
left:0;
right:0; }
.pg-body{
height:500px;
backgound-color:blue;
margin-top:50px;
}
</style>
</head>
<body>
<div class="pg-header">头部内容</div>
<div class="pg-body">内容部分</div> </body>
</html>

3.position:absolute 绝对定位,一锤子买卖;自身的应用场景不多;
当其与relative+absolute一起结合着用,应用场景就会增加;

如下:
在一个div块中,将内部的div块固定在这个div块的某个位置。
<div style="width:500px;height:200px;background-color:black;position:relative;margin:0 auto;">
<div style="position:absolute;left:0;bottom:0;width:50px;height:50px;background-color: white;border:1px;"></div>
</div>
拓展:
<div style="width:500px;height:200px;background-color:black;position:relative;margin:0 auto;">
<div style="position:absolute;left:0;bottom:0;width:50px;height:50px;background-color: white;border:1px;line-height: 50px;text-align:center;">笑脸</div>
<div style="position:absolute;left:80px;bottom:0;width:50px;height:50px;background-color: white;border:1px;line-height: 50px;text-align:center;">哭脸</div>
</div>

解析:
在外层div中设置positon:relative,该div标签是不会发生任何变化的;但是内部的div里面的position:absolute可以指定放在父类标签的固定位置。

实例:用户登陆窗口的输入框右侧放置图片。

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="height:50px;width:400px;position:relative;">
<input type="text" style="height:50px;width:360px;padding-right:40px;"/>
<span style="position:absolute;top:8px;right:0;
background-image: url(userpic.jpg);
height:40px;width:40px;
display:inline-block"></span>
</div>
</body>
</html>

4.opacity透明度,z-index层级顺序;

实例:设计三层的页面,最上层为输入层,第二层为遮罩层(透明度),第三层为正常页面;

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="z-index:10;
position:fixed;
top:50%;
left:50%;
background-color: white;
width:50px;height:100px;
"> <form>
用户名:<input type="text" name="用户名" value="admin"/>
密码:<input type="password" name="密码" value="admin"/>
<input type="submit" name="提交"/>
</form>
</div> <div style="z-index:9;
position:fixed;
bottom:0;
top:0;
left:0;
right:0;
opacity:0.5;
background-color: #6e6568;
"><br/>
<br/>遮罩层</div>
<div style="background-color: #c81623;height:5000px;">正文内容</div> </body>
</html>

解析:z-index属性用来设置div的层级顺序;
opacity属性用来设置透明度,0-1(完全透明-完全不透明)

5.overflow

问题:当img标签内的图片大小超过外层div时,会将div设置的边界冲开,为了避免这个问题可以使用overflow属性设置。(或设置img大小)
  overflow:hidden; 如果图片大小超过外层div设置,则隐藏大于部分,仅显示div设置的大小
  overflow:auto; 如果图片大小超过外层div设置,则自动出线滚动条可以查看整个图片内容;

 <div style="height:220px;width:300px;overflow:hidden">
<img src="1.jpg"/>
</div>

6.hover

当鼠标移动到当前标签上时,指定的css属性才会生效;

 .pg-header .menu:hover{
  background-color: red;
  }

实例:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.pg-header{
position:fixed;
right:0;
top:0;
left:0;
height:48px;
background-color: #2459a2;
line-height:48px;
}
.pg-body{
margin-top:50px;
}
.w{
width:980px;
margin:0 auto;
}
.pg-header .menu {
display: inline-block;
padding:0 10px;
}
.pg-header .menu:hover{
background-color: red;
}
</style>
</head>
<body>
<div class="pg-header">
<div class="w">
<a class="logo">logo</a>
<a class="all">全部</a>
<a class="menu">首页</a>
<a class="news">新闻</a>
<a class="regarding">关于</a> </div> </div>
<div class="pg-body">
<div class="w">content</div>
</div>
</body>
</html>

7.background-image background-repeat

background-image:url('grey.png')   指定背景图片
background-repeat: no-repeat       指定当背景图片小于div大小时,是否自动堆叠填充;
          no-repeat 不堆叠;
          repeat-X 水平方向堆叠;
          repeat-Y 竖直方向堆叠;
          repeat 水平和竖直方向自动堆叠;
          space
          inherit
          round
例:<div style="background-image:url('grey.png');height:5000px; width:250px;background-repeat: no-repeat"></div>
应用场景1:图片堆叠:指定的背景图片非常小,要堆满整个大块div,使用这种方法能够实现。

应用场景2:dig.chouti.com的点赞爱心小图片的获取。

 <div style="background-image:url('chouti.png');
height:20px; width:20px;
background-repeat:no-repeat;
background-position-x:0px;
background-position-y:-20px;
"></div>

简要写法:

 <div style="background:url(chouti.png) no-repeat 0 0;
width:20px;height:20px;
"></div>

作用:网页上加载这种小logo图示,使用一张图,用backgound-position和div的大小设置,调整在div中显示的图片的指定位置和内容,实现一次请求调用图片资源就可以实现相关的图示的加载。几乎所有的网站都用这种方式实现该功能。

CSS样式3的更多相关文章

  1. css样式让input垂直居中

    css样式让input垂直居中 css代码: .div1{ border: 1px solid #CCC; width:1120px; height:40px; margin:auto; displa ...

  2. 深度理解CSS样式表,内有彩蛋....

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. js设置css样式.

    在js设置css样式做法 var obj = document.getElementById('div'); obj.style.width = '100px'; obj.style.height = ...

  4. CSS样式表

    CSS样式及属性 样式标的基本概念 样式表的分类 1.内联样式表 和html联合显示,控制精确,但可重用性差,冗余多. 例:<p style="font-size:14px;" ...

  5. 脚本工具(获取某个文件夹下的所有图片属性批量生成css样式)

    问题描述: 由于有一次工作原因,就是将某个文件夹下的所有图片,通过CSS描述他们的属性,用的时候就可以直接引用.但是我觉得那个文件夹下的图片太多,而且CSS文件的格式又有一定的规律,所有想通过脚本来生 ...

  6. jQuery所支持的css样式

    jQuery所支持的css样式 backgroundPosition borderWidth borderBottomWidth borderLeftWidth borderRightWidth bo ...

  7. Yii2 assets注册的css样式文件没有加载

    准备引入layui.css文件的,在LayuiAssets类中已经配置了资源属性 <?php namespace frontend\assets; use yii\web\AssetBundle ...

  8. 获取元素计算后的css样式封装

    获取元素计算后的css样式封装: function getCss(obj,attribute) { if(obj.currentStyle) { return obj.currentStyle[att ...

  9. JS实战 · 仿css样式选择器

    代码如下: <html> <head>     <meta http-equiv="Content-Type" content="text/ ...

  10. CSS样式之优先级

    说到到css的样式优先级,今天偶再来回顾下,从css的样式优先级可分为两个部分: 1.从CSS代码放置的位置看权重优先级:     内联样式 > 内部嵌入样式 >外联样式 2.从样式选择器 ...

随机推荐

  1. springboot 切面添加日志功能

    1.新建一个springboot项目 2.定义个切面类,并指定切入点,获取所需记录信息(如:访问人IP, 访问地址,访问地址名称等) 3.新建数据库 SET FOREIGN_KEY_CHECKS=0; ...

  2. 快速搜索多个word、excel等文件中内容

    背景:要在多个文件甚至文件夹中找到文件中包含的某些内容 以win10举例: 1.打开一个文件夹 2.打开文件夹选项 3.配置搜索 4.搜索文件

  3. Jenkins中管道案例脚本(生命式语法)

    pipeline { agent any parameters { choice( choices: 'feature\nmaster\npercent10', description: '选择要发布 ...

  4. Python Modules and Packages – An Introduction

    This article explores Python modules and Python packages, two mechanisms that facilitate modular pro ...

  5. Python设计模式(10)-模板模式

    class DbManager: def insert(self): pass def dele(self): pass class DbManager: def insert(self): pass ...

  6. 004-流程控制-C语言笔记

    004-流程控制-C语言笔记 学习目标 1.[掌握]关系运算符和关系表达式 2.[掌握]逻辑运算符和逻辑表达式 3.[掌握]运算符的优先级和结合性 4.[掌握]if-else if-else结构的使用 ...

  7. 【翻译】创建String 使用“”还是构造函数(new String)

    在java中创建String,通常有以下两种方法. String x = "abc"; String y = new String("abc"); 它们之间有什 ...

  8. python干货-类属性和方法,类的方法重写

    类属性与方法 类的私有属性 __private_attrs: 两个下划线开头,表明为私有,外部不可用,内部使用时self.__private_attrs. 类的方法 在类的内部,使用 def 关键字来 ...

  9. 学会这项python技能,就再也不怕孩子偷偷打游戏了

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:鸟哥 PS:如果想了解更多关于python的应用,可以私信小编,资料 ...

  10. H - Knight Moves DFS

    A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...