【问题标题】:Center align buttons but left align the text inside居中对齐按钮,但左对齐里面的文本
【发布时间】:2017-04-07 00:04:10
【问题描述】:

我正在尝试创建一些纯 CSS 社交登录按钮。

但我不知道如何让文本左对齐,同时在按钮内居中。

“Login with Facebook”文本比“Login with Google”长,因此当按钮堆叠在一起时,文本不会对齐。

我希望文本中心在按钮内对齐,但我希望它彼此左对齐,以便每个按钮的“登录”一词对齐。

这是我想要实现的截图示例:

这是一个类似的问题和解决方案,但我无法使用 hrefs 而不是 divs:CSS: Center block, but align contents to the left

.shell {
  text-align: center;
}
.social-connect-button {
  display: inline-block;
  text-decoration: none;
  line-height: 2.65em;
  display: block;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  -ms-border-radius: 5px;
  -o-border-radius: 5px;
  border-radius: 5px;
  font-size: 18px;
  color: #fff!important;
}
.fb-login {
  background-color: #3b589e;
  margin-bottom: .7em;
}
.google-login {
  background-color: #dd4c39;
  cursor: pointer;
}
.fb-login i {
  float: left;
  background: #37528C;
  padding: 0 .4em;
  border-radius: 5px 0 0 5px;
}
.google-login i {
  float: left;
  background: #37528C;
  padding: 0 .4em;
  border-radius: 5px 0 0 5px;
}
.social-connect-button span {
  margin-left: .875em;
  margin-right: .875em;
}
<div class="shell">

  <a class="social-connect-button fb-login" href="[facebook-login-url]">
    <i>ICON</i>
    <span>Login with Facebook</span>
  </a>

  <a class="social-connect-button google-login" href="[google-login-url]">
    <i>ICON</i>
    <span>Login with Google</span>
  </a>

</div>

【问题讨论】:

  • 根据您的描述,我认为您无法在按钮中使用不同长度的文本来实现这两种情况。
  • 图像和您的代码结果看起来与我相似,只是宽度在您的情况下很长:/

标签: html css


【解决方案1】:

使用 text-align:left; 并根据需要添加一些边距以 .shell a span

.shell {
  text-align: left;
}
.social-connect-button {
  display: inline-block;
  text-decoration: none;
  line-height: 2.65em;
  display: block;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  -ms-border-radius: 5px;
  -o-border-radius: 5px;
  border-radius: 5px;
  font-size: 18px;
  color: #fff!important;
}
.fb-login {
  background-color: #3b589e;
  margin-bottom: .7em;
}
.google-login {
  background-color: #dd4c39;
  cursor: pointer;
}
.fb-login i {
  float: left;
  background: #37528C;
  padding: 0 .4em;
  border-radius: 5px 0 0 5px;
}
.google-login i {
  float: left;
  background: #37528C;
  padding: 0 .4em;
  border-radius: 5px 0 0 5px;
}
.social-connect-button span {
  margin-left: .875em;
  margin-right: .875em;
}
.shell a span{
  margin-left:4rem;
  }
<div class="shell">

  <a class="social-connect-button fb-login" href="[facebook-login-url]">
    <i>ICON</i>
    <span>Login with Facebook</span>
  </a>

  <a class="social-connect-button google-login" href="[google-login-url]">
    <i>ICON</i>
    <span>Login with Google</span>
  </a>

</div>

【讨论】:

    【解决方案2】:

    我做了两个更改,即
    将 .shell 属性 text-align:centre 向左更改
    和 .social-connect-button 跨度属性 margin-left: .875em;到左边距:25%; 希望您的答案是居中左对齐文本...:)

    .shell {
      text-align: left;
    }
    .social-connect-button {
      display: inline-block;
      text-decoration: none;
      line-height: 2.65em;
      display: block;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      -ms-border-radius: 5px;
      -o-border-radius: 5px;
      border-radius: 5px;
      font-size: 18px;
      color: #fff!important;
    }
    .fb-login {
      background-color: #3b589e;
      margin-bottom: .7em;
    }
    .google-login {
      background-color: #dd4c39;
      cursor: pointer;
    }
    .fb-login i {
      float: left;
      background: #37528C;
      padding: 0 .4em;
      border-radius: 5px 0 0 5px;
    }
    .google-login i {
      float: left;
      background: #37528C;
      padding: 0 .4em;
      border-radius: 5px 0 0 5px;
    }
    .social-connect-button span {
      margin-left: 25%;  
    /*margin-left: .875em;*/
      margin-right: .875em;
    }
    <div class="shell">
    
      <a class="social-connect-button fb-login" href="[facebook-login-url]">
        <i>ICON</i>
        <span>Login with Facebook</span>
      </a>
    
      <a class="social-connect-button google-login" href="[google-login-url]">
        <i>ICON</i>
        <span>Login with Google</span>
      </a>
    
    </div>

    【讨论】:

      【解决方案3】:

      text-align: left; 添加到.social-connect-button

      .shell {
        text-align: center;
      }
      .social-connect-button {
        text-align: left;
        display: inline-block;
        text-decoration: none;
        line-height: 2.65em;
        display: block;
        -webkit-border-radius: 5px;
        -moz-border-radius: 5px;
        -ms-border-radius: 5px;
        -o-border-radius: 5px;
        border-radius: 5px;
        font-size: 18px;
        color: #fff!important;
      }
      .fb-login {
        background-color: #3b589e;
        margin-bottom: .7em;
      }
      .google-login {
        background-color: #dd4c39;
        cursor: pointer;
      }
      .fb-login i {
        float: left;
        background: #37528C;
        padding: 0 .4em;
        border-radius: 5px 0 0 5px;
      }
      .google-login i {
        float: left;
        background: #37528C;
        padding: 0 .4em;
        border-radius: 5px 0 0 5px;
      }
      .social-connect-button span {
        margin-left: .875em;
        margin-right: .875em;
      }
      <div class="shell">
      
        <a class="social-connect-button fb-login" href="[facebook-login-url]">
          <i>ICON</i>
          <span>Login with Facebook</span>
        </a>
      
        <a class="social-connect-button google-login" href="[google-login-url]">
          <i>ICON</i>
          <span>Login with Google</span>
        </a>
      
      </div>

      【讨论】: