DivCSS布局绝对定位和浮动详细代码
时间: 2023-06-11 15:09:04 浏览: 69
绝对定位布局代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Absolute Positioning</title>
<style type="text/css">
.container {
position: relative;
border: 1px solid #000;
height: 300px;
width: 500px;
margin: 20px auto;
}
.box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #ccc;
border: 1px solid #000;
padding: 20px;
box-sizing: border-box;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<h1>Absolute Positioning</h1>
<p>This is an example of absolute positioning.</p>
</div>
</div>
</body>
</html>
```
浮动布局代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Float Layout</title>
<style type="text/css">
.container {
border: 1px solid #000;
height: 300px;
width: 500px;
margin: 20px auto;
}
.box {
float: left;
width: 33.33%;
background-color: #ccc;
border: 1px solid #000;
padding: 20px;
box-sizing: border-box;
}
.clearfix::after {
content: "";
display: table;
clear: both;
}
</style>
</head>
<body>
<div class="container clearfix">
<div class="box">
<h1>Float Layout</h1>
<p>This is an example of float layout.</p>
</div>
<div class="box">
<h1>Float Layout</h1>
<p>This is an example of float layout.</p>
</div>
<div class="box">
<h1>Float Layout</h1>
<p>This is an example of float layout.</p>
</div>
</div>
</body>
</html>
```
注意:为了清除浮动,我们在容器的类名中加入了 "clearfix",并在 CSS 中添加了相应的样式。