行业知识
【JAVA】CSS定位与CSS3属性、渐变、CSS3字体、2D变换
2026-11-17 08:24:14
来源:互联网转载
1 定位
1.1 相对定位
���对定位没有脱离文档流
定位元素的显示层级比普通元素高
定位元素可以通过margin,float调整位置,但不推荐
包含块:父元素
left和right同时写,右失效
上下同时写,下失效
*{ margin: 0; padding:0; } .outer{ width: 500px; border:1px solid black; background-color: chocolate; padding: 20px; } .box{ width: 80px; height: 80px; } .box1{ background-color: aqua; } .box2{ background-color: brown; position: relative; /* 正值离left多少距离 */ left:100px; bottom:100px; } .box3{ background-color: yellowgreen; } 在box2中加入:
float: left;
1.2 绝对定位
绝对定位脱离文档流
绝对定位+浮动:浮动失效
绝对定位+margin:可以但不推荐
包含块:第一个拥有定位的祖先元素,没有就是页面元素
子绝父相:这样父亲就可以管理儿子
子元素想在宽度上和父元素对齐,定位后left:0,right:0,全铺满,top:0,bottom:0
.outer{ width: 500px; background-color: aqua; border: 1px solid red; padding: 20px; position:relative; } .box{ width: 200px; height: 100px; font-size: 20px; } .box1{ background-color: blue; } .box2{ background-color: rgb(110, 238, 6); /* 绝对定位脱离文档流,与浮动不同,浮动是文字环绕图片或文字环绕文字,浮动会把3 t出来 */ position:absolute; } .box3{ background-color: rgb(248, 17, 232); } /* 鼠标碰到盒子,box2盒子往右移动显示出box3 */ .outer:hover .box2{ left:220px; } 1 2 3 1.3 固定定位
参考点:视口(红色框内部分)
position:fixed;
1.4 粘性定位
参考点:第一个有滚动机制的祖先元素
不脱离文档流
粘性定位+浮动:可以但不推荐
粘性定位+margin:可以但不推荐
*{ margin: 0; padding: 0; } .page-header{ height: 100px; background-color: orange; text-align: center; line-height: 100px; } .item{ background-color: #ccc; } .first,.second,.third{ background-color: skyblue; position: sticky; /* 参考点:离它最近的能滚动的祖先元素 */ /* 此处是content的p */ top: 0px; } .body{ height: 2000px; } .content{ height: 200px; overflow: scroll; } 头部 Aa a1
a2
a3
a4
a5
a6
a7
a8
Ab b1
b2
b3
b4
b5
b6
b7
b8
Ac c1
c2
c3
c4
c5
c6
c7
c8
效果: 划完一个item内容蓝条换下一个
1.5 定位层级
//通过z-index调整该块的定位层级z-index:n;
同级定位,后来者居上
CSS3
2 浏览器私有前缀
Can I use... Support tables for HTML5, CSS3, etc查浏览器能不能支持css3和h5新特性
Chrome、Edge、Safari浏览器内核:-webkit-
eg:-webkit-border-radius:20px
3 长度单位
rem:根元素字体大小倍数
10vm:视口宽度的10%
10vh:视口高度的10%
4 盒子属性
4.1 怪异盒模型
.box1{ width: 200px; height: 200px; padding: 100px; background-color: aqua; } .box2{ width: 200px; height: 200px; padding: 100px; box-sizing: border-box; background-color: brown; } 4.2 resize
.box1{ width: 200px; height: 200px; padding: 100px; background-color: aqua; /* 水平方向上可以调,也可垂直方向:vertical */ resize: horizontal; /* resize属性起作用必须有overflow */ overflow: hidden; }
4.3 box-shadow
.box1{ width: 200px; height: 200px; background-color: aqua; margin: 200px; /* 阴影1,水平|垂直 box-shadow:10px 10px; */ /* 阴影2 box-shadow: 20px 20px red; */ /* 阴影3 ,5px为模糊程度 box-shadow: 20px 20px 5px red; */ /* 阴影4 box-shadow: 20px 20px 5px; */ /* 阴影5 100px为外延值 box-shadow: 20px 20px 5px 100px red ; */ /* 阴影6,inset为内阴影 box-shadow: 20px 20px 5px 100px red inset; */ position: relative; top: 0; left: 0; transition: 1s linear all; } .box1:hover{ box-shadow: 20px 20px 5px red ; top: -1px; left: 0; } 4.4 opacity
.box2{ position: relative; width: 150px; } span{ /* 把文字放到图片上面 */ position: absolute; /* 左上角 */ top: 0; left: 0; color: red; width: 100%; background-color: white; opacity: 0.5; } .box2 img{ width: 150px; height: 150px; } 彭于晏 5 背景属性
5.1 background-origin
5.2 background-clip
.box1{ width: 500px; height: 500px; background-color: aqua; background-image: url('./image/landscape.png'); font-size: 100px; background-clip: text; /* background-clip: text;生效必需条件 */ color:transparent } 盒子测试
.box1{ width: 500px; height: 500px; background-color: aqua; padding: 50px; background-image: url('./image/landscape.png'); font-size: 100px; /* 只留下content内容区的图片 */ background-clip:content-box; border:5px dotted red; } 盒子测试 5.3 background-size
.box1{ width: 500px; height: 500px; background-color: aqua; background-image: url('./image/landscape.png'); /* 1.background-size: 400px 400px; */ /* 2.background-size: 100% 100%;参考父辈的宽高 */ /* 3.background-size: auto;默认 */ background-repeat: no-repeat; /* contain为等比缩放,让图片的宽或高与容器的宽或高相等,可能不能铺满 */ background-size:contain; /* 全铺满 background-size:cover; */ } 5.3 复合属性
应用:多背景图
.box1{ width: 500px; height: 500px; border: 2px red solid; background:url('./123.gif') no-repeat , url('./favicon.ico') no-repeat right top; } 6 边框属性
6.1 边框圆角
/* 宽或高的50% */ border-radius: 20%; /* 圆角,半径为30px */ border-radius: 30px;
6.2 边框外轮廓
/* 边框外轮廓 */ outline-offset: 5px; outline-color: blue; outline-width: 20px; outline-style: dotted;
7 文本属性
7.1 文本阴影
h1{ text-shadow: 50px 50px red; font-size: 100px; color: rgb(104, 247, 9); } 你学废了么
7.2 文本换行
h1{ width:400px; height: 400px; border: 1px red solid; /* white-space: pre;按原文显示 */ white-space: pre-wrap; /* white-space: pre-line;与white-space: pre-wrap;作用相同,但忽略文章前后的空格 */ /* white-space: nowrap;不换行 */ } 路上只我一个人,背着手踱着。这一片天地好像是我的;我也像超出了平常旳自己,到了另一世界里。我爱热闹,也爱冷静;爱群居,也爱独处。像今晚上,一个人在这苍茫旳月下,什么都可以想,什么都可以不想,便觉是个自由的人。白天里一定要做的事,一定要说的话,现在都可不理。这是独处的妙处,我且受用这无边的荷香月色好了。
7.3 文本溢出
在7.2代码中加入
overflow: hidden; /* text-overflow生效,overflow不能为visible */ text-overflow:ellipsis;
text-overflow:clip
7.4 文本修饰
h1{ width:420px; height: 400px; text-decoration: dashed overline; text-decoration-color: greenyellow; } 路上只我一个人,背着手踱着。
7.5 文本描边
h1{ font-size: 100px; -webkit-text-stroke: rgb(246, 154, 6) 10px; color: transparent; } 路上只我一个人,背着手踱着。
8 渐变
.box{ width: 300px; height: 200px; border: 1px solid black ; float: left; margin-right: 50px; } /* box1-box5线性渐变 */ .box1{ background-image: linear-gradient(red,yellow,green); } .box2{ background-image: linear-gradient(to top right,red,yellow,green); } .box3{ /* 逆时针20° */ background-image: linear-gradient(20deg,red,yellow,green); } .box4{ /* height: 200px;,0-50px纯红,50-100px变黄,100px-150px变绿 */ background-image: linear-gradient(red 50px,yellow 100px,green 150px); } .box5{ background-image: linear-gradient(red 50px,yellow 100px,green 150px); font-size: 80px; text-align: center; line-height: 200px; color: transparent; background-clip: text; } /* box6-box10 是径向渐变 */ .box6{ /* 从圆心(盒子中心点)开始,向外是红色黄色绿色 */ background-image: radial-gradient(red,yellow,green); } .box7{ /* 圆心在右上角 */ background-image: radial-gradient(at right top,red,yellow,green); } .box8{ /* 200px是横向的 */ background-image: radial-gradient(at 200px 50px,red,yellow,green); } .box9{ /* 10px是半径,从圆心向外算距离,10px以内都是红色 */ background-image: radial-gradient(red 10px,yellow 100px,green 200px); } .box10{ /* 150px 50px表示长半径,短半径,椭圆 */ background-image: radial-gradient(150px 50px at 200px 50px,red,yellow,green); } .box11{ /* 重复渐变:在没有发生渐变的区域,重新开始渐变 */ /* 参考box4 */ background-image: repeating-linear-gradient(red 50px,yellow 100px,green 150px); } .box12{ background-image: repeating-radial-gradient(red 10px,yellow 100px,green 200px);; } .box13{ background-image: repeating-linear-gradient(transparent 0,transparent 29px,gray 30px); } .box14{ width: 200px; height: 200px; border-radius: 50%; background-image:radial-gradient(at 80px 80px,white,gray); } 你学废了么 9 字体
9.1 web字体
在网上下载一个字体,导入代码文件夹中
@font-face{ font-family: "这是什么字体"; src: url('./font1/HeFengShuDaoZhaoHeFeiLongTi-Shan\(GEETYPE-FeilongGBT-Flash\)-2.ttf'); } h1{ font-size: 100px; font-family: "这是什么字体"; } 沿着荷塘,是一条曲折的小煤屑路。
缺点;字体占用空间大
iconfont-webfont平台
将下列文件放入代码font文件夹:
打开demo.html
将@font-face{}复制到style标签中
更改url,路径前加font文件夹路径
@font-face{ font-family: "这是什么字体"; src: url('./font1/HeFengShuDaoZhaoHeFeiLongTi-Shan\(GEETYPE-FeilongGBT-Flash\)-2.ttf'); } /* 高兼容性写法 */ @font-face { font-family:"webfont123"; font-display: swap; src: url('./font2/webfont.eot'); /* IE9 */ src: url('./font2/webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ url('./font2/webfont.woff2') format('woff2'), url('./font2/webfont.woff') format('woff'), /* chrome、firefox */ url('./font2/webfont.ttf') format('truetype'), /* chrome、firefox、opera、Safari, Android, iOS 4.2+*/ url('./font2/webfont.svg#webfont') format('svg'); /* iOS 4.1- */} h1{ font-size: 100px; font-family: "这是什么字体"; } h2{ font-family: "webfont123"; font-size: 100px; } h3{ font-size: 100px; } 沿着荷塘,是一条曲折的小煤屑路。
执子之手,将子拖走!子若不走,拍晕了继续拖走!
执子之手,将子拖走!子若不走,拍晕了继续拖走!
9.2 字体图标
iconfont-阿里巴巴矢量图标库点击icon
10 2D变换
10.1 位移
.outer{ width: 200px; height: 200px; border: 2px black solid; margin: 0 auto; margin-top: 200px; position: relative; } .inner{ width: 60px; height: 60px; background-color: aqua; /* 50%是自身width的50% */ /* 水平位移 transform: translateX(50%); */ /* /垂直位移 transform: translateY(50%); */ /* 只有后者transform: translateY(50%); 有效果 transform: translateX(50%); transform: translateY(50%); */ /* 水平+垂直位移 transform: translate(20px,20px); */ position: absolute; /* 此时50%相对于父元素的50% */ top:50%; left:50%; /* 基于上面代码垂直居中法一: margin-left: 30px; margin-top: 30px; */ /* 位移百分比说的是自己 */ /* 基于上面代码垂直居中法二 */ transform:translate(-50%,-50%) } 10.2 缩放
/* ()中数值>1放大,
上一篇:抖音音乐下载方法全攻略
下一篇:揭秘变量命名背后的秘密!