javascript - 在网络日历中定位 div 元素的问题
问题描述
我正在编写一个 html 日历并被困在定位 div 上。我动态生成 div,它们相互堆叠,这在大多数情况下都可以。有时我需要创建一个 div 并将其放置在页面顶部的第一个创建的 div 旁边。如果我使用浮动,它会移动得太远并且不会到达顶部。我可以使用位置来完成此操作,但它变得复杂。看起来应该很容易将它放到顶部,就像在使用 valign=top 的表中一样。我添加了一些 css 定位来显示我想要 div 的位置,但如果可能的话,我想在没有位置的情况下这样做。任何关于定位 div 的好方法的建议将不胜感激。
#container{
width:200px;
height:200px;
background-color:blue;
}
#item1{
width:50px;
height:50px;
background-color:red;
}
#item2{
width:50px;
height:50px;
background-color:yellow;
}
#item3{
width:50px;
height:50px;
background-color:green;
float: right;
position:relative;
top:-100px;
left:-100px;
}
<div id=container>
<div id=item1></div>
<div id=item2></div>
<div id=item3></div>
</div>
解决方案
也许您应该考虑在单元格中使用网格布局。您可以更好地控制内容在单元格中的显示方式,而无需使用浮动 div。此外,网格容器中的布局可以使事物保持统一并防止重叠使用
grid-template-columns: auto auto auto;
如果您只想要顶部的 2 列,只需将其更改为
grid-template-columns: auto auto ;
这是一个链接网格布局
和
#container{
width:200px;
height:200px;
background-color:blue;
display: grid;
grid-gap: 2px;
grid-template-columns: auto auto auto;
grid-template-rows: 60px auto auto;
}
#item1{
width:50px;
height:50px;
background-color:red;
}
#item2{
width:50px;
height:50px;
background-color:yellow;
}
#item3{
width:50px;
height:50px;
background-color:green;
}
<div id=container>
<div id=item1></div>
<div id=item2></div>
<div id=item3></div>
<div id=item3></div>
<div id=item3></div>
<div id=item1></div>
<div id=item1></div>
<div id=item1></div>
</div>
推荐阅读
- java - 如何用方法引用替换 lambda
- php - 嵌入 power bi 的 wp_redirect 问题
- node.js - 丑化嵌套的 global_defs
- python - Prometheus 库 Python - 动态实例名称
- mediawiki - 如何初始化 Mediawiki 共享数据库表?
- java - 如何使用@SQLDelete 保存 modified_by 和 modified_date?
- minio - Minio 拨号 tcp 127.0.1.1:443:连接:连接被拒绝
- javascript - 无法在选择标签中选择另一个选项。始终默认为带有值的第一个选项
- flutter - 我有三个带有用户输入的列表如何创建表格颤动
- sumo - 如何在相扑中设置车辆不断变道而不是立即变道?