CSS
基本用法
<head>
<style>
选择器{
属性名:属性值;
属性名:属性值;
}
</style>
</head>
- 选择器:要修饰的对象(东西)
- 属性名:修饰对象的哪一个属性(样式)
- 属性值:样式的取值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
p{
color:red;
background: #cccccc;
}
h2{
color:blue;
}
</style>
</head>
<body>
<p>CSS从入门到精通</p>
<h2>主讲:hector</h2>
</body>
</html>
2.1 内部样式
也称为内嵌样式,在页面头部通过style标签定义
对当前页面中所有符合样式选择器的标签都起作用
2.2 行内样式
也称为嵌入样式,使用HTML标签的style属性定义
只对设置style属性的标签起作用
2.3 外部样式
使用单独的 .CSS 文件定义,然后在页面中使用 link标签 或 @import指令引入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
/* 1.内部样式 */
p{
color:blue;
}
</style>
<!-- link链接外部样式文件 -->
<!-- <link rel="stylesheet" type="text/css" href="style/hello.css"> -->
<!-- 3.import导入外部样式文件 -->
<style>
/* @import "style/hello.css" */
@import url(style/hello.css);
</style>
</head>
<body>
<p>welcome to CSS!</p>
<p>欢迎来到CSS课堂!</p>
<hr>
<h2 style="color:red;">WEB前段工程师</h2>
<h2>JAVA开发工程师</h2>
<hr>
<div>嘿嘿</div>
<div>哈哈</div>
</body>
</html>
选择器
1.标签选择器
2.类选择器
3.ID选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
p{
color:red;
font-size:20px;
}
h2{
color:yellow;
}
.hello{
background: #cccccc;
}
.world{
font-weight:bold;
}
#haha{
color:blue;
}
</style>
</head>
<body>
<p>welcome to css!</p>
<p>hello world!</p>
<h2>WEB前端开发</h2>
<h3>Java开发</h3>
<hr>
<p class="hello">welcome to css!</p>
<p>hello world!</p>
<h2>WEB前端开发</h2>
<h3>Java开发</h3>
<div class="hello">主讲:Hector</div>
<div class="world">主讲:Hector</div>
<hr>
<h1 id="haha">哈哈</h1>
</body>
</html>
选择器优先级
优先级
行内样式>ID选择器>类选择器>标签选择器
原因:首先加载标签选择器,再加载类选择器,然后加载ID选择器,最后加载行内样式
后加载会覆盖先加载的同名样式
内外部样式加载顺
就近原则
原因:按照书写顺序依次加载,在同优先级的前提下,后加载的会覆盖先加载的同名样式,所以离的越近
越优先
常用属性
font-size
font-weight
font-family
font-style
color
line-height
盒子模型
属性
- width 宽度
- height 高度
- border 边框
- padding 内边距
- margin 外边距
border
表示盒子的边框
分为四个方向:
上top、右right、下bottom、左left
border-top、border-right、border-bottom、border-left
每个边框包含三种样式:
border-top-color、border-top-width、border-top-style
border-right-color、border-right-width、border-right-style
border-bottom-color、border-bottom-width、border-bottom-style
border-left-color、border-left-width、border-left-style
样式style的取值:
solid实线、dashed虚线、dotted点线、double双线、inset内嵌的3D线、outset外嵌的3D线
简写,三种方式:
按方向简写:
border-top、border-right、border-bottom、border-left
书写顺序:
border-顺序:width style color
按样式简写:
border-color、border-width、border-style
书写顺序:
border-样式:top right bottom left
必须按顺时针方向书写,同时可以缩写:
border-width:2px;--------->四个边框的宽度均为2px
border-width:1px 2px;
border-width:1px 2px 4px;
规则:如果省略,则认为上下一样,左右一样
终级简写:
如果四个边框样式完全相同,border:width style color;
padding
表示盒子的内边距,即内容与边框之间的距离
margin
表示盒子的外边距,即盒子与盒子之间的距离

浙公网安备 33010602011771号