为什么这些按钮不居中

时间:2015-07-09 13:59:41

标签: html css

我在标题中有疑问。我希望这些按钮居中。

我尝试了很多选项:floatdisplaytext-alignposition

http://codepen.io/anon/pen/pJVELZ

body {
  width: 100%;
}
.animate {
  transition: all 0.1s;
  -webkit-transition: all 0.1s;
}
.action-button {
  float: left;
  display: inline-block;
  margin: 0 auto;
  position: relative;
  text-align: center;
}
.blue {
  background-color: #3498DB;
  border-bottom: 5px solid #2980B9;
  text-shadow: 0px -2px #2980B9;
}
.red {
  background-color: #E74C3C;
  border-bottom: 5px solid #BD3E31;
  text-shadow: 0px -2px #BD3E31;
}
.green {
  background-color: #82BF56;
  border-bottom: 5px solid #669644;
  text-shadow: 0px -2px #669644;
}
.yellow {
  background-color: #F2CF66;
  border-bottom: 5px solid #D1B358;
  text-shadow: 0px -2px #D1B358;
}
.action-button:active {
  transform: translate(0px, 50px);
  -webkit-transform: translate(0px, 50px);
  border-bottom: 1px solid;
}
<html lang="en">

<head>
  <meta name="author" content="www.twitter.com/cheeriottis">
  <link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="css/core.css">
</head>

<body>
  <a href="#" class="action-button shadow animate blue">Hello</a>
  <a href="#" class="action-button shadow animate red">How</a>
  <a href="#" class="action-button shadow animate green">Are</a>
  <a href="#" class="action-button shadow animate yellow">You?</a>
</body>

</html>

4 个答案:

答案 0 :(得分:2)

body {      
  width: 100%;
  text-align: center; /* add this */        
}    
.action-button {
  /* float:left; */ /* remove this */
  display: inline-block;
  margin: 0 auto;
  position:relative;
  text-align: center;
}

Codepen Demo

答案 1 :(得分:2)

您的按钮未居中,因为您将它们浮动到左侧。使用text-align时,您需要将其应用于父元素,而不是您想要居中的元素。

就您的Codepen而言,从float: left;移除.action-button并将text-align: center;.action-button移至body

body {  
    width: 100%;
    text-align: center;
}
.action-button {
    display: inline-block;
    margin: 0 auto;
    position: relative;
}

Codepen Example

虽然您可能已经在实际代码中而不是Codepen中执行了此操作,但我建议您添加指向包含元素的链接,例如下面的伪标记。

<div class="nav">
    <a href=""></a>
    <a href=""></a>
    <a href=""></a>
</div>

答案 2 :(得分:2)

  

HTML

 <div class="holder">
  <a href="#" class="action-button shadow animate blue">Hello</a>
  <a href="#" class="action-button shadow animate red">How</a>
  <a href="#" class="action-button shadow animate green">Are</a>
  <a href="#" class="action-button shadow animate yellow">You?</a>
    </div>
  

CSS

 .holder{
      width: 100%;
      float:left;
      border:1px solid #ccc;
      text-align:center;
    }
    .holder a{
      text-align:center;
    }
  

从锚标记中删除float:left。

.action-button {
  /* float: left; */ /*here remove it from this class you are using */
  display: inline-block;
  margin: 0 auto;
  position: relative;
  text-align: center;
}

答案 3 :(得分:0)

a标记放入容器中,然后使用absolute定位和left:50%

body{
  
  width:100%;
  
  
}

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

.animate
{
	transition: all 0.1s;
	-webkit-transition: all 0.1s;
}

.action-button
{
  float:left;
  display: inline-block;
  margin: 0 auto;
  position:relative;
  text-align: center;

}

.blue
{
	background-color: #3498DB;
	border-bottom: 5px solid #2980B9;
	text-shadow: 0px -2px #2980B9;
}

.red
{
	background-color: #E74C3C;
	border-bottom: 5px solid #BD3E31;
	text-shadow: 0px -2px #BD3E31;
}

.green
{
	background-color: #82BF56;
	border-bottom: 5px solid #669644;
	text-shadow: 0px -2px #669644;
}

.yellow
{
	background-color: #F2CF66;
	border-bottom: 5px solid #D1B358;
	text-shadow: 0px -2px #D1B358;
}

.action-button:active
{
	transform: translate(0px,50px);
  -webkit-transform: translate(0px,50px);
	border-bottom: 1px solid;
}
<html lang="en">
<head>
  <meta name="author" content="www.twitter.com/cheeriottis">
  <link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" href="css/core.css">
</head>
<body>
  <div class="center-it">
  <a href="#" class="action-button shadow animate blue">Hello</a>
  <a href="#" class="action-button shadow animate red">How</a>
  <a href="#" class="action-button shadow animate green">Are</a>
  <a href="#" class="action-button shadow animate yellow">You?</a>
  </div>
</body>
</html>