html - 无法将图像设置为适合整个 div
问题描述
我想为我的网站创建一些标题,但实际上我无法正确设置背景图像。我想从我的背景图像中填充整个标题 div,但它实际上甚至不适合整个 div。我该怎么办?(请不要因为代码质量而责备我:D)
HTML
<!DOCTYPE html>
<html>
<head>
<link
href="css.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class = "headerContainer">
<div class = "headerCol1"><h1>LoremIpsum</h1></div>
<div class = "headerCol2"><h1>LoremIpsum</h1></div>
<div style = "clear:both;"/>
<div class = "headerCol3"><h1>LoremIpsum</h1></div>
<div style = "clear:both"/>
</div>
</body>
</html>
CSS
body{
background-color:#666766;
}
.headerContainer{
height:100vh;
border:solid 2px;
background-image:url("https://i2.wp.com/oddshjelpen.com/wp-content/uploads/2018/08/350851-download-free-website-background-1920x1080-for-mobile-hd.jpg?ssl=1");
background-size: contain;
background-position: left-top;
background-repeat:no-repeat;
}
.headerCol1{
height:100px;
width:600px;
background-color:yellow;
margin:0 auto;
}
.headerCol1 h1{
text-align:center;
font-size:40px;
padding:15px;
font-size:72px;
letter-spacing:15px;
}
.headerCol2{
height:70px;
width:500px;
background-color:yellow;
margin-top:150px;
float:right;
}
.headerCol2 h1{
margin:0;
padding:0;
text-align:center;
position:relative;
top:50%;
transform:translateY(-50%);
font-size:62px;
}
.headerCol3{
height:65px;
width:550px;
background-color:yellow;
margin-top:20px;
float:right;
}
.headerCol3 h1{
margin:0;
padding:0;
text-align:center;
position:relative;
top:50%;
transform:translateY(-50%);
font-size:56px;
}
解决方案
您必须更新background-size
到cover
CSS 下面的更新。
.headerContainer {
height: 100vh;
border: solid 2px;
background-image: url(https://i2.wp.com/oddshjelpen.com/wp-content/uploads/2018/08/350851-download-free-website-background-1920x1080-for-mobile-hd.jpg?ssl=1);
background-size: cover;
background-position: left-top;
background-repeat: no-repeat;
}
推荐阅读
- reactjs - 如何在没有反应路由器的情况下创建新页面
- python - 使用rest API时数据在颤动中消失
- javascript - 在 Discord.js 中创建动态查询 .addField
- python - Python 正则表达式在正则表达式测试器中工作,但在实际中不起作用
- c# - 无法映射 byte[] 或 byte[]?(可为空的字节 [])使用 AutoMapper ForCtorParam 函数
- python - Python:如何生成 2 的倍数的日期?
- python - 为处理来自 html(网页)的输入而开发的 Python 脚本未执行 shell 命令
- python-3.x - 提交后Django表单字段未设置值
- installation - 使用 pip 安装 dlib 时遇到问题
- python-3.x - pandas groupby 并用有序列扩大数据框