可伸缩的卡片
如何构建这种以图片为低 上浮文字的圆角卡片。
image.png
思路:
- 圆角可以通过
border-radius
属性设置 - 图片设置成
div
的background-image
来充当背景图 - 文字包裹在
div
中并设置成以div
为锚点的定位模式
代码实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.panel{
height: 80vh;
width:60vw;
border-radius: 40px;
background-size: cover;
background-repeat: no-repeat;
background-position: center;
position: relative;
cursor: pointer;
}
.panel h3{
color:#fff;
position: absolute;
bottom: 20px;
left:20px;
}
</style>
</head>
<body>
<div class="panel" style="background-image: url('https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');">
<h3>Explore The World</h3>
</div>
</body>
</html>
css代码
background-size
设置背景图片大小。图片可以保有其原有的尺寸,或者拉伸到新的尺寸,或者在保持其原有比例的同时缩放到元素的可用空间的尺寸。
这里设置成**cover
****, **缩放背景图片以完全覆盖背景区,可能背景图片部分看不见.
background-repeat
定义背景图像的重复方式。背景图像可以沿着水平轴,垂直轴,两个轴重复,或者根本不重复
这里设置成no-repeat
,即不要重复复制图片
background-position
为每一个背景图片设置初始位置。这个位置是相对于由 background-origin定义的位置图层的
这里设置成center用来居中背景图片
background-radius
设置元素的外边框圆角。当使用一个半径时确定一个圆形,当使用两个半径时确定一个椭圆。这个(椭)圆与边框的交集形成圆角效果。
position:absolute
这里将文字设置成absolute
,父元素div设置成relative
,那么bottom:20px
就是以父元素的底为锚点,向上20px