简体   繁体   English

带有border-radius的div中心文本

[英]Center text in div with border-radius

I want to vertical and horizontal center text in only one div which has a surrounding circle made with border-radius. 我想在只有一个div的垂直和水平中心文本中使用border-radius制作一个环绕圆圈。 Currently the text is shown in the lower right corner tho... 目前文字显示在右下角......

Look here for the fiddle: http://jsfiddle.net/gvoyu830/ 看这里的小提琴: http//jsfiddle.net/gvoyu830/

My css: 我的css:

DIV {
  display: inline-block;
  width: 10%;
  border: 1.2em solid #dddddd;
  border-radius: 50%;
  max-width: 1.2em;
  max-height: 1.2em;
  box-sizing: border-box;    
  margin: 0 auto;
}

Any ideas? 有任何想法吗?

It can be achieved simply by setting height / width of divs and a line-height which is equal to the height in order for vertical alignment. 它可以简单地通过设置来实现height / width的div和一个line-height ,其等于所述height ,以便为垂直取向。

text-align: center will do the horizontal alignment. text-align: center将执行水平对齐。

 div { display: inline-block; text-align: center; /* width: 10%; */ /* border: 1.2em solid #dddddd; */ background-color: #ddd; width: 2.4em; height: 2.4em; line-height: 2.4em; border-radius: 50%; /* max-width: 1.2em; */ /* max-height: 1.2em; */ /* box-sizing: border-box; */ /* margin: 0 auto; */ } 
 <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> 

http://jsfiddle.net/gvoyu830/1/ Center in a circle http://jsfiddle.net/gvoyu830/1/ Center in a circle

I did it for you, but only with relative position. 我是为你做的,但只是相对位置。 (else, the text align: center; don't exactely works because the begin of your string is the left of the number) (否则,文本对齐:中心;不要行事,因为你的字符串的开头是数字的左边)

JSFiddle 的jsfiddle

Add

text-align:center;
display: table-cell;
vertical-align: middle;

Example: 例:

DIV {
    display: inline-block;
    width: 10%;
    border: 1.2em solid #dddddd;
    border-radius: 50%;
    max-width: 1.2em;
    max-height: 1.2em;
    box-sizing: border-box;    
    margin: 0 auto;
   text-align:center;
    display: table-cell;
    vertical-align: middle;

}

Really want to contribute. 真的很想贡献。 This is what I've got: 这就是我所拥有的:

DIV {
    display:inline-block;
    width: 10%;
    border: 1.5em solid #dddddd;
    border-radius: 50%;
    max-width: 1.15em;
    max-height: 1.15em;
    margin: 0 auto;
    text-align:center;
}

Fiddle Here 在这里小提琴

UPDATED : (with background the same as border color) JsFiddle 更新 :(背景与边框颜色相同) JsFiddle

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

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