简体   繁体   English

之后的html伪元素的对齐

[英]Alignment of html pseudo-element after

How I can make center align my pseudo-element after? 之后如何使中心对齐我的伪元素? Like margin: 0 auto; 像边距:0自动; for block elements. 用于块元素。

<div>
    This is test text. This is test text. This is test text.
</div>

CSS code: CSS代码:

  div:after {
    position: absolute;
    display: block;
    content:"";

    border-color: #EAB920 transparent transparent transparent;
    border-style: solid;
    border-width: 2.5em;
    width: 0;
    height: 0;
}

It after element look like simple triangle. 后元素看起来像简单的三角形。 Property margin: 0 auto; 财产保证金:0自动; does not work. 不起作用。

If you want the pseudo element centered horizontally relative to the text, you set the display of the targeted element to inline-block so that the width shrinks to fit the content. 如果要使伪元素对于文本水平居中,则将目标元素的display设置为inline-block以便缩小宽度以适合内容。 Then remove the absolute positioning from the pseudo element and margin: auto will work as expected. 然后从伪元素和margin: auto删除绝对位置margin: auto将按预期工作。

Example Here 这里的例子

 .target { display: inline-block; } .target:after { content: ''; display: block; margin: auto; border-color: #EAB920 transparent transparent transparent; border-style: solid; border-width: 2.5em; width: 0; height: 0; } 
 <div class="target">This is test text. This is test text. This is test text.</div> 

If you don't want the pseudo element centered horizontally relative to the text, don't set the element to inline-block - (example) 如果您不希望伪元素相对于文本水平居中,请不要将元素设置为inline-block (示例)

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

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