css - 如何防止我的图片在 CSS 中溢出?
问题描述
这是显示正在发生的事情的代码笔https://codepen.io/designextras/pen/abdMwrP
我试图添加 overflow: hidden 和 overflow-x, y 但它仍然没有工作。
我在使用之前和之后,因为我有两个需要显示在右侧的 svg
.container {
max-width: 100%;
}
.container:before {
content: '';
display: block;
position: absolute;
background: url(images/svg-gradient.svg) no-repeat;
width: 100%;
height: 100%;
max-width: 855px;
top: 0%;
right: -5%;
}
.container:after {
content: '';
display: block;
position: absolute;
background: url(images/dashboard-svg.svg) no-repeat;
width: 100%;
height: 100%;
max-width: 855px;
top: 30%;
right: -10%;
transform: rotate(-20deg);
}
解决方案
您在 css 中添加它并禁用x
滚动,
body {
overflow-x: hidden
}
将此添加到.container:before
和.container:after
z-index: -1;
和完整的工作,
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Montserrat', sans-serif;
}
body {
overflow-x: hidden;
}
nav {
width: 100%;
height: 80px;
display: flex;
justify-content: center;
align-items: center;
position: relative;
}
.container {
max-width: 100%;
max-height: 100vh;
background: yellow;
}
.container:before {
content: '';
z-index: -1;
display: block;
position: absolute;
background: blue;
width: 100%;
height: 100%;
max-width: 855px;
top: 0%;
right: -5%;
}
.container:after {
content: '';
z-index: -1;
display: block;
position: absolute;
background: red;
width: 100%;
height: 100%;
max-width: 855px;
top: 30%;
right: -10%;
transform: rotate(-20deg);
}
.navbar-menu {
display: flex;
justify-content: space-between;
align-items: center;
width: 80%;
height: 80px;
}
#logo {
font-size: 2rem;
font-weight: bold;
text-decoration: none;
color: #2C2C2C;
}
ul {
display: flex;
}
li {
list-style: none;
margin-right: 2rem;
}
a {
text-decoration: none;
color: #000;
}
#btn {
background: #50CDFB;
height: 146px;
width: 130px;
padding: 10px 16px;
border-radius: 4px;
color: #fff;
}
.new-section {
background: red;
height: 500px;
width: 500px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet">
</head>
<body>
<div class="container">
<nav>
<div class="navbar-menu">
<a href="#" id="logo">Startup</a>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#" id='btn'>Signup</a></li>
</ul>
</div>
</nav>
<div>New section</div>
</div>
<div>I need to be under everything</div>
</body>
</html>
推荐阅读
- powershell - 接收特定传入邮件的窗口弹出消息或警报
- linux - ecryptfs-recover-private 正确的密码,但生成的目录也被加密
- python - 调用 find_elements...().text 算作 find_elements 函数本身吗?
- macos - 从不同的用户进程调用时 IOPCIDevice 无效
- c# - Azure .net Web 应用后端端口和 IP 地址
- excel - 将活动行单元格中的注释加载到用户窗体文本框
- css - 手机上的身体四处移动
- javascript - 优雅地在额外线程中从 Python websockets 服务
- powershell - 将 Microsoft Graph 中的电子邮件附件输出到磁盘上的文件中
- linux-kernel - 内核设备驱动中断时如何实现SPI传输