将图形对齐到文本右侧并居中以创建按钮 - 无法居中按钮

时间:2015-12-11 15:53:12

标签: html css alignment center

http://codepen.io/gingerman/pen/VevZLR(最好查看此处的代码)

当你打开codepen时,你会看到按钮向左对齐。

我在按钮按钮落下时没有按钮按钮的问题。

我在StackOverFlow上搜索过帮助,但没有找到解决方案。

谢谢:)

CSS

#button_container{
  position:absolute;
  text-align: center;
  top: 68px;
}


#button_join{
  display: inline-block;
    font-size:14px;
    font-weight:bold;
    color: #000;
    font-family:Helvetica, Arial, sans-serif;
    background-color:#fff;
    line-height: 31px;
  padding-left: 10px;
    border-radius:8px 0px 0px 8px;
  border:0 0 0 0;
}

#button_point{
  float:right;
  display: inline-block;
    height: 31px;
}





/* Default style, feel free to remove if not needed. */
body, body * {
    vertical-align: baseline;
    border: 0;
    outline: 0;
    padding: 0;
    margin: 0;
}

/* Div layer for the entire banner. */
#container_dc {
    position: absolute;
    width: 118px;
    height: 200px;
    border:1px solid #850f1e;
    top: 0px;
    left: 0px;
    margin: auto;
    overflow:hidden;
    font-family:Helvetica, Arial, sans-serif;
    font-weight:400;
    color:#fff;
    text-align:center;
}

#content_dc {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0px;
    left: 0px;
    background-color: #ee1a36;
    z-index: 100;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

HTML

<div id="container_dc">

  <div id="content_dc">

    <div id="button_container">

        <div id="button_join">centre me</div>
          <img id="button_point" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/269240/button_end_36_high.png">
        </div>

      </div>

    </div>

  </div>

</div>

2 个答案:

答案 0 :(得分:0)

您可以对ID为flex的父元素使用#content_dc,并为ID为#button_container的元素删除css规则:

/*REMOVE THIS
#button_container{
  position:absolute;
  text-align: center;
  top: 68px;
}*/

/*check comments in this rule*/
#content_dc {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0px;
  left: 0px;
  background-color: #ee1a36;
  z-index: 100;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  text-align: center;
  display: flex;/*add flex*/
  justify-content: center; /*add justify-content center*/
  align-items: center; /*add align-items center*/
}

#button_join {
  display: inline-block;
  font-size: 14px;
  font-weight: bold;
  color: #000;
  font-family: Helvetica, Arial, sans-serif;
  background-color: #fff;
  line-height: 31px;
  padding-left: 10px;
  border-radius: 8px 0px 0px 8px;
  border: 0 0 0 0;
}
#button_point {
  float: right;
  display: inline-block;
  height: 31px;
}
/* Default style, feel free to remove if not needed. */

body,
body * {
  vertical-align: baseline;
  border: 0;
  outline: 0;
  padding: 0;
  margin: 0;
}
/* Div layer for the entire banner. */

#container_dc {
  position: absolute;
  width: 118px;
  height: 200px;
  border: 1px solid #850f1e;
  top: 0px;
  left: 0px;
  margin: auto;
  overflow: hidden;
  font-family: Helvetica, Arial, sans-serif;
  font-weight: 400;
  color: #fff;
}
<div id="container_dc">

  <div id="content_dc">

    <div id="button_container">
      <div id="button_join">centre me</div>
      <img id="button_point" alt="" title="" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/269240/button_end_36_high.png" />
    </div>
  </div>
</div>

<强>参考

Using CSS flexible boxes

答案 1 :(得分:0)

做这些小改动。 在CSS中:

#button_container{
  position:relative;
  text-align: center;
  top: 68px;
  display: -webkit-inline-box;
}