如何将文本垂直和水平居中对齐?

时间:2017-11-06 06:52:56

标签: html css css-selectors

我正在尝试将文本放在我的div中,这是一个圆形,但它不起作用。垂直对齐和对齐文本/内容/项目不起作用。我不知道还能做什么。此外,插入"数字"影响对齐?



.stats {
  width: 100%;  
  display: inline-flex;
  height: auto;
  vertical-align: middle;
  text-align: center;
  align-content: space-around;
  padding: 20px;
  overflow: hidden;
  white-space: nowrap;
  vertical-align: middle;
}

.users{
    background-color: #000;
    color: #fff;
    margin-right: auto;
    margin-left: auto;
    padding: 5px;
    text-align: center;
    vertical-align: middle;
    height: 300px;
    display: inline-block;
    width: 315px;
    border-radius: 50%; 
    font-size: 14px;
}

   <div class="stats" >
            <div class="users ">
                <h2>Over <figure class="text-stat">#</figure> users and counting...</h2>
            </div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

添加了flex并修复了一些其他未使用的样式,希望它有所帮助!

&#13;
&#13;
* {
    box-sizing: border-box;
}

.stats {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: auto;
    padding: 20px;
    overflow: hidden;
    white-space: nowrap;
}

.users {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: black;
    color: white;
    padding: 5px;
    text-align: center;
    height: 300px;
    width: 315px;
    border-radius: 50%; 
    font-size: 14px;
}
&#13;
<div class="stats" >
    <div class="users">
        <h2>Over <figure class="text-stat">
            <span class="text-stat__number">#</span>
        </figure> users and counting...</h2>
    </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试使用display:flex;

.users {
    display: flex;
    align-items: center;
    justify-content: center;
}

&#13;
&#13;
.stats {
  width: 100%;
  display: inline-flex;
  height: auto;
  vertical-align: middle;
  text-align: center;
  align-content: space-around;
  padding: 20px;
  overflow: hidden;
  white-space: nowrap;
  vertical-align: middle;
}

.users {
  background-color: #000;
  color: #fff;
  margin-right: auto;
  margin-left: auto;
  padding: 5px;
  text-align: center;
  vertical-align: middle;
  height: 300px;
  display: inline-block;
  width: 315px;
  border-radius: 50%;
  font-size: 14px;
}

.stats {
  width: 100%;  
  display: inline-flex;
  height: auto;
  vertical-align: middle;
  text-align: center;
  align-content: space-around;
  padding: 20px;
  overflow: hidden;
  white-space: nowrap;
  vertical-align: middle;
}

.stats {
  width: 100%;  
  display: inline-flex;
  height: auto;
  vertical-align: middle;
  text-align: center;
  align-content: space-around;
  padding: 20px;
  overflow: hidden;
  white-space: nowrap;
  vertical-align: middle;
}

.users{
    display: flex;
    align-items: center;
    justify-content: center;
}
&#13;
<div class="stats">
  <div class="users ">
    <h2>Over
      <figure class="text-stat">#</figure> users and counting...</h2>
  </div>
&#13;
&#13;
&#13;