Web应用基础CSS入门学习总结

Web应用基础CSS入门学习总结

CSS是级联样式表,CSS将决定页面内容的展现方式(如页面布局、元素位置、距离、颜色、大小、是否显示、是否浮动、透明度等)

CSS如何生效

1.外部样式表
直接在HTML文本的<head>元素中插入<link>标签来引入CSS文件。

  <link rel="stylesheet" type="text/css" href="mycss.css">

然后在mycss.css文件中写入样式。

2.内部样式表
在HTML文件的<head>元素中插入<style>标签,在<style>元素中规定样式属性。

   <style>
    body {
      background-color: linen;
      text-align: center;
    }
    h1 {
      color: red;
    }
    .haha {
      margin-top: 100px;
      color: chocolate;
      font-size: 50px;
    }
  </style>

3.内联样式
直接把样式规则写到HTML的元素中。

   <h3 style="color:green;">I am a heading</h3>

CSS语法

1.组成部分

  • 选择器
    选择器是将被改变样式的对象
  • {}包裹的声明
    当有多条声明时,用;分隔开;
    每条声明由属性和值组成(property:value),属性是被设置对象的样式属性。

    2.CSS的注释方式
    /* */:此符号中的文字将不会对网页有任何影响。
    样式优先级:内联样式>内部样式表或外部样式表>浏览器缺省样式。

CSS的选择器

1.HTML元素选择器
如下所示:

p{
  color:red;
  text-align:center;  /* 文本居中 */
}

这个例子中,HTML元素<p>作为选择器,表明网页中所有属于<p>元素的对象都使用红色字体,并且文本居中。

2.id选择器
如下所示:

#sky{
  color: blue;
}
#foreat{
  color: green;
}
<p id="sky">蓝色的天空</p>
<p id="forest">绿色的森林</p>

这个例子中,idsky的元素呈现蓝色,idforest的元素呈现绿色。
id选择器前有#号。

3.class选择器
如下所示:

类名之前要加上.

.center{
  text-align: center;
}
.large{
  font-size: 30px;
}
.red{
  color: red;
}
<p class="center">我会居中显示的</p>
<p class="red">我是红色的</p>
<p class="center large red">我又红又大还居中</p>
<p class="red">我也可以是红的</p>

这个例子中,属于center类的文本将居中显示。

4.组合选择器

  • 后代选择器
    如:
<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.</p>
    </span>
  </div>
  <p>Paragraph 4. Not in a div .haha.</p>
  <p>Paragraph 5. Not in a div .haha.</p>
</body>
</html>

.hahap之间用空格分隔开。属于haha类的元素内的p元素将呈现黄色背景。
段落1、2、3都将有黄色的背景,而段落4、5没有。

  • 子选择器
    如:
<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>

.hahap之间用>连接。虽然段落3在.haha类中,但它的直接父元素是span,不是.haha的直接后代,所以不能选择。只有段落1、2有黄色背景。

颜色

可以采用颜色名称也可以使用颜色RGB16进制值来设定颜色
1.背景颜色

<h3 style="background-color:Tomato;">Tomato</h3>

这个三级标题的背景将被设置为Tomato色。

<h3 style="background-color:#ff0000;">#ff0000</h3>

这个三级标题背景将被设计为红色。

2.文本颜色

<h3 style="color:Tomato;">Hello World</h3>

这个三级标题的文本将被设置为Tomato色。

尺寸

heightwidth 设定元素内容占据的尺寸。常见的尺寸单位有:像数 px,百分比 %等。

.example-1 {
  width: 100%;
  height: 200px;
  background-color: powderblue;
  text-align: center;
}
.example-2 {
  height: 100px;
  width: 500px;
  background-color: rgb(73, 138, 60);
  text-align: right;
}

对齐

设置text-align="left"样式表示左对齐;text-align="right"右对齐;text-align="center"居中显示。

盒子模型

盒子模型
一个HTML元素可以看成是一个盒子。

  • Content是元素内容。
  • Padding,内边距,内容和边宽之间的空隙。
  • Border是盒子的边框,默认不显示。
  • Margin,外边距,本元素与其他元素之间的区域。

创建一个CSS文件(这里命名为mycss.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;
}
<html>
 <head>
    <link rel="stylesheet" href="./mycss.css">
  </head>
  <body>
    <div class="box1">我是内容一,外面红色的是我的边框。注意边框的内外都有25px的距离。</div>
    <div class="box2">我是内容二,外面蓝色的是我的边框。注意与上面元素的外边距,发生了叠加,不是50px而是25px。</div>
  </body>
</html>

注:用 width属性设置元素的宽度时,实际上只设置了其内容的宽度

接下来我们来试试设置元素的边框和边距的样式。

.example-1 {
  border: 1px dotted black; /* 上下左右都相同 */
}
.example-2 {
  border-bottom: 1px solid blue; /* 只设置底部边框 */
}
.example-3 {
  border: 1px solid grey;
  border-radius: 15px; /* 边框圆角 */
}
.example-4 {
  border-left: 5px solid purple;
}
<p class="example-1">I have black borders on all sides.</p>
<p class="example-2">I have a blue bottom border.</p>
<p class="example-3">I have rounded grey borders.</p>
<p class="example-4">I have a purple left border.</p>

定位

定位要用到position属性。

  • position的值为static时,是静态定位(元素默认定位方式)。元素将按照在HTML出现的先后顺序进行显示。
  • position的值为raletive时,元素将相对于它静态时的位置进行偏移。
    如:
.example-relative {
  position: relative;
  left: 60px;
  top: 40px;
  background-color: rgb(173, 241, 241);
}
<div class="example-relative">我偏移了正常显示的位置。去掉我的样式对比看看?</div>
  • 设置为固定定位position: fixed,元素将相对视口固定不变。也就是说,不论怎样拖动浏览器滚动条,元素位置一直固定在浏览器屏幕可见区域的某一处。
    如:
.example-fixed {
  position: fixed;
  bottom: 40px;
  right: 10px;
  padding: 6px 24px;
  border-radius: 4px;
  color: #fff;
  background-color: #9d0f0f;
  cursor: pointer;
  box-shadow: 0 3px 3px 0 rgba(0,0,0,0.3), 0 1px 5px 0 rgba(0,0,0,0.12), 0 3px 1px -2px rgba(0,0,0,0.2);
}
.broad {
  height: 5000px;
  width: 5000px;
  padding: 20px;
  background-color: darkkhaki;
}
<div class="broad">占位区域。请将浏览器窗口改变大小,看看右下角的按钮发生了什么?</div>
<div class="example-fixed">这个按钮是固定的</div>
  • 设置为绝对定位position: absolute,将使元素相对于其最近设置了定位属性(非static)的父元素进行偏移。
    如:
.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);
}
<div class="example-relative">这是父元素,有 relative 定位属性
  <div class="example-absolute">
    这是子元素,有 absolute 定位属性
  </div>
</div>

溢出

通过溢出overflow属性来处理溢出的部分。overflow属性有以下几个属性值:

  • visible:默认值。内容不会被修剪,会呈现在元素框之外。
  • hidden :内容会被修剪,并且其余内容是不可见的。
  • scroll:内容会被修剪,但是浏览器会显示滚动条以便查看其余的内容。
  • auto:如果内容被修剪,则浏览器会显示滚动条以便查看其余的内容。
  • inherit:规定应该从父元素继承 overflow 属性的值。

浮动

float属性让某元素水平方向上向左或右进行移动,其周围的元素也会重新排列。
如果希望浮动元素后面的元素在其下方显示,可使用clear: both样式来进行清除。

透明度

opacity对元素设置不透明度,值在[0.0~1.0]之间,值越低,透明度越高。

.opacity-2 {
      opacity: 0.2;
    }
.opacity-5 {
      opacity: 0.5;
    }
 .opacity-10 {
      opacity: 1;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值