【问题标题】:Center text in a circle css3 issue在一个圆圈中居中文本 css3 问题
【发布时间】:2018-07-12 08:59:54
【问题描述】:

我正在尝试制作带有文字的圆圈。问题是当字体变大时……文本与圆圈重叠。我该如何解决这个问题?

.circle {
    display: inline-block;
    font-size: 42px;
}

.circle label {
    cursor:pointer;
    height: 200px;
    width: 200px;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    border-radius: 50%;
    background: yellow;
}

label input[type="checkbox"] {
    display:none;
}
input[type="checkbox"]:checked + span {
    color: #fff;
}

JSFiddle Demo

【问题讨论】:

  • 你想让圆保持一个圆,还是变成一个椭圆可以?您希望内容缩小,还是圈子扩大?您希望所有圈子保持相同大小吗?
  • 只圈@FacundoCorradini
  • @BondBaby 文本可以换行还是需要保持在一行并增长?
  • 增加圆圈大小
  • @BondBaby 在下面查看我的答案。这不是一件容易的事,但我认为这会奏效。需要注意的是:您的 HTML 肯定已损坏。您绝对不应该在标签标签内使用跨度和输入。尝试重组为更有意义的东西

标签: html css styles stylesheet


【解决方案1】:

在根据内容调整 div 大小的同时保持圆形(不是椭圆形/椭圆形)根本不是一件容易的事。

有一种技术使用具有 100% 宽度和 100% padding-bottom 的绝对定位伪元素来保持圆圈.. 一个圆圈。

它依赖于这样一个事实,即 padding-top 和 padding-bottom 的百分比是根据宽度而不是大多数人期望的高度计算的,以防止无限循环。听起来违反直觉,但它确实有效。

然后是实际内容不是 100% 圆高度的问题(包装器也不是,因为圆是绝对定位的),因此使内容居中也是一项挑战。再一次,在 padding-top 上使用 % 所以它是根据宽度 + 负变换计算的: translateY 就可以了。

最后但并非最不重要的一点是,将单词保持在不同的行是宽度的工作:最小内容。

所有这一切,结果如下:

body{
/*just to display circles inline and centered*/
  display:flex; 
  align-items:center;
  flex-wrap: wrap;
}

.circle{
  padding:1em;
}

.inner{
/*centers the content*/
  padding:100% 20px 0 20px;
  transform:translateY(-50%);
/*sets the width as the biggest word*/
  width:min-content;
/*styling*/
  text-align:center; color:white; font-weight:bold; font-family:sans-serif; 
/*sets the label as inline-block, so it doesn't take 100% width*/
  display:inline-block;
/*prevents clicks outside the circle from switching the checkbox*/
  pointer-events:none;
}
.inner::before{
    content:"";
    position:absolute;
/*adjust for the padding + translateY on the element*/
    top:50%; left:0;
/*sets the width as 100% of the element*/
    width:100%;
/*sets the padding-bottom (and therefore, the height) as 100% the width of the element*/
    padding-bottom:100%;
/*styling*/
    background-color:steelblue;
    border-radius:50%;
/*puts it behind the content*/
    z-index:-1;
/*resets the pointer-events so clicking on the circle affects the checkbox */
    pointer-events:auto;
    cursor:pointer;
}
<input type="checkbox" id="check1">
<div class="circle">
  <label for="check1" class="inner">Really biiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiig circle</label>
</div> 

<input type="checkbox" id="check2">
<div class="circle">
  <label for="check2" class="inner">small circle</label>
</div> 

请注意,我已将标签调整为 .inner 标签,但将指针事件设置为无,然后在伪元素上将其重置,以防止在圆外(但在框内)的点击切换复选框

【讨论】:

  • 如何在不使用javascript的情况下让复选框与圆圈相对应?
  • @BondBaby 使用 .inner 作为标签而不是 div,将显示设置为 inline-block 这样它们就不会占用 100%,并且可能使用指针事件来防止圆圈外的点击切换关联的复选框。我已将其添加到 sn-p ;)
  • @BondBaby 请记住 min-content 部分在 IE 或 Edge 上不起作用,因此您可能需要手动在文本上插入换行符
【解决方案2】:

添加填充和溢出属性

.circle label {
  cursor: pointer;
  height: 200px;
  width: 200px;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  border-radius: 50%;
  background: yellow;
  overflow: hidden;  # add this
  padding: 10px;     # add this
}

【讨论】:

    【解决方案3】:

    使用CSS 属性word-break: 然后您可以将其值设置为break-all

    .circle label {
      word-break: break-all; 
    }
    

    请参阅文档:https://www.w3schools.com/cssref/css3_pr_word-break.asp

    .circle {
      display: inline-block;
      font-size: 42px;
    }
    
    .circle label {
      cursor: pointer;
      height: 200px;
      width: 200px;
      display: table-cell;
      text-align: center;
      vertical-align: middle;
      border-radius: 50%;
      background: yellow;
      word-break: break-all;
    }
    
    label input[type="checkbox"] {
      display: none;
    }
    
    input[type="checkbox"]:checked + span {
      color: #fff;
      font-size: 42px;
    }
    <div class="circle">
      <label>
        <input type="checkbox" id="Technology Operations" value="on"><span>Technology sdfsdfsdfsdf</span></label>
    </div>

    【讨论】:

      【解决方案4】:

      如果您希望它在编辑后成为一个完整的圆圈,请添加一个 padding,可能为 100px。这样之后形状仍然是圆形,因为填充均匀地应用于所有侧面。

       .circle label {
          padding: 100px; 
         }
      

      这将与

      相同
      padding: 100px 100px 100px 100px;
      

      填充:上、右、下、左。

      【讨论】:

        【解决方案5】:

        您可以在 CSS 中添加一些 padding

        https://jsfiddle.net/jve51qmb/7/

        .circle label {
          cursor: pointer;
          height: 200px;
          width: 200px;
          display: table-cell;
          text-align: center;
          padding: 20px 10px 10px 20px;
          vertical-align: middle;
          border-radius: 50%;
          background: yellow;
        }
        

        【讨论】:

        • 感谢@FacundoCorradini 修正了我的错字
        • 它没有在小提琴上工作,它只是将前 20px 应用于所有,然后忽略所有其他值。而且您的代码现在指示左侧和顶部填充 20px,右侧和底部填充 10px……这也没有任何意义。
        • 我知道我在演示用户可以为他们需要的边设置不同的填充。如果你注意到在小提琴中它正在应用直线 20。
        • 它不是一个圆圈 :(
        猜你喜欢
        • 1970-01-01
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 2013-07-09
        • 2015-11-05
        • 2022-06-29
        • 2020-06-06
        • 2016-04-25
        相关资源
        最近更新 更多