简体   繁体   English

将伪元素居中在父元素上并显示:table-cell

[英]Center pseudo-element on parent element with display: table-cell

How would I go about centering a pseudo-element on an element with display: table-cell ?我 go 如何将伪元素置于带有display: table-cell的元素上? My attempts have just centered it to the parent element with display: table .我的尝试只是将它集中到带有display: table的父元素。

its hard to figure out what you are trying to do when you don't provied any code, is it something like this you are trying to do?当您没有提供任何代码时,很难弄清楚您正在尝试做什么,您是否正在尝试做这样的事情? just paste this to an html document只需将其粘贴到 html 文档中

<style>
#table-container {
    display: table;
    padding: 5px;
}

.tr {
    display: table-row;
    padding: 10px;
}

.td {
    display: table-cell;
    padding: 10px;
    width: 100px;
    border: #000000 solid 1px;
    margin: 10px;
}
</style>
<div id="container">
    <div class="tr">
         <div class="td"></div>
         <div class="td"></div>
         <div class="td"></div>
    </div>
    <div class="tr">
         <div class="td"></div>
         <div class="td"></div>
         <div class="td"></div>
    </div>
</div>

To get a pseudo element to pick up values such as its dimensions and positioning from its 'owning' element you have to set their positions.要让伪元素从它的“拥有”元素中获取尺寸和定位等值,您必须设置它们的位置。

If the position of the owning element is not set then the system will search 'back up' the document until it finds an ancestor with position set and things will be set up in relation to that.如果拥有元素的 position 没有设置,那么系统将搜索“备份”文档,直到它找到一个设置了 position 的祖先,并且相关的事情将被设置。

In this snippet as a demo the pseudo element is positioned absolutely and the table-cell itself positioned relative.在这个作为演示的代码片段中,伪元素是绝对定位的,表格单元格本身是相对定位的。

 .parent { width: 20vmin; height: 20vmin; background: cyan; display: table; }.child { display: table-cell; position: relative; background: yellow; }.child::after { content: ''; width: 50%; height: 50%; background-color: pink; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
 <div class="parent"> <div class="child"></div> </div>

I solved it.我解决了。 The trick was to put a container in each cell that handled centering the element and thus its pseudo-element.诀窍是在每个处理元素及其伪元素居中的单元格中放置一个容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM