【问题标题】:How to vertically center text relative to a circle如何使文本相对于圆垂直居中
【发布时间】:2016-09-16 03:35:23
【问题描述】:

这是我迄今为止尝试过的:

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}
html {
  position: absolute;
}
body {
  position: relative;
}
.wrapper {
  display: table;
}
.circle {
  position: absolute;
  top: 25%;
  left: 25%;
  border-radius: 50%;
  width: 50%;
  height: auto;
  padding-top: 50%;
  background-color: #403C36;
  box-shadow: 1px 0.2px 25px 1px rgba(0, 0, 0, 0.25);
}
.content {
  display: table-cell;
  vertical-align: middle;
  position: absolute;
  top: 25%;
  left: 25%;
  width: 50%;
  height: auto;
  text-align: center;
}
<div class="wrapper">
  <div class="circle"></div>
  <div class="content">I want this to be centered vertically in the circle</div>
</div>

我无法将文本放入圆圈中,因为圆圈的padding-top 将其纵横比保持在 1:1。但是,如果有办法解决这个问题,我会接受的。

更新

这不能通过假设圆圈或内容的宽度和高度来解决,因为它是动态的,基于窗口的大小和用户提供的内容。可能是一两段文字。

【问题讨论】:

标签: html css geometry aspect-ratio


【解决方案1】:

我想我明白了,有两个 div:

<div class="circle">
<div class="text">
TEXT multiline
will be
</div>
</div>

CSS:

.circle {

  border-radius: 50%;
  width: 50%;
  height: auto;
 padding-top:50%;
  background-color: #403C36;
  box-shadow: 1px 0.2px 25px 1px rgba(0, 0, 0, 0.25);
  position:relative;
}

.text {

  position:absolute;
  color:#fff;
  text-align:center;
width:100%;
 transform: translateY(-50%);

top: 50%;


}

小提琴:https://jsfiddle.net/njngv3qk/

【讨论】:

  • 我也喜欢这种方法,但flex 指令似乎比使用 CSS 转换更简洁,所以我会采用这种方法。
【解决方案2】:

在您的课程内容中更改或添加以下属性。

HTML

<div class="wrapper"> <div class="circle"></div> <div class="content">I want this to be centered vertically in the circle</div> </div> CSS

.content {
  position: absolute;
  top: 92%;
  left: 25%;
  width: 50%;
  height: auto;
  text-align: center;
  transform : rotate(270deg);
}

【讨论】:

    【解决方案3】:

    你不需要一个包装 div,只需要一个 div

    HTML

    <div class="circle">Hello, World</div>
    

    CSS

    .circle {
      width: 10%;
      height: 30%;
      padding: 30%;
      border-radius: 50%;
      background-color: #001100;
      text-align: center;
      color: #ffffff;
    }
    

    而且,抱歉,我不确定如何创建帖子的“sn-ps”部分。

    编辑更新的比率,

    宽1:高3:宽3

    【讨论】:

    • 这不行,那不再是圆了,是椭圆了。
    • 保持widthheightpadding 相同的值会产生一个圆圈
    • 我已经测试了更多,并添加了一个新的比例......似乎更圆,除非你设置了宽度
    【解决方案4】:

    我会建议这样的事情

    body {
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .circle {
      width: 50%;
      padding-top: 50%;
      background: brown;
      border-radius: 50%;
      position: relative;
    }
    .content {
      position: absolute;
      width: 100%;
      height: 100%;
      border-radius: inherit;
      background: pink;
      top: 0;
      left: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      text-align: center
    }
    <div class="circle">
    
      <div class="content">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore, tenetur!</p>
      </div>
    </div>

    【讨论】:

    • 这是迄今为止最接近的答案,我可能会接受它。只是一个简单的问题,如果里面的圆圈和文本没有相对于页面居中,是否有可能?我只需要在.circle 之外添加另一个父元素来确定位置?
    • 如何使原始圆圈居中取决于您...但这不是本问题的主题。我应用于body 的 flex 属性将适用于圆的任何容器,以使 在该容器中居中。
    • 我刚试过这个,它弄乱了内容的格式。尝试添加另一个&lt;p&gt;,它将它们并排放置,而不是垂直堆叠。有什么办法可以改变吗?
    • 没关系,我在 .content 和 HTML 之间添加了另一个包装器,它修复了它。
    • 如果您想要多个段落,请在内容 div 中添加 flex-direction:column
    【解决方案5】:

    您可以在 (wrapper) 中插入文本 (content)。

    这可能是一个可能的解决方案: jsFiddle

    html,
    body {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
    }
    html {
      position: absolute;
    }
    body {
      position: relative;
    }
    .wrapper {
      display: table;
    }
    .circle {
      position: absolute;
      top: 25%;
      left: 25%;
      border-radius: 50%;
      width: 50%;
      height: auto;
      padding-top: 50%;
      background-color: #403C36;
      box-shadow: 1px 0.2px 25px 1px rgba(0, 0, 0, 0.25);
    }
    .content {
      position: absolute;
      top: 50%;
      left: 25%;
      width: 50%;
      text-align: center;
    }
    <div class="wrapper">
      <div class="circle">
        <div class="content">I want this to be centered vertically in the circle</div>
      </div>
    
    </div>

    【讨论】:

    • 不是top: 45%;,你怎么不用vertical-align: middle
    • 没关系,我明白为什么了。这仍然没有真正使文本居中。充其量这假设div.content 的尺寸是固定的。
    • @PatrickRoberts 如果您更改顶部:45%;到顶部:50% 它将使文本垂直居中。
    • 只有在一行文本中才是正确的。内容的大小也是未知的。
    猜你喜欢
    • 1970-01-01
    • 2018-07-07
    • 2012-04-14
    • 2015-02-23
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多