css:如何在中间画出带文字的圆圈?

dot*_*dot 136 css css-shapes

我在stackoverflow上找到了这个例子:

仅使用css绘制圆形

哪个好.但我想知道如何修改该示例,以便我可以在圆圈中间包含文本?

我也发现了这一点: 在CSS中以圆圈为中心的文本垂直和水平居中(如iphone通知徽章)

但由于某种原因,它不适合我.当我创建以下测试代码时:

<div class="badge">1</div>
Run Code Online (Sandbox Code Playgroud)

而不是一个圆圈,我得到一个椭圆形状.我正在尝试使用代码来了解如何让它工作.

Jaw*_*wad 301

的jsfiddle

.circle {
  width: 500px;
  height: 500px;
  border-radius: 50%;
  font-size: 50px;
  color: #fff;
  line-height: 500px;
  text-align: center;
  background: #000
}
Run Code Online (Sandbox Code Playgroud)
<div class="circle">Hello I am A Circle</div>
Run Code Online (Sandbox Code Playgroud)

  • 我个人删除line-height属性并添加vertical-align:middle; 显示:table-cell; 这样你的文本行仍然会居中.http://jsfiddle.net/z9bLtw99/ (12认同)
  • @dot:不是我真的在做 - http://bryanhadaway.com/how-to-create-circles-with-css/ (7认同)
  • 如果你可以使`border-radius:50%;`使你的代码事件更加优雅和便携,而不必每次都根据宽度和高度更改此属性;) (3认同)
  • 多行怎么样? (3认同)
  • @DamjanPavlica 查看未明确设置任何宽度或高度的 **[answer](http://stackoverflow.com/a/9359039/607874)** 的最新更新。也适用于多行,因为您可以在 jsfiddle 上进行实时验证 (2认同)

cim*_*non 56

如果你的内容要包装并且身高未知,这是你最好的选择:

http://cssdeck.com/labs/aplvrmue

.badge {
  height: 100px;
  width: 100px;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  border-radius: 50%; /* may require vendor prefixes */
  background: yellow;
}
Run Code Online (Sandbox Code Playgroud)

.badge {
  height: 100px;
  width: 100px;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  border-radius: 50%;
  background: yellow;
}
Run Code Online (Sandbox Code Playgroud)
<div class="badge">1</div>
Run Code Online (Sandbox Code Playgroud)

  • 这会产生椭圆形,而不是完美的圆形. (4认同)
  • 这是真正的解决方案。虽然我必须提到,`display: absolute` 打破了alingment - 但简单的解决方案是将它包装在另一个div中。 (2认同)
  • 对于绝对定位,您将使用包装器。但它给出了一个椭圆,因为它可以达到的最大宽度为 180 像素。因此,如果您将 min-width 指定为所需的宽度并将高度也设置为该值。你会得到一个圆圈。否则你会得到一个超过宽度和高度 &gt; 180px 的椭圆。 (2认同)

Moh*_*man 20

你可以使用css3 flexbox.

HTML:

<div class="circle-with-text">
    Here is some text in circle
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.circle-with-text {
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  display: flex;
}
Run Code Online (Sandbox Code Playgroud)

这将允许您具有垂直和水平中间对齐的单行和多行文本.

body {
  margin: 0;
}
.circles {
  display: flex;
}
.circle-with-text {
  background: linear-gradient(orange, red);
  justify-content: center;
  align-items: center;
  border-radius: 100%;
  text-align: center;
  margin: 5px 20px;
  font-size: 15px;
  padding: 15px;
  display: flex;
  height: 180px;
  width: 180px;
  color: #fff;
}
.multi-line-text {
  font-size: 20px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="circles">
  <div class="circle-with-text">
    Here is some text in circle
  </div>
  <div class="circle-with-text multi-line-text">
    Here is some multi-line text in circle
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 理想世界中的最佳解决方案。遗憾的是,flexbox 的实现中仍然存在许多跨浏览器错误:https://github.com/philipwalton/flexbugs (2认同)

小智 12

我想你想用椭圆形或圆形写文字?为什么不是这个?

<span style="border-radius:50%; border:solid black 1px;padding:5px">Hello</span>
Run Code Online (Sandbox Code Playgroud)


Ash*_*ish 11

使用 HTML 标签和不使用 CSS 绘制一个中间带有文本的圆圈

为此,HTML 具有 SVG 标记。如果您不想使用 CSS,则可以遵循此标准方法。

 <svg width="100" height="100">
   <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="white" />
     Sorry, your browser does not support inline SVG.
   <text fill="#000000" font-size="18" font-family="Verdana"
     x="15" y="60">ASHISH</text>
 </svg>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 供临时读者将来参考:如代码和图像所示,文本居中只是因为“font-size”、“font-family”、“x”和“y”对齐。对于任何未安装 Verdana 的人来说,它都不会居中。使用`x =“50%”y =“50%”dominant-baseline =“middle”text-anchor =“middle”`也不能解决问题。 (2认同)

Dav*_*ord 8

对于网页设计我最近得到了一个固定的圆圈问题,我必须解决中心和未知数量的文本,我想我会在这里分享解决方案,以寻找圈子/文本组合的其他人.

我遇到的主要问题是文本经常打破圆圈的界限.为了解决这个问题,我最终使用了4个div.一个矩形容器,指定圆的最大(固定)边界.在内部将是绘制圆的div,其宽度和高度设置为100%,因此更改父级的大小会更改实际圆的大小.在里面将是另一个矩形div,使用%'s,将创建一个文本边界区域,防止任何文本离开圆圈(大部分)然后最后是文本的实际div和垂直居中.

代码更有意义:

/* Main Container -  this controls the size of the circle */
.circle_container
{
	width : 128px;
	height : 128px;
	margin : 0;
	padding : 0;
/*	border : 1px solid red; */
}

/* Circle Main draws the actual circle */
.circle_main
{
	width : 100%;
	height : 100%;
	border-radius : 50%;
	border : 2px solid black;	/* can alter thickness and colour of circle on this line */
	margin : 0;
	padding : 0;
}

/* Circle Text Container - constrains text area to within the circle */
.circle_text_container
{
	/* area constraints */
	width : 70%;
	height : 70%;
	max-width : 70%;
	max-height : 70%;
	margin : 0;
	padding : 0;

	/* some position nudging to center the text area */
	position : relative;
	left : 15%;
	top : 15%;
	
	/* preserve 3d prevents blurring sometimes caused by the text centering in the next class */
	transform-style : preserve-3d;
	
	/*border : 1px solid green;*/
}

/* Circle Text - the appearance of the text within the circle plus vertical centering */
.circle_text
{
	/* change font/size/etc here */
	font: 11px "Tahoma", Arial, Serif;	
	text-align : center;
	
	/* vertical centering technique */
	position : relative;
	top : 50%;
	transform : translateY(-50%);
}
Run Code Online (Sandbox Code Playgroud)
<div class="circle_container">
	<div class="circle_main">
		<div class="circle_text_container">
			<div class = "circle_text">
				Here is an example of some text in my circle.
			</div>
		</div>
	</div>
</div>			
Run Code Online (Sandbox Code Playgroud)

您可以取消注释容器div上的边框颜色,以查看它的约束方式.

需要注意的事项:如果您放入太多文本或使用太长的文字/完整文本,您仍然可以打破圆圈的界限.它仍然不适合完全未知的文本(例如用户输入),但如果您隐约知道需要存储的文本数量最多,并相应地设置圆形大小和字体大小,则效果最佳.您可以设置文本容器div以隐藏任何溢出当然,但这可能只是看起来"破碎"并且不能替代在您的设计中正确地考虑最大尺寸.

希望这对某人有用!HTML/CSS不是我的主要学科,所以我相信它可以改进!


Dam*_*ica 8

对我来说,只有这个解决方案适用于多行文本:

.circle-multiline {
    display: table-cell;
    height: 100px;
    width: 100px;
    text-align: center;
    vertical-align: middle;
    border-radius: 50%;
    background: yellow;
}
Run Code Online (Sandbox Code Playgroud)
<div class="circle-multiline">Hello Wonderful World</div>
Run Code Online (Sandbox Code Playgroud)


Bru*_*mes 6

如果只有一行文本,则可以使用line-height属性,其值与元素的高度相同:

height:100px;
line-height:100px;
Run Code Online (Sandbox Code Playgroud)

如果文本有多行,或者内容是可变的,则可以使用padding-top:

padding-top:30px;
height:70px;
Run Code Online (Sandbox Code Playgroud)

示例:http//jsfiddle.net/2GUFL/


Fro*_*per 6

从 YouTube 页面得到这个,它的设置非常简单。绝对可维护和可重复使用。

.circle {
    position: absolute;
    top: 4px;
    color: white;
    background-color: red;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    line-height: 18px;
    font-size: 10px;
    text-align: center;
    cursor: pointer;
    z-index: 999;
}
Run Code Online (Sandbox Code Playgroud)
<div class="circle">2</div>
Run Code Online (Sandbox Code Playgroud)


Lig*_*gth 5

当然,你必须使用标签来做到这一点.一个为文本创建圆圈和其他.

这里有些代码可以帮到你

#circle {
    background: #f00;
    width: 200px;
    height: 200px;
    border-radius: 50%;
    color:black;

}
.innerTEXT{
    position:absolute;
    top:80px;
    left:60px;
}

<div id="circle">
    <span class="innerTEXT"> Here a text</span>
</div>
Run Code Online (Sandbox Code Playgroud)

这里的实例http://jsbin.com/apumik/1/edit

更新

这里的一些变化不那么小

http://jsbin.com/apumik/3/edit


小智 5

如果你使用Foundation 5和Compass框架,你可以尝试这个。

.sass 输入

$circle-width: rem-calc(25) !default;
$circle-height: $circle-width !default;
$circle-bg: #fff !default;
$circle-radius: 50% !default;
$circle-line-height: $circle-width !default;
$circle-text-align: center !default;

@mixin circle($cw:$circle-width, $ch:$circle-height, $cb:$circle-bg, $clh:$circle-line-height, $cta:$circle-text-align, $cr:$circle-radius) {
    width: $cw;
    height: $ch;
    background: $cb;
    line-height: $clh;
    text-align: $cta;
    @include inline-block;
    @include border-radius($cr);
}

.circle-default {
    @include circle;
}
Run Code Online (Sandbox Code Playgroud)

.css 输出

.circle-default {
  width: 1.78571rem;
  height: 1.78571rem;
  background: white;
  line-height: 1.78571rem;
  text-align: center;
  display: -moz-inline-stack;
  display: inline-block;
  vertical-align: middle;
  *vertical-align: auto;
  zoom: 1;
  *display: inline;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
  -ms-border-radius: 50%;
  -o-border-radius: 50%;
  border-radius: 50%;
}
Run Code Online (Sandbox Code Playgroud)


pgf*_*ing 5

HTML:

<div class="circle">
        <p class="inner">HELLO</p>
    </div>

Run Code Online (Sandbox Code Playgroud)

CSS:

.circle { 
background-color: darkgoldenrod;
height: 500px; width: 500px; 
border-radius: 50%;}

.inner {
    position: relative;
    top: 50%;
    text-align: center;
    line-height: 0px;
 
}
Run Code Online (Sandbox Code Playgroud)

第一个 div 类表示正在绘制的圆,您可以更改高度和宽度以适合您的使用。

内部类表示文本的位置,使用positionrelative可以让文本停留在圆圈内。

前 50% 垂直居中,文本对齐水平居中。0 的行高只会让它更清晰或更准确。