一、css中的水平居中的不同方式
1.在css中使用text-align和display属性
<!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>第一种居中方式</title>
<style>
.parent{
background: darkgray;
text-align: center;
}
.child{
width: 200px;
height: 200px;
background: darkred;
display: inline-block;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>
text-align的属性值一般常用的三种:
1.left 左对齐
2.right 右对齐
3.center 居中对齐
这里将其父div设置对齐方式为center,如果不给子级div加display属性时,子级div并不会居中,居中的将会是子级div的文本和父级div的文本,如果要将子级div居中就需要设置其display:inline-block;
* 当子级div的display设置成inline属性时,子级div的宽度和高度将会失效,但div可以实现居中效果,只是宽度和高度将无法设置
*当子级div的display属性设置成inline-block属性时,子级div的宽度和高度有效,一般常用这个属性,如果不给div设置display属性时,将会默认是block块级元素
*注意:text-align属性具有继承性,也就是说当父级设置了这个属性时,其所有子级(不管多少层子级标签)的文本内容将会默认是你所设置的对齐方式
2.在CSS中使用display和margin属性
<!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>第二种居中方式</title>
<style>
.parent{
background: gray;
}
.child{
width: 200px;
height: 200px;
background: indigo;
display: table;
margin: 0 auto;
}
</style>
</head>
<body>
<!-- 父级标签 -->
<div class="parent">
<!-- 子级标签-->
<div class="child"></div>
</div>
</body>
</html>
而这里可以不用设置display属性,同样也有居中效果,但是设置display属性时不能设置为inline属性值,否则居中同样会失效。这种居中方式的优缺点:优点:只需对子级标签进行设置就可以实现居中效果;缺点:如果子级标签脱离文档流,将会导致margin属性值失效。
3.在CSS中使用position和transform属性
<!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>第三种居中方式</title>
<style>
.parent{
width: 500px;
height: 200px;
position: relative;
background: #000;
}
.child{
background: green;
width: 200px;
height: 200px;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
</style>
</head>
<body>
<!-- 父级标签 -->
<div class="parent">
<!-- 子级标签 -->
<div class="child"></div>
</div>
</body>
</html>
优点:不论父级标签是否脱离了文档流,对子级标签都不会影响水平居中效果
缺点:transform属性是CSS3中新增属性,浏览器支持情况不好
二、css中的伪类:before和after
1.简介
css3为了区分伪类和伪元素,伪元素采用双冒号写法。
常见伪类——:hover,:link,:active,:target,:not(),:focus。
常见伪元素——::first-letter,::first-line,::before,::after,::selection。
“伪元素”,顾名思义。它创建了一个虚假的元素,并插入到目标元素内容之前或之后。
::before和::after下特有的content,用于在css渲染中向元素逻辑上的头部或尾部添加内容。
这些添加不会出现在DOM中,不会改变文档内容,不可复制,仅仅是在css渲染层加入。
所以不要用:before或:after展示有实际意义的内容,尽量使用它们显示修饰性内容,例如图标。
#example:before {
content: "#";
}
#example:after {
content: ".";
}
第一,我们用#example:before和#example:after来目标锁定相同的元素.严格的说,在代码中他们是伪元素。
第二,在内容模块中提到,伪元素如果没有设置“content”属性,伪元素是无用的。
可以设置content属性值为空,并且仅仅把他当做一个内容很少的盒子。像这样:
#example:before {
content: "";
display: block;
width: 100px;
height: 100px;
}
不可以完全的移除content属性,如果你移除了,伪元素将不会起作用。至少,content属性需要空引用作为它的值(即:content:“”)。
2、content属性
1、string
使用引号包一段字符串,将会向元素内容中添加字符串。如:a:after{content:""}
<!DOCTYPE html>
<meta charset="utf-8" />
<style type="text/css">
p::before{
content: "《";
color: blue;
}
p::after{
content: "》";
color: blue;
}
</style>
<p>平凡的世界</p>
2、attr()
通过attr()调用当前元素的属性,比如将图片alt提示文字或者链接的href地址显示出来。
<style type="text/css">
a::after{
content: "(" attr(href) ")";
}
</style>
<a href="http://www.cnblogs.com/starof">starof</a>
3、url()/uri()
用于引用媒体文件。
举例:“百度”前面给出一张图片,后面给出href属性。
<style>
a::before{
content: url("https://www.baidu.com/img/baidu_jgylogo3.gif");
}
a::after{
content:"("attr(href)")";
}
a{
text-decoration: none;
}
</style>
---------------------------
<body>
<a href="http://www.baidu.com">百度</a>
</body>
4、counter()
调用计数器,可以不使用列表元素实现序号功能。
配合counter-increment和counter-reset属性使用:
h2:before { counter-increment: chapter; content: "Chapter " counter(chapter) ". " }
<style>
body{
counter-reset: section;
}
h1{
counter-reset: subsection;
}
h1:before{
counter-increment:section;
content:counter(section) "、";
}
h2:before{
counter-increment:subsection;
content: counter(section) "." counter(subsection) "、";
}
</style>
------------------------------------------------
<body>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>
<h1>XML tutorials</h1>
<h2>XML</h2>
<h2>XSL</h2>
</body>
3、使用
1、清除浮动
清除浮动方法有多种,现在最常用的就是下面这种方法,仅需要以下样式即可在元素尾部自动清除浮动
.cf:before,
.cf:after {
content: " ";
display: table;
}
.cf:after {
clear: both;
}
.cf {
*zoom: 1;
}
2、模拟float:center的效果
float没有center这个取值,但是可以通过伪类来模拟实现。
这个效果实现很有意思,左右通过::before float各自留出一半图片的位置,再把图片绝对定位上去。
核心css如下:
#page-wrap { width: 60%; margin: 40px auto; position: relative; }
#logo { position: absolute; top: 0; left: 50%; margin-left: -125px; }
#l, #r { width: 49%; }
#l { float: left; }
#r { float: right; }
#l:before, #r:before { content: ""; width: 125px; height: 250px; }
#l:before { float: right; }
#r:before { float: left; }
3、做出各种图形效果
举例:一个六角星
<style>
#star-six {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid red;
position: relative;
}
#star-six::after {
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 100px solid red;
position: absolute;
content: "";
top: 30px;
left: -50px;
}
</style>
<body>
<div id="star-six"></div>
</body>
#star-six的div是一个正三角行,#star-six::after是一个倒三角形,通过绝对定位,调整其位置即可实现六角星的效果。
4、不使用图片创建小图标
举例:比如一个电话
很巧妙的应用一个div左border加圆角当机身,::before和::after配合圆角当听筒。
<style type="text/css">
#phone{width:50px;height:50px;border-left:6px solid #EEB422;border-radius:20%;transform:rotate(-30deg);-webkit-transform:rotate(-30deg);margin:20px;margin-right:0px;position:relative;display: inline-block;top: -5px;}
#phone:before{width:15px;height:15px;background:#EEB422;border-radius: 20%;content: "";position: absolute;left:-2px;top: 1px;}
#phone:after{width:15px;height:15px;background:#EEB422;border-radius: 20%;content: "";position: absolute;left:-3px;top: 34px;}
</style>
<div id="wraper">
<div id="phone"></div>
</div>
这个效果来自:http://www.w3cfuns.com/blog-5444604-5402127.html
有大神用伪元素创建了84种小图标,具体可查看http://nicolasgallagher.com/pure-css-gui-icons/
5、显示打印网页的URL
<style>
@media print {
a[href]:after {
content: " (" attr(href) ") ";
}
}
</style><body>
<a href="http://www.baidu.com">百度</a>
</body>
6、给blockquote添加引号
经常用到给blockquote 引用段添加巨大的引号作为背景,可以用 ::before 来代替 background 。好处是即可以给背景留下空间,还可以直接使用文字而非图片:
<meta charset="utf-8"/>
<style type="text/css">
blockquote::before {
content: open-quote;
color: #ddd;
z-index: -1;
font-size:80px;
}
</style>
<blockquote>引用一个段落,双引号用::before伪元素实现</blockquote>
7、超链接特效
举例:配合 CSS定位实现一个鼠标移上去,超链接出现方括号的效果
<meta charset="utf-8" />
<style type="text/css">
body{
background-color: #425a6c;
}
a {
position: relative;
display: inline-block;
outline: none;
color: #fff;
text-decoration: none;
font-size: 32px;
padding: 5px 20px;
}
a:hover::before, a:hover::after { position: absolute; }
a:hover::before { content: "\5B"; left: -10px; }
a:hover::after { content: "\5D"; right: -10px; }
</style>
<a>鼠标移上去出现方括号</a>
更多创意链接特效可参考: Creative Link Effects 。
8、::before和::after实现多背景图片
举例:一个标签应用5张背景图
三、meta标签的作用
meta标签是HTML标记head区的一个关键标签,它位于HTML文档的和
<head>
<!-声明文档使用的字符编码->
<metacharset=“utf-8”/>
<!-页面关键词->
<metaname=“keywords”content=“个人活动发布,会办app,活动管理,会议管理,社群管理“/>
<!-页面描述->
<metaname=”description“content=”发布个人会议,发布公司会议,w我们都可以帮你找到合适的会议地点和参会观众“>
<!-网页作者->
<metaname=”author“content=”xin1642868874@163.com“>
<!-搜索引擎抓取robotterms是一组使用逗号(,)分割的值,通常有如下几种取值:none,noindex,nofollow,all,index和follow。->
<metaname=“机器人“content=”索引,按照“>
<!-页面重定向和刷新->
<metahttp-equiv=”refresh“content=”0;url=“/>
<!-禁止Chrome浏览器中自动提示翻译->
<metaname=“google”value=“notranslate”>
<!--禁止百度转码->
<metahttp-equiv=“Cache-Control”content=“no-siteapp”>
<!-自定义标签:app版本号说明->
<metaname=“app-version”content=“1.13.3”>
<title>php中文网</title>
</head>
四、数组和链表的区别
1.数组
概念
元素在内存中是连续存储的,数组同链表一样,也是数据呈线性排列的一种数据结构。
优点
因为数据是连续存储的,内存地址连续,所以在查找数据的时候效率比较高;
缺点
添加和删除数据比较耗时。存储之前,我们需要申请一块连续的内存空间,并且在编译的时候就必须确定好它的空间的大小。在运行的时候空间的大小是无法随着你的需要进行增加和减少而改变的,当数据量比较大的时候,有可能会出现越界的情况,数据比较小的时候,又有可能会浪费掉内存空间。在改变数据个数时,增加、插入、删除数据效率比较低。
特点
数组的形式
查找数据
由于数据是存储在连续空间内,所以每个数据的内存地址都可以通过数组下标算出,我们就可以通过下标直接访问目标数据(随机访问)。
数组元素的增加
将数据添加到数组的任意位置,需要在数组的末尾增加新的存储空间,为了给新数据腾出位置,要把已有数据一个个移开,最后在空出来的要添加元素的位置写入要添加的新数据。
数组元素的删除
删除数组中,任意位置的数据,需要先删掉目标数据,然后把后面的数据一个个往空位移,最后删掉多余的空间。
2.链表
概念
链表是数据结构之一,其中的数据呈线性排列。
优点
添加和删除、插入比较方便
缺点
查询时速度比较慢
特点
链表中的每个数据都有一个指针,用于指向下一个数据的内存地址
在链表中,数据一般都是分散存储于内存中的,无须存储在连续空间内
查找数据
由于数据是分散存储,查找数据时,只能从第一个数据开始,顺着指针的指向一一往下访问(顺序访问)。
添加数据
添加数据时,只需要改变添加位置前后的指针指向就可以。
删除数据
数据的删除也一样,只需改变指针的指向就可以。
循环链表
链表尾部使用指针,并将指针指向链表头部的数据,称之为循环链表
双向链表
链表里每个数据都有两个指针,并且他们分别指向前后数据,称之为双向链表。
优点
不仅可以从前往后,还可以从后往前遍历数据。
缺点
指针数的增加会导致存储空间需求增加;添加和删除数据时需要改变更多指针的指向