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

What is causing my grayscale function to only impact part of the canvas?

As a beginner programmer, I have been experimenting with creating a grayscale function in JavaScript for practice. This is the code I have come up with: <canvas width='400' height='400'></canvas> <script> var can ...

creating a table displaying data in a horizontal format sourced from ajax/json transformation

I am currently working on a piece of code that retrieves a list of IDs from local storage and compares them to IDs in a JSON file. If there is a match, the corresponding data is displayed in a table. However, I am facing an issue with my table layout as i ...

concealing components during screen adjustments

There are 3 identical <div>s available: <div class="box">Hello World!</div> <div class="box">Hello World!</div> <div class="box">Hello World!</div> I need these <div>s to ...

Is it possible to showcase dates within input text boxes prior to POSTing the form?

I am currently working on a form that posts to a PHP script by selecting a date range. For a better understanding, you can check out my visual representation here. Before the data is posted, I would like to display the selected dates in empty boxes or inpu ...

Unable to expand Bootstrap navbar due to collapsing issue

I recently implemented a collapsed navbar on my website using bootstrap. However, I'm facing an issue where it does not open when clicking on the hamburger menu. After researching various solutions for this problem and trying them out, none of them s ...

changing size when hovered over with the mouse is not consistent between entering and exiting

Hi there, I'm currently utilizing the transform: scale(); property on a website and could use some assistance with a particular issue I haven't been able to find an answer for online. Here is the code snippet I'm working with: HTML: <d ...

How can I incorporate a custom color into a preset navbar on an HTML webpage?

<div class="navbar-header"> <button aria-controls="navbar" aria-expanded="false" data-target="#navbar" data-toggle="collapse" style="color: blue;" class="navbar-toggle collapsed" type="button"> <i class="fa fa-reo ...

Resizing a damaged HTML table to fit within a designated width using CSS

Here is a challenge - I have some broken HTML that I can't touch, and don't want to use JavaScript to fix. My goal is to make it fit within a specific width: http://jsfiddle.net/P7A9m/2/ <div id="main"> <table> <tr> ...

Expanding the outer div with Jquery's append() function to accommodate the inner div elements perfectly

I am facing an issue where my outer div does not expand automatically to fit the elements I append inside it using jQuery. The structure of my div is as follows: <div class="well" id='expand'> <div class="container"> < ...

Customizing the tab content background color in MaterializeCSS

I've been struggling to customize the background color of my MaterializeCSS tabs content by referring to their official documentation: If you visit the website, you'll notice that the default tabs have a white background, while the 'Test 1& ...

Adding hue to the portion of text following a JavaScript split() operation

I need assistance in printing the text entered in a textarea with different colors. I am separating the string using the split() method, which works fine. However, I now want to print the substrings in the textarea with colors. How can this be achieved? & ...

If every single item in an array satisfies a specific condition

I am working with a structure that looks like this: { documentGroup: { Id: 000 Children: [ { Id: 000 Status: 1 }, { Id: 000 Status: 2 ...

CSS login validator immobilized

In my Visual Studio project, I am facing an issue with applying CSS to two requiredfield validators. I have tried using the following code: #vdtrUserLogin { top:65px; left:750; position:absolute} #vdtrPasswordLogin0 { top:65px; left:900; position:absolute ...

Delete the CSS class from a specific HTML element

Having just started learning HTML and CSS, I encountered an issue. I applied a CSS class to a table element. Subsequently, I inserted a child table within the parent table. The problem arose when the CSS class of the parent table also influenced the child ...

Is it possible to convert the text.json file into a jQuery animation?

Looking to extract information from text.json and integrate it with my jquery.animation(). My goal is similar to the one demonstrated here. Instead of using "data" attributes like in that example, I want to parse the text based on the "ID" property as a k ...

Expansive dropdown menu stretching across the entire width, yet the content within restricted to a width of

I have recently made some updates to our website's dropdown menu, expanding the results displayed. However, I would like the dropdown contents to fit within the width of our page. I have tried adjusting the CSS with limited success: .menu > ul > ...

Font Awesome symbols using the class "fa-brands" are not functioning as expected

I have included a font awesome library in the header using the following code: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> However, when I try to display an ic ...

PHP is having trouble updating the database when multiple radio buttons are selected. It appears that only the most recent radio button selected will be updated in the database

In my form, there are 2 numbers and 4 radio buttons for each. Only one option can be selected per number. When the form is submitted, only the database for question 2 will be updated. However, if question 2 is left blank, then question 1's database w ...

Animated Gradient Header with CSS

I've been attempting to add an animated gradient to my header class, but so far I haven't been successful. I want the gradient to apply only to the header, not the body. Here's the link I'm using for reference: Can anyone offer some i ...

Limited functionality: MVC 5, Ajax, and Jquery script runs once

<script> $(function () { var ajaxSubmit = function () { var $form = $(this); var settings = { data: $(this).serialize(), url: $(this).attr("action"), type: $(this).attr("method") }; ...