好看的CSS效果
好看的效果
参考链接
1.复选框:
思路是使用复选框并进行隐藏,然后自己写一个div,并且用:before来放进div复选框这,不少地方采用绝对定位,在:checked时更改对应颜色。
<div class="container">
<h2>To it</h2>
<ul>
<li>
<input type="radio" id="f-option" name="selector" />
<label for="f-option">Pizza</label>
<div class="check"></div>
</li>
<li>
<input type="radio" id="s-option" name="selector" />
<label for="s-option">Bacon</label>
<div class="check">
<div class="inside"></div>
</div>
</li>
<li>
<input type="radio" id="t-option" name="selector" />
<label for="t-option">Cats</label>
<div class="check">
<div class="inside"></div>
</div>
</li>
</ul>
</div>
body,html{
height: 100%;
background: #222222;
font-family: 'lato', sans-serif;
}
.container{
display: block;
position: relative;
margin: 40px auto;
height: auto;
width: 500px;
padding: 20px;
}
h2{
color: #AAAAAA;
}
.container ul{
list-style: none;
margin: 0;
padding: 0;
overflow:auto;
}
ul li{
color:#AAAAAA;
display: block;
position: relative;
float: left;
width: 100%;
height: 100px;
border-bottom: 1px solid #333;
}
ul li input[type=radio]{
position: absolute;
visibility: hidden;
}
ul li label{
display: block;
position: relative;
font-weight: 300;
font-size: 1.35em;
padding: 25px 25px 25px 80px;
margin: 10px auto;
height: 30px;
z-index: 9;
cursor: pointer;
transition: all 0.25s linear;
}
ul li:hover label{
color: #fff;
}
ul li .check{
display: block;
position:absolute;
border: 5px solid #AAAAAA;
border-radius: 100%;
height: 25px;
width: 25px;
top: 30px;
left: 20px;
z-index: 5;
transition: border .25s linear;
-webkit-animation: border .25s linear;
}
ul li:hover .check{
border: 5px solid #fff;
}
ul li .check::before{
display: block;
position:absolute;
content: '';
border-radius: 100%;
height: 15px;
width: 15px;
top: 5px;
left: 5px;
margin: auto;
transition: all 0.25s linear;
}
input[type=radio]:checked ~ .check{
border:5px solid #0DFF92
}
input[type=radio]:checked ~.check::before{
background: #0DFF92;
}
input[type=radio]:checked ~ label{
color: #0DFF92;
}
对应效果:
2.菜单栏:使用复选框,设置为透明,使用三条span实现图案,然后三个span可以通过transform来定义旋转角度以及设置变换原点,然后使用选择器三条杆的选择方向不同.
<nav role="navigation">
<div id="menuToggle">
<input type="checkbox"/>
<span></span>
<span></span>
<span></span>
<ul id="menu">
<a href="#"><li>Home</li></a>
<a href="#"><li>About</li></a>
<a href="#"><li>Info</li></a>
</ul>
</div>
</nav>
body{
margin: 0;
padding: 0;
background: #232323;
color: #cdcdcd;
font-family: "Avenir Next","Avenir",sans-serif;
}
#menuToggle{
display: block;
position: relative;
top:50px;
left: 50px;
z-index: 1;
user-select: none;
}
#menuToggle a{
text-decoration: none;
color: #232323;
transition: color 0.3s ease;
}
#menuToggle a:hover{
color: tomato;
}
#menuToggle input{
display: block;
width: 40px;
height: 32px;
position: absolute;
top:-7px;
left: -5px;
cursor: pointer;
opacity: 0;
z-index:2;
}
#menuToggle span
{
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background:#cdcdcd;
border-radius: 3px;
z-index:1;
transform-origin: 4px 0px;
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),opacity 0.55s ease;
}
#menuToggle span:first-child
{
transform-origin: 0% 0%;
}
#menuToggle span:nth-last-child(2)
{
transform-origin: 0% 100%;
}
#menuToggle input:checked ~ span
{
opacity: 1;
transform: rotate(45deg) translate(-2px,-1px);
background: #232323;
}
#menuToggle input:checked ~ span:nth-last-child(3)
{
opacity: 0;
transform: rotate(0deg) scale(0.2,0.2);
}
#menuToggle input:checked ~ span:nth-last-child(2)
{
transform: rotate(-45deg) translate(0,-1px);
}
#menu
{
position:absolute;
width: 300px;
margin: -100px 0 0 -50px;
padding: 50px;
padding-top: 125px;
background: #ededed;
list-style-type: none;
-webkit-font-smoothing:antialiased;
transform-origin: 0% 0%;
transform: translate(-100%,0);
transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0);
}
#menu li
{
padding: 10px 0;
font-size: 22px;
}
#menuToggle input:checked ~ ul
{
transform: none;
}
- 注意label的for的使用和:before和:after的使用,通过把它们设置为绝对定位可以实现一些内嵌效果,此外可以手动阴影画勾号:
<h1 class="title">Pure CSS Custom Checkboxes</h1>
<ul class="unstyled centered">
<li>
<input class="styled-checkbox" type="checkbox" value="value1" id="styled-checkbox-1">
<label for="styled-checkbox-1">Checkbox</label>
</li>
<li>
<input class="styled-checkbox" type="checkbox" value="value2" id="styled-checkbox-2">
<label for="styled-checkbox-2">w1</label>
</li>
<li>
<input class="styled-checkbox" type="checkbox" value="value3" id="styled-checkbox-3" disabled>
<label for="styled-checkbox-3"></label>
</li>
</ul>
.styled-checkbox{
position: absolute;
opacity: 0;
&+ label{
position: relative;
cursor: pointer;
padding: 0%;
}
&+label::before{
content: '';
margin-right: 10px;
display: inline-block;
vertical-align: text-top;
width: 20px;
height: 20px;
background-color: white;
}
&:hover+label::before{
background: #f35429;
}
&:focus+label::before{
box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.12);
}
&:checked+label::before{
background: #f35429;
}
&:disabled+label{
color: #b8b8b8;
cursor:auto;
}
&:disabled+label::before{
box-shadow: none;
background: #ddd;
}
&:checked+label::after{//这是手动画勾
content: '';
position: absolute;
left: 5px;
top: 9px;
background: white;
width: 2px;
height: 2px;
box-shadow:
2px 0 0 white,
4px 0 0 white,
4px -2px 0 white,
4px -4px 0 white,
4px -6px 0 white,
4px -8px 0 white;
transform: rotate(45deg);
}
}
html {
background: lightgray;
}
body {
font-family: 'Source Sans Pro', sans-serif;
}
.unstyled {
margin: 0;
padding: 0;
list-style-type: none;
}
li {
margin: 20px 0;
}
.centered {
width: 300px;
margin: auto;
}
.title {
text-align: center;
color: rgb(69, 113, 236);
}
效果如下:
- 弹窗效果: background-clip设置背景图的延伸范围,在部分可以多使用绝对定位,使用fixed定位+top/b/l/r设置为0可以全屏覆盖.
<h1>Popup/Modal Windows</h1>
<div class="box">
<a class="button" href="#popup1">Let me Pop up</a>
</div>
<div id="popup1" class="overlay">
<div class="popup">
<a class="close" href="#">×</a>
<h2>Here i am</h2>
</div>
</div>
body{
font-family: Arial,sans-serif;
background: url(http://www.shukatsu-note.com/wp-content/uploads/2014/12/computer-564136_1280.jpg) no-repeat;
background-size: cover;
height: 100vh;
}
h1{
text-align: center;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
color: #06D85F;
margin:80px 0;
}
.box{
width: 40%;
margin: 0 auto;
background: rgba(255, 255, 255, 0.2);
padding: 35px;
border: 2px solid #fff;
border-radius: 20px/50px;
background-clip: padding-box;
text-align: center;
}
.button{
font-size: 1em;
padding: 10px;
color: #fff;
border:2px solid #06D85F;
border-radius: 20px/50px;
text-decoration: none;
cursor: pointer;
transition: all 0.3s ease-out;
}
.button:hover{
background: #06D85F;
}
.overlay{
position:fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.7);
transition: opacity 500ms;
visibility: hidden;
opacity: 0;
}
.overlay:target {
visibility: visible;
opacity: 1;
}
.popup{
margin: 70px auto;
padding: 20px;
background-color: #fff;
border-radius: 5px;
width: 30%;
position: relative;
transition: all 5s ease-in-out;
}
.popup h2{
margin-top: 0;
color:#333;
font-family:'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.popup .close{
position:absolute;
top: 20px;
right: 30px;
transition: all 200ms;
font-size: 30px;
font-weight: bold;
text-decoration: none;
color: #333;
}
.popup .close:hover{
color: #06D85F;
}
.popup .content{
max-height: 30%;
overflow: auto;
}
@media screen and(max-width:700px) {
.box{
width: 70%;
}
.popup{
width: 70%;
}
}
- box-sizing属性:content-box(默认值,元素的宽度之后再加上边框和内边距才能是最终宽度),border-box(把边框和padding算进宽度中)
- HSL颜色定义:如
background: hsl(50 80% 40%);
由色相,饱和度,和明度指定。对于一个悬浮渐变效果来说,思路是悬浮时在::before的左下角作为基准点缩放,不悬浮时以右下角作为基准点。
h1::before{
transform:scaleX(0);
transform-origin:bottom right;
}
h1:hover::before {
transform: scaleX(1);
transform-origin: bottom left;
}