初心始现 發表於 2021-7-6 21:55:00

CSS学习笔记

<p><img src="https://img-blog.csdnimg.cn/20200507153758833.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Bhbl9oMTk5NQ==,size_16,color_FFFFFF,t_70#pic_center" alt="在这里插入图片描述" loading="lazy"></p>
<h1 id="什么是css">什么是CSS</h1>
<p>如何学习</p>
<pre><code>1. CSS是什么
2. CSS怎么用(快速入门)
</code></pre>
<ol start="3">
<li>CSS选择器(重点+难点)</li>
<li>美化网页(文字、阴影、超链接、列表、渐变...)</li>
<li>盒子模型</li>
<li>浮动</li>
<li>定位</li>
<li>网页动画(特效效果)</li>
</ol>
<h2 id="什么是css-1">什么是CSS</h2>
<p>Cascading Style Sheet层叠样式表<br>
CSS:表现(美化网页)<br>
字体,颜色,边距,高度,宽度,背景图片,网页定位,网页浮动</p>
<h2 id="发展史">发展史</h2>
<p>CSS1.0</p>
<p>CSS2.0: DIV(块) + CSS ,HTML与CSS结构分离的思想,网页变得简单,SEO</p>
<p>CSS2.1: 浮动,定位</p>
<p>CSS3.0 :圆角,阴影,动画… 浏览器兼容性</p>
<h2 id="快速入门">快速入门</h2>
<p>建议使用规范</p>
<pre><code class="language-html">&lt;link rel="stylesheet" href="css/style.css"&gt;
</code></pre>
<p>CSS的优势:</p>
<ol>
<li>内容和表现分离;</li>
<li>网页结构表现统一,可以实现复用</li>
<li>样式十分的丰富</li>
<li>建议使用独立于html的css文件</li>
<li>利用SEO,容易被搜索引擎收录</li>
</ol>
<h2 id="css的三中导入方式">CSS的三中导入方式</h2>
<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;!--内部样式--&gt;
    &lt;style&gt;
      h1{
            color: green;
      }
    &lt;/style&gt;

    &lt;!--外部样式--&gt;
    &lt;link rel="stylesheet" href="css/style.css" /&gt;
&lt;/head&gt;
&lt;body&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>导入式:</li>
</ul>
<p>@import是CSS2.1特有的</p>
<pre><code class="language-html">&lt;style&gt;
@import url("css/style.css")
&lt;/style&gt;
</code></pre>
<ul>
<li>链接式:</li>
</ul>
<pre><code class="language-html">&lt;link rel="stylesheet" href="./"&gt;
</code></pre>
<h1 id="选择器">选择器</h1>
<blockquote>
<p>作用:选择页面上的某一个后者某一类元素</p>
</blockquote>
<h2 id="基本选择器">基本选择器</h2>
<ol>
<li>标签选择器:选择一类标签 标签{}</li>
</ol>
<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;style&gt;
      h1{
            color: orange;
            background: blue;
            border-radius: 10px;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;标签选择器&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<ol start="2">
<li>类选择器class:选择所有class一致的标签,跨标签,格式:.类名{}</li>
</ol>
<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;style&gt;
      /*类选择器的格式 .class的名称{}
            好处:可以多个标签归类,是同一个class,可以复用
      */
      .demo1{
            color: blue;
      }
      .demo2{
            color: red;
      }
      .demo3{
            color: aqua;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1 class = "demo1"&gt;类选择器:demo1&lt;/h1&gt;
&lt;h1 class="demo2"&gt;类选择器:demo2&lt;/h1&gt;
&lt;h1 class="demo3"&gt;类选择器:demo3&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<ol start="3">
<li>id 选择器:全局唯一,格式:#id名{}</li>
</ol>
<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;style&gt;
      /*id选择器:id必须保证全局唯一
            #id名称{}
            不遵循就近原则,优先级是固定的
            id选择器 &gt; 类选择器&gt;标签选择器
      */
      #demo1{
            color: aqua;
      }
      .demo2{
            color: red;
      }
      #demo2{
            color: orange;
      }
      h1{
            color: blue;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1 id="demo1"&gt;id选择器:demo1&lt;/h1&gt;
&lt;h1 class="demo2" id = "demo2"&gt;id选择器:demo2&lt;/h1&gt;
&lt;h1 class="demo2"&gt;id选择器:demo3&lt;/h1&gt;
&lt;h1&gt;id选择器:demo4&lt;/h1&gt;
&lt;h1&gt;id选择器:demo5&lt;/h1&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>优先级:id &gt; class &gt; 标签</p>
<h2 id="层次选择器">层次选择器</h2>
<ol>
<li>后代选择器:在某个元素的后面</li>
</ol>
<pre><code class="language-css">/*后代选择器*/
&lt;style&gt;
body p{
        background:red;
}
&lt;/style&gt;
</code></pre>
<ol start="2">
<li>子选择器:一代</li>
</ol>
<pre><code class="language-css">/*子选择器*/
&lt;style&gt;
body&gt;p{
        background:orange;
}
&lt;/style&gt;
</code></pre>
<ol start="3">
<li>相邻兄弟选择器(选择下面那个兄弟)</li>
</ol>
<pre><code class="language-css">/*相邻兄弟选择器:只有一个,相邻(向下)*/
&lt;style&gt;
.active+p{
background: red
}
&lt;/style&gt;

&lt;body&gt;
        &lt;p class="active"&gt;p1&lt;p&gt;
        &lt;p&gt;p2&lt;/p&gt;
&lt;/body&gt;
</code></pre>
<ol start="4">
<li>通用选择器(当前选中标签下面所有的标签)</li>
</ol>
<pre><code class="language-css">&lt;style&gt;
/*通用兄弟选择器,当前选中元素的向下的所有兄弟元素*/
        .active~p{
        background:red;
}
&lt;/style&gt;
&lt;body&gt;
        &lt;p class="active"&gt;p1&lt;p&gt;
        &lt;p&gt;p2&lt;/p&gt;
&lt;/body&gt;
</code></pre>
<h2 id="结构伪类选择器">结构伪类选择器</h2>
<p>伪类</p>
<pre><code class="language-html">&lt;style&gt;
      /*ul的第一个子元素*/
      ul li:first-child{
            background: aqua;
      }
      /*ul的最后一个子元素*/
      ul li:last-child{
            background: blue;
      }
      /*选中p1:定位到父元素,选择当前的第一个元素
            选择当前p元素 的父级元素,选中父级元素的第一个,并且是当前元素才生效!
      */
      p:nth-child(1){
            background: orange;
      }
      /*选中父元素下的,第2个p元素*/
      p:nth-of-type(2){
            background: red;
      }
    &lt;/style&gt;
</code></pre>
<h2 id="属性选择器常用">属性选择器(常用)</h2>
<p>id + class结合</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;style&gt;
         .demo a{
            display: block;
            height: 50px;
            width: 50px;
            float:left;
            border-radius: 10px;
            background: blue;
            text-align: center;
            color: beige;
            text-decoration: none;
            margin-right: 5px;
            font: bold 20px/50px Arial;
      }
         /*属性名,属性名=属性值(正则)
         =表示绝对等于
         *=表示包含
         ^=表示以...开头
         $=表示以...结尾
         存在id属性的元素a[]{}
         */
      /* a{
             background: red;
         }*/

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

      /*class中有links元素*/
       /* a{
            background: orange;
      }*/
      /*a{
            background: black ;
      }*/
      /*选中href中以http开头的元素*/
      a{
            background: orange;
      }
    &lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;p class="demo"&gt;
    &lt;a href="http://www.baidu.com" class="links item first" id="first"&gt;1&lt;/a&gt;
    &lt;a href="/adad/faf" class="links item2 first2" &gt;2&lt;/a&gt;
    &lt;a href="qwe123" class="links item3 first3" &gt;3&lt;/a&gt;
    &lt;a href="eweqe" class="links item4 first4" &gt;4&lt;/a&gt;
    &lt;a href="rrrrr" class="links item5 first5" &gt;5&lt;/a&gt;
    &lt;a href="ttt" class="links item6 first6" &gt;6&lt;/a&gt;
    &lt;a href="yyy" class="links item7 first7" &gt;7&lt;/a&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h1 id="美化页面">美化页面</h1>
<h2 id="为什么要美化网页">为什么要美化网页</h2>
<ol>
<li>有效的传递页面信息</li>
<li>美化网页,页面漂亮才能吸引客户</li>
<li>凸显页面的主题</li>
<li>提高用户的体验</li>
</ol>
<p>span标签:重点要突出的字,使用span标签套起来</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;style&gt;
      #title1{
            font-size: 50px;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

学习语言&lt;span id="title1"&gt;JAVA&lt;/span&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>font-family:字体<br>
font-size:字体大小<br>
font-weight:字体粗细</p>
<h2 id="字体样式">字体样式</h2>
<pre><code class="language-css">font-weight:bolder;/*也可以填px,但不能超过900,相当于bloder*/
/*常用写法:*/
font:oblique bloder 12px "楷体"
</code></pre>
<h2 id="文本样式">文本样式</h2>
<ol>
<li>
<p>颜色 color rgb rgba</p>
</li>
<li>
<p>文本对齐方式 text-align:center</p>
</li>
<li>
<p>首行缩进 text-indent:2em</p>
</li>
<li>
<p>行高 line-height:</p>
</li>
<li>
<p>装饰 text-decoration</p>
<pre><code class="language-css">text-decoration:underline/*下划线*/
text-decoration:line-through/*中划线*/
text-decoration:overline/*上划线*/
text-decoration:none/*超链接去下划线*/
</code></pre>
</li>
<li>
<p>文本图片水平对齐:vertical-align;middle</p>
<pre><code class="language-css">img,span{vetical-align:middle}
</code></pre>
</li>
</ol>
<h2 id="文本阴影和超链接伪类">文本,阴影和超链接伪类</h2>
<p>超链接伪类</p>
<pre><code class="language-css">&lt;style&gt;
        a{/*超链接有默认的颜色*/
                text-decoration:none;
                color:#000000;
        }
        a:hover{/*鼠标悬浮的状态*/
                color:orange;
        }
        a:active{/*鼠标按住未释放的状态*/
                color:green
        }
        a:visited{/*点击之后的状态*/
                color:red
        }
&lt;/style&gt;
</code></pre>
<p>阴影</p>
<pre><code class="language-css">/*        第一个参数:表示水平偏移
        第二个参数:表示垂直偏移
        第三个参数:表示模糊半径
        第四个参数:表示颜色
*/
text-shadow:5px 5px 5px 颜色
</code></pre>
<h2 id="列表-ul-li">列表 ul li</h2>
<pre><code class="language-css">/*list-style{
        none:去掉原点
        circle:空心圆
        decimal:数字
        square:正方形
}*/
ul li{
        height:30px;
        list-style:none;
        text-indent:1em;
}
a{
        text-decoration:none;
        font-size:14px;
        color:#000;
}
a:hover{
        color:orange;
        text-decoration:underline
}
/*放在div中,作为导航栏*/
&lt;div id="nav"&gt;&lt;/div&gt;
#nav{
        width:300px;
}
</code></pre>
<h2 id="背景">背景</h2>
<ol>
<li>背景颜色:background</li>
<li>背景图片</li>
</ol>
<pre><code class="language-css">background-image:url("");/*默认是全部平铺的*/
background-repeat:repeat-x/*水平平铺*/
background-repeat:repeat-y/*垂直平铺*/
background-repeat:no-repeat/*不平铺*/
</code></pre>
<ol start="3">
<li>综合使用</li>
</ol>
<pre><code class="language-css">background:red url("图片相对路劲") 270px 10px no-repeat
background-position:/*定位:背景位置*/
</code></pre>
<h2 id="渐变">渐变</h2>
<p>网址:https://www.grablent.com<br>
径向渐变、圆形渐变</p>
<h1 id="盒子模型">盒子模型</h1>
<h2 id="什么是盒子模型">什么是盒子模型</h2>
<ol>
<li>外边距: margin</li>
<li>内边距 :padding</li>
<li>边框 :border</li>
</ol>
<h2 id="边框">边框</h2>
<p>border:粗细 样式 颜色</p>
<ol>
<li>边框的粗细</li>
<li>边框的样式</li>
<li>边框的颜色</li>
</ol>
<h2 id="外边距----妙用居中">外边距----妙用:居中</h2>
<p>margin-left/right/top/bottom–&gt;表示四边,可分别设置,也可以同时设置如下</p>
<pre><code class="language-css">margin:0 0 0 0/*分别表示上、右、下、左;从上开始顺时针*/
/*例1:居中*/
margin:0 auto /*auto表示左右自动*/
/*例2:*/
margin:4px/*表示上、右、下、左都为4px*/
/*例3*/
margin:10px 20px 30px/*表示上为10px,左右为20px,下为30px*/
</code></pre>
<p>盒子的计算方式:<br>
margin+border+padding+内容的大小</p>
<p>总结:<br>
body总有一个默认的外边距 margin:0<br>
常见操作:初始化</p>
<pre><code class="language-css">margin:0;
padding:0;
text-decoration:none;
</code></pre>
<h2 id="圆角边框----border-radius">圆角边框----border-radius</h2>
<p>border-radius有四个参数(顺时针),左上开始<br>
圆圈:圆角=半径</p>
<h2 id="盒子阴影">盒子阴影</h2>
<h1 id="浮动">浮动</h1>
<h2 id="标准文档流">标准文档流</h2>
<p><img src="https://img-blog.csdnimg.cn/20200507094752916.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Bhbl9oMTk5NQ==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述" loading="lazy"></p>
<p>块级元素:独占一行 h1~h6 、p、div、 列表…<br>
行内元素:不独占一行 span、a、img、strong</p>
<blockquote>
<p>注: 行内元素可以包含在块级元素中,反之则不可以。</p>
</blockquote>
<h2 id="display重要">display(重要)</h2>
<ol>
<li>block:块元素</li>
<li>inline:行内元素</li>
<li>inline-block:是块元素,但是可以内联,在一行</li>
</ol>
<blockquote>
<p>这也是一种实现行内元素排列的方式,但是我们很多情况用float</p>
</blockquote>
<ol start="4">
<li>
<p>none:消失</p>
<pre><code class="language-CSS">&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;!--block 块元素
      inline 行内元素
      inline-block 是块元素,但是可以内联 ,在一行
    --&gt;
    &lt;style&gt;
      div{
            width: 100px;
            height: 100px;
            border: 1px solid red;
            display: inline-block;
      }
      span{
            width: 100px;
            height: 100px;
            border: 1px solid red;
            display: inline-block;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;div块元素&lt;/div&gt;
&lt;span&gt;span行内元素&lt;/span&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>QQ会员页面导航练习</p>
<p><img src="https://img-blog.csdnimg.cn/20200507102302375.png#pic_center" alt="在这里插入图片描述" loading="lazy"></p>
</li>
</ol>
<pre><code class="language-css">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;QQ会员&lt;/title&gt;
    &lt;link rel="stylesheet" href="css/style.css" /&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="wrap"&gt;
    &lt;!--头部--&gt;
    &lt;header class="nav-header"&gt;
      &lt;div class="head-contain"&gt;
            &lt;a href="" class="top-logo"&gt;&lt;img src="img/logo.png" width="145" height="90" /&gt;&lt;/a&gt;
            &lt;nav class="top-nav"&gt;
                &lt;ul&gt;
                  &lt;li&gt;&lt;a href=""&gt;功能特权&lt;/a&gt; &lt;/li&gt;
                  &lt;li&gt;&lt;a href=""&gt;游戏特权&lt;/a&gt; &lt;/li&gt;
                  &lt;li&gt;&lt;a href=""&gt;生活特权&lt;/a&gt; &lt;/li&gt;
                  &lt;li&gt;&lt;a href=""&gt;会员特权&lt;/a&gt; &lt;/li&gt;
                  &lt;li&gt;&lt;a href=""&gt;成长体系&lt;/a&gt; &lt;/li&gt;
                  &lt;li&gt;&lt;a href=""&gt;年费专区&lt;/a&gt; &lt;/li&gt;
                  &lt;li&gt;&lt;a href=""&gt;超级会员&lt;/a&gt; &lt;/li&gt;
                &lt;/ul&gt;
            &lt;/nav&gt;
            &lt;div class="top-right"&gt;
                &lt;a href=""&gt;登录&lt;/a&gt;
                &lt;a href=""&gt;开通超级会员&lt;/a&gt;
            &lt;/div&gt;
      &lt;/div&gt;
    &lt;/header&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<pre><code class="language-css">*{
    padding:0;
    margin: 0;
}
a{
    text-decoration: none;
}
.nav-header{
    height: 90px;
    width: 100%;
    background: rgba(0,0,0,.6);
}
.head-contain{
    width: 1180px;
    height: 90px;
    margin: 0 auto;
    text-align: center;
}
.top-logo,.top-nav,.top-nav li,.top-right{
    height: 90px;
    display: inline-block;
    vertical-align: top;
}
.top-nav{
    margin: 0 48px;
}
.top-nav li{
    line-height: 90px;
    width: 90px;
}
.top-nav li a{
    display: block;
    text-align: center;
    font-size: 16px;
    color: #fff;
}
.top-nav li a:hover{
    color: blue;
}

.top-right a{
    display: inline-block;
    font-size: 16px;
    text-align: center;
    margin-top: 25px;
    border-radius: 35px;
}
.top-right a:first-of-type{
    width: 93px;
    height: 38px;
    line-height: 38px;
    color: #fad65c;
    border: 1px #fad65c solid;
}
.top-right a:first-of-type:hover{
    color: #986b0d;
    background: #fad65c;
}
.top-right a:last-of-type{
    width: 140px;
    height: 40px;
    font-weight: 700;
    line-height: 40px;
    background: #fad65c;
    color: #986b0d;
}
.top-right a:last-of-type:hover{
    background: #fddc6c;
}
</code></pre>
<p>float:left/right左右浮动<br>
clear:both</p>
<p>overflow及父级边框塌陷问题<br>
clear:<br>
right:右侧不允许有浮动元素<br>
left:左侧不允许有浮动元素<br>
both:两侧不允许有浮动元素<br>
none:</p>
<p>解决塌陷问题方案:<br>
方案一:增加父级元素的高度;<br>
方案二:增加一个空的div标签,清除浮动</p>
<pre><code class="language-css">&lt;div class = "clear"&gt;&lt;/div&gt;
&lt;style&gt;
        .clear{
                clear:both;
                margin:0;
                padding:0;
}
&lt;/style&gt;
</code></pre>
<p>方案三:在父级元素中增加一个overflow:hidden</p>
<pre><code class="language-css">overflow:hidden/*隐藏*/
overflow:scoll/*滚动*/
</code></pre>
<p>方案四:父类添加一个伪类:after</p>
<pre><code class="language-css">#father:after{
        content:'';
        display:block;
        clear:both;
}
</code></pre>
<p>小结:</p>
<p>浮动元素增加空div----》简单、代码尽量避免空div<br>
设置父元素的高度-----》简单,元素假设没有了固定的高度,就会超出<br>
overflow----》简单,下拉的一些场景避免使用<br>
父类添加一个伪类:after(推荐)----》写法稍微复杂,但是没有副作用,推荐使用</p>
<h2 id="display与float对比">display与float对比</h2>
<p>display:方向不可以控制<br>
float:浮动起来的话会脱离标准文档流,所以要解决父级边框塌陷的问题。</p>
<h1 id="定位">定位</h1>
<h2 id="相对定位">相对定位</h2>
<p>相对定位:positon:relstive;<br>
相对于原来的位置,进行指定的偏移,相对定位的话,它仍然在标准文档流中,原来的位置会被保留</p>
<pre><code class="language-css">top:-20px;
left:20px;
bottom:-10px;
right:20px;
</code></pre>
<h2 id="绝对定位-absolute">绝对定位-absolute</h2>
<p>定位:基于xxx定位,上下左右~<br>
1、没有父级元素定位的前提下,相对于浏览器定位<br>
2、假设父级元素存在定位,我们通常会相对于父级元素进行偏移<br>
3、在父级元素范围内移动<br>
总结:相对一父级或浏览器的位置,进行指定的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留</p>
<pre><code class="language-css">&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;style&gt;
      div{
            margin: 10px;
            padding: 5px;
            font-size: 12px;
            line-height: 25px;
      }
      #father{
            border: 1px solid #666;
            padding: 0;
            position: relative;
      }
      #first{
            background-color: #a13d30;
            border: 1px dashed #b27530;

      }
      #second{
            background-color: green;
            border: 1px dashed #0ece4f;
            position: absolute;
            right:30px;
            top:30px
      }
      #third{
            background-color: red;
            border: 1px dashed #ff1b87;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id = "father"&gt;
    &lt;div id="first"&gt;第一个盒子&lt;/div&gt;
    &lt;div id="second"&gt;第二个盒子&lt;/div&gt;
    &lt;div id="third"&gt;第三个盒子&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h2 id="固定定位-fixed">固定定位-fixed</h2>
<pre><code class="language-css">&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;style&gt;
      body{
            height: 1000px;
      }
         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;
      }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;div&gt;div1&lt;/div&gt;
&lt;div&gt;div2&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

</code></pre>
<h2 id="z-index">z-index</h2>
<p><img src="https://img-blog.csdnimg.cn/2020050618393791.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3Bhbl9oMTk5NQ==,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述" loading="lazy"></p>
<p>图层~<br>
z-index:默认是0,最高无限~999</p>
<pre><code class="language-css">&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;link rel="stylesheet" href="css/style.css"&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;div id="content"&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;img src="images/bg.jpg" alt=""&gt;&lt;/li&gt;
      &lt;li class="tipText"&gt;学习微服务,找狂神&lt;/li&gt;
      &lt;li class="tipBg"&gt;&lt;/li&gt;
      &lt;li&gt;时间:2099-01=01&lt;/li&gt;
      &lt;li&gt;地点:月球一号基地&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<pre><code class="language-css">#content{
    width: 380;
    padding: 0px;
    margin: 0px;
    overflow: hidden;
    font-size: 12px;
    line-height: 25px;
    border: 1px solid yellow;
}
ul,li{
    padding: 0px;
    margin: 0px;
    list-style: none;
}
/*父级元素相对定位*/
#content ul{
    position: relative;
}
.tipText,.tipBg{
    position: absolute;
    width: 380px;
    height: 25px;
    top:216px
}
.tipText{
    color: white;
    z-index: 999;
}
.tipBg{
    background: orange;
    opacity: 0.5;/*背景透明度*/
    filter: alpha(opacity=50);
}
</code></pre><br><br>
来源:https://www.cnblogs.com/leeSeeN/p/14979170.html
頁: [1]
查看完整版本: CSS学习笔记