作为前端,在工作中难免会遇到关于排版的问题,以下是我整理的一些关于CSS的技巧,希望对你能有帮助。
一般我们会用JS实现,其实CSS就可以实现。 JS代码:
var str = 'hello world'; str.replace(/( |^)[a-z]/g,(L)=>L.toUpperCase() Heool World
var str = 'hello world';
str.replace(/( |^)[a-z]/g,(L)=>L.toUpperCase()
Heool World
css实现:
text-transform:capitalize; ( 文本中每个单词以大写字母开头) text-transform的属性值有: uppercase (全部大写) lowercase (全部小写) capitalize (首字母大写) none (默认)
text-transform:capitalize; ( 文本中每个单词以大写字母开头)
text-transform的属性值有:
uppercase (全部大写)
lowercase (全部小写)
capitalize (首字母大写)
none (默认)
如input元素: input:checked + .check { color:red; }
如input元素:
input:checked + .check {
color:red;
}
有时候我们给元素设置border-top的时候,并不想给第一个元素设置
li+li { border-top: 1px solid red; }
li+li {
border-top: 1px solid red;
display:table;
display:flow-root;
在input的type类型为text/password时,size的属性值代表input可容纳的字符的个数,除此之外size 属性会告诉用户端其初始宽度,宽度以 ‘px’ 的形式给出
position的属性大家常见的应该就是absolute、fixed、static 为大家介绍一个新的属性 sticky
position:sticky; 设置sticky的同时给元素一个(top,botton,left,right)即可使用 使用条件: 1、父元素不能overflow:hidden 或者 overflow:auto 2、必须指定 top、bottom、left、right 3、父元素的高度不能低于sticky元素的搞丢 4、sticky元素仅在其父元素内生效
position:sticky; 设置sticky的同时给元素一个(top,botton,left,right)即可使用
使用条件:
1、父元素不能overflow:hidden 或者 overflow:auto
2、必须指定 top、bottom、left、right
3、父元素的高度不能低于sticky元素的搞丢
4、sticky元素仅在其父元素内生效
原始的button按钮要重置挺麻烦的,要设置好几个属性
button { background: none; border: none; color: inherit; font: inherit; outline: none; padding: 0; }其实值需要设置 button { all:unset; }
button {
background: none;
border: none;
color: inherit;
font: inherit;
outline: none;
padding: 0;
其实值需要设置
all:unset;
文本省略号,相信大家都很常用
这是不折行的情况 div { white-space:nowrap;/* 规定文本是否折行 */ overflow: hidden;/* 规定超出内容宽度的元素隐藏 */ text-overflow: ellipsis; /* 规定超出的内容文本省略号显示,通常跟上面的属性连用,因为没有上面的属性不会触发超出规定的内容 */ } 折行 div { overflow: hidden; text-overflow: ellipsis; display: -webkit-box; /* 将对象作为弹性伸缩盒子模型显示 */ -webkit-line-clamp: 4; /* 控制最多显示几行 */ -webkit-box-orient: vertical; /* 设置或检索伸缩盒对象的子元素的排列方式 */ }
这是不折行的情况
div {
white-space:nowrap;/* 规定文本是否折行 */
overflow: hidden;/* 规定超出内容宽度的元素隐藏 */
text-overflow: ellipsis;
/* 规定超出的内容文本省略号显示,通常跟上面的属性连用,因为没有上面的属性不会触发超出规定的内容 */
折行
overflow: hidden;
display: -webkit-box; /* 将对象作为弹性伸缩盒子模型显示 */
-webkit-line-clamp: 4; /* 控制最多显示几行 */
-webkit-box-orient: vertical; /* 设置或检索伸缩盒对象的子元素的排列方式 */
div { width: 100px; padding: 0 10px; background: pink; margin-bottom: 10px; text-align-last:justify; /* 关键属性 */ }
width: 100px;
padding: 0 10px;
background: pink;
margin-bottom: 10px;
text-align-last:justify; /* 关键属性 */
当给元素添加行高我们需要分别添加到每一个元素上p、h,但是其实我们直接添加到body上就可以了
body { line-height:1; }
body {
line-height:1;
p:first-letter{ display:block; float:left; margin: 5px 5px 0 0; color:red; font-size:1.4rem; background:#ddd;}
p:first-letter{
display:block;
float:left;
margin: 5px 5px 0 0;
font-size:1.4rem;
background:#ddd;
a{ outline:none 或者 outline:0}
a{
outline:none 或者 outline:0
div { width:calc(100%-100px) 让宽度为100%的减去100px }
width:calc(100%-100px) 让宽度为100%的减去100px
<ul contenteditable="true"><li>11111111 </li><li>2222222</li><li>3333333</li></ul>
<ul contenteditable="true">
<li>11111111 </li>
<li>2222222</li>
<li>3333333</li>
</ul>
p{ font-size:0;} p{ text-indent:-1000;}
p{
font-size:0;
text-indent:-1000;
img { object-fit:cover; 或者 contain}
img {
object-fit:cover; 或者 contain
div { filter:blur(2px)}
filter:blur(2px)
div { width:fill-available; // 等同于block} div { width:fill-content; // 等同于inline-block}
width:fill-available; // 等同于block
width:fill-content; // 等同于inline-block
link的四种状态,需要按照下面的前后顺序来设置a:link a:visited a:hover a:active
link的四种状态,需要按照下面的前后顺序来设置
a:link a:visited a:hover a:active
浏览器的缺省字体大小是16px,你可以先将基准字体大小设为10px:
body {font-size:62.5%;}
后面统一采用em作为字体单位,2.4em就表示24px。
h1 {font-size: 2.4 em}
默认情况下,浏览器会用黑圈来充当列表标志,你可以用图片代替
ul li { background-image: url("path-to-your-image"); background-repeat: none; background-position: 0 0.5em; }
ul li {
background-image: url("path-to-your-image");
background-repeat: none;
background-position: 0 0.5em;
textarea { overflow: auto; }
这会让你的彩色照片变成黑白的图像
img.desaturate { filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); -ms-filter: grayscale(100%); -o-filter: grayscale(100%); }
img.desaturate {
filter: grayscale(100%);
-webkit-filter: grayscale(100%);
-moz-filter: grayscale(100%);
-ms-filter: grayscale(100%);
-o-filter: grayscale(100%);
一般我们给元素添加样式会先给所以的都添加,然后再取消我们不想要的哪个元素样式
/* add border */.nav li { border-right: 1px solid #666;}/* remove border */.nav li:last-child { border-right: none;}
/* add border */.nav li { border-right: 1px solid #666;}
/* remove border */.nav li:last-child { border-right: none;}
可以直接使用:not() 伪类实现
.nav li:not(:last-child) { border-right: 1px solid #666;}
css3属性,当一个元素设置之后将无法点击 .disabled { pointer-events: none; }
css3属性,当一个元素设置之后将无法点击
.disabled { pointer-events: none; }
.blur { color: transparent; text-shadow: 0 0 5px rgba(0,0,0,0.5);}
.blur {
color: transparent;
text-shadow: 0 0 5px rgba(0,0,0,0.5);
div {user-select: none; /* Standard syntax */}
原文链接:http://www.cnblogs.com/lbx-night/p/11644186.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728