02-CSS基础

1. css简介

  • CSS是 Cascading Style Sheets 的首字母缩写,意思是层叠样式表,如果说HTML是网页的结构,那么CSS就是网页化妆师,用于准确的选中元素,并赋予样式

  • css注释

    • /*
          css注释 ctrl+shift+"/"
      */
      
  • css的三种写法

    1. 内联式:通过标签的style属性,在标签上直接写样式。

      <div style="font: red"; font-size: 15px;>
          我就是我不一样的烟火
      </div>	
      
    2. 嵌入式:通过style标签,在网页上创建嵌入的样式表。

      <head>
          <meta charset="UTF-8">
          <title>css样式写在哪里</title>
          <style>
              div{
                  width: 50px;
                  height: 50px;
                  background: skyblue;
                  /*display: inline;*/
                  display: inline-block;
          </style>
      </head>
      <body>
          <div>div1</div>
          <div>div2</div>
          <span>span1</span>
          <span>span2</span>
      </body>
      
    3. 外联式:通过link标签,链接到外部样式表到页面中。

      <link rel="stylesheet" type="text/css" href="css/main.css">
      
  • 三种写法的优先级,遵循就近原则

2. css选择器、

  • 语法

  • 选择器

2.1 基本选择器

  • id选择器 :给唯一的标签赋予样式,其它元素不允许应用此样式

    • #box{color:red} 
      
      <div id="box">....</div> 
      
  • class选择器:给拥有相同calss属性,不管是不是同类标签赋予同样的样式

    • .red{
      	color:red
          font-size:20px
          margin-top:10px
      } 
      
      <div class="red">....</div>
      <h1 class="red">....</h1>
      <p class="red">....</p>
      
  • 标签选择器:给同类标签赋予同样的样式

    • *{margin:0;padding:0}
      div{
      	color:red
      }   
      
      <div>....</div>  
      <div class="box">....</div>
      
  • 优先级: id>class>标签

2.2 组合选择器

  • 后代选择器:选中所有同类后代标签

    • <!--会选中所有div下的span标签赋予样式-->
      .box span{
      	font-size: 15px;
      	color: red;
      }
      
      <div class="box">
          <span>学习</span>
          <p>
              吃饭
              <span>
                  睡觉
              </span>
          </p>
      </div>
      
  • 子代选择器:选中下一级所有同类标签

    • <!--会选中所有div下一级的span标签赋予样式
      	只会选中学习‘这个’span
      -->
      .box>span{
      	font-size: 15px;
      	color: red;
      }
      
      <div class="box">
          <span>学习</span>
          <p>
              吃饭
              <span>
                  睡觉
              </span>
          </p>
      </div>
      
  • 兄弟选择器:选中往下的所有兄弟标签

    • <!--会选中所有class="box2"往下的同级p标签赋予样式-->
      .box2~p{
      	font-size: 15px;
      	color: red;
      }
      
      <div class="box">
          <span>学习</span>
          <div class="box2">目标标签</div>
          <p>二弟</p>
          <p>小妹</p>
      </div>
      
  • 相邻选择器:选中往下相邻的标签,必须是相邻的

    • <!--会选中所有class="box2"往下的同级第一个p标(二弟)签赋予样式-->
      .box2+p{
      	font-size: 15px;
      	color: red;
      }
      
      <div class="box">
          <span>学习</span>
          <div class="box2">目标标签</div>
          <p>二弟</p>
          <p>小妹</p>
      </div>
      

2.3 分组选择器

  • 多个选择器,同时给多个标签赋予样式

  • .box1,.box2,.box3{width:100px;height:100px}
    .box1{background:red}
    .box2{background:pink}
    .box2{background:gold}
    
    <div class="box1">....</div>
    <div class="box2">....</div>
    <div class="box3">....</div>
    

2.4 属性选择器

  • 通过属性选择,一般自定义的属性会用到

  • div[class="box1"]{
    	background: red;
    	color: green;
    }
    
    <div class="box1">....</div>
    <div class="box2">....</div>
    <div class="box3">...</div>
    

2.5 伪类选择器

  • 常用的伪类选择器有hover,表示鼠标悬浮在元素上时的状态

  • <!--当鼠标滑过的时候显示颜色为绿色-->
    .box{
    	width: 300px;
    	height: 200px;
    	background: #aa8899;
    }
    .box:hover{
    	background: green;
    }
    
    <div class="box">
        我就是我
    </div>
    

2.6 伪元素选择器

  • 伪元素选择器有before和after,它们可以通过样式在元素中插入内容。

  • .box2:before{content:'行首文字';}
    .box3:after{content:'行尾文字';}
    
    <div class="box2">....</div>
    <div class="box3">....</div>
    

3. 文本样式

  • color 设置文字的颜色,如: color:red;

  • font-family 设置文字的字体 如:font-family:'微软雅黑';

  • font-style 设置字体是否倾斜

    • font-style:normal'; 设置不倾斜,
    • font-style:'italic'; 设置文字倾斜
  • font-size 设置文字的大小,如:font-size:12px;

  • font-weight 设置文字是否加粗

    • font-weight:bold; 设置加粗
    • font-weight:normal; 设置不加粗
  • font 同时设置文字的几个属性,写的顺序有兼容问题,建议按照如下顺序写: font:是否加粗 字号/行高 字体;如: font:normal 12px/36px '微软雅黑';

  • line-height 设置文字的行高,设置行高相当于在每行文字的上下同时加间距, 如:line-height:24px;

  • text-align 设置文字水平对齐方式,

    • center 水平居中
    • left 靠左
    • right 靠右
  • text-indent 设置文字首行缩进,如:text-indent:24px; 设置文字首行缩进24px;

  • letter-spacing 设置字距 如letter-spacing: 1.5px;

  • word-spacing 设置词距 如word-spacing: 10px;

  • text-decoration 设置文字的下划线如

    • none; 将文字下划线去掉
    • underline 设置下划线
    • overline 设置上划线
    • line-through 删除线
  • white-space 换行 方式

    • normal 正常换行
    • nowrap 不换行
  • list-style 去掉列表前面的小圆点

4. css颜色表示法

  1. 颜色名表示,比如:red 红色,gold 金色

  2. rgb表示,比如:rgb(255,0,0)表示红色

  3. 16进制数值表示,比如:#ff0000 表示红色,这种可以简写成 #f00

5. 背景样式

  • background-position 背景定位
  • background-repeat 背景铺盖
  • background-color:背景颜色
  • background-image:url(图片路径);
  • background-repeat:no-repeat;图片不重复
  • background-repeat:repeat-x(向x轴延伸)
  • background-repeat:repeat-y(向y轴延伸)
  • background-position:图片偏移
posted @ 2019-05-09 10:26  s小毛驴  阅读(160)  评论(0编辑  收藏  举报
返回顶部