我在定位盒元素時遇到了問題。更具體地說,保持兩個盒子元素不重疊。我重視我的頁面的圖像顯示,你明白我的意思,我的代碼:如何防止兩個盒子元素重疊?
重疊問題:
<!DOCTYPE html>
<html>
<head>
<title>TMNT - Rancid Tomatoes</title>
<link rel="stylesheet" href="movie.css">
<meta charset="utf-8" />
</head>
<body>
<div>
<img class="width" src="images/rancidbanner.png" alt="Rancid Tomatoes">
</div>
<h1>TMNT (2015)</h1>
<!---block one--->
<div class="div1">
<!---block two--->
<div class="div2">
<img class="tLeft" src="images/rottenlarge.png" alt="Rotten" /> <strong>33%</strong>
</div>
<!---block three--->
<div class="div3">
<img class="tRight" src="images/overview.png" alt="general overview" />
</div>
<!---box four--->
<div class="div4">
<p>HEllo relkgnaldfkgnadlgsknasdlkgnasldkgnaslkdgnasldkn sjdnaslkdjfnaslkdjfn sdgnaslkjgnlaskjgdn
</p>
</div>
</div>
</html>
body{
background-color: #C8C8C8;
}
h1{
text-shadow: 2px 3px gray;
margin-left: auto;
margin-right: auto;
width: 200px;
}
img.width{
width: 100%;
}
img.tLeft {
float: left;
z-index: -1;
padding-right: 3em;
}
img.tRight {
float: right;
}
.div1 {
position: relative;
width: 900px;
height: 700px;
margin-left: auto;
margin-right: auto;
border-radius: 20px;
box-shadow: 10px 10px 5px #A8A8A8;
background-color: #4dffa6;
overflow: hidden;
}
.div2 {
width: 100%;
height: auto;
border: 1px solid red;
overflow: hidden;
border-top-left-radius: 20px;
top: 0;
left: 0;
}
.div3 {
position: absolute;
width: auto;
height: auto;
border: 1px solid red;
overflow: hidden;
border-top-right-radius: 20px;
top: 0;
right: 0;
}
.div4 {
position: absolute;
width: auto;
height: auto;
border: 1px solid blue;
background-color: lightgray;
overflow: hidden;
left: 0;
}
strong{
font-size: 70px;
color: red;
}
直到你確實需要它時才使用位置。你可以刪除位置,並給兩個箱子浮動其中一個權利和一個主題離開和 – mkafiyan
Flexbox也是一個不錯的選擇@mkafiyan – 2016-09-07 05:49:43
@AhimanyuSingh是的,他也可以使用它 – mkafiyan