您的位置:首页 > Web前端 > CSS

css控制长方形图片在正方形区域显示,且不变形

2018-01-11 00:00 405 查看
实际图:600px*500px



效果图:



代码:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style>
* {
margin: 0px;
padding: 0px;
}

ul,
li {
list-style-type: none;
}

.box {
width: 1200px;
margin: 30px auto;
display: table;
}

.list li {
position: relative;
float: left;
width: 300px;
height: 300px;
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
}

.list li:before {
content: '';
position: absolute;
width: 100%;
height: 100%;
border: 1px solid red;
}

</style>

<body>
<div class="box">
<ul class='list'>
<li style="background-image: url(timg.jpeg)"></li>
<li style="background-image: url(timg.jpeg)"></li>
<li style="background-image: url(timg.jpeg)"></li>
<li style="background-image: url(timg.jpeg)"></li>
</ul>
</div>
</body>

</html>


总结:

主要代码:

background-size: cover;

background-repeat: no-repeat;
background-position: center center;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: