What is the best way to use CSS to connect the tips of shapes for perfect alignment?

In an attempt to utilize CSS3 for creating shapes and aligning them to resemble a certain image, I have successfully crafted a pink square along with 4 gray rectangles. Initially, my approach involved creating separate div elements for each shape and adjusting their margins and rotations accordingly. However, this method seems like hard coding in CSS, which may not be considered best practice.

#pinkBlock {
  width: 100px;
  height: 100px;
  background-color: #FFC0CB;
  left: 10px;
  top: 10px;
}

#upRect {
  width: 50px;
  height: 200px;
  background-color: #D3D3D3;
}

#downRect {
  width: 50px;
  height: 200px;
  background-color: #D3D3D3;
}

#leftRect {
  width: 50px;
  height: 200px;
  background-color: #D3D3D3;
}

#rightRect {
  width: 50px;
  height: 200px;
  background-color: #D3D3D3;
}

Currently, I have the 4 gray rectangles stacked on top of one another with the pink block positioned at the bottom. What steps should I take to ensure that the gray rectangles touch each other's tips while retaining the pink block in the center?

Answer №1

An elegant solution utilizing the power of CSS Grid layout - explore the demo below with detailed explanations:

.container {
  display: grid;
  grid-template-areas: '. top .'
                       'left center right'
                       '. bottom.'; /* defining the layout */
  grid-template-rows: 50px 200px 50px; /* setting the row heights */
  grid-template-columns: 50px 200px 50px; /* establishing the column widths */
}

#pinkBlock {
  background-color: #FFC0CB;
  grid-area: center;
  height: 50px;
  width: 50px;
  justify-self: center; /* horizontal alignment within grid item */
  align-self: center; /* vertical alignment within grid item */
}

#topRect {
  background-color: #D3D3D3;
  grid-area: top; /* positioning in the layout */
}

#bottomRect {
  background-color: #D3D3D3;
  grid-area: bottom; /* positioning in the layout */
}

#leftRect {
  background-color: #D3D3D3;
  grid-area: left; /* positioning in the layout */
}

#rightRect {
  background-color: #D3D3D3;
  grid-area: right; /* positioning in the layout */
}
<div class="container">
  <div id="pinkBlock"></div>
  <div id="topRect"></div>
  <div id="leftRect"></div>
  <div id="rightRect"></div>
  <div id="bottomRect"></div>
</div>

Answer №2

Here's a neat trick to achieve the desired effect using just a single element and multiple backgrounds:

.box {
  --d:20px;
  width:200px;
  height:200px;
  background:
    linear-gradient(grey,grey) top   / calc(100% - 2*var(--d)) var(--d),
    linear-gradient(grey,grey) bottom/ calc(100% - 2*var(--d)) var(--d),
    linear-gradient(grey,grey) left  / var(--d) calc(100% - 2*var(--d)),
    linear-gradient(grey,grey) right / var(--d) calc(100% - 2*var(--d)),
    linear-gradient(pink,pink) center/var(--d) var(--d);
 background-repeat:no-repeat;
 display:inline-block;
}
<div class="box">

</div>
<div class="box" style="--d:40px;">

</div>

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

Material UI AppBar fixed position that is not part of a grid container

Hey everyone, I'm really struggling with this issue. I recently set my AppBar to have a position of "fixed". However, now the appbar is no longer contained within its container and instead spans the entire screen. I've been trying to figure it ou ...

Struggling to break free from the confines of an overflow-hidden container in swiperjs due to

I have a slider that utilizes swiperjs and I want to incorporate a dropdown menu within each swiper-slide. However, I've encountered an issue where the dropdown menu is unable to escape the root container due to the positioning constraints. For a dem ...

Incorporating pre-designed elements into the bottom section of my

I'm currently facing an issue while trying to integrate a footer content from a template into my slideout footer. The problem is that the template footer is currently spanning across the entire width of the page, and I am struggling to contain it with ...

The floating label appears distorted when used with a datalist input in Bootstrap 5

How can I get the floating label to display correctly for datalists in Bootstrap 5? Currently, it looks like this... It's working fine without the form-floating class, but I want to use the floating form feature. <div class="form-floating" ...

Tips for adjusting the background color of an ag-grid component in a React application

Looking to make a full background color change in ag-grid within a React application. Experimented with row cell style, but aiming for the entire grid background change. Here is my code snippet: { headerName: "Module Name", field: "ModuleName", ...

Display or conceal columns based on their index

<div ng-controller="checkBoxController"> <div id="myModal" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="moda ...

Change the text inside a container without losing any associated event listeners using JavaScript or jQuery

Here is the HTML code: <div id="div001"> This is ${Row.1}. <p>${Row.2} explain something else.</p> <p>${Row.3} welcome you. <span>${Hello.World}, this is a new world.</span></p> </div> My objective is ...

Switching the body's background image dynamically using javascript

I'm attempting to switch up the background image using JavaScript. Here's how I've defined the background: body { background: black; overflow-x: hidden; overflow-y: hidden; } body:before { overflow-x: hidden; overflow ...

Angular's input event fails to trigger for dynamically generated fields when pasted into them

When working with a form that has dynamically generated input fields, I encountered an issue. I needed to display a preview of the input contents on a div while pasting content into the fields. This project is built using Angular 11. Below is my .ts file: ...

Tips for effectively removing objects from a PixiJS application

I have embarked on a game development project using Pixi.js that involves items falling from the top of the canvas. The functionality I've implemented so far allows items to spawn and move down the canvas until they reach the end of the window, at whi ...

What is the best way to showcase a div on top of all other elements in an HTML page?

As a newcomer to html and css, I have successfully created a div that contains city names. The issue I am currently facing is when I click on a button to display the div, it gets hidden behind the next section of my page. Take a look at the image below for ...

Tips on controlling the color of an input field in a form using React

Within my React app, there is a text input field situated inside a form that displays in its default gray color before any interaction: https://i.stack.imgur.com/FdNos.png Once the input field is clicked on, it changes to white as shown here: https://i.s ...

What is the best way to keep a bootstrap navbar fixed at the top when scrolling? (It's a bit tricky)

Check out this image of my website header. HERE Is there a way to achieve a smooth scrolling effect for the blue navbar as I scroll down the page? (Specifically just the navbar, excluding the logo and social media elements). Using position:fixed; does ...

When clicking on a div with the CSS property user-select set to none, the selected text in a contenteditable element is

I'm currently working on a unique JavaScript rich text editor that utilizes div elements with the CSS property user-select: none instead of traditional buttons. While this approach works perfectly in Firefox, I've encountered a major issue with G ...

Is it necessary to use the strict doctype if our code is already valid with the transitional doctype?

Are there any drawbacks to using a transitional doctype with presentational or deprecated elements, even if our code is valid in W3C validation with a transitional doctype? Will we need to rewrite or edit the code of websites using XHTML/HTML transitional ...

HTML scroll bar functioning intermittently

Here is my code snippet. table#projectTable > tbody , table#productTable > tbody{ height: 300px; overflow: auto; } The scrollbar is functional when clicking the top part, but not the bottom. It's quite odd. Upon usin ...

What makes the nav class different from the navbar class in Bootstrap?

Recently, I delved into learning about bootstrap and web development. To my surprise, I stumbled upon the nav and navbar classes in bootstrap 4. I'm curious to know what sets them apart from each other and when it's best to use one over the other ...

Adding a class to unhovered elements with jQuery/JS: a step-by-step guide

I have a variety of elements that are almost working how I want them to. There are four divs in total. Depending on the URL visited, I want a specific div to be fully transparent/active. The next div in line should animate in and out of transparency simul ...

Adjusting the width of the rail in Material UI's vertical Slider component in React

After attempting to adjust the width and height of the rail property on the material ui slider I obtained from their website demo, I have encountered an issue with changing the thickness. import React from "react"; import { withStyles, makeStyles } from " ...

Encountering a vast expanse of empty white void

Attempting to create a scrollbar within my content div instead of the entire window seems to be causing a large white space in the content box, and I'm struggling to understand why. Can someone take a look and help me identify the issue? You can view ...