第一种实现方式
使用flex布局实现文本水平垂直居中。
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>demo</title>
<style>
.ticket{
display: flex;
}
.left,.middle,.right{
height: 100px;
}
.left{
width: 100px;
background: blanchedalmond url(./images/icon_ticket_2.png) no-repeat center;
border-bottom-right-radius: 50px;
box-shadow: 50px 0 0 coral ;
}
.middle{
width: 300px;
background-color: coral;
color: white;
font-size: 36px;
display: flex;
align-items: center;
justify-content: space-evenly;
border-radius: 6px;
border-right: 2px dashed white;
}
.discount{
font-size: 48px;
font-weight: bolder;
}
.wrapper{
display: flex;
align-items: center;
}
.right{
width: 120px;
background-color: coral;
display: flex;
align-items: center;
justify-content: space-around;
font-size: 36px;
color: white;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="ticket">
<div class="left"></div>
<div class="middle">
<span class="wrapper">
<span class="discount">5.2</span><span class="text">折</span>
</span>
<span>商品券</span>
</div>
<div class="right">
<span>购</span>
</div>
</div>
</body>
</html>
第二种实现方式
使用 text-align:center
实现文本的水平居中;
使用 hight
等于line-height
、vertical-align:top
实现文本的垂直居中。
<!-- test.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>demo</title>
<style>
div{
float: left;
}
.left{
width: 100px;
height: 100px;
background: blanchedalmond url(./images/icon_ticket_2.png) no-repeat center;
border-bottom-right-radius: 50px;
box-shadow: 50px 0 0 coral ;
}
.middle{
width: 300px;
height: 100px;
line-height: 100px;
text-align: center;
background-color: coral;
color: white;
font-size: 36px;
border-radius: 6px;
border-right: 2px dashed white;
}
.discount{
font-size: 48px;
font-weight: bolder;
vertical-align: top;
}
.right{
width: 120px;
height: 100px;
line-height: 100px;
text-align: center;
background-color: coral;
color: white;
font-size: 36px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="ticket">
<div class="left"></div>
<div class="middle">
<span class="discount">5.2</span>折 商品券
</div>
<div class="right">
<span>购</span>
</div>
</div>
</body>
</html>
改进版的折扣券
radial-gradient()
示例1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<style>
div{
width: 100px;
height: 100px;
margin: 10px;
}
.box{
background: orange radial-gradient(white 50px,transparent 0);
}
.box2{
background:orange radial-gradient(circle at top right,white 50px,transparent 0);
}
.box3{
background: orange radial-gradient(circle at top right,white 10px, transparent 0) top right;
}
.box4{
background: orange radial-gradient(circle at bottom right,white 10px,transparent 0) bottom right;
}
.box5{
background: radial-gradient(circle at bottom right,white 10px,transparent 0) bottom right,
radial-gradient(circle at top right, white 10px,transparent 0) top right;
background-color: orange;
}
</style>
</head>
<body>
<div class="box"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="box5"></div>
</body>
</html>
示例2
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<style>
.left{
width: 200px;
height: 100px;
margin: 10px;
background: radial-gradient(circle at top right,white 10px,transparent 0) top right,
radial-gradient(circle at bottom right,white 10px,transparent 0) bottom right;
background-color: orange;
}
.right{
width: 100px;
height: 100px;
margin: 10px;
background: radial-gradient(circle at top left,white 10px,transparent 0) top left,
radial-gradient(circle at bottom left,white 10px,transparent 0) bottom left;
background-color: orange;
}
</style>
</head>
<body>
<div class="left"></div>
<div class="right"></div>
</body>
</html>
示例3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<style>
.container{
width: 300px;
height: 100px;
background-color: orange;
display: flex;
}
.left{
width: 60%;
background: radial-gradient(circle at top right,white 5px,transparent 0) top right,
radial-gradient(circle at bottom right,white 5px,transparent 0) bottom right;
}
.right{
width: 40%;
background: radial-gradient(circle at top left,white 5px,transparent 0) top left,
radial-gradient(circle at bottom left,white 5px, transparent 0) bottom left;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
示例4
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>test</title>
<style>
.container{
width: 300px;
height: 100px;
background-color: orange;
display: flex;
}
.left{
width: 60%;
background: radial-gradient(circle at top right,white 5px,transparent 0) top right,
radial-gradient(circle at bottom right,white 5px,transparent 0) bottom right;
}
.right{
width: 40%;
background: radial-gradient(circle at top left,white 5px,transparent 0) top left,
radial-gradient(circle at bottom left,white 5px, transparent 0) bottom left;
}
.left::after{
content: "";
display: block;
height: 90px;
margin-top: 5px;
border-right: 2px dashed white;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
改进版
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>demo</title>
<style>
.ticket{
display: flex;
}
.left,.middle,.right{
height: 100px;
}
.left{
width: 100px;
background: blanchedalmond url(./images/icon_ticket_2.png) no-repeat center;
border-bottom-right-radius: 50px;
box-shadow: 50px 0 0 coral ;
}
.middle{
width: 300px;
background: radial-gradient(circle at top right,white 5px,transparent 0) top right,
radial-gradient(circle at bottom right,white 5px,transparent 0) bottom right;
background-color: coral;
color: white;
font-size: 36px;
display: flex;
align-items: center;
justify-content: space-evenly;
}
.discount{
font-size: 48px;
font-weight: bolder;
}
.wrapper{
display: flex;
align-items: center;
}
.right{
width: 120px;
line-height: 100px;
text-align: center;
background: radial-gradient(circle at top left,white 5px,transparent 0) top left,
radial-gradient(circle at bottom left,white 5px,transparent 0) bottom left;
background-color: coral;
font-size: 36px;
color: white;
border-radius: 0 6px 6px 0;
position: relative;
}
.right::before{
position: absolute;
left: 0;
content: "";
height: 90px;
margin-top: 5px;
border-left: 2px dashed white;
}
</style>
</head>
<body>
<div class="ticket">
<div class="left"></div>
<div class="middle">
<span class="wrapper">
<span class="discount">5.2</span><span class="text">折</span>
</span>
<span>商品券</span>
</div>
<div class="right">
<span>购</span>
</div>
</div>
</body>
</html>