html - 在相对 div 中居中绝对内容

标签 html css

enter image description here如何将 span 内容居中放置在一个 div 中,并添加一个动态的 div 高度取决于出现的行数?

CSS:

div {
    position: relative; 
    height: auto; 
    width: 100%;
    margin: 0px auto;
}
span { 
    position: absolute; 
    width: 10px; 
    height: 10px; 
    background-color: #003858;
}

这是html内容:

<div>
 <span style="left: 12px; top: 0px;">&nbsp;</span>
 <span style="left: 24px; top: 0px;">&nbsp;</span>
 <span style="left: 36px; top: 0px;">&nbsp;</span>
 <span style="left: 48px; top: 0px;">&nbsp;</span>
 <span style="left: 60px; top: 0px;">&nbsp;</span>                  
 <span style="left: 12px; top: 15px;">&nbsp;</span>
 <span style="left: 24px; top: 15px;">&nbsp;</span>
 <span style="left: 36px; top: 15px;">&nbsp;</span>
 <span style="left: 48px; top: 15px;">&nbsp;</span>
 <span style="left: 60px; top: 15px;">&nbsp;</span>                 
 <span style="left: 12px; top: 30px;">&nbsp;</span>
 <span style="left: 24px; top: 30px;">&nbsp;</span>
 <span style="left: 36px; top: 30px;">&nbsp;</span>
 <span style="left: 48px; top: 30px;">&nbsp;</span>
 <span style="left: 60px; top: 30px;">&nbsp;</span>
 </div>

enter image description here

最佳答案

只需添加到您的容器 div 样式:

left: 50%;

检查这个 DEMO

您可以尝试根据您的跨度总宽度调整百分比。

关于html - 在相对 div 中居中绝对内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29473561/

相关文章:

css - polymer 铁媒体查询不适用于 dom 重复元素

html - 将我的右侧边栏带到页面顶部

css - 如何在 Material Design Lite 中输入带有前置文本的内容?

css - 将 div 扩展到 bootstraps 容器之外

javascript - iframe 链接在 iframe 内重定向

javascript - 通过点击一些改变两个 "div"的颜色,通过Javascript编程

javascript - 通过 HTML 按钮传递 PHP 函数(带参数)

javascript - 哪个css参数修改animate ('width' , 'hide' ) 函数

jquery - 制作一个值/普通文本,可点击且可更改

html - 通过 PayPal 传递选项