圆形div具有垂直对齐的文本和字幕

时间:2016-07-06 09:06:45

标签: html css web

我觉得这很明显,但我似乎无法在这里找到任何答案。

我试图制作5个圆形div,内联,中间有一个代表成绩的字母。然后,我希望主题标题低于成绩(但仍在圈内)。

Here is an example of what I'm trying to do.

这是我到目前为止所写的内容:

HTML:

<div class="subject">A</div>
<div class="subject">B</div>
<div class="subject">C</div>
<div class="subject">D</div>
<div class="subject">E</div>

CSS:

.subject {
    display: inline-block;
    background-color: gray;
    width: 200px;
    height: 200px;
    line-height: 200px;
    text-align: center;
    font-family: Arial;
    border-radius: 100%;
    color: white;
    font-size: 100px;

另外,我似乎无法使用以下方式将圆圈水平居中:

margin: 0 auto;

margin-left: auto;
margin-right: auto;

&#13;
&#13;
.subject {
  display: inline-block;
  background-color: gray;
  width: 200px;
  height: 200px;
  line-height: 200px;
  text-align: center;
  font-family: Arial;
  border-radius: 100%;
  color: white;
  font-size: 100px;
}
&#13;
<div class="subject">A</div>
<div class="subject">B</div>
<div class="subject">C</div>
<div class="subject">D</div>
<div class="subject">E</div>
&#13;
&#13;
&#13;

感谢任何建议,谢谢!

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作,我创建了另一个div并添加了text-align:center属性。现在这些圆圈水平居中。

&#13;
&#13;
.subject {
    display: inline-block;
    background-color: gray;
    width: 200px;
    height: 200px;
    line-height: 200px;
    text-align: center;
    font-family: Arial;
    border-radius: 100%;
    color: white;
  font-size: 100px;}

.circle{
  text-align:center;
}
&#13;
<div class="circle">
<div class="subject">A</div>
<div class="subject">B</div>
<div class="subject">C</div>
<div class="subject">D</div>
<div class="subject">E</div></div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

div包裹在容器中并向其添加text-align: center

使用伪元素:after插入字幕。请查看以下示例

.container {
  text-align: center;
}
.subject {
  border-radius: 100%;
  display: inline-block;
  background-color: gray;
  width: 200px;
  height: 200px;
  line-height: 200px;
  text-align: center;
  font-family: Arial;
  border-radius: 100%;
  color: white;
  font-size: 100px;
  position: relative;
}
.subject:after {
  display: inline-block;
  position: absolute;
  font-size: 16px;
  content: attr(data-sub);
  left: 50%;
  transform: translateX(-50%);
  top: 30%;
}
<div class="container">
  <div class="subject" data-sub="Maths">A</div>
  <div class="subject" data-sub="French">B</div>
  <div class="subject" data-sub="Biology">C</div>
  <div class="subject" data-sub="German">D</div>
  <div class="subject" data-sub="Chemistry">E</div>
</div>