使用CSS重叠多个div?
我正在尝试使用 CSS 使用多个 div 创建一个设计。
我已经为它编写了代码,但不知道我的代码有什么问题,因为我的左侧和右侧 div没有在垂直中心对齐,并且所有 div 都没有与主要的黄色居中 div重叠,这是我无法做到的达到。
注意:我试过这个, z-index但没有得到我想要的。
我得到的输出:
我想实现的输出:
我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
</head>
<style>
.maind {
margin: 0px auto;
width: 90%;
padding: 10px;
height: 900px;
background-color: rgb(9, 252, 9);
position: relative;
}
.fdiv {
margin: auto;
width: 25%;
padding: 10px;
height: 100px;
background-color: rgb(10, 233, 222);
margin-top: 30px;
border: 2px solid red;
}
.sdiv {
width: 55%;
height: 600px;
background-color: #ffff00ec;
}
.tdiv {
margin: auto;
width: 25%;
padding: 10px;
height: 100px;
background-color: rgb(10, 233, 222);
border: 2px solid red;
}
p {
text-align: center;
}
.wrap {
display: flex;
flex-direction: row;
}
.wr1 {
width: 25%;
height: 100px;
background-color: rgb(10, 233, 222);
border: 2px solid red;
}
</style>
<body>
<div>
<div>
<p>Some content here...</p>
</div>
<div>
<div>
<p>Some content here..</p>
</div>
<div>
<p>Some content here..</p>
</div>
<div>
Some content here...
</div>
</div>
<div>
<p>Some content here..</p>
</div>
</div>
</body>
</html>
回答
您可以使用Flexbox或Positioning。
使用Positioning可以更灵活地向holder元素添加内容。
虽然Flexbox在添加和对齐boxes.
# 定位
描述:
创建4 个元素作为框。
每个.box都有它的方向。
例子:<div></div>。
将它们全部包裹在div.boxes. 通过这种方式,您可以将.boxes 与holder 中的内容(如果有)分开,
<div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
设置 的样式position,.wrapper因此所有positioned absolute元素都保留在.wrapper.
.wrapper {position: relative;}
.wrapper {position: relative;}
最后,设置
.box.top {
top: 0;
left: 50%;
transform: translateX(-50%);
margin-top: -40px;
}
.box.top {
top: 0;
left: 50%;
transform: translateX(-50%);
margin-top: -40px;
}
每个的位置box:
示例:
笔记:
不要使用:
left性能上.box.right。top性能上.box.bottom。
它不会设置将它们推到边缘的负边距。
- 的情况下添加到保持器(内容
.wrapper),包裹在内容div.content和添加内利用空间padding。值padding的代码示例40px,其中它涉及到.boxES dimenstions。padding添加空格 ( ) 以防止和esoverflow之间出现。我们可以进一步使用和属性来设置es 的样式。content.boxes.boxoverflowz-index
欲了解更多有关使用负马林和箱dimenstions:
检查注意事项在Flexbox的
编码:
<div class="boxes">
<div class="box top"></div>
<div class="box right"></div>
<div class="box left"></div>
<div class="box bottom"></div>
</div>
# 弹性盒
描述:
创建3 个元素来保存.boxes。
top:拿着顶盒
center:抱着左和右框
bottom:持底框
换句话说:
- 每个
.box嵌套(孩子)在一个div具有class所述的方向。
例子:<div>BOX</div>。
- 左右嵌套在
center.
HTML:
<!-- top box -->
<div>
<div></div>
</div>
<!-- left, right boxes -->
<div>
<div>
<div></div>
</div>
<div>
<div></div>
</div>
</div>
<!-- bottom box -->
<div>
<div></div>
</div>
将它们全部包装在一个div.wrapper:
<div>
<!-- top box -->
<div></div>
<!-- left, right boxes -->
<div></div>
<!-- bottom box -->
<div></div>
</div>
以下会风格的线3层的元件和将它们设置为它们的位置,.top将居中(左右)和顶部,.center将被居中从所有方向,.bottom居中(左右),并且在底部,通过显示.wrapper children水平(flex-direction: column;) 并将 ( align-items: center;) 与 ( space-between) 它们居中,使用flex.
检查: Flexbox 完整指南
.wrapper {
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
}
然后我们对.center元素做一些类似的事情,通过显示它们left和right彼此相邻,居中和space-between它们。(flex-direction声明中没有属性,因为默认值在一行中(垂直))
.wrapper .center {
width: 100%; /* Don't delete, check notes */
display: flex;
justify-content: space-between;
align-items: center;
}
最后,使用负数, margin我们将 移到boxes边缘。
.top .box {
margin-top: -40px;
}
.bottom .box {
margin-bottom: -40px;
}
.center .left .box {
margin-left: -40px;
}
.center .right .box {
margin-right: -40px;
}
笔记:
的left和right 盒是(width: 80px),每个,这意味着margin应该是-40px(80/2 = 40),以在中心集合。
left:margin-left: -40pxright:margin-right: -40px
同为top和bottom,因为尺寸翻转。
top:margin-top: -40pxbottom:margin-bottom: -40px
这样,所有的boxes都将集中在边缘。
- 默认情况下,当用 显示时
flexbox,parent(.center) 将采用width它的内容/孩子(适合)!这意味着,width: 40px * 2因为我们在那里有2 个盒子。现在为了确保该space-between值有效,我们应该.center通过样式化它width来“拉伸”元素(父元素),它100%允许boxes 拥有尽可能多的space-between,然后每个box都将在它的位置上。
.wrapper .center {
width: 100%; /* Don't delete, check notes */
}
编码:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
position: relative;
width: 600px;
height: 600px;
border: 1px solid;
margin: 50px auto;
}
.box {
position: absolute;
border: 1px solid;
background-color: red;
}
.box.top, .box.bottom {
width: 200px;
height: 80px;
}
.box.left, .box.right {
width: 80px;
height: 200px;
}
.box.top {
top: 0;
left: 50%;
transform: translateX(-50%);
margin-top: -40px;
}
.box.bottom {
bottom: 0;
left: 50%;
transform: translateX(-50%);
margin-bottom: -40px;
}
.box.left {
top: 50%;
left: 0;
transform: translateY(-50%);
margin-left: -40px;
}
.box.right {
right: 0;
top: 50%;
transform: translateY(-50%);
margin-right: -40px;
}
.content {
padding: 40px; /* check notes */
}
编辑:
正如@anatolhiman 在评论中提到的:
但是负边距会产生一个问题,因为元素会左右溢出(尤其是在窄屏幕上)。
一个简单的解决方案:(两个例子都一样)
例如,将我们之前添加的HTML包装在另一个 中div,.container并使用 CSS 添加间距,或者padding或margin工作,取决于您的情况。
所以,问题是......
它是一个空间之内的.container?--> padding.
还是在它之外?--> margin.
给.containera background-color,调整窗口大小,并检查两者margin并padding查看差异。
HTML - 更新:
<div>
<div>
</div>
</div>
CSS - 添加:
/* outside space */
.container {margin: 50px;}
/* Or */
/* inside space */
.container {padding: 50px;}
- 您可能需要
margin在.wrapperfor 中编辑该属性top bottom。 - 添加了额外的空格 (
50px) 以包含.boxes 的空格。 - 请记住:
.wrapper{max-width: VALUE}在此功能中占有一席之地,因为它max-width是 X 但它可以更小。因此,如果属性是width:和不是,max-width那么它的行为会有所不同,并且不会按预期工作(完全响应),除非我们使用@media query或JavaScript。