20个很有很有很有用的CSS技巧

1.placeholder的颜色值设置

input::-webkit-input-placeholder{color:#AAAAAA;}
input:focus::-webkit-input-placeholder{color:#EEEEEE;}

2.双飞翼布局

<style type="text/css">
    .box-main,.box-left,.box-right{float:left;height: 50px;}
    .box{padding: 0 300px;min-width: 400px;}
    .box-main{background: #03a9f4;width: 100%;}
    .box-left{background: #00bcd4;width: 300px;margin-left: -100%;position: relative;left: -300px;}
    .box-right{background: #00bcd4;width: 300px;margin-left: -300px;position: relative;right: -300px;}
</style>  

<div class="box">
    <div class="box-main">主体</div>
    <div class="box-left">左侧</div>
    <div class="box-right">右侧</div>
</div>

3. 黑白图像

//这段代码会让你的彩色照片显示为黑白照片
img.desaturate {
    filter: grayscale(100%);
    -webkit-filter: grayscale(100%);
    -moz-filter: grayscale(100%);
    -ms-filter: grayscale(100%);
    -o-filter: grayscale(100%);
}

4.禁止ios 长按时不触发系统的菜单,禁止ios&android长按时下载图片

.css{-webkit-touch-callout: none}

5.禁止ios和android用户选中文字

.css{-webkit-user-select:none}

6. 使用 :not() 在菜单上应用/取消应用边框

//先给每一个li菜单项添加边框
.nav li {
  border-right: 1px solid #666;
}
//然后再除去最后一个元素
.nav li:last-child {
  border-right: none;
}

//直接使用 :not() 伪类来应用元素
.nav li:not(:last-child) {
  border-right: 1px solid #666;
}

7. 页面顶部阴影

//下面这个简单的 css3 代码片段可以给网页加上漂亮的顶部阴影效果:
body:before {
	content: "";
	position: fixed;
	top: -10px;
	left: 0;
	width: 100%;
	height: 10px;
	-webkit-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
	-moz-box-shadow: 0px 0px 10px rgba(0,0,0,.8);
	box-shadow: 0px 0px 10px rgba(0,0,0,.8);
	z-index: 100;
}

8. 给 body 添加行高

//不需要分别添加 line-height 到每个p,h标记等。只要添加到 body 即可:
body {
  line-height: 1;
}

9. 所有一切都垂直居中

//注:在IE11中要小心flexbox。
html, body {
  height: 100%;
  margin: 0;
}

body {
  -webkit-align-items: center;  
  -ms-flex-align: center;  
  align-items: center;
  display: -webkit-flex;
  display: flex;
}

10. 逗号分隔的列表

//让HTML列表项看上去像一个真正的,用逗号分隔的列表(如下图):
ul > li:not(:last-child)::after {
  content: ",";
}

这里写图片描述


11. 使用负的 nth-child 选择项目

//在CSS中使用负的 nth-child 选择项目1到项目n。
li {
  display: none;
}

li:nth-child(-n+3) {
  display: block;
}

这里写图片描述


12. 对图标使用 SVG

//SVG对所有的分辨率类型都具有良好的扩展性 并支持所有浏览器都回归到IE9了。
.logo {
  background: url("logo.svg");
}

13. 优化显示文本

//有时,字体并不能在所有设备上都达到最佳的显示,所以可以让设备浏览器来帮助你:
html {
  -moz-osx-font-smoothing: grayscale;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

14. 旋转动画

//loading图的旋转动画
.loading{
	animation: changehovertree 1.5s linear infinite;
	-webkit-animation: changehovertree 1.5s linear infinite;
}
@-webkit-keyframes changehovertree {
	0% {-webkit-transform:rotate(0)}
	50% {-webkit-transform:rotate(180deg)}
	100% {-webkit-transform:rotate(360deg)}
}
@keyframes changehovertree {
	0% {transform:rotate(0)}
	50% {transform:rotate(180deg)}
	100% {transform:rotate(360deg)}
}

15. 继承 box-sizing

//让 box-sizing 继承 html:
html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}

16. 文本溢出

/*单行文本溢出*/  
.ellipsis{
    white-space:nowrap  ;
    text-overflow:ellipsis;
    overflow:hidden
}  

/*多行文本溢出*/  
.ellipsis{
    display: -webkit-box!important;
    overflow: hidden;
    text-overflow: ellipsis;
    word-break: break-all;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 3;      //展示3行 
}  

17.CSS 写出三角形

//利用border来写三角形代码,并且兼容IE6.

/* create an arrow that points up */
div.arrow-up {
  width:0px;
  height:0px;
  border-left:5px solid transparent;  /* left arrow slant */
  border-right:5px solid transparent; /* right arrow slant */
  border-bottom:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}
 
/* create an arrow that points down */
div.arrow-down {
  width:0px;
  height:0px;
  border-left:5px solid transparent;
  border-right:5px solid transparent;
  border-top:5px solid #2f2f2f;
  font-size:0px;
  line-height:0px;
}
 
/* create an arrow that points left */
div.arrow-left {
  width:0px;
  height:0px;
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-right:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}
 
/* create an arrow that points right */
div.arrow-right {
  width:0px;
  height:0px;
  border-bottom:5px solid transparent;  /* left arrow slant */
  border-top:5px solid transparent; /* right arrow slant */
  border-left:5px solid #2f2f2f; /* bottom, add background color here */
  font-size:0px;
  line-height:0px;
}

18.文本渐变

h2 {
	display: inline-block;
	color: green;
	font-size: 30px;
	background-image: -webkit-gradient(linear, 0 0, 0 bottom, from(rgba(0, 128, 0, 1)), to(rgba(51, 51, 51, 1)));
	-webkit-background-clip: text;
	-webkit-text-fill-color: transparent;
}

这里写图片描述


19. 禁用鼠标事件

//链接如果设置了下面的样式就无法点击了
.disabled { 
	pointer-events: none; 
}

20. 文本模糊

.blur {
   color: transparent;
   text-shadow: 0 0 5px rgba(0,0,0,0.5);
}

这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫老板的豆

你的鼓励将是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值