在另一个Div中居中Div形状

时间:2016-08-18 12:14:42

标签: html css

我正在尝试并排设置4个div,其中每个都是一个较小的div六边形,我想在第一个div中居中。内部的形状有点复杂,因为它由几个div组成,形成六边形。我似乎无法完全摆脱这种形状以远离边缘。

我正在使用zurb基础框架 任何帮助将不胜感激。

由于 蚂蚁

http://codepen.io/Hornetant/pen/akrgaN?editors=1010

#one {
  height: 500px;
  background-color: red;
  float: left;
  padding: 0px;
}
#two {
  height: 500px;
  background-color: blue;
  float: left;
  padding: 0px;
}
#three {
  height: 500px;
  background-color: green;
  float: left;
  padding: 0px;
}
#four {
  height: 500px;
  background-color: orange;
  float: left;
  padding: 0px;
}
.hextext {
  padding: 10px 20px 10px 20px;
  color: #ffffff;
  font-family: 'Droid Sans', sans-serif;
  font-weight: 400;
  font-size: 100%;
  text-align: center;
}
.hexagongreen {
  position: relative;
  width: 150px;
  background-color: #1aa07d;
  margin-top: 60px;
}
.hexagongreen:before,
.hexagongreen:after {
  content: "";
  position: absolute;
  width: 0;
  border-left: 75px solid transparent;
  border-right: 75px solid transparent;
}
.hexagongreen:before {
  bottom: 100%;
  border-bottom: 40px solid #1aa07d;
}
.hexagongreen:after {
  top: 100%;
  width: 0;
  border-top: 40px solid #1aa07d;
}
<div class="row">
  <div class="medium-3 columns" id="one">
    <div class="hexagongreen">
      <div class="hextext"><b>DID YOU KNOW?</b>
        <br>Hayfever affects 15 million people in the UK
        <br>each year</div>
    </div>
  </div>
  <div class="medium-3 columns" id="two">
    <div class="hexagongreen">
      <div class="hextext"><b>DID YOU KNOW?</b>
        <br>Hayfever affects 15 million people in the UK
        <br>each year</div>
    </div>
  </div>
  <div class="medium-3 columns" id="three">
    <div class="hexagongreen">
      <div class="hextext"><b>DID YOU KNOW?</b>
        <br>Hayfever affects 15 million people in the UK
        <br>each year</div>
    </div>
  </div>
  <div class="medium-3 columns" id="four">
    <div class="hexagongreen">
      <div class="hextext"><b>DID YOU KNOW?</b>
        <br>Hayfever affects 15 million people in the UK
        <br>each year</div>
    </div>
  </div>
</div>

5 个答案:

答案 0 :(得分:0)

清除六角形文本上的浮动,并在左侧和右侧以自动边距自动居中,如下所示:

.hextext{
  padding:10px 20px 10px 20px;
  color:#ffffff;
  font-family: 'Droid Sans', sans-serif;
  font-weight: 400;
  font-size: 100%;
  text-align: center;
  float: none;
}



.hexagongreen {
  position: relative;
  width: 150px; 
  background-color: #1aa07d;
  margin-top:60px; 
  margin: 60px auto 0;
}

答案 1 :(得分:0)

您可以使用margin: 60px auto;。左/右边距的auto值水平居中六边形。请参阅下面的示例(全屏模式)。

另请参阅w3schools以获取与CSS对齐的一些示例或CSS-tricks作为居中指南。

.columns {
  float: left;
  padding: 0px;
  height: 500px;
  width: 25%;
}
#one {
  background-color: red;
}
#two {
  background-color: blue;
}
#three {
  background-color: green;
}
#four {
  background-color: orange;
}
.hextext {
  padding: 10px 20px 10px 20px;
  color: #ffffff;
  font-family: 'Droid Sans', sans-serif;
  font-weight: 400;
  font-size: 100%;
  text-align: center;
}
.hexagongreen {
  position: relative;
  width: 150px;
  background-color: #1aa07d;
  margin: 60px auto;
}
.hexagongreen:before,
.hexagongreen:after {
  content: "";
  position: absolute;
  width: 0;
  border-left: 75px solid transparent;
  border-right: 75px solid transparent;
}
.hexagongreen:before {
  bottom: 100%;
  border-bottom: 40px solid #1aa07d;
}
.hexagongreen:after {
  top: 100%;
  width: 0;
  border-top: 40px solid #1aa07d;
}
<div class="row">
  <div class="medium-3 columns" id="one">
    <div class="hexagongreen">
      <div class="hextext"><b>DID YOU KNOW?</b>
        <br>Hayfever affects 15 million people in the UK
        <br>each year</div>
    </div>
  </div>
  <div class="medium-3 columns" id="two">
    <div class="hexagongreen">
      <div class="hextext"><b>DID YOU KNOW?</b>
        <br>Hayfever affects 15 million people in the UK
        <br>each year</div>
    </div>
  </div>
  <div class="medium-3 columns" id="three">
    <div class="hexagongreen">
      <div class="hextext"><b>DID YOU KNOW?</b>
        <br>Hayfever affects 15 million people in the UK
        <br>each year</div>
    </div>
  </div>
  <div class="medium-3 columns" id="four">
    <div class="hexagongreen">
      <div class="hextext"><b>DID YOU KNOW?</b>
        <br>Hayfever affects 15 million people in the UK
        <br>each year</div>
    </div>
  </div>
</div>

答案 2 :(得分:0)

您需要在六角形框周围添加一个容器,并将宽度设置为100%,然后添加边距:50%auto;到六角盒本身。您可以在我链接的CodePen.io示例中看到一个工作示例。

HTML

     <div class="medium-3 columns"  id="one"> 
       <div class="hexagon-container">
           <div class="hexagongreen hexagon-center">
               <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div>
           </div>
       </div>
     </div>

CSS

.hexagon-container {
  width: 100%;
}

.hexagon-center {
  margin: 50% auto;
}

CodePen Example

答案 3 :(得分:0)

这里是将元素置于另一个元素中的代码:

/* center positioning */
/* first, you need a wrapper for that element to center it vertically within your parrent element */
.centered-wrapper{
    position:absolute;
    top:50%;
    transform:translateY(-50%);
    z-index:1;
    width:100%;
}
/* this is the element to be centered, here you are centering it horizontally */
.centered{
    margin:0 auto;
    float:none;
    padding-left:5%; /* you might not need this padding, it depends on how the rest of your css is set up */
}

.centered元素也需要固定的宽度(固定的意思是任何类型的设置宽度px%,等等) 就是这样,希望它有所帮助。

这是一个关于plunkr的例子: https://plnkr.co/edit/a0lruDZV1OpvsAtjaOEQ?p=preview

这里是您确切问题的解决方案,在您的codepen中运行它,看看它是否是您正在寻找的:

HTML:

<div class="row">
  <div class="medium-3 columns"  id="one"> 
    <div class="centered-wrapper">
      <div class="hexagongreen centered">
        <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div>
      </div>
    </div>
  </div>


<div class="medium-3 columns" id="two">
  <div class="centered-wrapper">
    <div class="hexagongreen centered">
      <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div>
    </div>
  </div>
</div>
<div class="medium-3 columns" id="three">
  <div class="centered-wrapper">
    <div class="hexagongreen centered">
      <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div>
    </div>
  </div>
</div>
<div class="medium-3 columns" id="four">
  <div class="centered-wrapper">
    <div class="hexagongreen centered">
      <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div>
    </div>
  </div>
</div>
</div>

CSS:

.columns{
  position:relative;
}

.centered-wrapper {
  position: absolute;
  top: 50%;
  transform: translateY(-68%);
  z-index: 1;
  width: 100%;
}


/* this is the element to be centered, here you are centering it horizontally */

.centered {
  margin: 0 auto;
  float: none;
  background-color: green; /* i have collored the divs to make it more clear what it's happening */
}
#one{
  height:500px;
  background-color:red;
float:left;
  padding:0px;
}
#two{
height:500px;
  background-color:blue;
 float:left;
  padding:0px;
}
#three{
height:500px;
  background-color:green;
float:left;
  padding:0px;
}
#four{
 height:500px;
  background-color:orange;
   float:left;
  padding:0px;
}

.hextext{
  padding:10px 20px 10px 20px;
  color:#ffffff;
 font-family: 'Droid Sans', sans-serif;
  font-weight: 400;
  font-size: 100%;
  text-align: center;
}

.hexagongreen {
  position: relative;
  width: 150px; 
  background-color: #1aa07d;
margin-top:60px; 
}

.hexagongreen:before,
.hexagongreen:after {
  content: "";
  position: absolute;
  width: 0;
  border-left: 75px solid transparent;
  border-right: 75px solid transparent;
}

.hexagongreen:before {
  bottom: 100%;
  border-bottom: 40px solid #1aa07d; 
}

.hexagongreen:after {
  top: 100%;
  width: 0;
  border-top: 40px solid #1aa07d; 
}

答案 4 :(得分:0)

.hexagongreen{
  margin: 60px auto;
}

&#13;
&#13;
#one{
  height:500px;
  background-color:red;
float:left;
  padding:0px;
}
#two{
height:500px;
  background-color:blue;
 float:left;
  padding:0px;
}
#three{
height:500px;
  background-color:green;
float:left;
  padding:0px;
}
#four{
 height:500px;
  background-color:orange;
   float:left;
  padding:0px;
}

.hextext{
  padding:10px 20px 10px 20px;
  color:#ffffff;
 font-family: 'Droid Sans', sans-serif;
  font-weight: 400;
  font-size: 100%;
  text-align: center;
}

.hexagongreen {
  position: relative;
  width: 150px; 
  background-color: #1aa07d;
margin-top:60px; 
}

.hexagongreen:before,
.hexagongreen:after {
  content: "";
  position: absolute;
  width: 0;
  border-left: 75px solid transparent;
  border-right: 75px solid transparent;
}

.hexagongreen:before {
  bottom: 100%;
  border-bottom: 40px solid #1aa07d; 
}

.hexagongreen:after {
  top: 100%;
  width: 0;
  border-top: 40px solid #1aa07d; 
}
&#13;
   <div class="row">
     <div class="medium-3 columns"  id="one"> 
       <div class="hexagongreen">
    <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div></div></div>


<div class="medium-3 columns" id="two">   <div class="hexagongreen">
    <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div></div></div>
<div class="medium-3 columns" id="three">   <div class="hexagongreen">
    <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div></div></div>
<div class="medium-3 columns" id="four">   <div class="hexagongreen">
    <div class="hextext"><b>DID YOU KNOW?</b><br>Hayfever affects 15 million people in the UK <br>each year</div></div></div>
</div>
&#13;
&#13;
&#13;