怎么让div随着窗体变小同时变小,保证显示完全不出现滚动条
时间: 2023-11-01 10:06:20 浏览: 39
你可以使用 CSS 中的 `max-width` 和 `max-height` 属性来限制 `div` 元素的最大宽度和高度。当浏览器窗口缩小时,`div` 元素也会相应地缩小,以保证其内容完全显示,而不出现滚动条。
以下是一个示例:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自适应大小的 div 元素</title>
<style>
.container {
max-width: 100%;
max-height: 100%;
overflow: hidden;
}
.content {
width: 100%;
height: 100%;
background-color: #f1f1f1;
padding: 20px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<p>这里是 div 内容</p>
</div>
</div>
</body>
</html>
```
在这个示例中,`.container` 元素使用 `max-width: 100%;` 和 `max-height: 100%;` 属性将其最大宽度和高度设置为浏览器窗口的宽度和高度。`.content` 元素使用 `width: 100%;` 和 `height: 100%;` 属性将其宽度和高度设置为与 `.container` 相同,并使用 `box-sizing: border-box;` 属性将内边距和边框计算在内。最后,使用 `overflow: hidden;` 属性隐藏 `.container` 元素的溢出部分。这样,当浏览器窗口缩小时,`.container` 和 `.content` 元素也会相应地缩小,以保证其内容完全显示,而不出现滚动条。