css学习心得

一·css生效

新建如下内容的一个 HTML文件(后缀为.html):

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <!-- 注意下面这个语句,将导入外部的 style.css 样式表文件 -->
  <link rel="stylesheet" type="text/css" href="style.css">
  <title>页面标题</title>
</head>
<body>
  <h1>喜羊羊与灰太狼</h1>
  <hr>
  <p class="xyy">喜羊羊</p>
  <p class="htl">灰太狼</p>
</body>
</html>

在同一目录新建一个样式表文件style.css(注意后缀名为css)如下

body {
  background-color: linen;
  text-align: center;
}
h1 {
  color: rgb(11, 146, 52);
}
.xyy {
  margin-top: 100px;
  color: rgb(30, 93, 210);
  font-size: 50px;
}
.htl{
  margin-top: 100px;
  color: rgb(60, 73, 97);
  font-size: 50px; 
}

得到了如下的页面
在这里插入图片描述

我们也可以将其放在一个文件里

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <!-- 注意下面这个语句,将导入外部的 style.css 样式表文件 -->
  <link rel="stylesheet" type="text/css" href="style.css">
  <title>页面标题</title>
  <style>
    body {
      background-color: rgb(219, 182, 146);
      text-align: center;
    }
    h1 {
      color: red;
    }
    .haha {
      margin-top: 100px;
      color: chocolate;
      font-size: 50px;
    }
  </style>
</head>
<body>
  <h1>喜羊羊</h1>
  <hr>
  <p class="haha">灰太狼</p>
</body>
</html>

在这里插入图片描述
得到这个界面

二·盒子模型

盒子模型指的是一个 HTML 元素可以看作一个盒子。从内到外,这个盒子是由内容 content, 内边距 padding, 边框 border, 外边距 margin构成的,如下图所示:

<html>
  <head>
    <link rel="stylesheet" href="./style.css">
  </head>
  <body>
    <div class="box1">我是内容一,外面红色的是我的边框。注意边框的内外都有25px的距离。</div>
    <div class="box2">我是内容二,外面蓝色的是我的边框。注意与上面元素的外边距,发生了叠加,不是50px而是25px。</div>
  </body>
</html>

再建立style.css

.box1 {
  height: 200px;
  width: 200px;
  background-color:#615200;
  color: aliceblue;
  border: 10px solid red;
  padding: 25px;
  margin: 25px;
}
.box2 {
  height: 300px;
  width: 300px;
  background-color:#004d61;
  color: aliceblue;
  border: 10px solid blue;
  padding: 25px;
  margin: 25px;
}

运行过后得下面的结果
在这里插入图片描述
右键点击空白界面,点击审查,可以看到布局
在这里插入图片描述

三·边框

在这里插入图片描述
在这里插入图片描述
输入上面的代码,得到下面的界面
在这里插入图片描述
有四个形状不同的边框

四·定位

position属性用于对元素进行定位。该属性有以下一些值:

static 静态
relative 相对
fixed 固定
absolute 绝对
设置了元素的position属性后,我们才能使用top, bottom, left, right属性,否则定位无效。

static
设置为静态定位position: static;,这是元素的默认定位方式,也即你设置与否,元素都将按正常的页面布局进行。
即:按照元素在 HTML出现的先后顺序从上到下,从左到右进行元素的安排。
在这里插入图片描述
在这里插入图片描述
得到以下界面
在这里插入图片描述

当我们删除他的款式,就得到了以下界面,可以看到区别,原来的文字从头开始,上面的颜色也变了
在这里插入图片描述
fixed
设置为固定定位position: fixed;,这将使得元素固定不动(即使你上下左右拖动浏览器的滚动条)。
此时元素固定的位置仍由top, bottom, left, right属性确定,但相对的是视口(viewport,就是浏览器的屏幕可见区域)
如下的代码将会在浏览器右下角固定放置一个按钮元素:
在这里插入图片描述
在这里插入图片描述
我们可以得到如下界面
在这里插入图片描述
我们改变界面的大小,发现右下角的按钮位置没有改变
在这里插入图片描述
absolute
设置为绝对定位position: absolute;,将使元素相对于其最近设置了定位属性(非static)的父元素进行偏移。
如果该元素的所有父元素都没有设置定位属性,那么就相对于这个父元素。

<div class="example-relative">这是父元素,有 relative 定位属性
  <div class="example-absolute">
    这是子元素,有 absolute 定位属性
  </div>
</div>
.example-relative {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid rgb(87, 33, 173);
}
.example-absolute {
  position: absolute;
  top: 80px;
  right: 5px;
  width: 200px;
  height: 100px;
  border: 3px solid rgb(218, 73, 16);
}

在这里插入图片描述
在这里插入图片描述

五·溢出

当元素内容超过其指定的区域时,我们通过溢出overflow属性来处理这些溢出的部分。
溢出属性有一下几个值:

visible 默认值,溢出部分不被裁剪,在区域外面显示
在这里插入图片描述

hidden 裁剪溢出部分且不可见
在这里插入图片描述

scroll 裁剪溢出部分,但提供上下和左右滚动条供显示

<!-- HTML -->
<div class="example-overflow-scroll-y">You can use the overflow property when you want to have better control of the
    layout. The overflow property specifies what happens if content overflows an element's box.
</div>
<!-- CSS -->
.example-overflow-scroll-y {
  width: 200px;
  height: 100px;
  background-color: #eee;
  overflow-y: scroll;
}

在这里插入图片描述
auto 裁剪溢出部分,视情况提供滚动条
在这里插入图片描述

六·浮动

在一个区域或容器内,我们可以设置float属性让某元素水平方向上向左或右进行移动,其周围的元素也会重新排列,我们常用这种样式来使图像和文本进行合理布局。

<html>
<head>
  <style>
    .example-float-right {
      float: right;
    }
  </style>
</head>
<body>
  <img src="zhi.jpg" class="example-float-right" alt="">
  <p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quidem, architecto officiis, repellendus
  corporis obcaecati, et commodi quam vitae vel laudantium omnis incidunt repellat qui eveniet fugiat totam
  modi nam vero!</p>
</body>
</html>

在这里插入图片描述
我们可以看到由于图片过大,得不到我们想要的结果,所以我们再选定图片的时候,需要注意他的大小。
在这里插入图片描述

七·不透明度

我们可以用opacity对任何元素(不过常用于图片)设置不透明度。
值在[0.0~1.0]之间,值越低,透明度越高

<html>
<head>
  <style>
    img {
      width: 25%;
      border-radius: 10px;
      float: left;
      margin: 10px;
    }
    .opacity-2 {
      opacity: 0.2;
    }
    .opacity-5 {
      opacity: 0.5;
    }
    .opacity-10 {
      opacity: 1;
    }
  </style>
</head>
<body>
  <img class="opacity-2" src="zhi.jpg">
  <img class="opacity-5" src="zhi.jpg">
  <img class="opacity-10" src="zhi.jpg">
</body>
</html>

在这里插入图片描述

八·组合选择器

后代选择器

以空格作为分隔,如:.haha p 代表在div元素内有.haha这种类的所有元素
在这里插入图片描述

<html>
<head>
  <style>
    .haha p {
      background-color: rgb(217, 146, 226);
    }
  </style>
</head>
<body>
  <div class="haha">
    <p>Paragraph 1 in the div .haha.</p>
    <p>Paragraph 2 in the div .haha>.</p>
    <span>
        <p>Paragraph 3 in the div .haha.</p>
    </span>
  </div>
  <p>Paragraph 4. Not in a div .haha.</p>
  <p>Paragraph 5. Not in a div .haha.</p>
</body>
</html>

子选择器

也称为直接后代选择器,以>作为分隔,如:.haha > p 代表在有.haha类的元素内的直接< p>元素

<html>
<head>
  <style>
    .haha > p {
      background-color: yellow;
    }
  </style>
</head>
<body>
  <div class="haha">
    <p>Paragraph 1 in the div .haha.</p>
    <p>Paragraph 2 in the div .haha.</p>
    <span>
        <p>Paragraph 3 in the div .haha - it is descendant but not immediate child.</p>
    </span> <!-- not Child but Descendant -->
  </div>
  <p>Paragraph 4. Not in a div .haha.</p>
  <p>Paragraph 5. Not in a div .haha.</p>
</body>
</html>

在这里插入图片描述

九·伪类和伪元素

伪类(pseudo-class)或伪元素(pseudo-element)用于定义元素的某种特定的状态或位置等。
比如我们可能有这样的需求:

鼠标移到某元素上变换背景颜色
超链接访问前后访问后样式不同
离开必须填写的输入框时出现红色的外框进行警示
保证段落的第一行加粗,其它正常

a:link {color:#FF0000;}     /* 未访问的链接 */
a:visited {color:#00FF00;}  /* 已访问的链接 */
a:hover {color:#FF00FF;}    /* 鼠标划过链接 */
/* 鼠标移到段落则改变背景颜色 */
p:hover {background-color: rgb(226, 43, 144);}
p:first-line{color:blue;}   /* 段落的第一行显示蓝色 */
p:first-letter{font-size: xx-large;}   /* 段落的第一个字超大 */

h1:before { content:url(smiley.gif); } /* 在每个一级标题前插入该图片 */
h1:after { content:url(smiley.gif); } /* 在每个一级标题后插入该图片 */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值