How do I responsively center text inside of a <div>

亡梦爱人 提交于 2019-12-18 09:45:54

问题


I have a <div> that contains three <div>s. In each of those <div> elements is a <p> element with text and 2 nested elements to make a radial progress bar. What I need is to put the text in the middle of the circles, and do it responsively using pure CSS. I need something like this:

The code has flaws, like that <p> inside of a <span> but I am fixing it in the new version with the help you guys provide.

.radius-container div {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.radius-container div:first-child {
  margin-right: 1%;
}

.radius {
  padding-top: 11em;
  height: 30em;
  text-align: center;
  border: 1px solid #858280;
  display: block;
  border-radius: 50%;
  width: 100%;
}

.radius3 {
  position: relative;
  padding-top: 10%;
  height: 15em;
  width: 50%;
  text-align: center;
  border: 1px solid #858280;
  border-left: 0;
  border-bottom: 0;
  border-top-right-radius: 100%;
  display: block;
  margin-left: 15em;
}

.radius3 p {
  position: absolute;
  right: 50%;
  top: 65%;
}
<div class="radius-container">
  <div><span class="radius"><p>SERBIAN<br>100%</p></span></div>
  <div><span class="radius"><p>ENGLISH<br>100%</p></span></div>
  <div><span class="radius3"><p>GERMAN<br>25%</p></span></div>
</div>

See also this jsFiddle


回答1:


After researching for long time for this issue, I found an generic solution which solves this kind of requeriments. I am proud of it, simple and elegant :)

.center-element{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

Hoping it helps, any doubt let me know. Cheers mate :)




回答2:


This code might help you.

 .innerDiv { 
  height: 100px;
  width: 100px;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  border-radius: 50%;
  }



回答3:


You can make a div with a border-radius to 50%. After, you can use the flex display to center verticaly and horizontaly.

html

div {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}
div div {
  display: inline-flex;
  flex-direction: column;
  width: 31%;
  margin: 1%;
  border-radius: 50%;
  border: 1px solid #999;
  align-items: center;
  justify-content: center;
}
div div span {
  text-align: center;
  color: #999;
}
<div>
  <div><span>Serbian</span><span>100%</span>
  </div>
  <div><span>Serbian</span><span>50%</span>
  </div>
  <div><span>Serbian</span><span>25%</span>
  </div>
</div>


来源:https://stackoverflow.com/questions/41721586/how-do-i-responsively-center-text-inside-of-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!