Creating a hexagon shape using CSS and showcasing various elements within it

I am facing a challenge with a hexagon created using CSS. I want to place a header, paragraph, and button inside the hexagon, but they keep getting hidden behind the hexagon's before and after formatting. You can access the code through this link: https://jsfiddle.net/o8a3pm3h/6/ . Any suggestions on how to properly position these elements on the surface of the hexagon div would be greatly 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>

**I understand that using inline CSS is generally discouraged, but it was necessary for this particular demonstration.

Answer №1

It is essential to properly position the text and adjust the z-index to ensure it appears above other elements:

.hexagon h4, .hexagon p, .hexagon button {
  position: relative;
  z-index: 1
}

Illustrative Example

#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);
}
.hexagon h4,
.hexagon p,
.hexagon button {
  position: relative;
  z-index: 1
}
<!--Kindly maintain the styling as it overlays 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>

Answer №2

To display the content accordingly, enclose it in a div, define its position, and assign a z-index:

.display-content {
  position: absolute;
  z-index: 10;
}

Check out the JSFiddle Demo here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for implementing conditional sections in an Angular template HTML file

Creating three different tables as described below: There is a significant amount of repetitive code. The only variation in each table is the studentPermissions[] array, which contains three objects for each student. Therefore, I am simply changing it ...

Navigating to a different page using JavaScript

Here is the code snippet I am working with: <p> <img alt="" src="imagini/slide1.png" style="height: 64px; width: 64px" id="imgClickAndChange" onclick="changeImage()" /> </p> <script language="javascript"> f ...

What is the process of importing a cascading style sheet into a C# object?

Currently, I am working on a web application and I would like to provide users with the flexibility to change the styling elements of CSS linked to a page through an admin screen. I have been exploring ways to load a CSS file into a C# object or convert ...

What is the best way to extract the content located between two specific HTML elements?

<div class="container"> <a href="#" class="link">asd</a> [Please extract this specific text] <a href="#" class="link">asd</a> Other unrelated content. </div> Is there a way to access the text inside certain elem ...

Display a division upon clicking a button using AngularJS and HTML

Looking to implement a functionality in my HTML page using angularJS. I am currently learning angularJs and experimenting with a feature where clicking on a button will display a specific div (EmployeeInfoDiv) on the webpage. The idea is to fill out some t ...

Customizing the Dropdown Arrow Icon to a Desired Icon of Choice

I'm looking to customize the dropdown select on my website by changing the arrow icon to a plus icon, which will then turn into a minus icon when clicked and the choices are displayed. I'm new to jquery and eager to learn more about this language ...

Difficulties encountered when trying to store a checkbox array data into a database

Within my application, I am faced with the task of extracting checkbox items from a list and storing them in my database. Currently, I am assembling these items into an array by iterating through the options, adding a checkbox before each one, and assignin ...

Incorporating an icon into a Bootstrap tab

I have encountered a minor issue while trying to include an icon in a bootstrap tab, resulting in some styling problems. In need of a CSS resolution that satisfies the following criteria: The icon must be aligned to the right of the link. The icon canno ...

Jquery's intermittent firing of .ajax within if statement

So, I've inherited a rather messy codebase that I need to modernize. The task involves upgrading from an old version of prototype to the latest jQuery 3.2.1 within a dated website built on php and smarty templates (not exactly my favorite). When a lo ...

Guide to truncating text based on screen size with ellipses

I'm currently working on a project with two bootstrap cards inside a page layout with columns. The issue I'm facing is that when the name of the card exceeds 11 characters (with a maximum of 15), resizing the screen between 768px-1200px causes th ...

Maximizing Efficiency on the Frontend: Balancing Requests with Caching

I am currently tackling a large website that has accumulated quite a bit of technical debt that needs to be addressed. The site contains a significant amount of JavaScript and CSS files being loaded. Currently, these files are aggregated and minified in la ...

What is the procedure for submitting all checkbox values through Google Apps Script?

My web app created using Google Apps Script can generate a custom table of data for users to select for calculations. I use Google Sheets to populate the table with values and checkboxes, but note that the table size may vary depending on the user. <fo ...

Issue with displaying multiple checkboxes using Materialize CSS in combination with Leaflet for web-mapping overlays

I'm currently using Materialize 0.97.7 along with Leaflet 1.0.1 (the latest version). <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.1/leaflet-src.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com ...

Is it possible to use jQuery to append an image in bold text on a webpage

I've been trying to implement this code but it's not functioning properly. Can someone please point out where I might be making a mistake? $('#ramka').append("<b><img src="cat.png"></img></b>"); ...

Struggling to Mask Password Input with Dots in HTML?

I am currently developing a User Registration System and I would like the password field to display as dots for security purposes. However, every time the page loads, the Email and Password fields are already filled in: Email: root Password: password ( ...

Uninstalling an Element Using jQuery

There is a requirement to dynamically insert city names into the input field with the ID citiesList. If the value of the input field is empty and the user clicks on the cancel button, then the input should be deleted. However, upon clicking the edit but ...

Using JavaScript to store CSS style properties in an array

In the midst of building a React-datagrid project, I am streamlining CSS properties for an array. However, there seems to be redundant CSS properties that I would like to consolidate into an array format. let _columns = []; let commonStyle = {"width":"20 ...

Change the value of a checkbox using a boolean attribute

Is it possible to assign a checkbox value based on an attribute set with PHP? At first glance, this task may seem strange, but the solution is actually quite simple. Here is an example of how you can achieve this using PHP: <input type="checkbox" val= ...

A step-by-step guide on implementing the bootstrap paginator library to paginate PHP echoed data

I'm currently working on a project that involves displaying data from a database in a table. To make the data paginated on the client side, I decided to use the bootstrap paginator library available at: Below is the code I'm using: In my header ...

Error 403 CSRF forbidden encountered while utilizing the request.post.get() function within the views.py file linked to the HTML page

Here is the views.py code snippet: from django.shortcuts import render from django.http import HttpResponse from django.http import HttpRequest from django.db import models from datetime import datetime def postdata(request): if request.method == "PO ...