CSS入门

一,初识

内部引用和外部引用

<head>
    <meta charset="UTF-8">
    <title>标题</title>
<!--  规范,<style> 可以编写css代码 每一个语句最好以分号结尾
  语法:
    选择器{
      声明1;
      声明2;
      声明3;
    }
-->
<!--  <style>-->
<!--    h1{-->
<!--      color: red;-->
<!--    }-->
<!--  </style>-->

<!--  外部引用-->
  <link rel="stylesheet" href="style.css">
</head>
二,导入方式
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--  内部样式-->

  <link rel="stylesheet" href="style.css">
  <style>
  h1{
    color: green;
  }
</style>
</head>
<body>

<!--优先级,就近原则-->

<!--行内样式:在标签元素中,编写一个style属性,编写样式即可-->
<h1 style="color: red">我是标题</h1>
</body>

<!-- ------------------------------------ -->
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--  导入式 CSS2.1-->
  <style>
    @import url("style.css");
  </style>
</head>
三,基本选择器

1.标签选择器

<style>
    /*
    标签选择器会选择页面上所有的这个标签的元素
     */
    h1{
        color: #1f8b8b;
        background: #230b0b;
        border-radius: 14px;
    }
    p{
        font-size: 30px;
    }
</style>

2.类选择器

<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    /*
    类选择器的格式 .class的名称{}
    好处:可以多个标签归类,是同一个class 可以复用
     */
    .java{
      color: #1f8b8b;
    }
    .py{
      color: #230b0b;
    }
    .c{
      color: aliceblue;
    }
  </style>
</head>
<body>
<h1 class="java">one</h1>
<h1 class="py">two</h1>
<h1 class="c">three</h1>

<p class="c">four</p>
</body>

3.id选择器

<head>
    <meta charset="UTF-8">
    <title>Title</title>

  <style>
    /*
    id选择器   : id 必须保证全局唯一
    #id名称{}
    不遵循就近原则
    id选择器 > class选择器 >  标签选择器
     */
    #java{
      color: antiquewhite;
    }
    .java{
      color: green;
    }
    .py{
      color: aqua;
    }
  </style>
</head>
<body>

<h1 id="java" class="java">one</h1>
<h1 id="py">two</h1>
<h1 class="java">three</h1>
<h1 class="java">four</h1>
<h1>five</h1>
</body>
四,层次选择器
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    /*p{
      background: #1f8b8b;
    }标签选择器*/
      
    /*后代选择器 body中的p标签*/
    body p{
      background: green;
    }
    /*子选择器 */
    body>p{
      background: antiquewhite;
    }
    /*相邻兄弟选择器 : 只有一个,相邻(向下)*/
    .active + p{
      background: aqua;
    }
    /*通用兄弟选择器 : 当前选中元素的向下的所有兄弟选择器*/
    .active~p{
      background: aqua;
    }
  </style>
</head>
<body>
<p>p0</p>
<p class="active">p1</p>
<p>p2</p>
<p>p3</p>
<ul>
  <li>
    <p>p4</p>
  </li>
  <li>
    <p>p5</p>
  </li>
  <li>
    <p>p6</p>
  </li>
</ul>
<p class="active">p7</p>
<p>p8</p>
</body>
五,结构伪类选择器
<head>
    <meta charset="UTF-8">
    <title>Title</title>

<!--  避免使用.class .id选择器-->
  <style>
  /* ul的第一个元素*/
  ul li:first-child{
    background: #1f8b8b;
    color: #1f8b8b;
  }
  /* ul的最后个元素*/
  ul li:last-child{
    background: aqua;
    color: aqua;
  }
  /* 选中 p1 : 定位到父元素,选择当前的第一个元素
  选择当前p元素的父级元素,选中父级元素的第一个,并且是当前元素才生效*/
  p:nth-child(1){
    background: #230b0b;
  }
  /*选中父元素,下的p元素的第二个,类型*/
  p:nth-of-type(2){
    background: #1a4e82;
  }
/*伪类选择器*/
  a:hover{
    background: red;
  }
  </style>
</head>
<body>
<!--  <a href="">1235</a>-->
<!--  <h1>h1</h1>-->
  <p>p1</p>
  <p>p2</p>
  <p>p3</p>
  <ul>
    <li>li1</li>
    <li>li2</li>
    <li>li3</li>
  </ul>
</body>
六,属性选择器
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    .demo a{
      float: left;
      display: block;
      height: 50px;
      width: 50px;
      border-radius: 10px;
      background: #1f8b8b;
      text-align: center;
      color: gainsboro;
      text-decoration: none;
      margin-right: 5px;
      font: bold 20px/50px Arial;
    }

  /*  属性名 属性名 = 属性值(正则)
  = 绝对匹配
  *= 包含匹配
  ^= 以xx开头
  $= 以xx结尾
  */

  /*存在id属性的元素 选中 a[]{}*/
  /*  a[id]{*/
  /*    background: antiquewhite;*/
  /*  }*/

    /*id=first的元素*/
    a[id=first]{
      background: aqua;
    }

    /*class中有link的元素 正则匹配*/
    a[class*="link"]{
      background: #c29303;
    }

    /*选中herf中http开头的*/
    a[href^=http]{
      background: #1a4e82;
    }

    /*选中herf中html结尾的*/
    a[href$=html]{
      background: #590fc1;
    }

  </style>
</head>
<body>
<p class="demo">
  <a href="https://www.baidu.com" class="link item first" id="first">1</a>
  <a href="" class="link item active" target="_blank" title="test">2</a>
  <a href="images/123.png" class="link item ">3</a>
  <a href="images/123.html" class="link item ">4</a>
  <a href="images/123.jpg" class="link item ">5</a>
  <a href="httpabc">6</a>
  <a href=/a.pdf"">7</a>
  <a href="/abc.pdf">8</a>
  <a href="abc.doc">9</a>
  <a href="abcd.doc" class="link item last">10</a>
</p>
</body>
七,span标签
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    #title1{
      font-size: 30px;
    }
  </style>
</head>
<body>
<!--span    标识突出字体-->
欢迎学习 <span id="title1">Java</span>
</body>
八,字体样式
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--
font-family:字体
font-size:字体大小
font-weight:字体风格

-->
  <style>
    body{
      font-family: "Arial Black" , 楷体;
      color: #1f8b8b;
    }
    h1{
      font-size: 20px;
    }
    .p1{
      font-weight: bolder;
    }
  </style>
</head>
<body>
<h1>完美世界股份有限公司</h1>
<p class="p1">
  简称“完美世界”,业务涵盖完美世界影视、完美世界游戏和完美世界电竞三大板块,是我国最大的影游综合体 [1-2]  。
</p>
<p>
  2004年,完美世界游戏正式创立,其推出的《完美世界》 [3] 以自主研发的3D引擎和来自《山海经》的中国传统经典内容大获成功。
</p>
<p>
  2007年7月,完美世界游戏成功于美国纳斯达克上市 [4]  。
</p>
<p>
  2008年,完美世界影视成立 [5]  ,并于2014年12月成功登陆A股市场,公司名称为“完美环球娱乐股份有限公司”(股票代码:002624) [6]  。
</p>
<p>
  2015年7月,完美世界游戏完成美股私有化
</p>
<p>
  I don't know the passion until i met you , I don't know the sorrow until i left you ,But now I do know the love when I cherish you
</p>
</body>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
      /*
      [ [ <'font-style'> || <font-variant-css21> || <'font-		weight'> || <'font-stretch'> ]? <'font-size'> [ / 		<'line-height'> ]? <'font-family'> ] | caption | icon | 	menu | message-box | small-caption | status-bar
  	<font-variant-css21> = [ normal | small-caps ]
      */
    p{
      font: oblique bolder 12px 楷体;
    }
  </style>
</head>
<body>
<p>
  2004年,完美世界游戏正式创立,其推出的《完美世界》 [3] 以自主研发的3D引擎和来自《山海经》的中国传统经典内容大获成功。
</p>
</body>
九,文本样式
<!--
颜色:
  单词 red
  RGB 0~F
  RGBA A 0-1

  text-align: 居中
  text-indent: 段落首行缩进
  行高和高度大小相同就可以使块的文字居中
-->
<style>
  h1{
    color: rgba(1,255,255,0.9);
    text-align: center;
  }
  .p1{
    text-indent: 2em;
  }
  .p3{
    background: #c29303;
    height: 200px;
    line-height: 200px;
  /*  高度和行高 */
  }
  /*下划线*/
  .l1{
    text-decoration: underline;
  }
  /*中划线*/
  .l2{
    text-decoration: line-through;
  }
  /*上划线*/
  .l3{
    text-decoration: overline;
  }
   /*水平对齐~ 参照物,a,b*/
  img,span{
    vertical-align: middle;
  }
</style>



<body>
<h1>完美世界股份有限公司</h1>

<p class="l1">1234</p>
<p class="l2">1234</p>
<p class="l3">1234</p>

<p>
  <img src="images/1.png" alt="">
  <span>asdadfag</span>
</p>
<p class="p1">
  简称“完美世界”,业务涵盖完美世界影视、完美世界游戏和完美世界电竞三大板块,是我国最大的影游综合体 [1-2]  。
</p>
<p>
  2004年,完美世界游戏正式创立,其推出的《完美世界》 [3] 以自主研发的3D引擎和来自《山海经》的中国传统经典内容大获成功。
</p>
<p class="p3">
  2007年7月,完美世界游戏成功于美国纳斯达克上市 [4]  。
</p>
<p>
  2008年,完美世界影视成立 [5]  ,并于2014年12月成功登陆A股市场,公司名称为“完美环球娱乐股份有限公司”(股票代码:002624) [6]  。
</p>
<p>
  2015年7月,完美世界游戏完成美股私有化
</p>
<p>
  I don't know the passion until i met you , I don't know the sorrow until i left you ,But now I do know the love when I cherish you
</p>
</body>
十,超链接伪类
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    /*默认颜色*/
    a{
      text-decoration: none;
      color: #1a4e82;
    }
    /*鼠标悬浮*/
    a:hover{
      color: #c29303;
      font-size: 50px;
    }
    /*鼠标按住未释放的状态*/
    a:active{
      color: #0877e5;
    }
    /*a:visited{
      color: antiquewhite;
    }*/
    /*阴影颜色 水平偏移 垂直偏移 阴影半径*/
    #sout{
      text-shadow: red 10px 10px 10px;
    }
  </style>
</head>
<body>
<a href="#">
  <img src="1.png" alt="">
</a>
<p>
  <a href="#">小黑猫哦</a>
</p>
<p>
  <a href="">姓名</a>
</p>
<p id="sout">
  好看
</p>
</body>
十一,列表
<head>
    <meta charset="UTF-8">
    <title>列表样式</title>
  <link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div id="nav">
  <h2 class="title">全部商品分类</h2>
  <ul>
    <li><a href="#">图书</a>&nbsp;&nbsp;<a href="#">音像</a>&nbsp;&nbsp;<a href="#">数字商品</a></li>
    <li><a href="#">家用电器</a>&nbsp;&nbsp;<a href="#">手机</a>&nbsp;&nbsp;<a href="#">数码</a></li>
    <li><a href="#">电脑</a>&nbsp;&nbsp;<a href="#">办公</a></li>
    <li><a href="#">家居</a>&nbsp;&nbsp;<a href="#">家装</a>&nbsp;&nbsp;<a href="#">厨具</a></li>
    <li><a href="#">服饰鞋帽</a>&nbsp;&nbsp;<a href="#">个护化妆</a></li>
    <li><a href="#">礼品箱包</a>&nbsp;&nbsp;<a href="#">钟表</a>&nbsp;&nbsp;<a href="#">珠宝</a></li>
    <li><a href="#">食品饮料</a>&nbsp;&nbsp;<a href="#">保健食品</a></li>
    <li><a href="#">彩票</a>&nbsp;&nbsp;<a href="#">旅行</a>&nbsp;&nbsp;<a href="#">充值</a>&nbsp;&nbsp;<a href="#">票务</a></li>
  </ul>
</div>
</body>

style.css

#nav{
    width: 300px;
    background: #b4b7bb;
}
.title{
    font-size: 18px;
    color: #0877e5;
    text-indent: 1em;
    line-height: 35px;
    /*颜色 图片 图片位置 平铺方式*/
    background: red url("../images/down.png") 250px no-repeat;
}
/*ul{*/
/*    background: #b4b7bb;*/
/*}*/
/*
list-style:
    none 去除圆点
    circle : 空心圆
    decimal 数字
    square 正方形
*/
ul li{
    height: 30px;
    list-style: none;
    text-indent: 1em;
    background: #b4b7bb;
    background-image: url("../images/right.png");
    background-position: 208px 3px;
    background-repeat: no-repeat;

}
a{
    text-decoration: none;
    font-size: 14px;
    color: #000;
}
a:hover{
    color: #c29303;
    text-decoration: underline;
}
十二,背景
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    div{
      width: 1000px;
      height: 700px;
      border: 1px solid red;
      background-image: url("images/1.png");
      /*默认全部平铺*/
    }
      /*x轴*/
    .div1{
      background-repeat: repeat-x;
    }
      /*y轴*/
    .div2{
      background-repeat: repeat-y;
    }
      /*单个*/
    .div3{
      background-repeat: no-repeat;
    }
  </style>
</head>
<body>

<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>

</body>
十三,渐变
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
     /*https://www.grabient.com/*/
    /*径向渐变  圆形渐变*/
    body{
      background-color: #D9AFD9;
      background-image: linear-gradient(45deg, #D9AFD9 0%, #97D9E1 100%);

    }
  </style>
</head>
<body>
阿松大
</body>
十四,盒子模型
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    body{
      /*body总有一个默认的外边距margin:0, 常见操作*/
      margin: 0;
    }
    /*border 粗细 样式 颜色*/
    #app{
      width: 300px;
      border: 1px solid red;
      padding:  0 0 0 0;
      margin: 0 auto;
    }
    h2{
      font-size: 16px;
      background: #97D9E1;
      line-height: 30px;
      height: 30px;
      margin: 0;
      color: azure;
    }
    form{
      background: #97D9E1;
    }
  div:nth-of-type(1) > input{
    border: 3px solid black;
  }
  div:nth-of-type(2) > input{
    border: 3px solid navajowhite;
  }
  div:nth-of-type(3) > input{
    border: 3px solid darkorchid;
  }
  </style>
</head>
<body>

<div id="app">
  <h2>会员登录</h2>
  <form action="#">
    <div>
      <span>姓名:</span>
      <input id="loginname" type="text" class="itxt" name="loginname" tabindex="1" _input="true" autocomplete="off" value="" placeholder="邮箱/账号名/登录手机">
    </div>
    <div>
    <span>密码:</span>
    <input type="password">
    </div>
    <div>
      <span>邮箱:</span>
      <input type="email" >
    </div>
  </form>
</div>

</body>
十五,外边距
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--
外边距的妙用 : 居中元素
margin: 0 auto ;
-->
  <style>
    #app{
      width: 300px;
      border: 1px solid red;
      /*padding:  0 0 0 0;*/
      margin: 0 auto ;
    }
    /*margin: 0 1px 3px 4px; 上 右 下 左 */
    h2{
      font-size: 16px;
      background: #97D9E1;
      line-height: 30px;
      height: 30px;
      margin: 0 ;
      color: azure;
    }
    form{
      background: #97D9E1;
    }
    input{
      border: 1px solid black;
    }
    div:nth-of-type(1) {
      padding: 10px 2px 10px 2px;
    }
  </style>
</head>
<body>

<div id="app">
  <h2>会员登录</h2>
  <form action="#">
    <div>
      <span>姓名:</span>
      <input id="loginname" type="text" class="itxt" name="loginname" tabindex="1" _input="true" autocomplete="off" value="" placeholder="邮箱/账号名/登录手机">
    </div>
    <div>
    <span>密码:</span>
    <input type="password">
    </div>
    <div>
      <span>邮箱:</span>
      <input type="email" >
    </div>
  </form>
</div>

</body>
十六,圆角边框
<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
      /*半圆和圆*/
    div{
      width: 100px;
      height: 100px;
      /*border: 10px solid red;*/
      background: red;
      border-radius: 0px 100px 100px 0px;

    }
    img{
      border-radius: 50px ;
    }
  </style>
</head>
<body>
<div></div>
  <img src="images/img.png" alt="">

</body>



  <style>

/*
      边框圆角
左上 右上 右下 左下
*/

    div{
      width: 100px;
      height: 100px;
      border: 10px solid red;
      border-radius: 100px;
      /* 圆 : width = border-radius*/
    }
  </style>
十七,display
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--
block 块元素
inline 行内元素
inline-block 是块元素 但是可以内含
-->
  <style>
    div{
      width: 100px;
      height: 100px;
      border: 1px solid red;
      display: inline;
    }
    span{
      width: 100px;
      height: 100px;
      border: 1px solid red;
      display: inline-block;
    }
  </style>
</head>
<body>
<div>
  div块元素
</div>
<span>
  span行内元素
</span>
</body>


<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--  overflow: scroll
侧边滑动条
-->

  <style>
    #nav:after{
      content: '';
      display: block;
      clear: both;
    }
    #nav{
      width: 160px;
      height: 100px;
      overflow: scroll;
    }
  </style>
</head>
<body>
<div id="nav">
  <img src="../23圆角边框/images/1.png" alt="">
  <p>
    Comparable接口的方式一旦指定,保证Comparable接口实现类的对象在任何位置都可以比较大小。
  </p>
</div>
</body>
十八,定位栏

1.固定定位

<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    body{
      height: 100px;
    }
    div:nth-of-type(1){
      width: 100px;
      height: 100px;
      background: red;
      position: absolute;
      right: 0;
      bottom: 0;
    }
    div:nth-of-type(2){
      width: 50px;
      height: 50px;
      background: yellow;
      position: fixed;
      right: 0;
      bottom : 0;
    }
  </style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>

2.相对定位

<head>
    <meta charset="UTF-8">
    <title>Title</title>
  <style>
    body{
      margin: 20px;
    }
    div{
      margin: 10px;
      padding: 5px;
      font-size: 12px;
      line-height: 25px;
    }
    /*position: absolute;
    若父级元素有定位则相对父级元素定位
    没有则相对浏览器定位
    */
    #father{
      border: 1px solid azure;
      padding: 0;
      position: relative;
    }


    #one{
      background: #97D9E1;
      border: 1px solid #824faf;
      position: relative;/* 相对定位:上下左右*/
      /*top: -10px;*/
      /*left: 20px;*/
    }
    #two{
      background: #a80d5d;
      border: 1px solid #33a73b;
        position: absolute;
        right: 30px;
    }
    #three{
      background: #787323;
      border: 1px solid #0db6b6;
      position: relative;
      /*bottom: 10px;*/
      /*right: 10px;*/
    }
  </style>
</head>
<body>
<div id="father">
  <div id="one">第一个盒子</div>
  <div id="two">第二个盒子</div>
  <div id="three">第三个盒子</div>

</div>
</body>

3.绝对定位

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    body{
      margin: 20px;
    }
    div{
      margin: 10px;
      padding: 5px;
      font-size: 12px;
      line-height: 25px;
    }
    #father{
      border: 1px solid azure;
      padding: 0;
    }
    #one{
      background: #97D9E1;
      border: 1px solid #824faf;
      position: absolute;/* 绝对定位:上下左右*/
      top: -10px;
      left: 20px;
    }
    #two{
      background: #a80d5d;
      border: 1px solid #33a73b;
    }
    #three{
      background: #787323;
      border: 1px solid #0db6b6;
      position: relative;
      bottom: 10px;
      right: 10px;
    }
  </style>
</head>
<body>
<div id="father">
  <div id="one">第一个盒子</div>
  <div id="two">第二个盒子</div>
  <div id="three">第三个盒子</div>

</div>
</body>
十九,z-index
<head>
    <meta charset="UTF-8">
    <title>Title</title>

  <link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="content">
  <ul>
    <li><img src="images/1.png" alt=""></li>
    <li class="tipText">校内黑猫</li>
    <li class="tipBg"></li>
    <li>shijian:2022</li>
    <li>yihao</li>
  </ul>
</div>
</body>

style.css

#content{
    width: 180px;

    padding: 0px;
    margin: 0px;
    overflow: hidden;
    font-size: 12px;
    line-height: 25px;
    border: 1px  solid #1a1918;
}
ul,li{
    padding: 10px;
    margin: 10px;
    list-style: none;
    color: red;
}
/*父级元素相对定位*/
#content ul{
    position: relative;

}
.tipText{
    position: absolute;
    width: 180px;
    top: 118px;

    /*z-index: 59;*/
}

.tipBg{
    background: #97D9E1;
    position: absolute;
    width: 180px;
    top: 126px;
    /*opacity: 0.5;!*背景透明度*!*/
    filter: alpha(opacity=50);/*历史版本*/
    /*z-index: 58;*/
}
.tipText{

}
二十,阴影
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    margin: 0 auto;
要求:块元素,块元素有固定的宽度
-->
    <style>
        img{
            /*margin: 0 auto;*/
            /*width: 100px;*/
            /*height: 100px;*/
            /*border: 10px solid red;*/
            border-radius: 50px ;
            box-shadow: 10px 10px 10px yellow;
        }
    </style>
</head>
<body>
<div style="width: 500px;display: block;text-align: center">
    <div >
        <img src="images/1.png" alt="">
    </div>
</div>
<div style="display: block;text-align: center">
    <img src="images/1.png" alt="" >
</div>
</body>

原文地址:http://www.cnblogs.com/blwx/p/16794302.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性