只是练习div定位。在下面的代码中,RED和GREEN div位于.content
div内。左边距(红色div左侧)和右边距(绿色div右侧)均设置为0.3%
因此,左右两侧有两条深蓝色小条垂直条纹(实际上是.content div的颜色)。)但是,如果你注意到,线条宽度不相等。请参阅屏幕截图以更好地理解这一点。为什么即使在两者上设置相等的边距之后呢?
html,body {
margin: 0px;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
background-color: rgba(173,192,241,1);
}
.wrapper {
height: 725px;
max-width: 960px;
margin-left: auto;
left: 0px;
top: 0px;
/* [disabled]background-color: rgba(15,26,155,1); */
margin-right: auto;
position: relative;
}
.topimage {
width: 100%;
max-width: 960px;
height: 100%;
max-height: 175px;
/* [disabled]background-color: rgba(0,102,204,1); */
position: absolute;
/* [disabled]border: thin solid rgba(255,0,0,1); */
}
.topimage img{
max-width: 100%;
max-height: 100%;
/* [disabled]margin-bottom: -9px; */
display: block;
margin-right: auto;
margin-left: auto;
border-radius: 15px 15px 0px 0px;
}
.menu {
background-color: rgba(15,26,155,1);
height: 100%;
max-height: 50px;
max-width: 960px;
position: relative;
top: 20px;
}
.content {
width: 100%;
height: 500px;
background-color: rgba(20,35,214,1);
position: relative;
top: 20px;
}
.leftcontent {
background-color: rgba(255,0,0,1);
float: left;
height: 100%;
max-height: 500px;
width: 100%;
max-width: 83.7%;
top: 175px;
/* [disabled]position: relative; */
border-left-color: rgba(205,205,205,1);
margin-left: 0.3%;
}
.rightcontent {
background-color: rgba(0,255,0,1);
float: right;
height: 100%;
max-height: 500px;
width: 100%;
max-width: 15.7%;
/* [disabled]position: relative; */
top: 175px;
margin-right: 0.3%;
}
.footer {
}

<div class="wrapper">
<div class="topimage">
</div>
<div class="menu">
</div>
<div class="content">
<div class="leftcontent">
</div>
<div class="rightcontent">
</div>
</div>
</div>
&#13;
答案 0 :(得分:2)
答案 1 :(得分:1)
浏览器可能会有小错误,将小数百分比值转换为实际像素。
以下进行了更改并且两侧的间距看起来相等。使用.content
display:table;
*{
box-sizing: border-box;
}
.content {
width: 100%;
height: 500px;
background-color: rgba(20,35,214,1);
position: relative;
top: 20px;
padding: 0 .3%;
display: table;
box-sizing: border-box;
}
.leftcontent {
background-color: rgba(255,0,0,1);
float: left;
height: 100%;
max-height: 500px;
width: 100%;
top: 175px;
border-left-color: rgba(205,205,205,1);
max-width: 84%;
}
.rightcontent {
background-color: rgba(0,255,0,1);
float: right;
height: 100%;
max-height: 500px;
width: 100%;
top: 175px;
max-width: 16%;
}
答案 2 :(得分:0)
*{
box-sizing:border-box;
}
html,body {
margin: 0px;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
background-color: rgba(173,192,241,1);
}
.wrapper {
height: 725px;
max-width: 960px;
margin-left: auto;
left: 0px;
top: 0px;
/* [disabled]background-color: rgba(15,26,155,1); */
margin-right: auto;
position: relative;
}
.topimage {
width: 100%;
max-width: 960px;
height: 100%;
max-height: 175px;
/* [disabled]background-color: rgba(0,102,204,1); */
position: absolute;
/* [disabled]border: thin solid rgba(255,0,0,1); */
}
.topimage img{
max-width: 100%;
max-height: 100%;
/* [disabled]margin-bottom: -9px; */
display: block;
margin-right: auto;
margin-left: auto;
border-radius: 15px 15px 0px 0px;
}
.menu {
background-color: rgba(15,26,155,1);
height: 100%;
max-height: 50px;
max-width: 960px;
position: relative;
top: 20px;
}
.content {
width: 100%;
height: 500px;
background-color: rgba(20,35,214,1);
position: relative;
top: 20px;
padding-left:3px;
padding-right:3px;
}
.leftcontent {
background-color: rgba(255,0,0,1);
float: left;
height: 100%;
max-height: 500px;
width: 100%;
max-width: 83.7%;
top: 175px;
/* [disabled]position: relative; */
border-left-color: rgba(205,205,205,1);
}
.rightcontent {
background-color: rgba(0,255,0,1);
float: right;
height: 100%;
max-height: 500px;
width: 100%;
max-width: 16.3%;
/* [disabled]position: relative; */
top: 175px;
}
.footer {
}
<div class="wrapper">
<div class="topimage">
</div>
<div class="menu">
</div>
<div class="content">
<div class="leftcontent">
</div>
<div class="rightcontent">
</div>
</div>
</div>
答案 3 :(得分:0)
如果我们从左侧内容和右侧内容中删除保证金,那么您可以注意到两个div之间已经存在一些空间,现在为了减少您可以使用此保证金低于此值。视觉上创建左右相同。
.leftcontent {
margin-left:0.35%;
}
.rightcontent {
margin-right:0.25%;
}
答案 4 :(得分:0)
这是因为保证金左边&amp;保证金权利使用百分比(%)。你使用max-width左右和margin-left&amp; margin-right根据max-width计算。所以,它是不同的。你可以使用margin-left和margin-right和px。