sam_smith sam_smith - 4 months ago 29
HTML Question

CSS Hexagon with elements inside

I have a hexagon created by CSS. I am trying to get a header, paragraph and button inside the hexagon, but all of these elements are hiding behind the hexagon's before and after formatting. Here is a link to the code: https://jsfiddle.net/o8a3pm3h/6/ .
Any suggestions of how to place the elements on the surface of the hexagon div are appreciated.



#hex3 {
width: 200px;
height: 200px;
}
#color3 {
background-color: #CED7DC;
}
.hexagon-wrapper {
text-align: center;
margin: 20px;
position: relative;
display: inline-block;
}
.hexagon {
height: 100%;
width: calc(100% * 0.57735);
display: inline-block;
z-index: 1;
}
.hexagon:before {
position: absolute;
top: 0;
right: calc((100% / 2) - ((100% * 0.57735) / 2));
background-color: inherit;
height: inherit;
width: inherit;
content: '';
transform: rotateZ(60deg);
}
.hexagon:after {
position: absolute;
top: 0;
right: calc((100% / 2) - ((100% * 0.57735) / 2));
background-color: inherit;
height: inherit;
width: inherit;
content: '';
transform: rotateZ(-60deg);
}

<!--Please maintain the styling here because its on top of an image-->
<div id="hex3" class="hexagon-wrapper" style="position:absolute; top:80px; right:400px;">
<div id="color3" class="hexagon">
<h4>TEST</h4>
<p>TESTTTTTTTTTT</p>
<button type="button" class="btn btn-secondary" id="grid-row-btn-2" onclick="location.href='/#/'">
DISCOVER MORE &nbsp;<span class="glyphicon glyphicon-chevron-right"></span>
</button>
</div>
</div>





**Also, I know that inline CSS is bad practice, but it is only for this demo.

Answer

Just wrap the content in a div, position it, and give it a z-index:

.content {
  position: relative;
  z-index: 5;
}

JSFiddle Demo