示:文章尾部有全部源码,大家可以CTRL + C直接拿走,不用谢我;当然,我还是建议大家都能通过学习自己手写实现。
使用过CSS transition属性的童鞋们应该都清楚,当元素在过渡开始或者结束时的高度为auto时,动画是不生效的;那么如何才能实现元素高度的改变动画效果呢?本篇文章将为大家提供一个基于Vue3的非常简洁的解决方案。
要实现高度的改变动画,我们需要在动画进行之前为元素设置准确的高度。
当元素由可见变为隐藏状态时,我们需要先获取元素的计算高度,将其设置到style属性上,然后执行一个触发浏览器渲染引擎重绘的动作,然后再将高度设置为0,这样高度的改变动画就会正常进行。
当元素由隐藏变为可见状态时,我们需要先将高度设置为auto,然后获取元素的计算高度,再将高度设置为0,然后执行一个触发浏览器渲染引擎重绘的动作,然后再将高度设置为计算高度,这样高度的改变动画就会正常进行。
现在,根据以上实现原理分析,我们创建一个高度的改变动画通用组件CollapseTransition.vue。该组件非常简单,仅需30多行代码。我几乎每行代码都有注释,大家应该能看懂吧?
<template>
<transition v-bind="listeners">
<!-- 当visible的值发生改变时,过渡组件的监听器就会触发 -->
<div v-show="visible" class="x-collapse-transition">
<slot />
</div>
</transition>
</template>
<script setup>
defineProps({ visible: Boolean })
const listeners={
// 元素由隐藏变为可见
onEnter (/** @type {HTMLElement} */ el) {
el.style.height='auto' // 将高度设为auto,是为了获取该元素的计算高度
const endHeight=window.getComputedStyle(el).height // 计算高度
el.style.height='0px' // 将高度再设置为0
el.offsetHeight // 强制重绘,重绘后再改变高度才会产生动画
el.style.height=endHeight // 设置为计算高度
},
onAfterEnter (/** @type {HTMLElement} */ el) {
el.style.height=null // 过渡进入之后,将高度恢复为null
},
// 元素由可见变为隐藏
onLeave (/** @type {HTMLElement} */ el) {
el.style.height=window.getComputedStyle(el).height // 计算高度
el.offsetHeight // 强制重绘,重绘后再改变高度才会产生动画
el.style.height='0px' // 将高度设置为0
},
onAfterLeave (/** @type {HTMLElement} */ el) {
el.style.height=null // 过渡离开之后,将高度恢复为null
}
}
</script>
<style lang="scss">
.x-collapse-transition {
overflow: hidden;
transition: height .22s ease-in-out;
}
</style>
以上就是实现高度的改变动画的通用组件源码,童鞋们理解了吗?是不是非常简单?现在,我们实现折叠面板组件。使用过element-ui这样的UI库的童鞋们应该都知道,折叠面板是由两个组件折叠面板Collapse和折叠面板项CollapseItem组合而成;
现在,我们先实现CollapseItem.vue组件。为了节省篇幅,我将源码中的空行全部去掉了,缩进比较规范,自认为可读性还行;源码如下,一共30多行,我直接在源码中添加了注释,就不过多解释了。
<template>
<div :class="[cls, { 'is-active': isActive }]">
<div :class="`${cls}_header`" @click="onToggle">
<div :class="`${cls}_title`">
<slot name="title">{{ title }}</slot>
</div>
<i :class="['x-icon-arrow-right', `${cls}_arrow`, { 'is-active': isActive }]"></i>
</div>
<x-collapse-transition :visible="isActive">
<div :class="`${cls}_content`">
<slot />
</div>
</x-collapse-transition>
</div>
</template>
<script setup>
import { computed, inject } from 'vue'
import { COLLAPSE_INJECT_KEY } from '../../constants' // 当它是个字符串常量就行
import { N, S, B } from '../../types' // N: Number, S: String, B: Boolean
import { genKey } from '../../utils' // 生成唯一性的数值key,之前的文章中有源码
import XCollapseTransition from './CollapseTransition.vue' // 折叠动画组件
const props=defineProps({
name: [S, N],
title: S,
disabled: B
})
const collapse=inject(COLLAPSE_INJECT_KEY, '') // 注入折叠面板组件提供的一些属性和方法
const cls='x-collapse-item'
const idKey=computed(()=> props.name || genKey()) // 如果没提供name,我们生成一个key
const isActive=computed(()=> collapse.includes(idKey.value)) // 是否展开状态
function onToggle () { // 内容可见时隐藏,隐藏时可见
collapse.updateModel(idKey.value)
}
</script>
这是CollapseItem.vue组件的样式。
@import './common/var.scss';
.x-collapse-item {
font-size: 13px;
background-color: #fff;
color: $--color-text-primary;
border-bottom: 1px solid $--border-color-lighter;
&:first-child {
border-top: 1px solid $--border-color-lighter;
}
&_header {
display: flex;
align-items: center;
justify-content: space-between;
height: 48px;
cursor: pointer;
}
&_content {
line-height: 1.8;
padding-bottom: 25px;
}
&_arrow {
margin-right: 8px;
transition: transform .2s;
&.is-active {
transform: rotate(90deg);
}
}
}
现在,我们实现Collapse.vue组件。该组件仍然只有30多行,大家理解起来应该很轻松,我就直接在源码里添加注释作为讲解了;
<template>
<div class="x-collapse">
<slot />
</div>
</template>
<script setup>
import { provide, reactive, ref, watch } from 'vue'
import { COLLAPSE_INJECT_KEY } from '../../constants' // 一个字符串常量
import { S, B, A } from '../../types' // 参看CollapseItem组件
const props=defineProps({
modelValue: [S, A], // Vue3使用modelValue取代了Vue2中的value
accordion: B // 是否手风琴模式,手风琴模式只能有1个面板项是展开状态
})
const emit=defineEmits(['update:modelValue', 'change'])
function emitValue (v) {
emit('update:modelValue', v) // 与props.modelValue结合实现双向数据绑定
emit('change', v)
}
const model=ref(props.modelValue)
watch(()=> props.modelValue, v=> model.value=v)
provide(COLLAPSE_INJECT_KEY, { // 提供2个方法用于注入子组件,给子组件调用
includes (v) { // 根据面板的key,判断是否包含该面板项
return props.accordion ? model.value===v : (model.value || []).includes(v)
},
updateModel (v) { // 更新面板项的内容折叠和展开状态
const { value }=model
if (props.accordion) {
model.value=value===v ? null : v
emitValue(model.value)
} else {
if (!value) model.value=[]
const index=model.value.indexOf(v)
index > -1 ? model.value.splice(index, 1) : model.value.push(v)
emitValue(model.value)
}
}
})
</script>
以上就是折叠面板组件的实现。包括折叠动画组件,一共仅需不到150行代码,是不是非常简单?童鞋们都理解了吗?不管有什么疑问,童鞋们都可以问我。感谢阅读!
CSS 是什么?
CSS是Cascading Style Sheets的简称,中文称为层叠样式表。
属性和属性值用冒号隔开,以分号结尾。
CSS 四种引入方式:
1.行内式
行内式是在标签的style属性中设定CSS样式。
<div style="..."></div>
2.嵌入式
嵌入式是将CSS样式集中写在网页的<head>标签的<style></style>标签对中。
<head>
...
<style type="text/css">
...此处写CSS样式
</style>
</head>
3.导入式
将一个独立的.css文件引入HTML文件中,导入式使用@import 引入外部CSS文件,<style>标记也是写在<head>标记中。
导入式会在整个网页装载完后再装载CSS文件。
<head>
...
<style type="text/css">
@import "My.css"; 此处注意.css文件的路径
</style>
</head>
4.链接式
将一个独立的.css文件引入到HTML文件中,使用<link>标记写在<head>标记中。
链接式会以网页文件主体装载前装载CSS文件。
<head>
...
<link href="My.css" rel="stylesheet" type="text/css">
</head>
样式应用顺序:
.nick {
color: yellow !important;
}
基本选择器:
1.通用元素选择器
* 表示应用到所有的标签。
* {color: yellow}
2.标签选择器
匹配所有使用 div 标签的元素(可以匹配所有标签)
div {color: yellow}
3.类选择器
匹配所有class属性中包含info的元素。
语法:.类名{样式}(类名不能以数字开头,类名要区分大小写。)
.Mycolor {color: yellow}
<h3 class="Mycolor">nick</h3>
4.ID选择器
使用id属性来调用样式,在一个网页中id的值都是唯一的(是W3C规范而不是规则,所以不会报错)。
语法:#ID名{样式}(ID名不能以数字开头)
#Mycolor {color: yellow}
<h3 id="Mycolor">Nick.</h3>
组合选择器:
1.多元素选择器
同时匹配h3,h4标签,之间用逗号分隔。
h3,h4 {color: yellow;}
<h3>Nick</h3>
<h4>Jenny</h4>
2.后代元素选择器
匹配所有div标签里嵌套的P标签,之间用空格分隔。
div p {color: yellow;}
<div>
<p>Nick</p>
<div>
<p>Nick</p>
</div>
</div>
3.子元素选择器
匹配所有div标签里嵌套的子P标签,之间用>分隔。
div > p {color: yellow;}
<div>
<p>Nick</p>
<p>Nick</p>
</div>
4.毗邻元素选择器
匹配所有紧随div标签之后的同级标签P,之间用+分隔(只能匹配一个)。
div + p {color: yellow;}
<div>Nick</div>
<p>Nick</p>
属性选择器:
1.[title] & P[title]
设置所有具有title属性的标签元素;
设置所有具有title属性的P标签元素。
[title]
{
color: yellow;
}
p[title]
{
color: yellow;
}
<div title>Nick</div>
<p title>Nick</p>
2.[title=Nick]
设置所有title属性等于“Nick”的标签元素。
[title="Nick"]
{
color: yellow;
}
<p title="Nick">Nick</p>
3.[title~=Nick]
设置所有title属性具有多个空格分隔的值、其中一个值等于“Nick”的标签元素。
[title~="Nick"]
{
color: yellow;
}
<p title="Nick Jenny">Nick</p>
<p title="Jenny Nick">Nick</p>
4.[title|=Nick]
设置所有title属性具有多个连字号分隔(hyphen-separated)的值、其中一个值以"Nick"开头的标签元素。
例:lang属性:"en"、"en-us"、"en-gb"等等
[title|="Nick"]
{
color: yellow;
}
<p title="Nick-Jenny">Nick</p>
5.[title^=Nick]
设置属性值以指定值开头的每个标签元素。
[title^="Nick"]
{
color: yellow;
}
<p title="NickJenny">Nick</p>
6.[title$=Nick]
设置属性值以指定值结尾的每个标签元素。
[title$="Nick"]
{
color: yellow;
}
<p title="JennyNick">Nick</p>
7.[title*=Nick]
设置属性值中包含指定值的每个元素
[title*="Nick"]
{
color: yellow;
}
<p title="SNickJenny">Nick</p>
伪类选择器:
1. link、hover、active、visited
a:link{color: black}
a:hover{color: yellow}
a:active{color: blue}
a:visited{color: red}
<a href="#">Nick</a>
2. before、after
p {
color: yellow;
}
p:before{
content: "before...";
}
p:after{
content: "after...";
}
<p> Nick </p>
1. 颜色属性:
color
transparent
opacity
2. 字体属性:
font-style: 用于规定斜体文本
font-weight: 设置文本的粗细
font-size: 设置字体的大小
font-family:字体名称
font:简写属性
3. 文本属性:
white-space: 设置元素中空白的处理方式
direction: 规定文本的方向
text-align: 文本的水平对齐方式
line-height: 文本行高
vertical-align: 文本所在行高的垂直对齐方式
text-indent: 文本缩进
letter-spacing: 添加字母之间的空白
word-spacing: 添加每个单词之间的空白
text-transform: 属性控制文本的大小写
text-overflow: 文本溢出样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--<link href="cc2.css" rel="stylesheet" type="text/css">-->
<style>
div {
width: 100px;
height: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<div>索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁 索宁</div>
</body>
</html>
text-decoration: 文本的装饰
text-shadow:文本阴影
word-wrap:自动换行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
p {
width: 150px;
height: 160px;
background-color: #FFA500;
/*边框阴影*/
box-shadow: 10px 10px 5px #888;
/*自动换行*/
word-wrap: break-word;
}
h1 {
text-shadow: 5px 5px 5px #888;
}
</style>
</head>
<body>
<p>
When you are old and grey and full of sleep,And nodding by the fire, take down this book,And slowly read, and dream of the soft look
</p>
<h1>索宁</h1>
</body>
</html>
a {
text-decoration: none;
/*去除a标签下划线*/
}
4. 背景属性
background-color: 背景颜色
background-image 设置图像为背景
background-position 设置背景图像的位置坐标
background-repeat 设置背景图像不重复平铺
background-attachment 背景图像是否固定或者随着页面的其余部分滚动
background 简写
5. 列表属性
list-style-type: 列表项标志的类型
list-style-image:将图象设置为列表项标志
list-style-position:列表项标志的位置
list-style:缩写
1. 边框
border-style:边框样式
border-color:边框颜色
border-width:边框宽度
border-radius:圆角
border: 简写
box-shadow:边框阴影
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div {
border:2px solid;
border-radius:25px;
width: 140px;
}
</style>
</head>
<body>
<div>
点赞哦!dear.
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.radius1 {
display: inline-block;
width: 100px;
height: 100px;
background-color: yellow;
border-radius: 20px;
}
.radius2 {
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
border-radius: 20px 35px;
}
.radius3 {
display: inline-block;
width: 100px;
height: 100px;
background-color: blue;
border-radius: 20px 35px 50px;
}
.radius4 {
display: inline-block;
width: 100px;
height: 100px;
background-color: green;
border-radius: 20px 35px 50px 60px;
}
</style>
</head>
<body>
<div>
<span class="radius1"></span>
<span class="radius2"></span>
<span class="radius3"></span>
<span class="radius4"></span>
</div>
</body>
</html>
边框实现各种三角符号:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.triangle-one {
display: inline-block;
border-top: 50px red solid;
border-right: 50px green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-two {
display: inline-block;
border-top: 0 red solid;
border-right: 50px green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-stree {
display: inline-block;
border-top: 50px red solid;
border-right: 0 green solid;
border-bottom: 50px yellow solid;
border-left: 50px blue solid;
}
.triangle-four {
display: inline-block;
border-top: 50px red solid;
border-right: 0 green solid;
border-bottom: 0 yellow solid;
border-left: 50px blue solid;
}
.triangle-five {
display: inline-block;
border: 50px transparent solid;
border-top: 50px red solid;
}
.triangle-six {
display: inline-block;
border: 50px transparent solid;
border-bottom: 50px yellow solid;
}
.triangle-seven {
display: inline-block;
border: 50px transparent solid;
border-top: 60px red solid;
border-right: 0;
}
.triangle-eight {
display: inline-block;
border: 50px transparent solid;
border-left: 30px yellow solid;
border-bottom: 0;
}
</style>
</head>
<body>
<div class="triangle-one"></div>
<div class="triangle-two"></div>
<div class="triangle-stree"></div>
<div class="triangle-four"></div>
<div class="triangle-five"></div>
<div class="triangle-six"></div>
<div class="triangle-seven"></div>
<div class="triangle-eight"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.back {
width: 1000px;
height: 1000px;
margin: 0 auto;
background-color: #ddd;
position: relative;
}
.back-in {
position: absolute;
width: 1020px;
height: 45px;
left: -20px;
top: 50px;
background-color: #2F4F4F;
}
.back-img {
border: 20px solid transparent;
border-top: 10px solid dimgrey;
border-right: 0;
display: inline-block;
position: absolute;
top: 95px;
left: -20px;
}
.back-font {
line-height: 9px;
margin-left: 30px;
color: white;
}
</style>
</head>
<body>
<div class="back">
<div class="back-in"><h3 class="back-font">妹子求关注 ^.^</h3></div>
<div class="back-img"></div>
</div>
</body>
</html>
2.★ 盒子模型
一个标准的盒子模型:
padding:用于控制内容与边框之间的距离;
margin: 用于控制元素与元素之间的距离;
一个参数,应用于四边。
两个参数,第一个用于上、下,第二个用于左、右。
三个参数,第一个用于上,第二个用于左、右,第三个用于下。
边框在默认情况下会定位于浏览器窗口的左上角,但是并没有紧贴着浏览器的窗口的边框,这是因为body本身也是一个盒子,外层还有html,
在默认情况下,body距离html会有若干像素的margin,所以body中的盒子不会紧贴浏览器窗口的边框了。
解决方法:
body {
margin: 0;
}
3.★ display
4. visibility
5.★ float 浮动
让一行显示两个块级标签,会脱离文档流
clear 清除浮动:
6. clip 剪裁图像
rect 剪裁定位元素:
7. overflow 设置当对象的内容超过其指定高度及宽度时如何显示内容
8.★ position 规定元素的定位类型
9. z-index 元素层叠顺序
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.z-index1 {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
z-index: -1;
}
.z-index2 {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
top: 20px;
left: 20px;
z-index: 5;
}
</style>
</head>
<body>
<div class="z-index1"></div>
<div class="z-index2"></div>
</body>
</html>
10. outline 边框轮廓
11. zoom 缩放比例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.zoom1 {
zoom: 100%;
}
.zoom2 {
zoom: 150%;
}
.zoom3 {
zoom: 200%;
}
</style>
</head>
<body>
<div class="zoom1">Nick 100%</div>
<div class="zoom2">Nick 200%</div>
<div class="zoom3">Nick 300%</div>
</body>
</html>
12. cursor 鼠标的类型形状
鼠标放在以下单词上,There will be a miracle:
url: 自定义光标
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!--<link href="cc2.css" rel="stylesheet" type="text/css">-->
<style>
body {
cursor: url("mouse.png"), auto;
/*图片地址:http://images.cnblogs.com/cnblogs_com/suoning/845162/o_mouse.png*/
}
</style>
</head>
<body>
<div><img src="http://images.cnblogs.com/cnblogs_com/suoning/845162/o_ns.png" height="100%" width="100%"></div>
</body>
</html>
Auto: 默认
Default: 默认
e-resize
ne-resize
nw-resize
n-resize
se-resize
sw-resize
s-resize
w-resize
Crosshair
Pointer
Move
text
wait
help
not-allowed
13. transform、transition 动画效果
transform 转换,变形
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nick</title>
<meta charset="utf-8" />
<style type="text/css">
div {
border: 1px solid black;
height: 30px;
width: 30px;
background-color: yellow;
/*transform-origin: 50px 50px;*/
transform-origin: left;
transform: rotate(50deg);
/*transform: skew(50deg,50deg);*/
/*transform: translate(50px,50px);*/
/*transform: scale(2);*/
}
</style>
</head>
<body>
<div></div>
</body>
</html>
Transition 平滑过渡
#property 指定属性对应类型
1、color: 通过红、绿、蓝和透明度组件变换(每个数值单独处理),如:background-color,border-color,color,outline-color等CSS属性;
2、length:真实的数字,如:word-spacing,width,vertical- align,top,right,bottom,left,padding,outline-width,margin,min-width,min- height,max-width,max-height,line-height,height,border-width,border- spacing,background-position等属性;
3、percentage:真实的数字,如:word-spacing,width,vertical- align,top,right,bottom,left,min-width,min- height,max-width,max-height,line-height,height,background-position等属性;
4、integer 离散步骤(整个数字),在真实的数字空间,以及使用floor()转换为整数时发生,如:outline-offset,z-index等属性;
5、number真实的(浮点型)数值,如:zoom,opacity,font-weight等属性;
6、transform list。
7、rectangle:通过x、 y、 width和height(转为数值)变换,如:crop;
8、visibility:离散步骤,在0到1数字范围之内,0表示“隐藏”,1表示完全“显示”,如:visibility;
9、shadow:作用于color、x、y、和blur(模糊)属性,如:text-shadow;
10、gradient:通过每次停止时的位置和颜色进行变化。它们必须有相同的类型(放射状的或是线性的)和相同的停止数值以便执行动画,如:background-image;
11、paint server (SVG):只支持下面的情况:从gradient到gradient以及color到color,然后工作与上面类似;
12、space-separated list of above:如果列表有相同的项目数值,则列表每一项按照上面的规则进行变化,否则无变化;
13、a shorthand property:如果缩写的所有部分都可以实现动画,则会像所有单个属性变化一样变化。
#支持执行transition效果的属性
Property Name Type
background-color as color
background-position as repeatable list of simple list of length, percentage, or calc
border-bottom-color as color
border-bottom-width as length
border-left-color as color
border-left-width as length
border-right-color as color
border-right-width as length
border-spacing as simple list of length
border-top-color as color
border-top-width as length
bottom as length, percentage, or calc
clip as rectangle
color as color
font-size as length
font-weight as font weight
height as length, percentage, or calc
left as length, percentage, or calc
letter-spacing as length
line-height as either number or length
margin-bottom as length
margin-left as length
margin-right as length
margin-top as length
max-height as length, percentage, or calc
max-width as length, percentage, or calc
min-height as length, percentage, or calc
min-width as length, percentage, or calc
opacity as number
outline-color as color
outline-width as length
padding-bottom as length
padding-left as length
padding-right as length
padding-top as length
right as length, percentage, or calc
text-indent as length, percentage, or calc
text-shadow as shadow list
top as length, percentage, or calc
vertical-align as length
visibility as visibility
width as length, percentage, or calc
word-spacing as length
z-index as integer
鼠标放在以下图片上,There will be a miracle:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nick</title>
<meta charset="utf-8" />
<style type="text/css">
.img-see-2016-7-2 {
background-image: url("http://images.cnblogs.com/cnblogs_com/suoning/845162/o_sea.jpg");
background-size: 660px;
background-repeat: no-repeat;
height: 300px;
width: 600px;
transition-duration: 30s;
transition-timing-function: ease;
transition-property: background-size;
}
.img-see-2016-7-2:hover {
background-size: 2000px;
}
</style>
</head>
<body>
<div class="img-see-2016-7-2"></div>
</body>
</html>
作者:suoning
原文链接:https://www.cnblogs.com/suoning/p/5625582.html
<h1 style=" width:300px;"> </h1>
内部样式要写在<head></head>标签之间
<style type=" text/css"> 样式</style>
<link href=" 样式表位置" rel=" stylesheet" type=" text/css">
link这个也要放在<head></head>之间
第一种:
<style type=" text/css">
@import " 直接写css文件路径"
</style>
第二种:
<style type=" text/css">
@import url(写样式表文件路径)
</style>
一、选择器
1.派生选择器
例如:(选择div里面的span)
div span{给样式}
<div>
<span>哈哈哈</span>
</div>
2.还有
列如:(p和a 都选择)
p,a{给样式}
<p>你好1</p>
<a>你好</a>
二、
背景颜色:
background-color: #ccc;
background意思是背景
color 意思是颜色
背景图片:
background-image: url(写图片路径);
background-repeat: repeat-y;
repeat 意思是重复平铺
repeat-y 意思是按y轴平铺
repeat-x 意思是按x轴平铺
background-repeat:no-repeat 意思是不平铺
background-position(设置背景图像在网页上的位置。)
background-position:center;(意思是定位图片的位置)
1.center(居中)
2.top (上)
3.right (右)
4.left (左)
5.bottom (下)
background-position:50% 50%;(这种方法也可以使背景图片居中)
background-attachment:fixed(使背景固定不随滚动条走)
fixed(意思是固定的)
attachment(意思是附在那里的意思)
background-size (规定背景图片的尺寸是css3的)
size 意思是尺寸
属性: 1.contain
(把图像图像扩展至最大尺寸,以使其宽度和高度完全适应内容区域 )
意思就是按等比例缩放填满区域
2.cover(铺满区域)
3.percentage(以父元素的百分比来设置背景图像的宽度和高度)
注意:如果只设置一个值,则第二个值会被设置为 "auto"。
三、字体
这里要注意引号嵌套时外层要用双引号内层要用单引号
font-family:"微软雅黑","字体二","字体三"........;(定义字体)
定义多个字体的好处就是系统会默认从第一个字体开始找,如果系统没有这个字体则默认使用浏览器设定的字体
font-style:normal;(定义字体如何显示)
normal(是正常显示)
italic(文本倾斜显示)
font-weight:900; (设置文本的粗细)
weight(意思是重量的意思)
默认值:normal 等于400
还可以设置为 bold 等同于700
注意:设置文本粗细的值为: 100 ~ 900 为字体指定了 9 级加粗度
font-size:50px;(设置文字的大小)
单位有:px em
1em=16像素
2em 等于当前字体尺寸的两倍。
四、文本
text-indent:;
text(文本)
indent(缩进)
单位:em
px
注意:
可以设置为负值。利用这种技术,可以实现很多有趣的效果,比如“悬挂缩进”,即第一行悬挂在元素中余下部分的左边:
text-align (用于文本的对齐)
align(意思是排列)
后面的属性有:1.left(左对齐)
2.right(右对齐)
3.center(居中)
4.justify(像word文档里面的两端对齐)
word-spacing(设置改变字(单词)之间的标准间隔)
spacing(间隔)
单位: em
px
使用这个无论是词语还是文字都必须有空格才能实现效果。
列如:
(你好 欢迎你来到 我的工作室。)
text-decoration(一般使用于去掉下划线)
decoration(装饰的意思)
属性:1.none 去掉下划线
2.underline (添加下划线)
3.overline (添加上面划线)
4.line-through (穿过一条线,一般多用于打折的价格划一条线)
5.blink (闪烁的)
line-height(用于设置行高)
letter-spacing 字母间隔
五、a标签的一个样式
a:hover (鼠标指针位于链接的上方)
用法:<style>
a:hover{
font-size:20px;
}
</style>
当鼠标滑上去的时候字体变大20px。
六、列表
列表样式:
如何去掉无序列表前面的小圆点:
list-style:none;
七、表格:
table, th, td
{
border: 1px solid blue;
}
border:边界 边框
1px 定义边框的粗细
solid 定义实线
blue 定义颜色
border-collapse:collapse;(将边框折叠为单一边框)
border-spacing:50px;(将格与格之间的间距拉开,
但是是边框线没有合并的情况下才能生效)
八、行内元素转换为块元素
display:block;
block 使其转化为块元素
inline 使其转化为行内元素
九、外边距与内边距
内边距:
padding-top (上)
padding-right (右)
padding-bottom (下)
padding-left (左)
外边距:
margin-top (上)
margin-right (右)
margin-bottom (下)
margin-left (左)
注意:与其到底是使用外边距还是内边距依据谁对谁而定!!
十、相对定位:(相对定位虽然元素离开原来的位置,但是不释放原来的空间)
position: relative;
left: 30px;
top: 20px;
十一、绝对定位:(绝对定位元素离开,并且释放原来的空间)
position: absolute;
left: 30px;
top: 20px;
为了浏览器适应,所以使用绝对居中时上面一定要写一个相对定位作为参照。
十二、overflow:溢出部分的处理
scroll(卷帘) 出现滚动条
hidden 隐藏
auto 自动出现滚动条
伪类选择器:
#a ul li:first-child{
margin-left:0px;
}
first-child(第一个孩子的意思)
以上这段代码便是找到类a里面的ul 里面的Li 将第一个li标签的外左间距去掉。
last-child(最后一个)
#a ul li:nth-child(1){
括号里面是几选择的就是第几个
如果说三的倍数就是写 3N
}
十三、CSS样式的权重问题:
ID>Class>标签本身(属性选择器)
十四、相对定位绝对定位的理解:
绝对定位:将被赋予此定位方法的对象从文档流中拖出,使用left,right,top,bottom等属性相对于其最接近的一个最有定位设置的父级对象进行绝对定位,如果对象的父级没有设置定位属性,即还是遵循HTML定位规则的,则依据body对象左上角作为参考进行定位。
相对定位(relative):对象不可层叠,依据left,right,top,bottom等属性在正常文档流中偏移自身位置。
且不释放原来的位置。依据当前位置进行定位。
十五、边边阴影:
box-shadow(给边框阴影)
div
{
box-shadow: 10px 10px 5px #888888;
阴影水平偏移值(可取正负值);阴影垂直偏移值(可取正负值);阴影模糊值;阴影颜色
}
给边框切圆角:
border-radius:4px;
注意这里面的值取偶不取奇
相关属性: border-top-right-radius ,
border-bottom-right-radius ,
border-bottom-left-radius ,
border-top-left-radius
文本溢出时显示为省略号(..........)
text-overflow :ellipsis
resize:none;
十六、input点击以后蓝色边框的去除:
outline:none; 去除input的蓝色的那个线
十七、 ul ol dl 默认都有一个外边距:
给他们所有的margin:0;padding:0;
*请认真填写需求信息,我们会在24小时内与您取得联系。