css

CSS+DIV详解

css+div基本介绍

层叠样式表是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。

CSS目前最新版本为CSS3,是能够真正做到网页表现与内容分离的一种样式设计语言。相对于传统HTML的表现而言,CSS能够对网页中的对象的位置排版进行像素级的精确控制,支持几乎所有的字体字号样式,拥有对网页对象和模型样式编辑的能力,并能够进行初步交互设计,是目前基于文本展示最优秀的表现设计语言。CSS能够根据不同使用者的理解能力,简化或者优化写法,针对各类人群,有较强的易读性。

DIV是层叠样式表中的定位技术,全称DIVision,即为划分。有时可以称其为图层。

<div> 可定义文档中的分区或节(division/section)。

<div> 标签可以把文档分割为独立的、不同的部分。它可以用作严格的组织工具,并且不使用任何格式与其关联。

 

PS:

简单的理解就是CSS是用来控制HTML的样式的,使HTML样式和内容分离,DIV是HTML的一个独立单位,可以用CSS来任意控制位置,实现定位。

DIV可以放任意HTMl元素,DIV也是可以嵌套放置的。

css+div的优势

1.符合W3C的标准。

2.内容样式分离,对搜索引擎更加友好。

3.样式调整更为方便。

4.使代码更为整洁,尤其是在大型项目中,整洁的代码,开发效率更高。

 

使用css+div之后,自然而然的就不在去使用table的布局方法了,但是table布局显示数据的便捷性,是css+div无法替代,故该使用table时,还是要使用table,不要为了统一而将本该简单的问题复杂化。


css的基本语法

html中(内部css):

<标签style="样式属性: 样式值1 样式值2;">内容</标签>

css中(外部css):

如果是外部css,则首先要引用css文件,格式为:

<link type=”text/css” rel=”stylesheet” href=”路径” />

css文件中选择器的格式为:

选择器名 {

样式属性: 样式值1 样式值2;(支持多个样式值的样式属性)

样式属性: 样式值;

}

实例:


 
 
  1. <!-- Doctype:文档类型 指定dtd:说明当前这个html版本 -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>HTML1.html </title>
  6. <!-- 关键字 用于SEO 给搜索引擎爬虫用的 -->
  7. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  8. <!-- 文档的描述 -->
  9. <meta http-equiv="description" content="this is my page">
  10. <!-- 给浏览器文件的编码格式 -->
  11. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  12. <!-- 引入CSS -->
  13. <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  14. <style type="text/css">
  15. .style1{
  16. font-size: 20px;
  17. color: red;
  18. font-weight:bold;
  19. font-style: italic;
  20. text-decoration: underline;
  21. }
  22. img{
  23. /*使用滤镜将图片变成黑白色,在低版本的浏览器有用*/
  24. filter: gray;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <span style="font-size: 30px;color: blue;">栏目一 </span>
  30. <span style="font-size: 10px;color: red;font-style: italic;">栏目二 </span>
  31. <span style="font-size: 30px;color: yellow;font-weight: bold;">栏目三 </span>
  32. <span style="font-size: 30px;color: green;font-weight: lighter;">栏目四 </span>
  33. <span style="font-size: 30px;color: green;font-weight: bolder;">栏目五 </span>
  34. <br/>
  35. <span class="style1">栏目一 </span>
  36. <span class="style1">栏目二 </span>
  37. <span class="style1">栏目三 </span>
  38. <span class="style1">栏目四 </span>
  39. <span class="style1">栏目五 </span>
  40. <br/>
  41. <img src="../resource/images/1.jpg" alt='图片' width='100px'/>
  42. <img src="../resource/images/image01.jpg" width='100px'> </img>
  43. </body>
  44. </html>

css的4种选择器

类选择器 

基本语法:

.类选择器名{

属性名:属性值;

}

PS:在元素中类选择器是可以多个一起使用的,如果在多个类选择器中,同意属性样式重复定义,那么,以后面那个选择器为准。

其格式为:<span class="style1 style2 style3">内容</span>

 

实例:

css中

css中

.style1{

font-size: 20px;

font-weight: bold;

background-color: purple;

}

html中

<span class="style1">内容</span>


ID选择器

基本语法:

#id选择器名{

属性名:属性值;

}

PS:在元素中ID选择器只能使用一个,其优先级也是最高的。

 

实例:

css中

#style2{

     font-size: 30px;

     background-color: skyblue;

}

html中

<span id="style2">内容</span>


标签选择器

基本语法:

body{

     color:silver;

}

PS:标签选择器就是对HTML元素的样式进行定义。如果不是HTML元素,即使定义也无法识别,例如:aaa{ font-size: 20px;} <aaa>aaaa</aaa>这样不存在于HTML标签库的元素是不能被识别并且定义的。


通配符选择器

如果希望所有的元素都符合某一种样式,可以使用通配符选择器。

基本语法:

/*使用通配符选择器对外边距和内边距清零*/

*{

margin: 0;

padding: 0;

}

PS:通配符选择器可以和父子选择器结合使用,接下来会有相应内容。


注意事项:

当同一个元素被下列选择器修饰时,优先级的大小为:

ID选择器>类选择器>标签选择器>通配符选择器

原因:越特殊的选择器,优先级越高。

 

父子选择器

父子选择器是在已定义类或者ID选择器的标签中,采取递进的方式对子孙元素定义样式。

基本语法:

css中

#style2 span{

     font-style:italic;

     color:red;

}

html中

<span id="style2">这是一则<span>非常重要</span>的新闻</span><br/>

注意事项:

(1)子选择器标签必须是html可以识别的标记

(2)父子选择器可以有多级

(3)父子选择器可以适用于id选择器和class选择器

 

群组选择器

在css文件中,如果有多个类/id选择器,它们都有相同的部分,我们可以把相同的css样式放在一起,用,隔开,这样就是一个群组选择器。

实例:

/*招生广告*/

.ad_stu{

width :136px;

background-color:#FC7E8C;

margin:5px 0 0 5px;

}

/*广告2*/

.ad_2{

width :457px;

background-color:#7CF574;

margin:5px 0 0 6px;

}

/*房地产广告*/

.ad_house{

height:196px;

background-color:#7CF574;

margin:5px 0 0 5px;

}

.ad_stu,.ad_2,.ad_house{

width :152px;

float:left;

}



综合实例:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML2.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML2.css">
  9. </head>
  10. <body>
  11. 新闻
  12. <br/>
  13. <span class="style1">新闻1 </span>
  14. <span class="style1">新闻2 </span>
  15. <span class="style1 style3">新闻3 </span>
  16. <span class="style1">新闻4 </span>
  17. <span class="style1" id="style2">新闻5 </span>
  18. <br/>
  19. <span id="style2"> <span><span></span>重要 </span>的消息 </span>
  20. <br/>
  21. <a href="http://www.baidu.com">百度 </a>
  22. </body>
  23. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. /*类选择器*/
  3. .style1 {
  4. font-weight: bold;
  5. font-size: 20px;
  6. background-color: pink;
  7. color: blue;
  8. }
  9. .style3 {
  10. font-style: italic;
  11. text-decoration: underline;
  12. color: green;
  13. }
  14. /*ID选择器*/
  15. #style2 {
  16. font-weight: 30px;
  17. background-color: silver;
  18. color: black;
  19. }
  20. /* 父子选择器 */
  21. #style2 span {
  22. font-style: italic;
  23. color: red;
  24. }
  25. #style2 span span {
  26. font-weight: bold;
  27. color: green;
  28. }
  29. /*标签选择器*/
  30. body {
  31. color: yellow;
  32. }
  33. /* 通配符选择器 */
  34. * {
  35. margin: 5px 0px 0px 0px;
  36. padding: 0px;
  37. }
  38. /*超链接的4种状态*/
  39. /*
  40. a:link - 普通的、未被访问的链接
  41. a:visited - 用户已访问的链接
  42. a:hover - 鼠标指针位于链接的上方
  43. a:active - 链接被点击的时刻
  44. */
  45. a :LINK {
  46. color: black;
  47. text-decoration: none;
  48. }
  49. a :VISITED {
  50. color: silver;
  51. text-decoration: overline;
  52. }
  53. < pre name=" code" class=" css"> a :HOVER {
  54. text-decoration: underline;
  55. }
a:ACTIVE {color: green;text-decoration: line-through;}
 





 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML4.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML4.css" />
  9. </head>
  10. <body>
  11. <font class="s1">梁山排行榜 </font> <br/>
  12. <span class="sj">宋江 </span> <br/>
  13. <span class="lyw">卢员外 </span> <br/>
  14. <span class="wy">吴用 </span> <br/>
  15. <span class="bzt">豹子头 </span> <br/>
  16. <span class="ddgs">大刀关胜 </span> <br/>
  17. </body>
  18. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. .s1 {
  3. font-size: 30px;
  4. color: blue;
  5. }
  6. .sj {
  7. font-style: italic;
  8. color: red;
  9. }
  10. .lyw {
  11. }
  12. .wy {
  13. font-style: italic;
  14. text-decoration: underline;
  15. }
  16. .bzt {
  17. }
  18. .ddgs{
  19. }
  20. .sj, .lyw, .wy, .bzt, .ddgs {
  21. background-color: gray;
  22. font-weight: bold;
  23. }


块元素和行内元素

行内元素,又叫内联元素,内联元素只能容纳文本或者其他内联元素。

常见内联元素:

     <span><a><input><img>

特点是:只占内容的宽度,默认不换行,行内元素一般放文本或者其它的行。

 

块元素一般都从新行开始,可以容纳文本,其它内联元素和其它块元素,即使内容不能占满一行,块元素也要把整行占满。

常见块元素:  

<div><p>

特点是:不管内容有多少,都要换行,同时占满整行,块元素可以放文本,行内元素,块元素。

 

注意事项:

1、一些css属性对行内元素不生效,例如margin,left,但是这个是于浏览器版本类型有关的。

2、如果在行内元素里面放置块元素,行内元素将被截断,也就是说因为块元素必须要占满一行,那么这样做的结果就是一行变为三行,分别是,行内元素,块元素,行内元素。

3、div就是块元素,那么就拥有块元素的一切特性。

html中:

<span class="s1">行内元素1</span>

<span class="s1">行内元素2</span>

<div class="s2">块元素1</div>

<div class="s2">块元素2</div>

css中:

.s1{

background-color: silver;

font-size:20px;

}

.s2{

background-color: skyblue;

font-size:30px;

font-style: italic;

}


块元素和行内元素的转换

如果我们希望一个元素按照块元素的方式显示,则: display:block;

<span style=”display:block;” class=”s2”>abc</span>

如果我们希望一个元素按照行内元素的方式显示,则: display:inline;

<div style=”display:inline;” class=”s2”>hello</div>

同样的,这也可以写到CSS中。


PS:还有个常见的叫inline-block(行内块),它兼顾行内和块元素的特性,占用内容宽度,并且支持width,margin等的设置,也能看做块元素。这就是为什么叫做行内块元素的原因了。


综合实例:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML6.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML6.css" />
  9. </head>
  10. <body>
  11. <div class="s1">第一个div </div>
  12. <span class="s2">第一个span </span>
  13. <span class="s3">第二个span </span>
  14. <div class="s4">第二个div </div>
  15. Hello World
  16. </body>
  17. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. .s1 {
  3. text-decoration: underline;
  4. font-size: 20px;
  5. font-weight: bold;
  6. }
  7. .s2 {
  8. color: red;
  9. font-size: 25px;
  10. font-style: italic;
  11. }
  12. .s3 {
  13. color: yellow;
  14. font-weight: bolder;
  15. font-size: 15px;
  16. display: block;
  17. }
  18. .s4 {
  19. text-decoration: line-through;
  20. display: inline;
  21. }
  22. .s1, .s2, .s3, .s4 {
  23. background-color: silver;
  24. }

盒子模型

盒子模型是CSS中最重要的内容之一,其核心思想就是将HTML中的所有元素都看成是盒子,盒子里有实际内容(内容content),盒子可能有填充物(内边距padding),盒子可能有壳(边框border),盒子可能和其他盒子有距离(外边距margin),由这些东西就构成了盒子模型。

细节说明:

1、任何一元素都可以看做是一个盒子。

2、嵌套盒子模型的参照物不一样,则使用的属性不一样。外层盒子的panding等价于内层盒子的margin。

3、padding值过大会改变盒子的大小,margin值过大会挤到盒子外面,但盒子并没有变化。

4、如果不想破坏盒子模型的整体外观的话,尽量少使用padding。

综合实例:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML7.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML7.css" />
  9. </head>
  10. <body>
  11. <div class="s1">
  12. <img src="../resource/images/1.jpg" />
  13. </div>
  14. </body>
  15. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. border: 1px solid red; /* 给body指定了border的宽度,样式,颜色 */
  4. width: 800px;
  5. height: 1000px;
  6. margin: 0 auto; /* 边距为0,居中 */
  7. }
  8. .s1 {
  9. width: 50px;
  10. height: 52px;
  11. border: 1px solid blue;
  12. margin-top: 10px;
  13. margin-left: 100px;
  14. border-top: 1px solid red;
  15. }
  16. .s1 img {
  17. width: 40px;
  18. margin-top: 5px;
  19. margin-left: 5px;
  20. }





 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML8.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML8.css" />
  9. </head>
  10. <body>
  11. <div class="div1">
  12. <ul>
  13. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  14. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  15. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  16. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  17. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  18. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  19. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  20. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  21. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  22. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  23. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  24. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  25. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  26. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  27. <li> <img src="../resource/images/image01.jpg"/> <a href="#">超链接 </a> </li>
  28. </ul>
  29. </div>
  30. </body>
  31. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. margin: 0px;
  4. padding: 0px;
  5. }
  6. /* 控制图片显示位置 */
  7. .div1 {
  8. width: 500px;
  9. height: 300px;
  10. border: 1px solid #b4b4b4;
  11. margin-left: 100px;
  12. margin-top: 20px;
  13. }
  14. /* 用于控制显示图片区域的宽度和高度 */
  15. .div1 ul {
  16. width: 350px;
  17. height: 250px;
  18. border: 1px solid red;
  19. list-style-type: none;
  20. padding: 0px;
  21. margin: 0px auto;
  22. }
  23. /* 用于控制单个图片区域的大小 */
  24. .div1 ul li {
  25. width: 50px;
  26. height: 62px;
  27. border: 1px solid blue;
  28. float: left; /* 左浮动 */
  29. margin-left: 5px;
  30. margin-top: 5px;
  31. }
  32. .div1 ul li a {
  33. font-size: 12px;
  34. margin-left: 5px;
  35. margin-bottom: 10px;
  36. text-decoration: none;
  37. }
  38. .div1 ul li img {
  39. width: 40px;
  40. margin: 5px 5px;
  41. }





 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML9.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML9.css" />
  9. </head>
  10. <body>
  11. <div>
  12. <div class="div1">
  13. <span class="div1-s1">类型1 </span>
  14. <span class="div1-s2">更多 </span>
  15. </div>
  16. <div class="div2">
  17. <ul>
  18. <li> <img src="../resource/images/1.jpg"/> <a href="#">超链接1 </a> </li>
  19. <li> <img src="../resource/images/1.jpg"/> <a href="#">超链接1 </a> </li>
  20. <li> <img src="../resource/images/1.jpg"/> <a href="#">超链接1 </a> </li>
  21. </ul>
  22. </div>
  23. <div class="div1">
  24. <span class="div1-s1">类型2 </span>
  25. <span class="div1-s2">更多 </span>
  26. </div>
  27. <div class="div2">
  28. <ul>
  29. <li> <img src="../resource/images/2.jpg"/> <a href="#">超链接2 </a> </li>
  30. <li> <img src="../resource/images/2.jpg"/> <a href="#">超链接2 </a> </li>
  31. <li> <img src="../resource/images/2.jpg"/> <a href="#">超链接2 </a> </li>
  32. </ul>
  33. </div>
  34. </div>
  35. </body>
  36. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. margin: 0px auto;
  4. padding: 0px;
  5. width: 1000px;
  6. }
  7. .div1 {
  8. /* border: 1px solid red; */
  9. }
  10. .div1-s1 {
  11. margin-left: 10px;
  12. }
  13. .div1-s2 {
  14. margin-left: 400px;
  15. color: blue;
  16. }
  17. .div2 {
  18. /* border: 1px solid blue; */
  19. height: 150px;
  20. }
  21. .div1, .div2 {
  22. margin: auto;
  23. width: 500px;
  24. }
  25. /* 控制图片显示位置 */
  26. .div2 ul {
  27. height: 150px;
  28. width: 500px;
  29. margin : 0px;
  30. /* border: 1px solid red; */
  31. list-style-type: none;
  32. margin: auto;
  33. padding: 0px;
  34. }
  35. /* 用于控制显示图片区域的宽度和高度 */
  36. .div2 ul li {
  37. width: 130px;
  38. height: 130px;
  39. padding: 0px;
  40. margin-left: 25px;
  41. margin-top: 10px;
  42. float: left;
  43. /* border: 1px solid blue; */
  44. }
  45. /* 用于控制单个图片区域的大小 */
  46. .div2 ul li img {
  47. width: 110px;
  48. margin: auto;
  49. }
  50. .div2 ul li a {
  51. margin-left: 30px;
  52. text-decoration: none;
  53. }


浮动

浮动是一个重要的概念:分为左浮动,右浮动,清除浮动。

默认情况下,div纵向排列,但是有时候需要它横向排列,这时候就能用浮动解决。

实例:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML10.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML10.css" />
  9. </head>
  10. <body>
  11. <div class="div1" id="div2">div1 </div>
  12. <div class="div1">div2 </div>
  13. <div class="div1">div3 </div>
  14. </body>
  15. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. margin: 0px auto;
  4. padding: 0px;
  5. width: 1000px;
  6. height: 800px;
  7. border: 1px solid red;
  8. }
  9. .div1 {
  10. height: 100px;
  11. width: 150px;
  12. background-color : aqua;
  13. margin: 5px;
  14. border: 1px solid blue;
  15. background-color: aqua;
  16. }
  17. #div2 {
  18. float: right; /* 右浮动:让该元素,尽量向右边移动,知道碰到他的父元素的右边界 */
  19. }




 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML11.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML11.css" />
  9. </head>
  10. <body>
  11. <img src="../resource/images/1.jpg" /> 文字文字文字文字文字文字文字文字文字文字文字文
  12. 文字文字文字文字文字文字文字文字文字文字文字文字文字字文字文字文字文字文字文字文字文字文字文字文字文字
  13. 文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文
  14. 文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文
  15. 文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文
  16. 文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文
  17. 文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文
  18. 文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文
  19. 文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文字文
  20. </body>
  21. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. margin: 0px auto;
  4. padding: 0px;
  5. width: 1000px;
  6. height: 800px;
  7. border: 1px solid red;
  8. }
  9. img {
  10. width: 150px;
  11. float: left;
  12. margin-right: 10px;
  13. }

PS:清除浮动现在不是很能理解,以后补充。所知道的是当3个div并列排列,前两个用到左浮动,第三个div则于前两个处于同一行,如果第三个div清楚浮动,则第三个div处于前两个div下面。清除用到的是clear属性,clear共有(none,left,right,both)四种模式,none为默认情况,不清除左右浮动元素。left表示清除左边浮动,right表示清除右边浮动,both表示清除两边浮动。


PS:现在知道clear是什么意思了,设置clear属性的元素(一般为块级元素)能将左右两边浮动同时或部分清除,both同时,left左边,right右边,none不清除(默认情况),清除的含义就是使得设置clear属性的元素不会被浮动元素遮盖。


案例:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title> </title>
  6. <style type="text/css">
  7. #div1, #div2, #div4 {
  8. border: 1px solid red;
  9. float: left;
  10. width: 100px;
  11. height: 50px;
  12. }
  13. #div1 {
  14. height: 200px;
  15. }
  16. #div3 {
  17. border: 1px solid red;
  18. width: 100px;
  19. height: 50px;
  20. clear: both;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="div1">1 </div>
  26. <div id="div2">2 </div>
  27. <div id="div3">3 </div>
  28. <div id="div4">4 </div>
  29. </body>
  30. </html>

结果


PS:由此说明清除元素,不会出现卡住的情况。


注意事项:

1、如果一行不够排下所有的div,则会自动换行。

2、在元素超过一行,如果有某个div过大,则会卡住别的div。

3、如果过大的那个div是一行中的倒数第二个div,那么被卡住的那个div会在第一个div下面。因为如果放在卡住的那个div后面,明显空间不够。


标准流/非标准流

标准流:在网页布局中,写在前面的元素出现在前面,写在后面的元素,显示在后面,这是默认的布局方式,也称标准流。

非标准流:在实际网页布局中,可能需要使用非标准流的方式来布局(让某个元素脱离它本身的位置)。


定位

四种定位形式:

static(标准流)

之前写的的所有HTML都是使用static定位的。static是默认的方式,对static而言,left和right是不生效的。

 

relative(非标准流:相对定位前元素的位置)(相对定位)

元素框偏离某个位置(left 和 top),元素仍然保持为定位前的形状,它原本所占的空间仍保留,从这一角度看,好像该元素仍然在文档流/标准流中一样。

特别说明:relative的参照点是它原来的位置,进行移动。

 

实例:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML12.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML12.css" />
  9. </head>
  10. <body>
  11. <div class="div1">内容1 </div>
  12. <div class="div1" id="special">内容2 </div>
  13. <div class="div1">内容3 </div>
  14. <div class="div1">内容4 </div>
  15. </body>
  16. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. margin: 0px auto;
  4. padding: 0px;
  5. width: 1000px;
  6. height: 800px;
  7. border: 1px solid red;
  8. }
  9. .div1 {
  10. width: 100px;
  11. height: 60px;
  12. background-color: gray;
  13. float: left;
  14. margin-left: 10px;
  15. margin-top: 10px;
  16. }
  17. /* relative是相对定位:相对于父元素的坐标系进行定位 */
  18. #special {
  19. position: relative; /* 相对定位 */
  20. left: 40px; /* 在原来的位置,向右移动大小 */
  21. top: 100px; /* 在原来的位置,向下移动大小 */
  22. }

absolute(非标准流:相对最近的非标准流父元素的位置,无则相对body)(绝对定位)

相当于元素从原来的位置脱离,并让出自己的空间,后面的元素就会占有让出的空间。

特别说明:absolute定位是相对于离自己最近的那个非标准流盒子而言的。

实例:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML13.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML13.css" />
  9. </head>
  10. <body>
  11. <div class="div2">
  12. <div class="div1">内容1 </div>
  13. <div class="div1" id="special">内容2 </div>
  14. <div class="div1">内容3 </div>
  15. <div class="div1">内容4 </div>
  16. </div>
  17. </body>
  18. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. margin: 0px auto;
  4. padding: 0px;
  5. width: 1000px;
  6. height: 800px;
  7. border: 1px solid red;
  8. }
  9. .div1 {
  10. width: 100px;
  11. height: 60px;
  12. background-color: gray;
  13. float: left;
  14. margin-left: 10px;
  15. margin-top: 10px;
  16. }
  17. #special {
  18. position: absolute; /* 绝对定位 */
  19. left: 600px; /* 在原来的位置,向右移动大小 */
  20. top: 100px; /* 在原来的位置,向下移动大小 */
  21. }
  22. /* absolute定位是以最近的已定位(非标准流)祖先来定位的,当祖先用relative,absolute,fixed定位时,
  23. 子元素用absolute定位就是相对于祖先元素的坐标系定位,而没有已定位(非标准流)祖先元素时,则相对于body进行定位 */
  24. .div2 {
  25. margin: 1px auto;
  26. border: 1px solid blue;
  27. width: 800px;
  28. height: 700px;
  29. position: relative;
  30. left: 50px;
  31. top: 50px;
  32. }


fixed(非标准流:相对body的位置)

元素框的表现类似于将position设置为absolute,不过其包含块是视窗本身,也就是body元素。


实例:


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML14.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML14.css" />
  9. </head>
  10. <body>
  11. <div class="div2">
  12. <div class="div1">内容1 </div>
  13. <div class="div1" id="special">内容2 </div>
  14. <div class="div1">内容3 </div>
  15. <div class="div1">内容4 </div>
  16. </div>
  17. </body>
  18. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. margin: 0px auto;
  4. padding: 0px;
  5. width: 1000px;
  6. height: 800px;
  7. border: 1px solid red;
  8. }
  9. .div1 {
  10. width: 100px;
  11. height: 60px;
  12. background-color: gray;
  13. float: left;
  14. margin-left: 10px;
  15. margin-top: 10px;
  16. }
  17. /* fixed则是相对于body进行定位 */
  18. #special {
  19. position: fixed; /* 固定定位 */
  20. left: 700px; /* 在原来的位置,向右移动大小 */
  21. top: 150px; /* 在原来的位置,向下移动大小 */
  22. }
  23. .div2 {
  24. margin: 1px auto;
  25. border: 1px solid blue;
  26. width: 800px;
  27. height: 700px;
  28. position: relative;
  29. left: 50px;
  30. top: 50px;
  31. }


案例:仿搜狐首页部分布局


 
 
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>HTML15.html </title>
  5. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  6. <meta http-equiv="description" content="this is my page">
  7. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  8. <link rel="stylesheet" type="text/css" href="../resource/css/HTML15.css" />
  9. </head>
  10. <body>
  11. <!-- 首部 -->
  12. <div class="head">
  13. <div class="login">登录表单 </div>
  14. <div class="setpage">设为首页 </div>
  15. <div class="seta">设置超链接 </div>
  16. </div>
  17. <!-- 导航部分 -->
  18. <div class="nav">
  19. <img alt="logo" src="../resource/images/logo.png" />
  20. <div>导航条 </div>
  21. </div>
  22. <!-- 广告部分-->
  23. <div class="ad">
  24. <div class="ad1">招生 </div>
  25. <div class="ad2">广告 </div>
  26. <div class="ad3">房地产广告 </div>
  27. <div class="ad4">
  28. <img alt="ad" src="../resource/images/ad2.png" />
  29. </div>
  30. </div>
  31. </body>
  32. </html>


 
 
  1. @ CHARSET "UTF-8";
  2. body {
  3. margin: 0px auto;
  4. padding: 0px;
  5. width: 1000px;
  6. border: 1px solid red;
  7. }
  8. /* 首部 */
  9. .head{
  10. margin: 0px;
  11. padding: 0px;
  12. border: 1px solid blue;
  13. width: 1000px;
  14. height: 25px;
  15. margin-bottom: 5px;
  16. }
  17. .login {
  18. width: 500px;
  19. margin-right: 5px;
  20. margin-left: 5px;
  21. }
  22. .setpage {
  23. width: 200px;
  24. margin-right: 5px;
  25. }
  26. .seta {
  27. width: 280px;
  28. }
  29. .login, .setpage, .seta {
  30. float: left;
  31. height: 25px;
  32. background-color: silver;
  33. }
  34. /* 导航条部分 */
  35. .nav {
  36. border: 1px solid red;
  37. width: 1000px;
  38. height: 67px;
  39. margin-bottom: 10px;
  40. }
  41. .nav img {
  42. width: 140px;
  43. margin-right: 5px;
  44. }
  45. .nav div {
  46. background-color: aqua;
  47. width: 855px;
  48. }
  49. .nav img, .nav div {
  50. float: left;
  51. height: 67px;
  52. }
  53. /* 广告部分 */
  54. .ad {
  55. margin-bottom: 10px;
  56. border: 1px solid red;
  57. width: 1000px;
  58. height: 186px;
  59. }
  60. .ad1 {
  61. background-color: orange;
  62. width: 140px;
  63. }
  64. .ad2 {
  65. background-color: aqua;
  66. width: 500px;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值