What's the best way to link up rotated div elements?

I'm currently attempting to connect rotated divs at a 45-degree angle, but I'm struggling to find the solution. Here is the current state of the page:

Here is a screenshot of the page:

https://i.sstatic.net/jqPYJ.png

This is what I am aiming for:

https://i.sstatic.net/tcxA1.png

.box {
    border-style: groove;
    width: 100px;
    height: 100px;
    transform: rotate(45deg);
}
<!-- Bootstrap-5 -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c3e3333282f282e3d2c1c69726d726f">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">


<!-- Body -->
<div class="d-flex w-100 h-100 justify-content-center align-items-center p-5">
<div class="d-flex flex-wrap justify-content-center align-items-center">
<div class="box">test</div>
<div class="box">test</div>
<div class="box">test</div>
<div class="box">test</div>
<‘div class="box">test<;/div>, ...

Answer №1

In my latest article, I showcased a demo that you can check out at this link: https://css-tricks.com/hexagons-and-beyond-flexible-responsive-grid-patterns-sans-media-queries/

All the detailed code snippets and information can be found there:

.main {
  display:flex;
  --s: 100px;  /* size  */
  --m: 4px;    /* space */
  --r: calc(var(--s) + 4*var(--m) - 2px);
}

.container {
  font-size: 0;
}

.container div {
  width: var(--s);
  margin: var(--m);
  height: var(--s);
  display: inline-block;
  font-size:initial;
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  background: red;
  margin-bottom: calc(var(--m) - var(--s)*0.5);
}
.container div:nth-child(odd) {
  background:green;
}
.container::before {
  content: "";
  width: calc(var(--s)/2 + var(--m));
  float: left;
  height: 140%;
  shape-outside: repeating-linear-gradient(     
                   #0000 0 calc(var(--r) - 3px),      
                   #000  0 var(--r));
}
<div class="main">
  <div class="container">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

To further enhance this design by incorporating transforms and borders, you can make the following modifications:

.main {
  display:flex;
  --s: 100px;  /* size  */
  --m: 4px;    /* space */
  --r: calc(var(--s) + 4*var(--m) - 2px);
}

.container {
  font-size: 0;
}

.container div {
  width: var(--s);
  margin: var(--m);
  height: var(--s);
  display: inline-block;
  font-size:initial;
  transform:rotate(45deg) scale(0.707);
  border:2px solid;
  box-sizing:border-box;
  margin-bottom: calc(var(--m) - var(--s)*0.5);
}

.container::before {
  content: "";
  width: calc(var(--s)/2 + var(--m));
  float: left;
  height: 140%;
  shape-outside: repeating-linear-gradient(     
                   #0000 0 calc(var(--r) - 3px),      
                   #000  0 var(--r));
}
<div class="main">
  <div class="container">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </div>
</div>

Answer №2

body {
  margin-top: 200px;
}

.container {
  padding: 50px 0px;
  display: flex;
  width: 500px;
  flex-wrap: wrap;
  margin-top: -98px;
}

.box {
  height: 100px;
  width: 100px;
  border: 1px solid black;
  margin: -16px 21px;
  transform-origin: bottom right;
  transform: rotate(45deg);
}
.container.withMargin {
  margin-left: 72px;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
    <link href="./src/styles.css" />
  </head>

  <body>
    <div class="container">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
    <div class="container withMargin">
      <div class="box"></div>
      <div class="box"></div>
    </div>
    <div class="container">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </body>
</html>

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

Updating the value of a localStorage key after each successful AJAX call in JavaScript

I'm currently facing a challenge with my local storage key value. The issue is that it doesn't update the value when the AJAX call successfully returns data. It only retains the old stored data, requiring me to manually refresh the page by pressi ...

"Why are all the rows from my query being returned in my HTML/PHP/AJAX code

I've been working on a webpage that allows users to input a minimum GPA in a textbox to search a database and display records of students who meet that specific criteria. The HTML code calls a separate PHP file and utilizes AJAX to call a function. Ho ...

Are your macOS devices not displaying the Scrollbar CSS buttons correctly?

I need help troubleshooting why my buttons are not appearing in the scrollbar on macOS. They function properly on Windows, so I suspect there may be a typo or error in my code. Can anyone take a look and provide some insight? ::-webkit-scrollbar { wid ...

Arranging Form Elements with Bootstrap styling

Check out the Bootply I created to demonstrate an issue I'm facing: http://www.bootply.com/8fPmr31Wd7 When scrolling down, you'll notice that "People" and "Places" are indented on the sides. It's possible that I'm not using the correc ...

Instructions for including user ID in the URL after successfully logging in with their information

Hello everyone, I have a question and I would appreciate some help. I am attempting to ensure that when a user logs in with their credentials, their ID is passed and visible in the URL bar like this: ?id=000. I have been trying various ways but have not b ...

Tips for speeding up page loading when using the same image multiple times on a page

Is it possible to utilize a JavaScript Image object? If yes, could you provide an illustrative example? And is it feasible to achieve this with jQuery? ...

Modify the alignment and orientation of the data within RadHtmlChart

I have a chart: <telerik:RadHtmlChart ID="chrtInvestmentAmount" runat="server" Transitions="true" DataSourceID="SqlDataSource1" Height="256px" Skin="Glow" Width="1024px" RenderMode="Auto"> <PlotArea> <Series> ...

Comparing the name and URL of a link using Selenium webdriver in C#

I am currently looking for ways to automate the testing of links on a Sharepoint site, ensuring they are connected to the correct URL. However, I have hit a roadblock when it comes to performing the comparison. After locating the links and adding them to ...

Tips for avoiding HTML <tags> in the document.write function

I am trying to paint the actual HTML code within a tag using JavaScript but escaping it doesn't seem to be working. function displayHTMLString(n){ document.write("'<table>'"); for (i in range(0,n)){ ...

What is the best way to switch a single class using jQuery without impacting other elements with the same class

I'm in the process of implementing a commenting system similar to Reddit on my website. Each comment is equipped with a small button in the top right corner that allows users to collapse the comment. To achieve the collapsing effect, I am utilizing j ...

Creating an infinite scroll with a gradient background: a step-by-step guide

I am currently working on a project to develop an infinite scrolling webpage with a dynamic gradient background that changes based on the user's scroll position. While researching, I came across some code for infinite scrolling using time and date. I ...

PHP multiline string using PHP language

I am struggling with echoing a large amount of PHP and HTML code. Despite trying the obvious solutions, I am unable to make it work: <?php echo ' <?php if ( has_post_thumbnail() ) { ?> <div class="gridly-image"><a href="< ...

Issue with dynamically adjusting flex box width using JavaScript

Currently, I am developing a user interface that heavily relies on flexbox. The layout consists of a content area and a sidebar that can be toggled by adding or removing a specific class. Whenever the sidebar is toggled, the content area needs to be manua ...

What is the process of generating a row in AngularJS?

I am struggling to create a row as shown in the image. Currently, my circle is not positioned on the right side and my "P" element is not receiving the background color. Here is the code snippet: http://plnkr.co/edit/qIz2rFgW8n3J92evCRTd?p=preview Is it p ...

Creating a fixed "sub-page" within Wicket (e.g.: linking a folder in Wicket)

I am in the process of creating a web application with the help of Wicket. While the majority of the website is generated dynamically through Wicket, I have a specific section that needs to function as a standalone "static" html website. Essentially, this ...

Is Material-ui's sx feature able to produce optimized utility classes like tailwindcss?

When utilizing Tailwind CSS, it is compiled in the following manner: <div class="font-bold"></div> <div class=".font-bold"></div> This compiles to: .font-bold{ font-weight: bold; } So... when using SX like thi ...

Concealing a message within a section that has been minimized to a width of 0

Recently, I've encountered a challenge in attempting to set the width of a section to 0 and have all its contents also disappear. Despite changing the section's width to 0px, the text inside continues to be displayed and even shifts off to the si ...

Complete a wtforms form by selecting a date using the DateField

I am looking to create a website using bootstrap 5 and flask-wtforms (python 3.11). I have a Form with a Datepicker, and when I select a date, I want the Form to be submitted automatically. Currently, I have a submit button that works fine, but I would pre ...

Should pages be indexed to index.php or should the top and bottom parts of the page be created and included in all content pages?

I've been out of the webpage creation game for a while, but I've recently decided to get back into it. Like everyone else, I want to learn the best way to do this. However, I find myself facing a dilemma on how to structure my pages. My initial i ...

Is your localhost contact form experiencing issues?

I am receiving an error even though I believe I have done everything correctly. I am currently using localhost, could that be the issue? If not, what else could it be? The error is occurring on this line: $name = $_POST['name']; Here is the co ...