天天看点

学习CSS

初识

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
 <!--规范,<style> 可以编写css的代码,每一个声明,最好用分号结尾
     语法:
         选择器{
             声明1;
             声明2;
             声明3;
         }
 -->
     <style>
         h1{
             color: aqua;
         }
     </style>
 </head>
 <body>
 <h1>我是标题</h1>
 </body>
 </html>      

建议规范

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
 <!--规范,<style> 可以编写css的代码,每一个声明,最好用分号结尾
     语法:
         选择器{
             声明1;
             声明2;
             声明3;
         }
 -->
     <link rel="stylesheet" href="css/style.css">
 </head>
 <body>
 <h1>我是标题</h1>
 </body>
 </html>      
 h1{
     color: aqua;
 }      

css的导入方式

 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
 <!--    内部样式-->
     <style>
         h1{
             color: green;
         }
     </style>
 ​
     <link rel="stylesheet" href="css/style.css">
 ​
 </head>
 <body>
 ​
 <!--优先级:就近原则-->
 ​
 <!--行内样式:在标签元素中,编写一个style属性,编写样式即可-->
 <h1 style="color: red;">我是标题</h1>
 </body>
 </html>      
 /*外部样式*/
 h1{
     color: yellow;
 }      
外部样式的两种方式
  • 链接式
 <link rel="stylesheet" href="css/style.css">      
  • 导入式
 <style>
     @import url("css/style.css");        
 </style>      

选择器

选择页面的某一个或者某一类元素

基本选择器
  • 标签选择器
  • 类选择器 class
  • Id选择器
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
   <style>
     #style1 {
       /*id选择器 : id必须保证全局唯一!
       #id名称{}
       不遵循就近原则,固定的
       id选择器>class选择器>标签选择器
       */
       color: #f60707;
     }
     .class_style{
       /* 类选择器的样式: .class的名称{}
       好处:可以多个标签归类,是同一个class*/
       color: #11e311;
     }
     h1{
       /*标签选择器:会选择到页面上所有这个标签的元素*/
       color: #1919e7;
     }
     p{
       color: #f50707;
     }
   </style>
 </head>
 <body>
 <h1 class="class_style" id="style1">标题1</h1>
 <h1 class="class_style">标题2</h1>
 <h1 class="class_style">标题3</h1>
 <h1>标题4</h1>
 <h1>标题5</h1>
 <p>你好啊,我的朋友</p>
 </body>
 </html>      
层次选择器
  • 后代选择器
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
   <style>
     /*后代选择器:在某个元素后代*/
     body p{
       background: #c8e719;
     }
     /*子选择器,一代*/
     body>p{
       background: #f50707;
     }
     /*相邻兄弟选择器 只有一个(向下)*/
     .active + p{
       background: #1919e7;
     }
     /*通用兄弟选择器,当前选择元素的向下的所有兄弟元素*/
     .inactive ~ p{
       background: aqua;
     }
   </style>
 </head>
 <body>
 <p>p0</p>
 <p class="active">p1</p>
 <p>p2</p>
 <p>p3</p>
 <p class="inactive">p4</p>
 <p>p5</p>
 <p>p6</p>
 <ul>
   <li>
     <p>p4</p>
   </li>
   <li>
     <p>p5</p>
   </li>
   <li>
     <p>p6</p>
   </li>
 </ul>
 </body>
 </html>      
结构选择器
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
     <style>
  /*选择子类的第一个元素*/
     ul li:first-child{
         background: #f50707;
     }
  /*选择子类的第一个元素*/
     ul li:last-child{
         background: #11e311;
     }
  /*选中p1:定位到父元素,选择当前的第一个元素
  选中当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效*/
     p:nth-child(3){
         background: #1919e7;
     }
 ​
      p:nth-child(4){
          background: #e719c5;
      }
     /* 选中父元素下的p元素的第二个类型,不必须是当前元素*/
     p:nth-of-type(2){
         background: #c8e719;
     }
 ​
     a:hover{
         background: aqua;
     }
 ​
     </style>
 ​
 </head>
 <body>
     <a href="">2131213141</a>
     <h1>好</h1>
     <p>p1</p>
     <p>p2</p>
     <p>p3</p>
     <ul>
         <li>li1</li>
         <li>li2</li>
         <li>li3</li>
     </ul>
 </body>
 </html>      
属性选择器
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Title</title>
   <style>
     .demo a{
       float: left;
       display: block;
       height: 50px;
       width: 50px;
       border-radius: 10px;
       background: #19e723;
       text-align: center;
       color: #d21a1a;
       text-decoration: none;
       margin-right: 8px;
       font:bold 10px/50px Arial;
     }
   /*  属性名 , 属性名 = 属性值
   = 绝对等于
   *= 包含
   ^= 以什么开头
   $= 以什么结尾
   穿在id属性的元素   a[]{}
   */
     a[id]{
       background: #c8e719;
     }
     a[id=first]{
       color: #131212;
     }
     a[class*=links]{
       text-align: end;
     }
     a[href*=abc]{
       background: darkgrey;
     }
     a[href^=images]{
       background: #1919e7;
     }
     a[href$=doc]{
       background: aqua;
     }
   </style>
 </head>
 <body>
 <p class="demo">
 ​
   <a href="https://www.baidu.com" class="links item first" id="first">1</a>
   <a href="https://www.study.com" class="links item active">2</a>
   <a href="images/123.png" class="links item">3</a>
   <a href="images/123.gif" class="links item">4</a>
   <a href="images/123.jpg" class="links item">5</a>
   <a href="abc" class="links item">6</a>
   <a href="/abc.pdf" class="links item">7</a>
   <a href="/dnf.doc" class="links item">8</a>
   <a href="lol.doc"