三列布局在谷歌浏览器中无法正常工作



很久以前,我按照教程为我的网站创建了一个 3 列布局;它曾经在每个浏览器中都能正常工作,但现在使用 Chrome 它会在页面上的任何其他元素之前添加一个空白区域。

这是一段CSS和html代码:

*{margin:0; padding:0;}
body {
font-family: "Gill Sans Mt", Arial, Helvetica;
text-align:center;	
}
img { border: none }
body, html, #wrapper {
width:100%;
height:100%;
position:relative;
}
#wrapper {display: table;}
#left {
position:absolute;
left:0;
top:0;
width:125px;
height:100%;
background-image:url(https://awranking.altervista.org/images/tile_sx1.jpg);
background-repeat:repeat-y;
z-index:-1;
display: table-cell;
}
#right {
position:absolute;
right:0;
top:0;
width:125px;
height:100%;	
background-image:url(https://awranking.altervista.org/images/tile_dx1.jpg);
background-repeat:repeat-y;
z-index:-1;
display: table-cell;
}
#middle {
display: table-cell;
height:100%;
margin-bottom:30px;
padding-bottom:30px;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Page</title>
<link href="test.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="left"></div>
<div id="middle"> 
In girum imus nocte ecce et consumimur igni
</div> 
<div id="right"></div>
</div> 
</body>
</html>

这是我想在浏览器上尝试的测试页面: 测试页面

使用position: absolute真的不应该用于构建您的主布局,它应该主要用于在您的页面中精确定位内容,例如模态或对话框。

您应该使用display: inline-block或 CSS 网格或 CSS Flexbox。

使用 CSS Grid,如果要设置 3 列布局,只需在父容器上使用display: gridgrid-template-columns: 1fr 1fr 1fr立即创建 3 列。

#container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
#column-1 {
background-color: red;
}
#column-2 {
background-color: blue;
}
#column-3 {
background-color: yellow;
}
<div id="container">
<div id="column-1">A</div>
<div id="column-2">B</div>
<div id="column-3">C</div>
</div>

您可以使用此代码 -- 使用 3 列布局,带引导程序 设计响应式

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Hello, world!</title>
<link href="test.css" rel="stylesheet" type="text/css" />
<style type="text/css">
* {
margin: 0;
padding: 0;
}
body {
font-family: "Gill Sans Mt", Arial, Helvetica;
text-align: center;
}
img {
border: none
}
.left {
width: 100%;
background-image: url(https://awranking.altervista.org/images/tile_sx1.jpg);
background-repeat: no-repeat;
background-size: cover;
margin: 0;
padding: 25px;
color: #ffffff;
}
.right {
width: 100%;
background-image: url(https://awranking.altervista.org/images/tile_sx1.jpg);
background-repeat: no-repeat;
background-size: cover;
margin: 0;
padding: 25px;
color: #ffffff;
}
.middle {
width: 100%;
background-image: url(https://awranking.altervista.org/images/tile_sx1.jpg);
background-repeat: no-repeat;
background-size: cover;
margin: 0;
padding: 25px;
color: #ffffff;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12 col-md-4 col-sm-4 col-xs-12">
<p class="left">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tristique ligula eu tellus consectetur, vel imperdiet leo pharetra.</p>
</div>
<div class="col-12 col-md-4 col-sm-4 col-xs-12">
<p class="middle">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tristique ligula eu tellus consectetur, vel imperdiet leo pharetra.</p>
</div>
<div class="col-12 col-md-4 col-sm-4 col-xs-12">
<p class="right">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tristique ligula eu tellus consectetur, vel imperdiet leo pharetra.</p>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>

最新更新