【问题标题】:Text on the top not center in the button顶部的文本不在按钮的中心
【发布时间】:2017-12-12 14:23:01
【问题描述】:

我有顶部带有文字的按钮

这是代码

.settings-button-new {
    width: 150px;
    height: 150px;
    text-decoration: none;
    line-height: 14px;
    text-align: center;
    position: relative;
    margin-left: 20px;
    margin-bottom: 20px;
    border: solid 4px #d3dbde;
    -webkit-border-radius: 13px;
    -moz-border-radius: 13px;
    border-radius: 13px;
    font: 15px Arial, Helvetica, sans-serif;
    color: #1c559b;
    background-color: #ffffff;
    background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
    background-image: -ms-linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc',GradientType=0 );
    background-image: linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
    -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
    -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
    box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
}

.settings-button-new span {
    display: block;
    position: absolute;
    top:0;
    left:0;
}
 <button class="settings-button-new"><span>Home</span></button>

它在顶部,但没有居中,如您所见。

我尝试使用text-align: center;,但似乎不起作用。

如何让按钮居中?

谢谢你的帮助

【问题讨论】:

  • 在没有绝对标记的情况下更新了我的答案,我还清理了样式。

标签: javascript jquery html css


【解决方案1】:

在下方添加居中的css代码,如果不需要垂直居中则top:0 or as your need;transform: translate(-50%, 0);

.settings-button-new span {
      display: block;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

.settings-button-new {
  width: 150px;
  height: 150px;
  text-decoration: none;
  line-height: 14px;
  position: relative;
  margin-left: 20px;
  margin-bottom: 20px;
  border: solid 4px #d3dbde;
  -webkit-border-radius: 13px;
  -moz-border-radius: 13px;
  border-radius: 13px;
  font: 15px Arial, Helvetica, sans-serif;
  color: #1c559b;
  background-color: #ffffff;
  background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -ms-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc', GradientType=0);
  background-image: linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
  -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
  box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
}

.settings-button-new span {
  display: block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<button class="settings-button-new"><span>Home</span></button>

只有中心。

.settings-button-new span {
          display: block;
          position: absolute;
          top: 5px;
          left: 50%;
          transform: translate(-50%, 0);
        }

.settings-button-new {
  width: 150px;
  height: 150px;
  text-decoration: none;
  line-height: 14px;
  position: relative;
  margin-left: 20px;
  margin-bottom: 20px;
  border: solid 4px #d3dbde;
  -webkit-border-radius: 13px;
  -moz-border-radius: 13px;
  border-radius: 13px;
  font: 15px Arial, Helvetica, sans-serif;
  color: #1c559b;
  background-color: #ffffff;
  background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  background-image: -ms-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc', GradientType=0);
  background-image: linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
  -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
  -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
  box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
}

.settings-button-new span {
  display: block;
  position: absolute;
  top: 5px;
  left: 50%;
  transform: translate(-50%, 0);
}
<button class="settings-button-new"><span>Home</span></button>

【讨论】:

    【解决方案2】:
    .settings-button-new span {
    margin: 0px;
    text-align: center;
    

    }

    【讨论】:

      【解决方案3】:

      也许这段代码可以帮助你

      .settings-button-new {
          width: 150px;
          height: 150px;
          text-decoration: none;
          line-height: 14px;
          text-align: center;
          position: relative;
          margin-left: 20px;
          margin-bottom: 20px;
          border: solid 4px #d3dbde;
          -webkit-border-radius: 13px;
          -moz-border-radius: 13px;
          border-radius: 13px;
          font: 15px Arial, Helvetica, sans-serif;
          color: #1c559b;
          background-color: #ffffff;
          background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
          background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
          background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
          background-image: -ms-linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
          filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc',GradientType=0 );
          background-image: linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
          -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
          -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
          box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
      }
      
      .settings-button-new span {
          display: block;
          position: absolute;
          top:0;
          left: 50%;
          transform: translateX(-50%);
      }
       <button class="settings-button-new"><span>Home</span></button>

      【讨论】:

      • 就是这样。谢谢
      【解决方案4】:

      你能把 span 改成 div 吗, text-align:center 就可以正常工作了

      【讨论】:

        【解决方案5】:

        您可以在不使用绝对定位的情况下删除垂直对齐文本:您可以为此使用vertical-align 和伪元素。 Source answer for this technique.

        还有

        • 在这种情况下您不需要绝对位置(和text-align: center)。
        • 我建议您删除供应商前缀属性,因为它们适用于非常旧的浏览器
        • linear-gradient 适用于 background,而不适用于 background-image 属性

        演示:

        /* styles to align button's text at the top */
        .settings-button-new:after {
          content: "";
          display: inline-block;
          vertical-align: top;
          height: inherit;
        }
        
        /* styles to align button's text at the top */
        .settings-button-new span {
          vertical-align: top;
          display: inline-block;
        }
        
        /* Cleaned up button styles */
        .settings-button-new {
          width: 150px;
          height: 150px;
          text-decoration: none;
          margin-left: 20px;
          margin-bottom: 20px;
          border: solid 4px #d3dbde;
          border-radius: 13px;
          font: 15px Arial, Helvetica, sans-serif;
          color: #1c559b;
          background: #fff linear-gradient(top, #fff 0%, #fcfcfc 100%);
          box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #fff;
        }
        <button class="settings-button-new">
          <span>Home</span>
        </button>

        【讨论】:

          【解决方案6】:

          删除位置。只需添加margin:auto

          .settings-button-new {
              width: 150px;
              height: 150px;
              text-decoration: none;
              line-height: 14px;
              text-align: center;
              position: relative;
              margin-left: 20px;
              margin-bottom: 20px;
              border: solid 4px #d3dbde;
              -webkit-border-radius: 13px;
              -moz-border-radius: 13px;
              border-radius: 13px;
              font: 15px Arial, Helvetica, sans-serif;
              color: #1c559b;
              background-color: #ffffff;
              background-image: -moz-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
              background-image: -webkit-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
              background-image: -o-linear-gradient(top, #ffffff 0%, #fcfcfc 100%);
              background-image: -ms-linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
              filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfcfc', endColorstr='#fcfcfc',GradientType=0 );
              background-image: linear-gradient(top, #ffffff 0%,#fcfcfc 100%);
              -webkit-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
              -moz-box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
              box-shadow: 0px 0px 2px #bababa, inset 0px 0px 1px #ffffff;
          }
          
          .settings-button-new span {
              margin:auto;
          }
          &lt;button class="settings-button-new"&gt;&lt;span&gt;Home&lt;/span&gt;&lt;/button&gt;

          【讨论】:

            猜你喜欢
            • 2021-08-03
            • 2019-08-07
            • 2016-06-07
            • 2021-12-23
            • 2013-04-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-10-24
            相关资源
            最近更新 更多