述明 發表於 2022-8-3 17:09:00

CSS学习笔记

<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>1、CSS的四种导入方式</li><li>2、选择器<ul><li>2.1 基本选择器</li></ul></li><li>id名{}<ul><li>2.2 层次选择器</li><li>2.3 结构伪类选择器</li><li>2.4 属性选择器</li></ul></li><li>3、美化网页<ul><li>3.1 字体样式</li><li>3.2 文本样式</li><li>3.3 文本阴影和超链接伪类</li><li>3.4 列表样式</li><li>3.5 背景图像应用及渐变</li><li>3.6 盒子模型</li><li>3.7 圆角边框</li><li>3.8 阴影</li><li>3.9 浮动<ul><li>3.9.1 标准文档流</li><li>3.9.2 display</li><li>3.9.3 float</li><li>3.9.4 display 和 float 对比</li></ul></li><li>3.10 父级边框塌陷问题</li><li>3.10 定位<ul><li>3.10.1 相对定位</li><li>3.10.2 绝对定位</li><li>3.10.3 固定定位fixed</li><li>3.10.4 z-index</li></ul></li></ul></li><li>附录:伪元素和伪类</li></ul></div><p></p>
<p><font size="5"><strong>CSS:层叠样式表(Cascading Style Sheets)</strong></font></p><font size="5">
<h1 id="1css的四种导入方式">1、CSS的四种导入方式</h1>
<p><strong>link标签</strong></p>
标签最常见的用途是链接样式表。
<p><img src="https://img-blog.csdnimg.cn/fccf88abf9cf4a7eaac68de4bd4f34c1.png"><br>
优先级:就近原则,哪个离元素近实现哪个,这里行内样式优先级最高</p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;!--内部样式--&gt;
    &lt;style&gt;
      h1{
            color:green;
      }
    &lt;/style&gt;
    &lt;!--外部样式--&gt;
    &lt;link rel="stylesheet" href="css/style.css"&gt;
&lt;!--优先级:就近原则,哪个离元素近实现哪个,这里行内样式优先级最高--&gt;
&lt;!--行内样式,在标签元素中,编写一个style属性,编写样式即可--&gt;
&lt;h1 style="color:red"&gt;我是标题&lt;/h1&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>拓展:外部样式两种写法</p>
<ul>
<li>链接式:<br>
<em>HTML</em></li>
</ul>
<pre><code class="language-html"> &lt;link rel="stylesheet" href="css/style.css"&gt;
</code></pre>
<ul>
<li>导入式:<br>
<em>@import 是 CSS2.1特有的</em><br>
缺点--先展示结构再渲染</li>
</ul>
<pre><code class="language-html">    &lt;style&gt;
      @import url("css/style.css");
    &lt;/style&gt;
</code></pre>
<h1 id="2选择器">2、选择器</h1>
<blockquote>
<p>作用:选择页面上的某一个或者某一类元素</p>
</blockquote>
<h2 id="21-基本选择器">2.1 基本选择器</h2>
<p><strong>优先级: id选择器&gt;类选择器&gt;标签选择器</strong></p>
<ul>
<li><strong>标签选择器</strong></li>
</ul>
<p>选中同一类标签</p>
<blockquote>
<p>标签名{}</p>
</blockquote>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;标签选择器&lt;/title&gt;
    &lt;style&gt;
      /*标签选择器,会选择页面上所有的标签*/
      h1{
          color: #000000;
          background: #e7d5d5;
          border-radius: 10px;/*圆角*/
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;学习&lt;/h1&gt;
&lt;h1&gt;听歌&lt;/h1&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<ul>
<li><strong>类选择器 class</strong></li>
</ul>
<p>选择class相同的标签,可以跨标签使用</p>
<blockquote>
<p>.类名{}</p>
</blockquote>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;类选择器&lt;/title&gt;
    &lt;style&gt;
      /*可以复用 用class分组使用*/
      .bt1{
          color: #e7d5d5;
      }
      .bt2{
          color: #b2da89;
      }
      .bt3{
          color: #84b2d3;
      }
    &lt;/style&gt;


&lt;/head&gt;
&lt;body&gt;
&lt;h1 class="bt1"&gt;标题1&lt;/h1&gt;
&lt;h1 class="bt2"&gt;标题2&lt;/h1&gt;
&lt;h1 class="bt3"&gt;标题3&lt;/h1&gt;
&lt;p class="bt3"&gt;p标签&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/a8ccf28735a945a0968024e6786fd729.png"></p>
<ul>
<li><strong>id选择器</strong></li>
</ul>
<p>id必须保证全局唯一</p>
<blockquote>
<h1 id="id名">id名{}</h1>
</blockquote>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;id选择器&lt;/title&gt;
    &lt;style&gt;
      /*id必须保证全局唯一
          优先级: id选择器&gt;类选择器&gt;标签选择器
      */
      #bt1{
          color: #b2da89;
      }
      #bt2{
            color: #e7d5d5;
      }
      #bt3{
            color: #84b2d3;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1 id="bt1"&gt;标题1&lt;/h1&gt;
&lt;h1 id="bt2"&gt;标题2&lt;/h1&gt;
&lt;h1 id="bt3"&gt;标题3&lt;/h1&gt;
&lt;p id="bt4"&gt;p标签&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h2 id="22-层次选择器">2.2 层次选择器</h2>
<ul>
<li><strong>后代选择器</strong><br>
在某个元素后面</li>
</ul>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;层次选择器&lt;/title&gt;
    &lt;style&gt;
      /*后代选择器*/
      body ul{
            background: #7da8be;
      }
      body li{
            background: #b2da89;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;p1&lt;/p&gt;
&lt;p&gt;p1&lt;/p&gt;
&lt;p&gt;p3&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;
      &lt;p&gt;p4&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;p&gt;p5&lt;/p&gt;
    &lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;
            &lt;p&gt;p7&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
            &lt;p&gt;p8&lt;/p&gt;
      &lt;/li&gt;

    &lt;/ul&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/e8f34839ccdd4be39bcc869d161e33e8.png"></p>
<ul>
<li><strong>子选择器</strong></li>
</ul>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;层次选择器&lt;/title&gt;
    &lt;style&gt;
      /*子选择器*/
      body&gt;p{
            background: #84b2d3;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;p1&lt;/p&gt;
&lt;p&gt;p1&lt;/p&gt;
&lt;p&gt;p3&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;
      &lt;p&gt;p4&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;p&gt;p5&lt;/p&gt;
    &lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;
            &lt;p&gt;p7&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
            &lt;p&gt;p8&lt;/p&gt;
      &lt;/li&gt;

    &lt;/ul&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/f8fd8e3354f7470e9f5f904eef4058cd.png"></p>
<ul>
<li><strong>相邻兄弟选择器</strong><br>
只选择<strong>相邻且同级</strong>的在下面的一个</li>
</ul>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;层次选择器&lt;/title&gt;
    &lt;style&gt;
      /*兄弟选择器*/
      .bro + p{
            background: #e7d5d5;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;p1&lt;/p&gt;
&lt;p class = "bro"&gt;p2&lt;/p&gt;
&lt;p&gt;p3&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;
      &lt;p&gt;p4&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;p&gt;p5&lt;/p&gt;
    &lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;
            &lt;p&gt;p7&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
            &lt;p&gt;p8&lt;/p&gt;
      &lt;/li&gt;

    &lt;/ul&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/467a1c3d5cf348a49ac45b37d37fc865.png"></p>
<ul>
<li><strong>通用选择器</strong><br>
选择<strong>相邻且同级</strong>的在下面的所有</li>
</ul>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;层次选择器&lt;/title&gt;
    &lt;style&gt;
      /*通用选择器*/
      .bro~p{
            background: #e7d5d5;
      }

    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p class = "bro"&gt;p1&lt;/p&gt;
&lt;p&gt;p2&lt;/p&gt;
&lt;p&gt;p3&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;
      &lt;p&gt;p4&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
      &lt;p&gt;p5&lt;/p&gt;
    &lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;
            &lt;p&gt;p7&lt;/p&gt;
      &lt;/li&gt;
      &lt;li&gt;
            &lt;p&gt;p8&lt;/p&gt;
      &lt;/li&gt;

    &lt;/ul&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/1a6f9f39a4f84e758dc9509c78927624.png"></p>
<h2 id="23-结构伪类选择器">2.3 结构伪类选择器</h2>
<ul>
<li><strong>first-child 和 last-child</strong></li>
</ul>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;结构伪类选择器&lt;/title&gt;
    &lt;style&gt;
      /*ul的第一个子元素*/
      ul li:first-child{
      background: #e7d5d5;
      }
      /*ul的最后一个子元素*/
      ul li:last-child{
      background: #84b2d3;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p class = "bro"&gt;p1&lt;/p&gt;
&lt;p&gt;p2&lt;/p&gt;
&lt;p&gt;p3&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;p4&lt;/li&gt;
&lt;li&gt;p5&lt;/li&gt;
&lt;li&gt;p6&lt;/li&gt;
&lt;ul&gt;
    &lt;li&gt;p7&lt;/li&gt;
    &lt;li&gt;p8&lt;/li&gt;
    &lt;li&gt;p9&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/db2bfe73625a4b93a3e0b4c9ef658f5e.png"></p>
<ul>
<li><strong>nth-child() 和 nth-of-type()</strong></li>
</ul>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;结构伪类选择器&lt;/title&gt;
    &lt;style&gt;
      /*选中当前p元素的父级元素,1--选中父级元素的第一个子元素*/
      p:nth-child(1){
          background: #e7d5d5;
      }
      /*选中父元素里第二个类型为p的元素*/
      p:nth-of-type(2){
          background: cornflowerblue;
      }

    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p class = "bro"&gt;p1&lt;/p&gt;
&lt;p&gt;p2&lt;/p&gt;
&lt;p&gt;p3&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;p4&lt;/li&gt;
&lt;li&gt;p5&lt;/li&gt;
&lt;li&gt;p6&lt;/li&gt;
&lt;ul&gt;
    &lt;li&gt;p7&lt;/li&gt;
    &lt;li&gt;p8&lt;/li&gt;
    &lt;li&gt;p9&lt;/li&gt;
&lt;/ul&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/5c404809139f4bd59a2e8757bb04c3a0.png"></p>
<ul>
<li><strong>hover</strong><br>
触碰到元素背景变红色</li>
</ul>
<pre><code class="language-html"> p:hover{
          background: red;
      }

</code></pre>
<h2 id="24-属性选择器">2.4 属性选择器</h2>
<p>id+class 结合<br>
<img src="https://img-blog.csdnimg.cn/abe4906f7304406c9b163756d4d9f699.png"></p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;属性选择器&lt;/title&gt;
    &lt;style&gt;
      .demo a{
            float: left;/*向左漂移*/
            display: block;
            height: 50px;
            width: 50px;
            border-radius: 10px; /*圆角弧度*/
            background: blue;
            color: #c9c4c4;
            text-align: center; /*文字居中*/
            text-decoration: none;/*去下划线*/
            margin-right: 10px; /*外边距向右*/
            font: bold 20px/50px Arial; /*20--字体大小 50--行高*/
      }

      /*选中所有存在id属性的元素*/
      a{
            background: yellow;
      }
      a{
            background: white;
      }
      /*属性名,属性名=属性值(正则)
         *= 包含
         ^= 以什么开头
         $= 以什么结尾
       */
      a{
            background: green;
      }
      
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p class="demo"&gt;
&lt;a href="http://www.baidu.com" class="link first" id ="one"&gt;1&lt;/a&gt;
&lt;a href="http://www.fgdf.com" class="first" &gt;2&lt;/a&gt;
&lt;a href="www.dfgdfdu.com" class="2first" id ="three"&gt;3&lt;/a&gt;
&lt;a href="" class="link4" &gt;4&lt;/a&gt;
&lt;a href="baidu.com" class="link5" &gt;5&lt;/a&gt;
&lt;a href="http:.com" class="link6" &gt;6&lt;/a&gt;
&lt;a href="http://www" class="li" &gt;7&lt;/a&gt;

&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h1 id="3美化网页">3、美化网页</h1>
<p>span 标签:重点突出的字,使用span套起来</p>
<h2 id="31-字体样式">3.1 字体样式</h2>
<pre><code class="language-html">&lt;style&gt;
      body{
            font-family: 微软雅黑;
            font-weight: bold; /* 字体粗细 */
      }
      h1{
            font-size: 30px;
      }
    &lt;/style&gt;
</code></pre>
<h2 id="32-文本样式">3.2 文本样式</h2>
<ul>
<li>颜色 color</li>
<li>文本对齐方式 text-align</li>
<li>首行缩进 text-indent</li>
<li>行高 line-height</li>
<li>下划线</li>
</ul>
<pre><code class="language-html">&lt;style&gt;
    h1{
      color: rgba(0,255,255,0.9); /* rgba 第四个属性A为透明度 */
      text-align: center; /*文本居中*/
    }
    p{
      text-indent: 2em; /*首行缩进*/
      line-height: 25px;
    }
    .p1{
      text-decoration: underline;/*下划线*/
    }
    .p2{
      text-decoration: line-through;/*中划线*/
    }
    .p3{
      text-decoration: overline;/*上划线*/
    }
&lt;/style&gt;
</code></pre>
<ul>
<li>水平对齐参照物</li>
</ul>
<pre><code class="language-html">img,span{
      vertical-align: middle;
    }
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/5220205c69ae4b4a935fea180baad859.png"></p>
<h2 id="33-文本阴影和超链接伪类">3.3 文本阴影和超链接伪类</h2>
<pre><code class="language-html"> &lt;style&gt;
      /*鼠标悬浮状态*/
      a:hover{
            color: orange;
            font-size: 25px;
      }
      /*鼠标按住未释放的颜色*/
      a:active{
            color: green;
      }
      /*阴影颜色 水平 垂直偏移 阴影半径*/
      a{
            text-shadow: #84b2d3 10px -10px 2px;
      }
    &lt;/style&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/b8ab11300b244ffc85745d6aac8b7d3f.png"></p>
<h2 id="34-列表样式">3.4 列表样式</h2>
<p><strong>div相当于一个空标签 用来装东西</strong><br>
list-style—--none 去点<br>
—————circle 空心圆<br>
—————decimal 数字</p>
<pre><code class="language-css">#nav{
    /*div相当于一个空标签 用来装东西*/
    width:400px;
}
.title{
    font-size: 18px;
    font-weight: bold;
    text-indent: 1em;
    line-height: 35px;
}
/*list-style
none 去点
circle 空心圆
decimal 数字
*/
ul li {
    height: 30px;
    list-style: none; /*去掉圆点*/
}
a{
    text-decoration: none;
}
</code></pre>
<h2 id="35-背景图像应用及渐变">3.5 背景图像应用及渐变</h2>
<ul>
<li><strong>背景图片</strong></li>
</ul>
<pre><code class="language-html"> &lt;style&gt;
      /*solid 代表实线*/
      div{
            width: 1000px;
            height: 700px;
            border: 1px solid red;
            /*默认全部平铺*/
            background-image: url("images/lan.jpg");
      }
      .div1{
            /*只铺水平*/
            background-repeat: repeat-x;
      }
      .div2{
            /*不平铺*/
            background-repeat: no-repeat;
      }
    &lt;/style&gt;
</code></pre>
<ul>
<li><strong>图片漂移</strong></li>
</ul>
<pre><code class="language-html">/*颜色 图片 图片位置 平铺方式*/
    background: red url("images/d.gif") 270px 10px no-repeat ;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/fb52b7b868644b0aa4a890c46c2f5a67.png"></p>
<ul>
<li><strong>渐变</strong></li>
</ul>
<pre><code class="language-html"> background-image: linear-gradient(115deg,#FFFFFF 0%,#84b2d3 50%, #9fa3d2 100%);
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/dc8a814951c443a18cd8de6ff60e84d7.png"></p>
<h2 id="36-盒子模型">3.6 盒子模型</h2>
<p><img src="https://img-blog.csdnimg.cn/07fb2bae580d4d4c80a8c560f64277ac.png"></p>
<ul>
<li><strong>margin:外边距 和 border:边框</strong><br>
外边距作用: 居中元素<br>
border: 粗细 样式 颜色<br>
solid---实线<br>
dashed---虚线</li>
</ul>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;盒子模型&lt;/title&gt;
    &lt;style&gt;
      body{
            margin: 0;/* 默认有外边距,置0*/
      }
      /*border: 粗细 样式 颜色   solid---实线 dashed---虚线*/
      #box{
            width:300px;
            border:1px solid red;
      }
      form{
            background: orange;
      }
      /*选中所有div标签下的第一个类型为input的属性*/
      div:nth-of-type(1)input{
            border: 1px solid black;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id = "box"&gt;
      &lt;h2&gt;会员登录&lt;/h2&gt;
      &lt;form action="#"&gt;
            &lt;div&gt;
                &lt;span&gt;账号:&lt;/span&gt;
                &lt;input type="text"&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;span&gt;密码:&lt;/span&gt;
                &lt;input type="text"&gt;
            &lt;/div&gt;
            &lt;div&gt;
                &lt;span&gt;邮箱:&lt;/span&gt;
                &lt;input type="text"&gt;
            &lt;/div&gt;
      &lt;/form&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<ul>
<li><strong>padding:内边距</strong><br>
<em>padding: 上 右 下 左 顺时针</em></li>
</ul>
<pre><code class="language-html">    &lt;style&gt;
   /*padding: 上 右 下 左 顺时针*/
      div:nth-of-type(1){
            border: 1px solid black;
            padding: 10px 10px 10px 0px;
      }
    &lt;/style&gt;
</code></pre>
<h2 id="37-圆角边框">3.7 圆角边框</h2>
<p><strong>border-radius:左上 右上 右下 左下 顺时针</strong><br>
只有两个值的话----左上=右下右上=左下<br>
只有一个值的话----四个角都改变<br>
圆圈: 圆角=宽度/高度</p>
<pre><code class="language-html">&lt;style&gt;
      /*左上 右上 右下 左下 顺时针
            只有两个值的话 左上=右下右上=左下
            圆圈: 圆角=宽度/高度
      */
      div{
            width: 100px;
            height: 100px;
            border: 10px solid royalblue;
            border-radius: 100px 50px 20px 10px;
      }
         img{
            border: 1px solid royalblue;
            border-radius: 294px;
      }
    &lt;/style&gt;
</code></pre>
<h2 id="38-阴影">3.8 阴影</h2>
<p><strong>box-shadow: +右-左+下-上模糊半径 颜色</strong></p>
<pre><code class="language-html">&lt;style&gt;
      div{
            /*+右-左+下-上模糊半径 颜色*/
            width: 100px;
            height: 100px;
            border: 10px solid royalblue;
            border-radius: 100px 50px 20px 10px;
            box-shadow: 20px 10px 10px yellow ;
      }
      img{
            box-shadow: 20px 10px 100px yellow ;
      }
    &lt;/style&gt;

</code></pre>
<h2 id="39-浮动">3.9 浮动</h2>
<h3 id="391-标准文档流">3.9.1 标准文档流</h3>
<p><em>行内元素可以被包含在块级元素中,反之则不可以</em></p>
<blockquote>
<p>块级元素:独占一行<br>
h1~h6 p div 列表……<br>
行内元素:不独占一行<br>
span a img strong……</p>
</blockquote>
<h3 id="392-display">3.9.2 display</h3>
<pre><code class="language-html"> &lt;style&gt;
      /*display: block; 块元素
            display: inline; 行内元素
            display: inline-block; 即是行内元素也是块元素(保持块元素的特性但可以写在一行)
            display: none; 不显示
      */
      div{
            width: 100px;
            height: 100px;
            border: 1px solid orange;
            display: inline-block;
      }
      span{
            width: 100px;
            height: 100px;
            border: 1px solid orange;
            display: inline-block;
      }

    &lt;/style&gt;
</code></pre>
<h3 id="393-float">3.9.3 float</h3>
<p>拿出来 浮上去</p>
<blockquote>
<p>clear:both; 两侧都不允许有浮动元素<br>
clear:left; 左侧都不允许有浮动元素<br>
clear:right; 右侧都不允许有浮动元素<br>
否则另起一行</p>
</blockquote>
<pre><code class="language-css">div{
    margin: 10px;
    padding: 5px;
}
#yang{
    border: 1px #000 solid;
}
.img1{
   border: 2px #1121de dashed;
    display: inline-block;
    float:left;
}
.img2{
    border: 2px #1121de dashed;
    display: inline-block;
    float:left;
}
.img3{
    border: 2px #1121de dashed;
    display: inline-block;
    float:left;
    clear:both; /*不贴着上一个浮*/
}
</code></pre>
<h3 id="394-display-和-float-对比">3.9.4 display 和 float 对比</h3>
<ul>
<li><strong>display</strong><br>
方向不可控制</li>
<li><strong>float</strong><br>
浮动起来会脱离标准文档流,所以要解决父级边框塌陷问题</li>
</ul>
<h2 id="310-父级边框塌陷问题">3.10 父级边框塌陷问题</h2>
<p><strong>浮动元素超出边框</strong></p>
<p><em>解决方法</em>:</p>
<p><strong>1、增加父级元素高度</strong><br>
<strong>2、在同级的元素最后增加一个空的div标签,清除浮动</strong></p>
<pre><code class="language-html"> &lt;div class="clear"&gt;&lt;/div&gt;
</code></pre>
<pre><code class="language-css">.clear{
    clear: both;
}
</code></pre>
<p><strong>3、overflow</strong><br>
下拉场景避免使用</p>
<blockquote>
<p>1、overflow:hidden超出部分隐藏,一般情况下,在页面中,一般溢出后会显示省略号,比如,当一行文本超出固定宽度就隐藏超出的内容显示省略号。<br>
2、overflow:hidden 清除浮动:一般而言,父级元素不设置高度时,高度由随内容增加自适应高度。当父级元素内部的子元素全部都设置浮动float之后,子元素会脱离标准流,不占位,父级元素检测不到子元素的高度,父级元素高度为0。<br>
3、overflow:hidden 解决外边距塌陷:父级元素内部有子元素,如果给子元素添加margin-top样式,那么父级元素也会跟着下来,造成外边距塌陷。</p>
</blockquote>
<pre><code class="language-html">overflow: scroll; //滑动条显示
overflow: hidden;
</code></pre>
<p><strong>4、父类添加一个伪类:after</strong><br>
相当于自动添加一个2中的 div 标签<br>
yang是浮动元素的父类</p>
<pre><code class="language-csharp">#yang:after{
    content: "";
    display: block;
    clear: both;
}
</code></pre>
<h2 id="310-定位">3.10 定位</h2>
<h3 id="3101-相对定位">3.10.1 相对定位</h3>
<p>相对定位: 相对于自己原来的位置偏移,仍然在 标准文档流中,原来的位置会被保留<br>
<strong>position: relative;</strong></p>
<pre><code class="language-html"> &lt;style&gt;
      /*相对定位: 相对于自己原来的位置偏移
            position: relative; top/left/right/bottom
      */
      div{
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
      }
      #father{
            border: 1px cornflowerblue solid;
      }
      #first{
            border: 2px #7da8be dashed;
            position: relative;
            top:-20px;
            left:20px
      }
      #second{
            border: 2px #be9fd2 dashed;
            position: relative;
            bottom:20px;
      }
      #third{
            border: 2px #b2da89 dashed;
      }
    &lt;/style&gt;
</code></pre>
<ul>
<li><strong>方块练习</strong></li>
</ul>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;方块练习&lt;/title&gt;
    &lt;style&gt;
      #father{
            width: 300px;
            height: 300px;
            border: 2px solid red;
            padding: 10px;
      }
      a{
            width: 100px;
            height: 100px;
            background-color: #ee6bc5;
            text-decoration: none;
            text-align: center;
            line-height: 100px;
            display: block;
      }
      .a2,.a4{
            position: relative;
            top:-100px;
            left:200px;
      }
      .a5{
            position: relative;
            top:-300px;
            left:100px;
      }
      a:hover{
            background-color: #1a5bd3;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id = "father"&gt;
    &lt;div &gt;&lt;a href="#" class="a1"&gt;链接1&lt;/a&gt;&lt;/div&gt;
    &lt;div &gt;&lt;a href="#" class="a2"&gt;链接2&lt;/a&gt;&lt;/div&gt;
    &lt;div &gt;&lt;a href="#" class="a3"&gt;链接3&lt;/a&gt;&lt;/div&gt;
    &lt;div &gt;&lt;a href="#" class="a4"&gt;链接4&lt;/a&gt;&lt;/div&gt;
    &lt;div &gt;&lt;a href="#" class="a5"&gt;链接5&lt;/a&gt;&lt;/div&gt;

&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><img src="https://img-blog.csdnimg.cn/126eed3f897a416ea4b7d8b17dc24a39.png"></p>
<h3 id="3102-绝对定位">3.10.2 绝对定位</h3>
<p>相对于XXX定位,不在标准文档流中,原来的位置不会被保留<br>
1、没有父级元素条件的前提下,相对于浏览器定位<br>
2、假设父级元素存在定位,通常相对于父级元素偏移<br>
3、在父级元素范围内定位,不能为负值</p>
<pre><code class="language-html"> &lt;style&gt;
      /*绝对定位: 相对于XXX的位置偏移
            position: absolute;
      */
      div{
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
      }
      #father{
            border: 1px cornflowerblue solid;
      }
      #first{
            border: 2px #7da8be dashed;
            position: absolute;
            top: 20px;
            left:20px
      }
      #second{
            border: 2px #be9fd2 dashed;

      }
      #third{
            border: 2px #b2da89 dashed;
      }
    &lt;/style&gt;
</code></pre>
<h3 id="3103-固定定位fixed">3.10.3 固定定位fixed</h3>
<p>位置固定不动</p>
<pre><code class="language-html"> &lt;style&gt;
      body{
            height: 1000px; /*产生滑动条*/
      }
      div:nth-of-type(2){
            width: 50px;
            height: 50px;
            background: orange;
            position: fixed;
            text-align: center;
            line-height: 50px;
      }
    &lt;/style&gt;
</code></pre>
<h3 id="3104-z-index">3.10.4 z-index</h3>
<p>图层----向上一层/向下一层<br>
z-index: 最低层是0,最高层是999<br>
opacity: 0.5;---透明度0.5</p>
<pre><code class="language-css">#content{
    border: 2px #1a5bd3 solid;
    overflow: hidden;
    line-height: 25px;
    font-size: 12px;
}
ul,li{
    list-style: none;
}
/*父级元素相对定位*/
#content ul{
    position: relative;
}
.tipt,.tipb{
    position: absolute;
    top: 80px;
    right: 240px;
    width: 200px;
    height: 25px;
    color: #0a0a0a;
}
/*z-index: 最低层是0,最高层是999
    opacity: 0.5;---透明度0.5
    filter: alpha(opacity=50); filter过滤器--和上面效果相同(早期版本支持)
*/
.tipt{
    z-index: 999;
}
.tipb{
    background: #1a5bd3;
    /*opacity: 0.5;*/
    filter: alpha(opacity=50);
}
</code></pre>
<h1 id="附录伪元素和伪类">附录:伪元素和伪类</h1>
<p><img src="https://img-blog.csdnimg.cn/ca585a4fdba74bd5b4255a12bbfd664e.jpeg"></p>
<p><img src="https://img-blog.csdnimg.cn/8bc3fac6d60d42cb95773771c4d3edac.jpeg"></p>
</font><br><br>
来源:https://www.cnblogs.com/zhiqisuns/p/16547863.html
頁: [1]
查看完整版本: CSS学习笔记