简体   繁体   English

我可以更改在:e之后/ before伪元素内定义的内容字符串的高度吗?

[英]Can I change the height of a content string defined inside :after/before pseudo-elements?

I'm using :after pseudo-element to show the separator for the title for a card like below. 我正在使用:after伪元素来显示卡片标题的分隔符,如下所示。

 .lock-flag { font-size: 1em; } .lock-flag .lock-icon { margin-right: 0.5em; } .lock-flag .lock-icon:after { content: "-"; } .lock-flag .lock-separator::after { content: "|"; color: #666; } 
 <div class="lock-flag"> <span class="lock-icon"></span> <span class="lock-separator"></span> </div> <span class="title">Title</span> 

The problem is the separator displays correctly at normal size but when screen resizes, the separator size remains the same though the container size (lock-separator) varies based on the screen size as the font-size for body is getting changed through @media. 问题是分隔符可以正常大小正确显示,但是当调整屏幕大小时,分隔符大小保持不变,尽管容器大小(锁分隔符)根据屏幕大小而有所不同,因为通过@media更改了正文的字体大小。

Please suggest how to resize the separator size. 请提出如何调整分隔纸尺寸的建议。 Is this the right method to have separator placed like this? 这样放置分隔符是正确的方法吗?

EDIT: As per the comment suggested, changed the element from span to div for block display. 编辑:根据建议的注释,将元素从跨度更改为div以显示块。 Still the problem is same. 问题仍然是一样的。 As seen below, the expectation is to match the height of the separator to lock icon. 如下所示,期望将分隔符的高度与锁定图标相匹配。 The heights of the separator and container varies as per pictures 2 & 3. 分离器和容器的高度根据图片2和3。

1em尺寸 分离器的实际尺寸 容器的实际尺寸

My proposal: use ::pseudo on the element which size varies. 我的建议:在大小变化的元素上使用::pseudo In this demo, I chose to vary the dimensions of the .media (would be the lock icon in your case) which affects its ::after but it can be switched to be ::before of the text on the right (positioned as left: negative-value; ). 在此演示中,我选择更改.media的尺寸(在您的情况下为锁定图标),这会影响它的::after但可以将其切换为::before text(在右侧文本::before (位于left: negative-value; )。
As absolute positioning is involved, you've to make sure there's enough space between icon and text where the separator will be displayed. 由于涉及绝对定位,因此必须确保图标和文本之间有足够的空间来显示分隔符。

➡️ Codepen Code️Codepen

 .parent { display: inline-flex; /* or flex */ justify-content: space-between; align-items: center; /* vertical aligning */ height: 100%; outline: 1px dotted #aaa; } .media { position: relative; display: block; width: 2rem; height: 2rem; margin-right: 2rem; background-color: #0888; } .media::after { content: ''; position: absolute; top: 0; bottom: 0; right: -1.2rem; display: block; width: 1px; /* height: 100%; can replace t:0 b:0 */ border-left: 1px solid #444; } .tall { width: 4rem; height: 4rem; } .title { margin: 0; } section { margin: 1rem; } section:not(:first-child) { margin-top: 1rem; } 
 <section> <div class="parent"> <span class="media"></span> <p class="title">Heading</p> </div> </section> <section> <div class="parent"> <span class="media tall"></span> <p class="title">Heading</p> </div> </section> 

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

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