css常用奇淫技巧(不定期更新)
一、常用技巧
清除浮动
- 添加新的元素 、应用 clear:both;
- 父级定义 overflow: auto;
- 父元素也设置浮动;
- 使用br标签和其自身的html属性:《br clear=”all”/》 clear=”all | left | right | none”;
- 最高大上的方法,强烈推荐 parentDom:after{content: “ “;display: block;clear: both;}
垂直居中
1 2 3
| <div class="box box1"> <span>垂直居中</span> </div>
|
1 2 3 4 5
| .box { width: 200px; height: 200px; background: red; }
|
方法1:table-cell
1 2 3 4 5
| .box1 { display: table-cell; vertical-align: middle; text-align: center; }
|
方法2:display:flex(部分低版本浏览器不兼容)
1 2 3 4 5
| .box2 { display: flex; justify-content:center; align-items:Center; }
|
方法3:绝对定位和负边距(已知元素高度)
1 2 3 4 5 6 7 8 9 10 11
| .box3 {position:relative;} .box3 span { position: absolute; width:100px; height: 50px; top:50%; left:50%; margin-left:-50px; margin-top:-25px; text-align: center; }
|
方法4:绝对定位和0(已知元素高度)
1 2 3 4 5 6 7 8 9 10 11
| .box {position:relative;}
.box4 span { width: 50%; height: 50%; background: #000; overflow: auto; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; }
|
方法5:display:flex和margin:auto
1 2 3 4 5
| .box5 { display: flex; text-align: center; } .box5 span {margin: auto;}
|
文本超出部分隐藏
单行文本超出部分隐藏
1 2 3 4 5
| #ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
多行文本超出部分隐藏
1 2 3 4 5 6
| #ellipsis { overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
|
二、形状技巧
三角形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #triangle1 { width: 0; height: 0; border-top: 50px solid red; border-right: 50px solid orange; border-bottom: 50px solid yellow; border-left: 50px solid green; }
#triangle2 { width: 0; height: 0; border-top: 50px solid red; border-right: 50px solid transparent; border-bottom: 50px solid transparent; border-left: 50px solid transparent; }
|
![image]()
未读数量
1 2 3 4 5 6 7
| #superscript { width: 50px; height: 50px; background: red; padding:0 20px; border-radius: 20px; }
|
![image]()
字体边框同色
1 2 3 4 5 6 7 8 9 10 11 12
| #app{ width: 100px; height: 100px; color: red; font-size: 30px;
border: 10px solid; }
|
![image]()
放大镜
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| #div5{ width: 50px; height: 50px; border: 5px solid #000000; border-radius: 50%; position: relative; }
#div5::after{ content: ' '; display: block; width: 8px; height: 60px; border-radius: 5px; background: #000000; position: absolute; right: -22px; top: 38px; transform: rotate(-45deg); }
|
![image]()
-
Post title: css常用奇淫技巧(不定期更新)
-
Post author: Chao
-
Create time: 2018-08-30 09:43:00
-
Post link: 2018/08/30/css常用奇淫技巧-不断更新/
-
Copyright notice: All articles in this blog are licensed under BY-NC-SA unless stating additionally.
$tools-item-width = 2.2rem
$tools-item-font-size = 1.1rem
$tools-item-border-radius = 0.1rem
.side-tools-container {
position relative
.tools-item {
width $tools-item-width
height $tools-item-width
margin-bottom 0.2rem
color var(--text-color-3)
font-size $tools-item-font-size
background var(--background-color-1)
border-right none
border-radius $tools-item-border-radius
box-shadow 0.1rem 0.1rem 0.2rem var(--shadow-color)
cursor pointer
i {
color var(--text-color-3)
}
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
i {
color var(--background-color-1)
}
}
+keep-tablet() {
width $tools-item-width * 0.9
height $tools-item-width * 0.9
margin-bottom 0.2rem
font-size $tools-item-font-size * 0.9
}
&.rss {
a {
width 100%
height 100%
border-radius $tools-item-border-radius
&:hover {
color var(--background-color-1)
background var(--primary-color)
box-shadow 0.2rem 0.2rem 0.4rem var(--shadow-color)
}
}
}
}
.side-tools-list {
transform translateX(100%)
opacity 0
transition-t("transform, opacity", "0, 0", "0.2, 0.2", "linear, linear")
&.show {
transform translateX(0)
opacity 1
}
}
.exposed-tools-list {
if (hexo-config('style.scroll.percent') == true) {
.tool-scroll-to-top {
display none
&.show {
display flex
}
&:hover {
.percent {
display none
}
.arrow-up {
display flex
}
}
.arrow-up {
display none
}
.percent {
display flex
font-size 1rem
}
}
}
}
}
$icon-size = 1.2rem
$search-header-height = 3rem
.search-pop-overlay {
position fixed
top 0
left 0
z-index $z-index-8
display flex
width 100%
height 100%
background rgba(0, 0, 0, 0)
visibility hidden
transition-t("visibility, background", "0, 0", "0.3, 0.3", "ease, ease")
&.active {
background rgba(0, 0, 0, 0.35)
visibility visible
.search-popup {
transform scale(1)
}
}
.search-popup {
z-index $z-index-6
width 70%
height 80%
margin auto
background var(--background-color-1)
border-radius 0.4rem
transform scale(0)
transition-t("transform", "0", "0.3", "ease")
+keep-tablet() {
width 80%
}
+keep-mobile() {
width 90%
}
.search-header {
display flex
align-items center
height $search-header-height
padding 0 1rem
background var(--text-color-6)
border-top-left-radius 0.2rem
border-top-right-radius 0.2rem
.search-input-field-pre {
margin-right 0.2rem
color var(--text-color-3)
font-size 1.3rem
cursor pointer
}
.search-input-container {
flex-grow 1
padding 0.2rem
.search-input {
width 100%
color var(--text-color-3)
font-size 1.2rem
background transparent
border 0
outline 0
&::-webkit-search-cancel-button {
display none
}
&::-webkit-input-placeholder {
color var(--text-color-4)
font-size 1rem
}
}
}
.close-popup-btn {
color var(--text-color-3)
font-size $icon-size
cursor pointer
&:hover {
color var(--text-color-1)
}
}
}
#search-result {
position relative
display flex
box-sizing border-box
height 'calc(100% - %s)' % $search-header-height
padding 0.3rem 1.5rem
overflow auto
.search-result-list {
width 100%
height 100%
font-size 1rem
li {
box-sizing border-box
margin 0.8rem 0
padding 0.8rem 0
border-bottom 0.1rem dashed var(--border-color)
&:last-child {
border-bottom none
}
.search-result-title {
position relative
display flex
align-items center
margin-bottom 0.8rem
padding-left 1rem
font-weight bold
&::after {
position absolute
top 50%
left 0
width 0.4rem
height 0.4rem
background var(--text-color-3)
border-radius 50%
transform translateY(-50%)
content ''
}
}
.search-result {
margin 0
padding-left 1rem
line-height 2rem
word-wrap break-word
}
a {
&:hover {
color var(--text-color-3)
}
}
.search-keyword {
color var(--primary-color)
font-weight bold
border-bottom 0.1rem dashed var(--primary-color)
}
}
}
#no-result {
margin auto
color var(--text-color-4)
}
}
}
}