html - 将 3 个 div 浮动在另一个的顶部
问题描述
我已经写了所有内容,但不能让 3 个 div 出现在另一个的顶部。
我怎样才能使红色、蓝色和黄色相互重叠?
.box{
width:150px;
height:150px;
}
.red{
background:#bf1900;
}
.yellow{
background:#bfa900;
}
.blue{
background:#1d00bf;
}
.green{
width: 100%;
height: 100px;
background:#00700f;
position: absolute;
bottom: 0;
}
.black{
background: black;
position: absolute;
top: 0;
right: 0;
width: 250px;
}
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="box red"></div>
<div class="box yellow"> </div>
<div class="box blue"> </div>
<div class="box green">Always on the bottom with 100% width</div>
<div class="box black"><font color="white">Always on the right top</font></div>
</body>
</html>
解决方案
您可以通过使用以下属性来实现:
例如
position: absolute
- 通过使位置绝对,您可以使用top
和left
属性来移动框。
z-index: 1
- 重叠单个框
top: 10px
- 将盒子从它的容器顶部移动
left: 10px
- 将盒子从其容器的左侧移动
示例代码:
.box{
width:150px;
height:150px;
}
/* Make the 3 boxes' position absolute*/
.red, .yellow, .blue{
position: absolute;
}
/* Add z-index, top, and left properties to individual boxes */
/* Use z-index: 9999, i.e. something that's higher than the rest if you want blue box to always be on top of others. */
.red{
background:#bf1900;
z-index: 1;
top: 24px;
left: 16px;
}
.yellow{
background:#bfa900;
z-index: 2;
top: 16px;
left: 12px;
}
.blue{
background:#1d00bf;
z-index: 3;
}
.green{
width: 100%;
height: 100px;
background:#00700f;
position: absolute;
bottom: 0;
}
.black{
background: black;
position: absolute;
top: 0;
right: 0;
width: 250px;
}
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="box red"></div>
<div class="box yellow"> </div>
<div class="box blue"> </div>
<div class="box green">Always on the bottom with 100% width</div>
<div class="box black"><font color="white">Always on the right top</font></div>
</body>
</html>
推荐阅读
- mysql - sql查询在nodejs应用程序中无法正常工作
- angularjs - 在angularjs中使用@Url.Action调用控制器方法
- laravel - 选择表单选项的问题,其中 url 中的参数
- react-native - 大数据的本地存储解决方案,包括 React Native 上的图像
- python - 为什么我的“for循环”尽管遍历了所有键,但只对最后一个键起作用?
- ios - 从 Safari 的自定义 URL 启动应用程序在 iOS >12.3 中无法正常工作
- java - statement.executeQuery)在 NetBeans java 中不起作用
- php - Laravel 如何连接表和合并列,然后在 Eloquent 上设置“LIKE”查询?
- c# - 如何仅匹配上传的xml文件的指定xml标签
- python - 如何修复期望值:第 1 行第 1 列(字符 0)错误