Web技术之CSS

一、CSS概述

1、一条CSS样式规则由两个主要的部分构成:选择器,以{}包裹的一条或多条声明,其中:
①选择器是您需要改变样式的对象。
②每条声明由一个属性和一个值组成。(无论是一条或多条声明,都需要用{}包裹,且声明用;分割)
③属性(property)是您希望设置的样式属性(style attribute)。每个属性有一个值。属性和值被冒号分开。
2、选择器包括:id选择器和class选择器
①id选择器:
②class选择器:

二、CSS简单应用

1、外部样式表

HTML代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <!-- 注意下面这个语句,将导入外部的 mycss.css 样式表文件 -->
  <link rel="stylesheet" type="text/css" href="mycss.css">
  <title>页面标题</title>
</head>
<body>
  <h1>我是有样式的</h1>
  <hr>
  <p class="haha">还是有点丑:)</p>
</body>
</html>

CSS代码:

body {  
  background-color:
  linen;    text-align: center;
 } 
 h1 {  
   color: blue;  } 
  .haha {  
  margin-top: 100px;  
  color: yellowgreen;  
  font-size: 50px;  }

在这里插入图片描述

2、颜色

色彩的应用在我的页面中必然是经过我的千挑万选深思熟虑,所以一定要了解颜色名称或者颜色RGB16进制值,如:

<!-- 颜色名称 -->
<h3 style="background-color:Tomato;">Tomato</h3>
<h3 style="background-color:Orange;">Orange</h3>
<h3 style="background-color:DodgerBlue;">DodgerBlue</h3>
<h3 style="background-color:MediumSeaGreen;">MediumSeaGreen</h3>
<h3 style="background-color:Gray;">Gray</h3>
<h3 style="background-color:SlateBlue;">SlateBlue</h3>
<h3 style="background-color:Violet;">Violet</h3>
<h3 style="background-color:LightGray;">LightGray</h3>
<hr>
<!-- 颜色值,3个字节分别代表RGB(Red,Green,Blue)的0~255的值 -->
<h3 style="background-color:#ff0000;">#ff0000</h3>
<h3 style="background-color:#0000ff;">#0000ff</h3>
<h3 style="background-color:#3cb371;">#3cb371</h3>
<h3 style="background-color:#ee82ee;">#ee82ee</h3>
<h3 style="background-color:#ffa500;">#ffa500</h3>
<h3 style="background-color:#6a5acd;">#6a5acd</h3>
<!-- 文本颜色 -->
<h3 style="color:Tomato;">Hello World</h3>
<p style="color:DodgerBlue;">Lorem ipsum dolor sit, amet consectetur adipisicing elit.</p>
<p style="color:MediumSeaGreen;">Ad facilis est ducimus rem consectetur, corporis omnis, eveniet esse dolor molestiae numquam odio corrupti, sed obcaecati praesentium accusamus? Tempora, dolor a?</p>

3、尺寸

HTML代码:

<html> 
<head>   
<link rel="stylesheet" href="./mycss.css"> 
</head> 
<body>   
<div class="example-1">      
这个元素高 200 pixels,占据全部宽度   
</div>    <div class="example-2">    
这个元素宽200像素,高300像素   
</div>
</body>
</html>

CSS代码

.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;
}

在这里插入图片描述

三、CSS的重点应用

1、盒子模型

HTML代码:

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

CSS代码:

.box1 {
  height: 200px;
  width: 200px;
background-color:#ee4ed3;
  color: aliceblue;
  border: 10px solid rgb(97, 14, 76);
  padding: 50px;
  margin: 50px;
}
.box2 {
  height: 300px;
  width: 300px;
  background-color:#075669;
  color: aliceblue;
  border: 10px solid rgb(10, 56, 87);
  padding: 50px;
  margin: 50px;
}

在这里插入图片描述
进行审查,可以看到布局参数
进行审查,可以看到布局参数

2、边框

HTML代码:

<html>
  <head>
    <link rel="stylesheet" href="./mycss.css">
  </head>
  <body>
<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>
  </body>
</html>

CSS代码:

.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;
}

3、溢出

HTML代码:

<!DOCTYPE html>
<html>    
<head>    
<link rel="stylesheet" href="./mycss.css">     
</head>
<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>
</html>

CSS代码:

.example-overflow-scroll-y {
  width: 200px;
  height: 100px;
  background-color: #eee;
  overflow-y: scroll;
}

在这里插入图片描述

4、浮动

浮动前浮动前
HTML代码:

<html>
<head>
  <style>
    .example-float-right {
      float: right;
    }
  </style>
</head>
<body>
  <img src="tx1.jpeg" alt="MDB Logo" width="100" height="200"class="example-float-right" alt="">  
  <img src="tx2.jpeg" alt="MDB Logo" width="100" height="200"class="example-float-right" alt=""> 
   <img src="tx3.jpeg" alt="MDB Logo" width="100" height="200"class="example-float-right" alt="">  
   <img src="tx4.jpeg" alt="MDB Logo" width="100" height="200"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>

浮动后右浮动后

5、不透明度

HTML代码:

<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="https://mdbootstrap.com/img/Photos/Horizontal/Nature/4-col/img%20(87).jpg">
  <img class="opacity-5" src="https://mdbootstrap.com/img/Photos/Horizontal/Nature/4-col/img%20(87).jpg">
  <img class="opacity-10" src="https://mdbootstrap.com/img/Photos/Horizontal/Nature/4-col/img%20(87).jpg">
</body>
</html>

在这里插入图片描述

四、小结

相比于HTML,CSS的操作更加精细复杂,同时也能使页面更加美观,我因为学习做网页页面对这个专业增加了些许好感,感动!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值