在父级Div中横向居中2个Div按钮

时间:2015-07-15 16:00:14

标签: html css

使用DIV创建了两个导航按钮,尝试将两者都放在响应式布局中的父DIV中。父DIV的宽度是响应的,但按钮的宽度是固定的。两个按钮应该在父级中心彼此相邻,两侧都有均匀的空间。

查看其他一些有关此问题的帖子,尝试了所有解决方案,包括添加:margin:0px auto; - 并尝试添加两个:margin-left: auto; margin-right: auto - 这两个选项均无效。

不知道某些按钮式CSS代码是否会导致它无法居中,或者我是否遗漏了其他内容。<\ n> <\ n> <\ n> / p>

以下是代码:

&#13;
&#13;
#head-map {
  clear: none;
  float: left;
  margin-left: 30%;
  width: 100%;
  display: block;
  margin-right: 1%;
  text-align: center;
  padding-top: 0px;
}
#map-button {
  height: 35px;
  width: 70px;
  background-color: #12A483;
  border-color: #9dacc8;
  border-style: solid;
  border-width: 3px;
  border-radius: 15px;
  -moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  text-align: center;
  padding-top: 7px;
  box-shadow: 0px 2px 7px #292929;
  -moz-box-shadow: 0px 3px 8px #292929;
  -webkit-box-shadow: 0px 3px 8px #292929;
  float: left;
  display: inline-block;
  margin: 0px auto;
}
#espanol-button {
  height: 35px;
  width: 100px;
  background-color: #12A483;
  border-color: #9dacc8;
  border-style: solid;
  border-width: 3px;
  border-radius: 15px;
  -moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  text-align: center;
  padding-top: 7px;
  box-shadow: 0px 2px 7px #292929;
  -moz-box-shadow: 0px 3px 8px #292929;
  -webkit-box-shadow: 0px 3px 8px #292929;
  float: left;
  display: inline-block;
  margin: 0px auto;
}
a.whitelink {
  color: white;
}
a.whitelink:hover {
  color: #00133e;
  text-decoration: none;
}
&#13;
<div id="head-map">
  <div id="map-button"><a class="whitelink" href="#" target="new">Map</a>
  </div>
  <div id="espanol-button"><a class="whitelink" href="#" target="self">Español</a>
  </div>
</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:4)

制作内部div display: inline-block并将其与text-align: center置于父级中心。移除花车。

简化示例

注意每个内部div在标记之间没有空格。这prevents extra whitespace being displayed between inline elements.

div {
  text-align: center;
}
div > div {
  display: inline-block;
  width: 100px;
  background: #F00;
  margin: 10px;
}
<div id="head-map">
  <div id="map-button"><a class="whitelink" href="#" target="new">Map</a>
  </div><div id="espanol-button"><a class="whitelink" href="#" target="self">Español</a>
  </div>
</div>

答案 1 :(得分:2)

那是因为您正在使用float: left;并导致text-align: center;无效。移除浮子。例如:

#head-map {
    clear: none;
    width: 100%;
    display: block;
    margin-right: 1%;
    text-align: center;
    padding-top: 0px;
}

#map-button {
    height: 35px;
    width: 70px;
    background-color: #12A483;
    border-color: #9dacc8;
     border-style: solid;
     border-width: 3px;
     border-radius: 15px;
      -moz-border-radius:15px;
      -webkit-border-radius:15px;
     text-align: center;
    padding-top: 7px;
    box-shadow:0px 2px 7px #292929;
      -moz-box-shadow: 0px 3px 8px #292929;
      -webkit-box-shadow: 0px 3px 8px #292929;
    display: inline-block;
    margin:0 10px;
}

#espanol-button {
    height: 35px;
    width: 100px;
    background-color: #12A483;
    border-color: #9dacc8;
     border-style: solid;
     border-width: 3px;
     border-radius: 15px;
      -moz-border-radius:15px;
      -webkit-border-radius:15px;
        text-align: center;
    padding-top: 7px;
    box-shadow:0px 2px 7px #292929;
      -moz-box-shadow: 0px 3px 8px #292929;
      -webkit-box-shadow: 0px 3px 8px #292929;
    display: inline-block;
    margin:0 10px;
}

a.whitelink {
    color: white;   
}

a.whitelink:hover {
    color: #00133e;
    text-decoration: none;
}
  <div id="head-map">
    <div id="map-button"><a class="whitelink" href="#" target="new">Map</a>
    </div>
    <div id="espanol-button"><a class="whitelink" href="#" target="self">Español</a>
    </div>
  </div>

答案 2 :(得分:1)

您需要在两个按钮周围添加包装器 div以及一个明确的div。

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

<div id="head-map">
  <div class="wrapper">
    <div id="map-button"><a class="whitelink" href="#" target="new">Map</a>
    </div>
    <div id="espanol-button"><a class="whitelink" href="#" target="self">Español</a>
    </div>
    <div class="clear"></div>
  </div>
  </div>

这是CSS。请注意 .clear 类。

#head-map {
    clear: none;
    float: left;
    margin-left: 30%;
    width: 100%;
    display: block;
    margin-right: 1%;
    text-align: center;
    padding-top: 0px;
  background:blue;
}
.wrapper {
  width:182px;
  margin:0 auto;
}
#map-button {
    height: 35px;
    width: 70px;
    background-color: #12A483;
    border-color: #9dacc8;
     border-style: solid;
     border-width: 3px;
     border-radius: 15px;
      -moz-border-radius:15px;
      -webkit-border-radius:15px;
     text-align: center;
    padding-top: 7px;
    box-shadow:0px 2px 7px #292929;
      -moz-box-shadow: 0px 3px 8px #292929;
      -webkit-box-shadow: 0px 3px 8px #292929;
    float:left;
    display: inline-block;
    margin:0px auto;
}
.clear{clear:both}
#espanol-button {
    height: 35px;
    width: 100px;
    background-color: #12A483;
    border-color: #9dacc8;
     border-style: solid;
     border-width: 3px;
     border-radius: 15px;
      -moz-border-radius:15px;
      -webkit-border-radius:15px;
        text-align: center;
    padding-top: 7px;
    box-shadow:0px 2px 7px #292929;
      -moz-box-shadow: 0px 3px 8px #292929;
      -webkit-box-shadow: 0px 3px 8px #292929;
    float:left;  
    display: inline-block;
    margin:0px auto;
}

a.whitelink {
    color: white;   
}

a.whitelink:hover {
    color: #00133e;
    text-decoration: none;
}

答案 3 :(得分:1)

当然,flex-box可以做到这一点

#head-map {
  padding-top: 0px;
  display: flex;
  justify-content: space-around;
}
#map-button {
  height: 35px;
  width: 70px;
  background-color: #12A483;
  border-color: #9dacc8;
  border-style: solid;
  border-width: 3px;
  border-radius: 15px;
  -moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  text-align: center;
  padding-top: 7px;
  box-shadow: 0px 2px 7px #292929;
  -moz-box-shadow: 0px 3px 8px #292929;
  -webkit-box-shadow: 0px 3px 8px #292929;
}
#espanol-button {
  height: 35px;
  width: 100px;
  background-color: #12A483;
  border-color: #9dacc8;
  border-style: solid;
  border-width: 3px;
  border-radius: 15px;
  -moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  text-align: center;
  padding-top: 7px;
  box-shadow: 0px 2px 7px #292929;
  -moz-box-shadow: 0px 3px 8px #292929;
  -webkit-box-shadow: 0px 3px 8px #292929;
}
a.whitelink {
  color: white;
}
a.whitelink:hover {
  color: #00133e;
  text-decoration: none;
}
<div id="head-map">
  <div id="map-button"><a class="whitelink" href="#" target="new">Map</a>
  </div>
  <div id="espanol-button"><a class="whitelink" href="#" target="self">Español</a>
  </div>
</div>

上面的示例将按钮分隔在整个页面上。

FIDDLE显示它们居中,并添加了一些轻微的左/右边距。