慧9建 发表于 2018-8-9 10:32:20

python_day13_CSS

  1 、CSS导入
  1.1、行内式
  1.2、㠌入式
  1.3、导入式
  1.4、链接式
  2、CSS选择器
  2.1.1、*所有标签
  2.1.2、标签
  2.1.3、id号设置
  2.1.4、class选择器
  2.1.5、组合选择器
  3、组合选择器
  3.1.1、多元素选择
  3.1.2、后代元素选择器
  3.1.3 子代元素选择器
  3.1.4 毗(pi)邻元素选择器
  4、属性选择器
  4.1.1、匹配具有某个属性的元素
  4.1.2、匹配所有属性等于val的元素
  4.1.3、匹配其中一个等于val的值
  4.1.4、锚定val前一个元素
  5、伪类
  6、css优先级和继承
  7、外边距和内边
  8、flost属性
  CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离。
  css操作方式:
  1、怎么找到标签(如何操作html标签)
  2、如何操作标签对象 (element)
  1、CSS导入
  1.1、行内式
<body>  
    <p style=&quot;color: black;background-color: hotpink&quot;>hello world</p>
  
</body>

  1.2、㠌入式
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>css</title>
  
    <style>
  
      p{
  
            color: black;
  
            background-color: hotpink;
  
            width: 50%;
  
            height: 30px;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <p>hello world</p>
  
</body>
  
</html>

  1.3、 导入式
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>css</title>
  
    <style>
  
      @import &quot;test1.css&quot;;
  
    </style>
  
</head>
  
<body>
  
    <p>hello world</p>
  
</body>
  
</html>

  css类风格, 导入有限,并且页面在打开时,如果有延时它会先执行html内容,然后再去执行css文件, 不建议使用
  1.4、链接式
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>css</title>
  
    <link href=&quot;test1.css&quot; rel=&quot;stylesheet&quot;>
  
</head>
  
<body>
  
    <p>hello world</p>
  
</body>
  
</html>

  注意:导入式会在整个网页装载完后再装载CSS文件,因此这就导致了一个问题,如果网页比较大则会儿出现先显示无样式的页面,闪烁一下之后,再出现网页的样式。这是导入式固有的一个缺陷。使用链接式时与导入式不同的是它会以网页文件主体装载前装载CSS文件,因此显示出来的网页从一开始就是带样式的效果的,它不会象导入式那样先显示无样式的网页,然后再显示有样式的网页,这是链接式的优点。
  2、css选择器
  2.1.1、*所有的body内容都会被设置
<head>  
    <style>
  
      *{ background-color: yellowgreen; }
  
    </style>
  
</head>
  
<body>
  
    <p>hello 1 </p>
  
    <div>heelo 2</div>
  
</body>

  2.1.2、标签: 只设置自定义的标签
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>css_chiose</title>
  
    <style>
  
      p{
  
            background-color: darkblue;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <p>hello 1 </p>
  
    <div>heelo 2</div>
  
</body>

  2.1.3、id号设置
<head>  
    <style>
  
      #strong{
  
            background-color: hotpink;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <b id=&quot;strong&quot;>hello b </b>
  
</body>

  2.1.4、class选择器
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>css_chiose</title>
  
    <style>
  
      .twob{      # 或者b.twob
  
            background-color: aqua;
  
      }
  

  
    </style>
  
</head>
  
<body>
  
    <b class=&quot;twob&quot;>hello b2 </b>
  
</body>

  3、组合选择器
  3.1.1、多元素选择
<head>  
    <style>
  
      .twob,#twoc{
  
            background-color: darkblue;
  
      }
  

  
    </style>
  
</head>
  
<body>
  
    <b class=&quot;twob&quot;>hello b2 </b><hr>
  
    <b id=&quot;twoc&quot;>hello b3</b>
  
</body>

  3.1.2、后代元素选择器
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>css_chiose</title>
  
    <style>
  
      .divs p {      # 后代元素,只要元素中包含了,那么就会控制网页的改变
  
            color: yellow;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div class=&quot;divs&quot;>
  
      <a href=&quot;&quot;>di yi</a>
  
      <div>
  
            <p>two</p>
  
            <div>three</div>
  
      </div>
  
    </div>
  
</body>

  3.1.3 子代元素选择器
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>css_chiose</title>
  
    <style>
  
      .divs > p {
  
            color: brown;
  
            background-color: yellow;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div class=&quot;divs&quot;>
  
      <a href=&quot;&quot;>di yi</a>
  
      <div>
  
            <p>two</p>
  
            <div>three</div>
  
      </div>
  
      <p>ok</p>
  
    </div>
  
</body>

  3.1.4 毗(pi)邻元素选择器
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>css_chiose</title>
  
    <style>
  
      .divs + div {
  
            color: brown;
  
            background-color: yellow;
  
      }
  

  
    </style>
  
</head>
  
<body>
  
    <div>11111111111111</div>
  
    <div class=&quot;divs&quot;>
  
      <a href=&quot;&quot;>di yi</a>
  
      <div>
  
            <p>two</p>
  
            <div>three</div>
  
      </div>
  
      <p>ok</p>
  
    </div>
  

  
    <div>22222222222222</div>
  
</body>

  注意: 如果定义的标签与类选择或id选择不毗邻(中间还有其它标签) 那么它就不会生效。
  嵌套规则:

[*]  块级元素可以包含内联元素或某些块级元素,但内联元素不能包含块级元素,它只能包含其它内联元素。
[*]  有几个特殊的块级元素只能包含内联元素,不能包含块级元素。如h1,h2,h3,h4,h5,h6,p,dt
[*]  li内可以包含div
[*]  块级元素与块级元素并列、内联元素与内联元素并列。
  4、属性选择器
  4.1.1、匹配具有某个属性的元素
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      {
  
            color: brown;
  
      }
  
    </style>
  
</head>
  
<body>
  
      # 自定义属性名称
  
    <div xiong=&quot;xx&quot;>hello div</div>
  
</body>

  4.1.2、匹配所有属性等于val的元素
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      # 匹配属性等于val的元素
  
      {
  
            color: brown;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div xiong=&quot;xx&quot;>hello div</div>
  
    <div xiong=&quot;yy&quot;>hello div2</div>
  
</body>

  4.1.3、匹配其中一个等于val的值
<html lang=&quot;en&quot;>  
<head>
  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      {
  
            color: brown;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div xiong=&quot;xx&quot;>hello div</div>
  
    <div xiong=&quot;yy&quot;>hello div2</div>
  
    <div xiong=&quot;xyz&quot;>hello div3</div>
  
</body>
# 跟等于号 (=) 类似
  4.1.4、锚定val前一个元素
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      # 锚定前一个值为x的元素
  
      {
  
            color: brown;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div xiong=&quot;xx&quot;>hello div</div>
  
    <div xiong=&quot;yy&quot;>hello div2</div>
  
    <div xiong=&quot;xyz&quot;>hello div3</div>
  
</body>

  4.1.4、锚定val最后一个元素
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      # 锚定最后一个值为z的元素
  
      {
  
            color: brown;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div xiong=&quot;xx&quot;>hello div</div>
  
    <div xiong=&quot;yy&quot;>hello div2</div>
  
    <div xiong=&quot;xyz&quot;>hello div3</div>
  
</body>

  5、伪类
a:link(没有接触过的链接),用于定义了链接的常规状态。  

  
a:hover(鼠标放在链接上的状态),用于产生视觉效果。
  

  
a:visited(访问过的链接),用于阅读文章,能清楚的判断已经访问过的链接。
  

  
a:active(在链接上按下鼠标时的状态),用于表现鼠标按下时的链接状态。
  

  
伪类选择器 : 伪类指的是标签的不同状态:
  

  
         a ==> 点过状态 没有点过的状态 鼠标悬浮状态 激活状态
  

  
a:link {color: #FF0000} /* 未访问的链接 */
  

  
a:visited {color: #00FF00} /* 已访问的链接 */
  

  
a:hover {color: #FF00FF} /* 鼠标移动到链接上 */
  

  
a:active {color: #0000FF} /* 选定的链接 */ 格式: 标签:伪类名称{ css代码; }
  6、css优先级和继承
  所谓CSS优先级,即是指CSS样式在浏览器中被解析的先后顺序。
  样式表中的特殊性描述了不同规则的相对权重,它的基本规则是:
  1 内联样式表的权值最高       style=&quot;&quot; -------------1000
  2 统计选择符中的ID属性个数。    #id      ------------- 100
  3 统计选择符中的CLASS属性个数。 .class   ------------- 10
  4 统计选择符中的HTML标签名个数。 p       ------------- 1
  CSS的继承性:
  继承是CSS的一个主要特征,它是依赖于祖先-后代的关系的。继承是一种机制,它允许样式不仅可以应用于某个特定的元素,还可以应用于它的后代。例如一个BODY定义了的颜色值也会应用到段落的文本中。
  任何显示申明的规则都可以覆盖其继承样式,此外,继承是CSS重要的一部分,我们甚至不用去考虑它为什么能够这样,但CSS继承也是有限制的。有一些属性不能被继承,如:border, margin, padding, background等。
  1、文内的样式优先级为1,0,0,0,所以始终高于外部定义。这里文内样式指形如<div style=&quot;color:red>blah</div>的样式,而外部定义指经由<link>或<style>卷标定义的规则。
  2、有!important声明的规则高于一切。
  3、如果!important声明冲突,则比较优先权。
  4、如果优先权一样,则按照在源码中出现的顺序决定,后来者居上。
  5、由继承而得到的样式没有specificity的计算,它低于一切其它规则(比如全局选择符*定义的规则)。
  1、背景颜色
background-color: cornflowerblue  
background-image: url('1.jpg');
  
background-repeat: no-repeat;# no-repeat只留一张,默认使用(repeat:平铺满)
  
background-position: right top(20px 20px);(横向:left center right)(纵向:top center bottom)
    简写:<body style=&quot;background: 20px 20px no-repeat #ff4 url('1.jpg')&quot;>  
            <div style=&quot;width: 300px;height: 300px;background: 20px 20px no-repeat #ff4 url('1.jpg')&quot;>
  注意:如果将背景属性加在body上,要记得给body加上一个height,否则结果异常,这是因为body为空,无法撑起背景图片;另外,如果此时要设置一个width=100px,你也看不出效果,除非你设置出html。
  2、文本属性
  font-size: 10px;
  text-align: center;   横向排列
  line-height: 200px;   文本行高 通俗的讲,文字高度加上文字上下的空白区域的高度 50%:基于字体大小的百分比
  vertical-align:-4px设置元素内容的垂直对齐方式 ,只对行内元素有效,对块级元素无效
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      .text{
  
            width: 100px;
  
            height: 100px;
  
            background: yellow;
  
            text-align: center;
  
            line-height: 100px;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div class=&quot;text&quot;>文本属性</div>
  
</body>

  # 注意: 如果填写的文本长度大于weight长度,那么多出的就会在第二行显示

  text-indent: 200px;   首行缩进   效果图

  word-spacing: 20px;   字符之间的空隙
<head>  
    <style>
  
      .text{
  
            # 两个字符之间的空隙
  
            word-spacing: 10px;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div class=&quot;text&quot;>text text</div>
  
</body>

text-transform: capitalize;    首字符大写<div id=&quot;ssss&quot;>sssssssaaaa</div>
  3、边框属性
border-style: solid;  

  
border-color: chartreuse;
  

  
border-width: 20px;
  

  
简写:border: 30px rebeccapurple solid;
<head>  
    <style>
  
      #textid{
  
            width: 300px;
  
            height: 100px;
  
            /*text-indent: 200px;*/
  
            border: 1px solid blue;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div id=&quot;textid&quot;>222222222223333333333aaaaaaa</div>
  
</body>

  4、列表属性
  ul,ol{   list-style: decimal-leading-zero;
  list-style: none; <br>         list-style: circle;
  list-style: upper-alpha;
  list-style: disc; }
  5、dispaly属性
  none
  block
  inline
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      .div1,#dddd,.ddbb{
  
            width: 100px;
  
            height: 100px;
  
      }
  
      .div1{
  
            background: yellow 20px bottom;
  
      }
  
      #dddd{
  
            display: inline-block;
  
            background: blue 20px;
  
            line-height: 100px;
  
            vertical-align: middle;
  
      }
  
      .ddbb{
  
            display: inline-block;
  
            background: brown 20px;
  
            # 让文本在颜色框最中间
  
            line-height: 100px;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div class=&quot;div1&quot;>div1</div>
  
    <span id=&quot;dddd&quot;><a href=&quot;&quot;>ddddd</a></span>
  
   <b class=&quot;ddbb&quot;>asdf</b>
  
</body>

  # 解决上图,两个图之间的空隙
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      .div1,#dddd,.ddbb{
  
            width: 100px;
  
            height: 100px;
  
      }
  
      .div1{
  
            background: yellow 20px bottom;
  
      }
  
      #dddd{
  
            display: inline-block;
  
            background: blue;
  
      }
  
      .ddbb{
  
            display: inline-block;
  
            background: brown;
  
      }
  
      #outher{
  
            # 边框的粗细, 并且让它立即生效
  
            /*border: 1px solid;*/
  
            word-spacing: -8px;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div class=&quot;div1&quot;>div1</div>
  
    <div id=&quot;outher&quot;>
  
      <span id=&quot;dddd&quot;><a href=&quot;&quot;>sddd</a></span>
  
      <b class=&quot;ddbb&quot;>asdf</b>
  
    </div>
  
</body>

  7、外边距和内边
  margin:       用于控制元素与元素之间的距离;margin的最基本用途就是控制元素周围空间的间隔,从视觉角度上达到相互隔开的目的。
  padding:      用于控制内容与边框之间的距离;
  Border(边框)围绕在内边距和内容外的边框。
  Content(内容) 盒子的内容,显示文本和图像。
  重要: 当您指定一个CSS元素的宽度和高度属性时,你只是设置内容区域的宽度和高度。要知道,完全大小的元素,你还必须添加填充,边框和边距。.
margin:10px 5px 15px 20px;-----------上 右 下 左  
margin:10px 5px 15px;----------------上 右左 下
  
margin:10px 5px;---------------------上下右左
  
margin:10px;    ---------------------上右下左
与外边框相隔20px
  外边距的重叠只产生在普通流文档的上下外边距之间,这个看起来有点奇怪的规则,其实有其现实意义。设想,当我们上下排列一系列规则的块级元素(如段   落P)时,那么块元素之间因为外边距重叠的存在,段落之间就不会产生双倍的距离。又比如停车场
  1兄弟div: 上面div的margin-bottom和下面div的margin-top会塌陷,也就是会取上下两者margin里最大值作为显示值
  2父子div: 父级div中没有 border,padding,inline content,子级div的margin会一直向上找,直到找到某个标签包括border,padding,inline content            中的其中一个,然后按此div 进行margin ;
  # 出现塌陷解决办法, 配置父级div中增加border,padding,inline content 标签
  # 配置一个类似相册的图片
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>边框</title>
  
    <!--<link href=&quot;test1.css&quot; rel=&quot;stylesheet&quot;>-->
  
    <style>
  
      @import &quot;test1.css&quot;;
  
    </style>
  
</head>
  
<body>
  
    <span id=&quot;outpage&quot;>
  
      <div class=&quot;div1&quot;>hello div1</div>
  
      <div class=&quot;div2&quot;>hello div2</div>
  
      <div class=&quot;div3&quot;>hello div3</div>
  
      <div class=&quot;div4&quot;>hello div4</div>
  
    </span>
  
</body>
  test1.css
#outpage{  
    display: inline-block;
  
    border: 3px solid blue;
  
    width: 350px;
  
    height: 250px;
  
    background: aliceblue no-repeat;
  
}
  
.div1,.div2,.div3,.div4{
  
    display: inline-block;
  
    width: 80px;
  
    height: 40px;
  
    border: 3px solid rebeccapurple ;
  
}
  
.div1,.div2,.div3,.div4{
  
    /*内联标签*/
  
    padding: 20px;
  
    /*外联标签*/
  
    margin: 15px;
  
    /*颜色*/
  
    background: crimson;
  
}
  效果图

  8、flost属性
  文字来源于: http://www.cnblogs.com/yuanchenqi/articles/5977825.html
  block元素通常被现实为独立的一块,独占一行,多个block元素会各自新起一行,默认block元素宽度自动填满其父元素宽度。block元素可以设置width、height、margin、padding属性;
  inline元素不会独占一行,多个相邻的行内元素会排列在同一行里,直到一行排列不下,才会新换一行,其宽度随元素的内容而变化。inline元素设置width、height属性无效。inline元素的margin和padding属性。水平方向的padding-left, padding-right, margin-left, margin-right都产生边距效果;但竖直方向的padding-top, padding-bottom, margin-top, margin-bottom不会产生边距效果。
  常见的块级元素有 div、form、table、p、pre、h1~h5、dl、ol、ul 等。
  常见的内联元素有span、a、strong、em、label、input、select、textarea、img、br等
  所谓的文档流,指的是元素排版布局过程中,元素会自动从左往右,从上往下的流式排列。
  脱离文档流,也就是将元素从普通的布局排版中拿走,其他盒子在定位的时候,会当做脱离文档流的元素不存在而进行定位。
  只有绝对定位absolute和浮动float才会脱离文档流。
  ---部分无视和完全无视的区别?需要注意的是,使用float脱离文档流时,其他盒子会无视这个元素,但其他盒子内的文本依然会为这个元素让出位置,环绕在周围(可以说是部分无视)。而对于使用absolute position脱离文档流的元素,其他盒子与其他盒子内的文本都会无视它。(可以说是完全无视)
  浮动的表现
  定义:浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止。由于浮动框不在文档的普通流中,所以文档的普通流中的浮动框之后的块框表现得就像浮动框不存在一样。(注意这里是块框而不是内联元素;浮动框只对它后面的元素造成影响)
  注意 当初float被设计的时候就是用来完成文本环绕的效果,所以文本不会被挡住,这是float的特性,即float是一种不彻底的脱离文档流方式。无论多么复杂的布局,其基本出发点均是:“如何在一行显示多个div元素”。
  清除浮动:
  在非IE浏览器(如Firefox)下,当容器的高度为auto,且容器的内容中有浮动(float为left或right)的元素,在这种情况下,容器的高度不能自动伸长以适应内容的高度,使得内容溢出到容器外面而影响(甚至破坏)布局的现象。这个现象叫浮动溢出,为了防止这个现象的出现而进行的CSS处理,就叫CSS清除浮动。
  复制代码
  clear语法:
  clear : none | left | right | both
  取值:
  none : 默认值。允许两边都可以有浮动对象
  left : 不允许左边有浮动对象
  right : 不允许右边有浮动对象
  both : 不允许有浮动对象
  但是需要注意的是:clear属性只会对自身起作用,而不会影响其他元素。如果一个元素的右侧有一浮动对象,而这个元素设置了不允许右边有浮动对象,即clear:right,则这个元素会自动下移一格,达到本元素右边没有浮动对象的目的。
<head>  
    <meta charset=&quot;UTF-8&quot;>
  
    <title>Title</title>
  
    <style>
  
      body{
  
            padding: 0px;
  
            margin: 0px;
  
      }
  
      .div1,.div2,.div3,.div4{
  
            border: 3px solid;
  
      }
  
      .div1{
  
            width: 100px;
  
            height: 100px;
  
            background: blue;
  
            float: left;
  

  
      }
  
      .div2{
  
            width: 200px;
  
            height: 100px;
  
            background: darkgreen;
  
            float: left;
  

  
      }
  
      .div4{
  
            width: 200px;
  
            height: 200px;
  
            background: mediumslateblue;
  
            float: left;
  
      }
  

  
      .div3{
  
            width: 100px;
  
            height: 200px;
  
            background: black;
  
            float: left;
  
      }
  
    </style>
  
</head>
  
<body>
  
    <div class=&quot;div1&quot;>div1</div>
  
    <div class=&quot;div2&quot;>div2</div>
  
    # 清除左右两边的符点属性,效果如下图
  
    <div class=&quot;div4&quot; style=&quot;clear: both&quot;>div4</div>
  
    <div class=&quot;div3&quot;>div3</div>
  
</body>

    <div class=&quot;div4&quot; div4</div>不清空左右的浮点那么效果如下

  快写: div.div*4再安一下tab会出来四个<div>
页: [1]
查看完整版本: python_day13_CSS