CSS Basic

What is CSS?

  • stands for Cascading Style Sheets (层叠样式表)
  • defines how to display HTML elements (定义如何显示HTML元素). 

HTML was never intended to contain tags for formatting a document, but to define the content of a document, HTML从来没有打算包含一些标签用于定义文档的格式。

CSS Syntax:

A CSS rule set consists of a selector  (指向你希望样式化的HTML元素) and a declaration block (包含一个或多个由分号分隔开的声明). 每一个声明包含 a property name (性质名称) and a value (值), 并由冒号隔开.

Example:

Comments:

CSS  comment starts with /* and ends with*/

HTML comment start with <!-- and ends with-->

JavaScript single line comments start with //.

And a JavaScript multiline comments start with /* and end with*/.

CSS Selectors

CSS selectors are used to "find" (or select) HTML elements : 

CSS选择器用于查找或选择HTML元素,可以是以下几种:

1. element Selector : selects elements based on the element name.

2. id Selector: 针对一个特别的元素定义一个特别的样式. (如果你想要选择一个唯一的元素,那么确保 id 在页面中是唯一存在的。)

3. class Selector: uses the HTML class attribute. 使用该选择器用于一组元素。

To minimize the code, you can group selectors 选择器分组, separate each selector with a comma. 

用逗号隔开、组合拥有相同CSS代码的HTML元素,以简化代码。

注意:id、class名字不要以 numer 数字开头。

Three Ways to Insert CSS

常见的三种引入 CSS 的方法:

1. External style sheet (外部样式表): 使用<link>元素,定义在页面<head>区域,样式保持在以 .css 为后缀的文件中。Is ideal when the style is applied to many pages. 注意: The type attribute is not necessary in HTML5

<head>
<link rel="stylesheet" href="mystyle.css">
</head>

2. Internal style sheet (内部样式表): 使用 <style> 元素,定义在页面<head>区域, define a style for one HTML page.

<head>
<style>
hr {color:sienna;}
p {margin-left:20px;}
body {background-image:url("images/background.gif");}
</style>
</head>

3. Inline Styling (行内样式): 使用style attribute,针对一个HTML元素应用唯一的样式。小心使用,不然失去了样式表的优势(把内容与表现混合在一起了)

<p style="color:sienna;margin-left:20px;">This is a paragraph.</p>

Cascading Order

CSS样式表层叠优先级。

Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority: 

当我们对同一个HTML元素进行多次CSS样式定义,这些样式将层叠起来就像形成一个新的虚拟的样式表。层叠规则如下,数字越大优先级越高

  1. Browser default
  2. External style sheet and Internal style sheet (in the head section)注意:外部样式表和内部样式表在头部书写的顺序也会影响优先级。
  3. Inline style (inside an HTML element)

Tip:css文件中,对同一个HTML元素同时使用多个class时,class选择器写在 越后面层叠优先级越高

.a1{color:red;}
.a2{color:green;}
.a3{color:blue;}
<p class="a1 a2 a3">test</p>
<!--文字颜色最终为blue-->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值