-4

I want to create this design in HTML/CSS and add a text over it on each looking square.

union squares

How can i achieve it in html and CSS?

This pattern should be responsive also that's why i want it coded so later on various screens it can me modified and adjusted. I want to know if its possible in css and also is there any library for these types of shapes?

I tried clip mask but it doesn't give the whole thing as i have shown. I also tried adding the image directly and adding the text over it in position absolute manner but its breaking the design on small screens below 1200px. Any help will be appreciated.

Here is the attached code it gives the first half only i want to know how can i achieve all four.

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.union-shape {
  width: 800px;
  height: 300px;
  background: linear-gradient(to bottom, #e8ebfa, #c2c6f1);
  clip-path: path("M 0,50 A 50,50 0 0 1 50,0 H 450 A 50,50 0 0 1 500,50 V 120 A 50,50 0 0 0 550,170 H 850 A 50,50 0 0 1 900,220 V 250 A 50,50 0 0 1 850,300 H 50 A 50,50 0 0 1 0,250 Z");
  position: relative;
}

/* Optional Grid Overlay */
.union-shape::before {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(rgba(255, 255, 255, 0.1) 1px, transparent 1px),
    linear-gradient(to right, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 40px 40px;
  top: 0;
  left: 0;
}
<body>
  <div class="union-shape"></div>
</body>

1 Answer 1

0

The trouble with doing this exclusively in CSS is that vector paths (such as you’d need for a curvy clip-path like this) cannot be expressed in percentages. The trick to making a vector path responsive is to use an SVG. The points in the path relate to the intrinsic size of the SVG, but then the entire SVG can be scaled responsively. An SVG can be used a responsive background image, too.

  1. The first job is to create an SVG containing your desired path, using an SVG editor. My favourite is Boxy SVG. Apply the gradient fill to it.
  2. Next you need to URL-encode the SVG so that it can be used as a background-image in CSS. Use this tool by yoksel to do that.
  3. Set the background-image of .union-shape to your URL-encoded SVG.
  4. Set background-size: contain so that the image will scale.
  5. Make .union-shape responsive by setting width: 100%, leave height as auto, then set aspect-ratio to match the actual aspect ratio of your SVG. The SVG I created for this example is 800x250, so the aspect ratio is 800/250 or 16/5.
  6. Absolutely position and size your four content squares, using percentages to make them responsive. You can calculate the percentages from the SVG: divide x values by the width and y values by the height, then multiply by 100.

enter image description here

body {
  background-color: #f5f5f5;
}

.union-shape {
  width: 100%;
  aspect-ratio: 16/5;
  position: relative;
  background-image: url("data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 800 250' width='800px' height='250px'%3E%3Cdefs%3E%3ClinearGradient gradientUnits='userSpaceOnUse' x1='400' y1='0' x2='400' y2='250' id='gradient-0'%3E%3Cstop offset='0' style='stop-color: rgb(232, 235, 250);'/%3E%3Cstop offset='1' style='stop-color: rgb(194, 198, 241);'/%3E%3C/linearGradient%3E%3C/defs%3E%3Cpath d='M 19 14.796 L 218.862 14.796 C 229.355 14.796 237.862 23.303 237.862 33.796 L 237.9 56.1 C 237.9 66.6 246.4 75.1 256.9 75.1 C 256.9 75.1 367.5 75.1 367.466 75.082 C 378 75.1 386.1 65.4 386.146 54.926 C 386.1 54.9 386.146 19 386.146 19 C 386.146 8.507 394.653 0 405.146 0 L 605.008 0 C 615.501 0 624.008 8.507 624.008 19 L 624 22.8 C 624 33.3 632.5 41.8 643 41.8 L 781 41.821 C 791.493 41.821 800 50.328 800 60.821 L 800 209.738 C 800 220.231 791.493 228.738 781 228.738 L 581.138 228.738 C 570.645 228.738 562.138 220.231 562.138 209.738 C 562.138 209.738 562.1 205.9 562.1 205.9 C 562.1 195.4 553.6 186.9 543.068 186.914 C 543.1 186.9 445.9 186.9 445.9 186.9 C 435.4 186.9 426.9 195.4 426.9 205.9 L 426.887 231 C 426.887 241.493 418.38 250 407.887 250 L 208.025 250 C 197.532 250 189.025 241.493 189.025 231 L 189 220.7 C 189 210.2 180.5 201.7 170 201.7 L 19 201.713 C 8.507 201.713 0 193.206 0 182.713 L 0 33.796 C 0 23.303 8.507 14.796 19 14.796 Z' style='stroke: rgb(0, 0, 0); stroke-width: 0px; fill: url(&quot;%23gradient-0&quot;);'/%3E%3C/svg%3E");
  background-size: contain;
  margin-bottom: 2em;
}

/* Optional Grid Overlay */
.union-shape::before {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(rgba(255, 255, 255, 0.1) 1px, transparent 1px),
    linear-gradient(to right, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 40px 40px;
  top: 0;
  left: 0;
}

.union-shape > * {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
}

.union-shape.with-borders > * {
  border: 2px solid rgb(255 0 0 / 0.5);
}

.union-shape > :nth-child(1) {
  left: 0px;
  top: 5.92%;
  width: 29.75%;
  height: 74.8%;
}

.union-shape > :nth-child(2) {
  left: 23.625%;
  top: 30%;
  width: 29.75%;
  height: 70%;
}

.union-shape > :nth-child(3) {
  left: 48.25%;
  top: 0%;
  width: 29.75%;
  height: 74.8%;
}

.union-shape > :nth-child(4) {
  left: 70.25%;
  top: 16.72%;
  width: 29.75%;
  height: 74.8%;
}
<div class="union-shape with-borders">
  <div>One</div>
  <div>Two</div>
  <div>Three</div>
  <div>Four</div>
</div>

<div class="union-shape">
  <div>One</div>
  <div>Two</div>
  <div>Three</div>
  <div>Four</div>
</div>

You may also find useful information in this thread.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.