简体   繁体   English

如何在跨度文本上使背景色变为圆形

[英]How to make background-color circular on a span text

I have a spanned text 1 with background color red. 我有一个跨区文本1与背景颜色为红色。 I want to make the background color round which will enclose the spanned text 1 . 我想使背景颜色变圆,将包围跨文本1 But what i have at the moment isn't doing the trick. 但是我现在没有解决这个问题。

How can i get this done? 我怎样才能做到这一点?

 .text-span{ background:red; border-radius:20px; } 
 <span class="text-span">1</span><span>info</span> 

First, you need to display it inline-block. 首先,您需要内联显示它。 This allows you to manipulate its size. 这使您可以操纵其大小。

Then, you need to give him the same height, same width to make it square. 然后,您需要给他相同的高度,相同的宽度以使其成正方形。

After that, you will make a border radius of 50% so that it becomes round. 之后,将边界半径设置为50%,以使其变为圆形。

Finally, you can center it by giving it the line-height of the height, and center aligning the text. 最后,您可以通过将其设置为高度的行高并将文本居中对齐来居中。

And voilà ! 和瞧!

 .text-span{ background:red; border-radius:50%; height: 26px; width: 26px; line-height: 26px; display: inline-block; text-align: center; margin-right: 6px; } 
 <span class="text-span">1</span><span>info</span> 

Try this : 尝试这个 :

 .text-span{ background:red; border-radius:50%; display:inline-block; width:20px; height:20px; text-align:center; } 
 <span class="text-span">1</span><span>info</span> 

Hope it helps!! 希望能帮助到你!!

You can't absolutely center the content inside the circle using just the padding. 您不能仅使用填充将内容绝对居中于圆内。 I suggest you this snippet to center anything inside it with a simple snippet. 我建议您使用此代码段,以一个简单的代码段将其中的任何内容居中。

This solution have just a note, you need to set the circle size big enough or crop the content with the text-overflow property. 此解决方案仅需注意,您需要将圆的大小设置得足够大或使用text-overflow属性裁剪内容。

 .text-span { display: inline-block; background: rebeccapurple; border-radius: 50%; position: relative; width: 30px; height: 30px; font-family: sans-serif; margin-right: 8px; vertical-align: middle; } .text-span::before { content: attr(data-content); color: #FFF; position: absolute; top: 50%; left: 50%; font-size: 13px; font-weight: 700; line-height: 1; transform: translate(-50%, -50%); } 
 <div> <span class="text-span" data-content="1"></span> info </div> <div> <span class="text-span" data-content="99"></span> info </div> 

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

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