CSS小技巧使用

1、清除浮动
浮动给我们的代码带来的麻烦,想必不需要多说,我们会用很多方式来避免这种麻烦,其中我觉得最方便也是兼容性最好的一种是....// 清除浮动
.clearfix{
  zoom: 1;
}
.clearfix:after{
  display: block;
  content: ‘‘;
  clear: both;
}

2、垂直水平居中
在css的世界里水平居中比垂直居中来的简单一些,经过了多年的演化,依然没有好的方式来让元素垂直居中(各种方式各有优缺点,但都不能达到兼容性好,破坏力小的目标),以下是几种常见的实现方式绝对定位方式且已知宽高
position: absolute;
top: 50%;
left: 50%;
margin-top: -3em;
margin-left: -7em;
width: 14em;
height: 6em;

绝对定位 + 未知宽高 + translate
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
//需要补充浏览器前缀

flex 轻松搞定水平垂直居中( 未知宽高)
display: flex;
align-items: center;
justify-content: center;

3、 文本末尾添加省略号
当文本的内容超出容器的宽度的时候,我们希望在其默认添加省略号以达到提示用户内容省略显示的效果。宽度固定,适合单行显示...
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;

宽度不固定,适合多行以及移动端显示
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;

4、制造文本的模糊效果
当我们希望给文本制造一种模糊效果感觉的时候,可以这样做color: transparent;
text-shadow:0 0 2px rgba(0,0,0,.5);

5、动画实现简洁loading效果
我们来实现一个非常简洁的loading效果.loading:after{
  display: inline-block;
  overflow: hidden;
  vertical-align: bottom;
  content: ‘\2026‘;
  -webkit-animation: ellipsis 2s infinite;
}

// 动画部分
@-webkit-keyframes ellipsis{
  from{
    width: 2px;
  }
  to{
    width: 15px;
  }
}

6、自定义文本选中样式
默认情况下,我们在网页上选中文字的时候,会给选中的部分一个深蓝色背景颜色(可以自己拿起鼠标试试),如果我们想自己定制被选中的部分的样式呢?// 注意只能修改这两个属性 字体颜色 选中背景颜色

element::selection{
  color: green;
  background-color: pink;
}
element::-moz-selection{
  color: green;
  background-color: pink;
}

7、顶角贴纸效果
有时候我们会有这样的需求,在一个列表展示页面,有一些列表项是新添加的、或者热度比较高的,就会要求在其上面添加一个贴纸效果的小条就像hexo默认博客的fork me on github那个效果一样,如下图。 接下来我们开始一步步完成最左边的这个效果
html
<div class="wrap">
  <div class="ribbon">
    <a href="#">Fork me on GitHub</a>
  </div>
</div>

css
/* 外层容器几本设置  */
.wrap{
  width: 160px;
  height:160px;
  overflow:hidden;
  position: relative;
  background-color: #f3f3f3;
}

.ribbon{
  background-color: #a00;
  overflow: hidden;
  white-space: nowrap;
  position: absolute;
  /* shadom */
  -webkit-box-shadow: 0 0 10px #888;
  -moz-box-shadow: 0 0 10px #888;
  box-shadow: 0 0 10px #888;
  /* rotate */
  -webkit-transform: rotate(-45deg);
  -moz-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  transform: rotate(-45deg);
  /* position */
  left: -50px;
  top: 40px;
}

.ribbon a{
  border: 1px solid #faa;
  color: #fff;
  display: block;
  font: bold 81.25% ‘Helvetica Neue‘, Helvetica, Arial, sans-serif;
  margin: 1px 0;
  padding: 10px 50px;
  text-align: center;
  text-decoration: none;
  /* shadow */
  text-shadow: 0 0 5px #444;
}

8、input占位符
当我们给部分input类型的设置placeholder属性的时候,有的时候需要修改其默认的样式。input::-webkit-input-placeholder{
  color: green;
  background-color: #F9F7F7;
  font-size: 14px;
}
input::-moz-input-placeholder{
  color: green;
  background-color: #F9F7F7;
  font-size: 14px;
}
input::-ms-input-placeholder{
  color: green;
  background-color: #F9F7F7;
  font-size: 14px;
}

9、移动端可点击元素去处默认边框
在移动端浏览器上,当你点击一个链接或者通过Javascript定义的可点击元素的时候,会出现蓝色边框,说实话,这是很恶心的,怎么去掉呢?-webkit-tap-highlight-color: rgba(255,255,255,0);

10、首字下沉
要实现类似word中首字下沉的效果可以使用以下代码element:first-letter{
  float:left;
  color:green;
  font-size:30px;
}

11、小三角
在很多地方我们可以用得上小三角,接下来我们画一下四个方向的三角形.triangle{
  /* 基础样式 */
  border:solid 10px transparent;
}
/*下*/
.triangle.bottom{
 border-top-color: green;
}
/*上*/
.triangle.top{
 border-bottom-color: green;
}
/*左*/
.triangle.top{
 border-right-color: green;
}
/*右*/
.triangle.top{
 border-left-color: green;
}

可以看出画一个小三角非常简单,只要两行样式就可以搞定,对于方向只要想着画哪个方向则设置反方向的样式属性就可以
12、鼠标手型
一般情况下,我们希望在以下元素身上添加鼠标手型a
submit
input[type="iamge"]
input[type="button"]
button
label
selecta[href],input[type=‘submit‘], input[type=‘image‘],input[type=‘button‘], label[for], select, button {
  cursor: pointer;
}

13、屏蔽Webkit移动浏览器中元素高亮效果
在访问移动网站时,你会发现,在选中的元素周围会出现一些灰色的框框,使用以下代码屏蔽这种样式-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
 移动端兼容性问题1.手机旋转字体会自动调整
*{text-size-adjust:none}
2.click出现点击区域闪一下
a{-webkit-tap-highlight-color:rgba(0,0,0,0)}
3.textarea,input默认框内有阴影
textarea,input{appearance:none}
4.iOS下默认识别页面中的电话
<meta name="format-detection" contnent="telephone=no">
5.:active兼容处理
(1)给body添加ontouchstart
(2)document.addEventListener(‘touchstart‘,function(){},false)
6.某些圆角实效
background-clip:padding-box;
7.IE10 Inputy有叉号
input:ms-clear{display:none}
 * {
	margin: 0;
	padding: 0;
	text-decoration: none;
	-webkit-overflow-scrolling: touch !important;
	/*iOS惯性滚动*/
	outline: none;
	-webkit-font-smoothing: antialiased;
	/*字体细长*/
	-moz-osx-font-smoothing: grayscale;
}

body {
	position: relative;
	margin: 0 auto;
	width: 100%;
	height: 100%;
	min-width: 900px;
	overflow-x: hidden;
	font-family: "微软雅黑";
	-webkit-touch-callout: none;
	/*禁用长按页面时的弹出菜单*/
	-webkit-tap-highlight-color: white;
	box-sizing: border-box;
}

li {
	list-style: none;
}

ul,
ol {
	list-style-type: none;
}

select,
input,
img,
select {
	vertical-align: middle;
}

img {
	border: none;
	display: inline-block
}

i {
	font-style: normal
}

a {
	text-decoration: none;
	-webkit-appearance: none;
}

*:focus {
	outline: none;
}

input,
textarea,
button {
	resize: none;
	-webkit-appearance: none;
	outline: none;
}

input {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;
}

strong {
	font-weight: bold;
}

h3,
h4 {
	font-weight: normal
}

input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder {
	color: #cecece;
}

input:-moz-placeholder,
textarea:-moz-placeholder {
	color: #cecece;
}

input[type="button"],
input[type="submit"],
input[type="file"],
button {
	cursor: pointer;
	-webkit-appearance: none;
}

table {
	border-collapse: collapse;
	border-spacing: 0;
}

.hover-hand {
	cursor: pointer;
	/*悬浮显示手*/
}

/*禁止选中copy*/

.dont-select {
	-moz-user-select: none;
	-webkit-user-select: none;
	-ms-user-select: none;
	-khtml-user-select: none;
	user-select: none;
}

/*float*/

.left {
	float: left;
}

.right {
	float: right;
}

.clearfloat:after {
	content: "";
	display: block;
	height: 0;
	clear: both;
	zoom: 1;
	visibility: hidden;
}

.clearfloat {
	zoom: 1;
	clear: both;
}

.clear {
	clear: both;
	zoom: 1;
}

.hide {
	display: none !important;
}

.show {
	display: block;
}

/*font-size*/

.font12 {
	font-size: 12px;
}

.font13 {
	font-size: 13px;
}

.font14 {
	font-size: 14px;
}

.font15 {
	font-size: 15px;
}

.font16 {
	font-size: 16px;
}

.font18 {
	font-size: 18px;
}

.font19 {
	font-size: 19px;
}

.font20 {
	font-size: 20px;
}

.font22 {
	font-size: 22px;
}

.font24 {
	font-size: 24px;
}

.font26 {
	font-size: 26px;
}

.font28 {
	font-size: 28px;
}

.font30 {
	font-size: 30px;
}

.font32 {
	font-size: 32px;
}

.font36 {
	font-size: 36px;
}

.font48 {
	font-size: 48px;
}

.font60 {
	font-size: 60px;
}

.color-white {
	color: white;
}

.color-red {
	color: red;
}

.color-green {
	color: green;
}

.color-black {
	color: black;
}

.cl1685d3 {
	color: #1685D3;
}

.bg1685D3 {
	background: #1685D3;
}

.color-blue {
	color: blue;
}

.color-yellow {
	color: yellow;
}

.color-pink {
	color: pink;
}

.bg-yellow {
	background: yellow;
}

.bg-red {
	background: red;
}

.border-blue {
	border: 1px solid blue;
}

.border-black {
	border: 1px solid black;
}

.border-white {
	border: 1px solid white;
}

.tc {
	text-align: center;
}

.tl {
	text-align: left;
}

.tr {
	text-align: right;
}

/*一行多行显示省略号*/

.one-line {
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	/*clip  修剪文本。*/
}

.more-line {
	display: -webkit-box !important;
	overflow: hidden;
	text-overflow: ellipsis;
	word-break: break-all;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
}

/*flex*/

.flex {
	display: flex;
	flex-wrap: nowrap;
	flex-direction: row;
	flex-flow: row nowrap;
	justify-content: flex-start;
	/*flex-start | flex-end | center | space-between | space-around;*/
	align-items: flex-start;
	/*flex-start | flex-end | center | baseline | stretch;*/
	align-content: flex-start;
	/*flex-start | flex-end | center | space-between | space-around | stretch;*/
	align-self: auto;
}

/*移动端1px*/

.onepx-border:before {
	content: ‘‘;
	position: absolute;
	top: 0px;
	left: 0px;
	width: 200%;
	height: 200%;
	border: 1px solid blue;
	transform-origin: 0 0;
	transform: scale(0.5, 0.5);
	box-sizing: border-box;
	border-radius: 10px;
}

/*滚动条样式*/

::-webkit-scrollbar {
	width: 6px;
	height: 6px
}

::-webkit-scrollbar-track-piece {
	background: #eee;
}

::-webkit-scrollbar-thumb:vertical {
	background: #666;
}

第二版:

* {
	margin: 0;
	padding: 0;
	border: 0px;
	-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
	/*清除手机tap事件后element 时候出现的一个高亮*/
	text-decoration: none;
	-webkit-overflow-scrolling: touch !important;
	/*iOS惯性滚动*/
	outline: none;
	-webkit-font-smoothing: antialiased;
	/*字体细长*/
	-moz-osx-font-smoothing: grayscale;
}

body {
	position: relative;
	margin: 0 auto;
	width: 100%;
	height: 100%;
	min-width: 900px;
	overflow-x: hidden;
	font-family: "微软雅黑";
	-webkit-touch-callout: none;
	/*禁用长按页面时的弹出菜单*/
	-webkit-tap-highlight-color: white;
	box-sizing: border-box;
	-webkit-transform: translateZ(0);
	/*CSS开启硬件加速*/
	-webkit-backface-visibility: hidden;
	/*使用CSS transforms 或者 animations时可能会有页面闪烁的bug*/
}

li {
	list-style: none;
}

ul,
ol {
	list-style-type: none;
}

select,
input,
img,
select {
	vertical-align: middle;
}

img {
	border: none;
	display: inline-block
}

i {
	font-style: normal
}

a {
	text-decoration: none;
	-webkit-appearance: none;
}

*:focus {
	outline: none;
}

input,
textarea,
button {
	resize: none;
	-webkit-appearance: none;
	/*移除浏览器默认的样式,比如chrome的input默认样式*/
	outline: none;
}

input {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;
}

strong {
	font-weight: bold;
}

h3,
h4 {
	font-weight: normal
}

input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder {
	color: #cecece;
}

input:-moz-placeholder,
textarea:-moz-placeholder {
	color: #cecece;
}

input[type="button"],
input[type="submit"],
input[type="file"],
button {
	cursor: pointer;
	-webkit-appearance: none;
}

table {
	border-collapse: collapse;
	border-spacing: 0;
}

.hover-hand {
	cursor: pointer;
	/*悬浮显示手*/
}

.use-3D {
	-webkit-transform: rotateY(60deg);
	/* Chrome, Safari, Opera */
	-webkit-transform-style: preserve-3d;
	/* Chrome, Safari, Opera */
	transform: rotateY(60deg);
	transform-style: preserve-3d;
}

.perspective {
	/*perspective 透视  : 这个属性的存在决定你看到的元素是2d还是3d。一般设置在包裹元素的父类上。*/
	perspective: 400px;
}

/*禁止选中copy*/

.dont-select {
	-moz-user-select: none;
	-webkit-user-select: none;
	-ms-user-select: none;
	-khtml-user-select: none;
	user-select: none;
}

/*float*/

.left {
	float: left;
}

.right {
	float: right;
}

.clearfloat:after {
	content: "";
	display: block;
	height: 0;
	clear: both;
	zoom: 1;
	visibility: hidden;
}

.clearfloat {
	zoom: 1;
	clear: both;
}

.clear {
	clear: both;
	zoom: 1;
}

.hide {
	display: none !important;
}

.show {
	display: block;
}

/*font-size*/

.font12 {
	font-size: 12px;
}

.font13 {
	font-size: 13px;
}

.font14 {
	font-size: 14px;
}

.font15 {
	font-size: 15px;
}

.font16 {
	font-size: 16px;
}

.font18 {
	font-size: 18px;
}

.font19 {
	font-size: 19px;
}

.font20 {
	font-size: 20px;
}

.font22 {
	font-size: 22px;
}

.font24 {
	font-size: 24px;
}

.font26 {
	font-size: 26px;
}

.font28 {
	font-size: 28px;
}

.font30 {
	font-size: 30px;
}

.font32 {
	font-size: 32px;
}

.font36 {
	font-size: 36px;
}

.font48 {
	font-size: 48px;
}

.font60 {
	font-size: 60px;
}

.color-white {
	color: white;
}

.color-red {
	color: red;
}

.color-green {
	color: green;
}

.color-black {
	color: black;
}

.cl1685d3 {
	color: #1685D3;
}

.bg1685D3 {
	background: #1685D3;
}

.color-blue {
	color: blue;
}

.color-yellow {
	color: yellow;
}

.color-pink {
	color: pink;
}

.bg-yellow {
	background: yellow;
}

.bg-red {
	background: red;
}

.border-blue {
	border: 1px solid blue;
}

.border-black {
	border: 1px solid black;
}

.border-white {
	border: 1px solid white;
}

.tc {
	text-align: center;
}

.tl {
	text-align: left;
}

.tr {
	text-align: right;
}

/*一行多行显示省略号*/

.one-line {
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	/*clip  修剪文本。*/
}

.more-line {
	display: -webkit-box !important;
	overflow: hidden;
	text-overflow: ellipsis;
	word-break: break-all;
	-webkit-box-orient: vertical;
	-webkit-line-clamp: 2;
}

.auto-gp {
	/*自动换行*/
	word-wrap: break-word;
	word-break: normal;
}

/*flex*/

.flex {
	display: flex;
	flex-wrap: nowrap;
	flex-direction: row;
	flex-flow: row nowrap;
	justify-content: flex-start;
	/*flex-start | flex-end | center | space-between | space-around;*/
	align-items: flex-start;
	/*flex-start | flex-end | center | baseline | stretch;*/
	align-content: flex-start;
	/*flex-start | flex-end | center | space-between | space-around | stretch;*/
	align-self: auto;
}

/*移动端1px*/

.onepx-border:before {
	content: ‘‘;
	position: absolute;
	top: 0px;
	left: 0px;
	width: 200%;
	height: 200%;
	border: 1px solid blue;
	transform-origin: 0 0;
	transform: scale(0.5, 0.5);
	box-sizing: border-box;
	border-radius: 10px;
}

/*滚动条样式*/

::-webkit-scrollbar {
	width: 6px;
	height: 6px
}

::-webkit-scrollbar-track-piece {
	background: #eee;
}

::-webkit-scrollbar-thumb:vertical {
	background: #666;
}

作者:鸠摩智 链接:https://juejin.im/post/59eb0c43f265da4321532f34 来源:掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

原文地址:https://www.cnblogs.com/xc0420/p/8461876.html

时间: 2024-07-30 23:51:28

CSS小技巧使用的相关文章

CSS小技巧

CSS 小技巧 总结的网络上的资源,以备自己后续参考... 使用:not()去除导航上不需要的边框 为body添加行高 垂直居中任何元素 逗号分离的列表 使用负nth-child选择元素 使用SVG图标 文本显示优化 在纯CSS幻灯片上使用max-height 继承box-sizing 表格单元格等宽 使用Flexbox摆脱边界Hack 使用属性选择器选择空链接 使用:not()添加/去除导航上不需要的边框 添加边框… /* 添加边框 */ .nav li { border-right: 1px

css小技巧(1)

1.-webkit-overflow-scrolling: touch; 解决ios滑动时无缓冲问题 2.::-webkit-scrollbar 设置ios滑动时是否显示滚动条 3.::selection 选中文字时文字颜色和选中色 <!doctype html> <html> <head> <meta charset="utf-8"> <meta content="width=device-width,initial-s

Css 小技巧总结

相对偏移 指定left top等属性就能够够完美控制一个元素的位置 如: position:relative; left:2px; 今天遇到一个很好玩的问题,当两个并排浮动框,当一个框的长度太大时就会把另一个框顶到下一行,这个时候就可以用margin-right:-2px;将右外距设置为负数来解决! css写表达式控制图片大小,当图片大小大于某一数值时,强制设置其宽 高为指定大小. img { max-width: 163px; width:expression(this.width > 163

前端开发者应该知道的 CSS 小技巧

一些小技巧让你的CSS技术更专业 使用:not()去除导航上不需要的边框 为body添加行高 垂直居中任何元素 逗号分离的列表 使用负nth-child选择元素 使用SVG图标 文本显示优化 在纯CSS幻灯片上使用max-height 继承box-sizing 表格单元格等宽 使用Flexbox摆脱边界Hack 使用属性选择器选择空链接 使用:not()添加/去除导航上不需要的边框 添加边框… CSS 1 2 3 4 5 /* 添加边框 */ .nav li { border-right: 1p

作为一枚web前端开发工程师 这些CSS 小技巧你值得掌握

http://web.jobbole.com/85142/ 使用:not()去除导航上不需要的边框 为body添加行高 垂直居中任何元素 逗号分离的列表 使用负nth-child选择元素 使用SVG图标 文本显示优化 在纯CSS幻灯片上使用max-height 继承box-sizing 表格单元格等宽 使用Flexbox摆脱边界Hack 使用属性选择器选择空链接 使用:not()添加/去除导航上不需要的边框 添加边框- CSS 1 2 3 4 5 /* 添加边框 */ .nav li { bor

前端知识 | CSS小技巧-自适应椭圆

背景知识: border-radius 属性的基本用法. 难题: 你可能注意到过,给任何正方形元素设置一个足够大的 border-radius,就可以把它变成一个圆形.所用到的 CSS 代码如下所示: 图1.1给元素设置固定宽高以及一半长度的 border-radius,可以得到一个圆形 你可能还注意到了,如果指定任何大于 100px 的半径,仍然可以得到一个圆形.规范特别指出了这其中的原因: "当任意两个相邻圆角的半径之和超过 borderbox 的尺寸时,用户代理必须按比例减小各个边框半径所

前端常用CSS小技巧

注意:以下前缀兼容性写法注释-o-:Opera-ms://IE10-moz://火狐-webkit://Safari 4-5, Chrome 1-9 1. 去除 iOS移动端 input,textarea输入框聚焦时上方内阴影样式 input, textarea, select{ appearance: none; -moz-appearance: none; //Firefox -webkit-appearance: none; //Safari 和 Chrome } appearance属性

CSS小技巧收藏

居中对齐 很多时候我们需要把一个元素在其父级容器里水平.垂直居中对齐.以下我列出了常用的几种方法: 1.在知道子元素宽度与高度的情况下进行居中,采用位置定位:absolute + margin .parent { position: relative; } .child { position: absolute; width: 100px; height: 60px; top: 50%; left: 50%; margin: -30px 0 0 -50px; } 2.在不知道子元素高与宽的情况下

Web前端开发基础 第四课(CSS小技巧)

水平居中设置-行内元素 我们在实际工作中常会遇到需要设置水平居中场景,今天我们就来看看怎么设置水平居中的. 如果被设置元素为文本.图片等行内元素时,水平居中是通过给父元素设置 text-align:center 来实现的.如下代码: html代码: <body> <div class="txtCenter">我是文本,哈哈,我想要在父容器中水平居中显示.</div> </body> css代码: <style> div.txt