Arrange 4 divs (children) at each corner of the parent element

Currently, I am facing a challenge in positioning 4 divs in the corners of their parent div. The HTML code structure is as follows:

<div class="resize">
    <div class="n w res"></div>
    <div class="n e res"></div>
    <div class="s e res"></div>
    <div class="s w res"></div>
</div>

The associated CSS styling is outlined below:

.resize .res {
    width: 6px;
    height: 6px;
    border: 1px solid black;
    position: relative;
}

.resize .res.n { top: -4px; }
.resize .res.s { bottom: -4px; }
.resize .res.w { left: -4px; }
.resize .res.e { right: -4px; }

.resize {
    width: 100px;
    background: red;
    height: 100px;
}

Despite the defined styling, the .res div elements are not aligning correctly at the corners...

If you have any insights on how to resolve this issue, please provide guidance.

Link to JSFIDDLE for reference

Answer №1

To make the code work properly, you should update the position type of .res to absolute and add position:relative in the resize class.
It is important for absolute divs to be nested within a relative positioned parent to function correctly.
Give this a try:

.resize .res {
    width: 6px;
    height: 6px;
    border: 1px solid black;
    position: absolute;
}

.resize .res.n { top: -4px; }
.resize .res.s { bottom: -4px; }
.resize .res.w { left: -4px; }
.resize .res.e { right: -4px; }

.resize {
    width: 100px;
    background: red;
    height: 100px;
    position:relative;
}

DEMO

Answer №2

If you want to achieve your goal, make sure to incorporate position:absolute; in the styling of the .res div elements. Remember that for an absolutely positioned div (such as .resize in your scenario), its parent should have a relatively positioned element.

For additional details, check out this resource.

Access the FIDDLE here

CSS :

.resize .res {
    width: 6px;
    height: 6px;
    border: 1px solid black;
    position:absolute;
}

.resize .res.n { top: -4px; }
.resize .res.s { bottom: -4px; }
.resize .res.w { left: -4px; }
.resize .res.e { right: -4px; }

.resize {
    position: relative;
    width: 100px;
    background: red;
    height: 100px;
}

Answer №3

Are you aiming for this specific outcome?

Here is the HTML code:

<div class="resize">
    <div class="n w res"></div>
    <div class="n e res"></div>
    <div class="s e res"></div>
    <div class="s w res"></div>
</div>    

This is the CSS code:

.resize .res {
    width: 6px;
    height: 6px;
    border: 1px solid black;
    position: absolute;
}

.resize .res.n { top: -4px; }
.resize .res.s { bottom: -4px; }
.resize .res.w { left: -4px; }
.resize .res.e { right: -4px; }

.resize {
    width: 100px;
    background: red;
    height: 100px;
    position: relative;
}

Check out the demo to see it in action.

The utilization of the position property leads to the creation of a new block formatting context, which will be in relation to the nearest ancestor with a defined position property.

Answer №4

To achieve the desired layout, I suggest positioning the resizing div as relative and the four contained divs as absolute (assuming that the intention is for the four divs to be located within the resize div):

.resize .res {
    width: 6px;
    height: 6px;
    border: 1px solid black;
    position: absolute;
}

.resize .res.n { top: 0px; }
.resize .res.s { bottom: 0px; }
.resize .res.w { left: 0px; }
.resize .res.e { right: 0px; }

.resize {
    width: 100px;
    background: red;
    height: 100px;
    position: relative;
}

Answer №5

In order to resize the div, you must set the position relative for the outer div and position absolute for the inner div.

.resize .res {
    width: 6px;
    height: 6px;
    border: 1px solid black;
    position: absolute;
}

.resize .res.n { top: -4px; }
.resize .res.s { bottom: -4px; }
.resize .res.w { left: -4px; }
.resize .res.e { right: -4px; }

.resize {
    width: 100px;
    background: red;
    height: 100px;
    position: relative;
}

Answer №6

Make sure to include position: relative in .resize and position: absolute in .resize .res

Check out this example on jsfiddle

.resize .res {
    width: 6px;
    height: 6px;
    border: 1px solid black;
    position: absolute;
}

.resize .res.n { top: -4px; }
.resize .res.s { bottom: -4px; }
.resize .res.w { left: -4px; }
.resize .res.e { right: -4px; }

.resize {
    width: 100px;
    background: red;
    height: 100px;
    position:relative;
}

Answer №7

To ensure proper alignment, remember to declare parent as relative and child as absolute

Check out the demonstration here

If you haven't specified the position of the parent element, that could be the issue:

.resize{
   position: relative; /*make sure this is included */
}
.resize .res {
    width: 6px;
    height: 6px;
    border: 1px solid black;
    position: absolute; /*this line was updated*/
}

Answer №8

You have chosen to utilize position: relative for the small rectangles, signifying their position in relation to the original point.

In order to accurately position them in the corners, relative to their parent container, it is necessary to apply the following CSS:

.resize {
    position: relative;
}
.resize .res {
    position: absolute;
}

Feel free to view the updated JSFiddle demo

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

Switch up text using jQuery on css-tricks.com

I am having trouble with the code I found on a page about toggling text using jQuery from this link. Could someone please explain to me the significance of ':visible'? Thank you. Here is the fiddle link, and below is the code that I copied. Vi ...

Tips for maintaining the original height of an image within an <img> element using HTML and CSS

I am facing an issue with a grid where I add grid-items, and then use the thumbnail class to provide dimensions to the thumbnail. When I insert an image into the <img> tag, it alters the height dimension, which is not desirable. I want the thumbnail ...

What is the superior choice for developing a card game: creating it using SVG and the kinetic.js library or utilizing the oCanvas library

When it comes to creating a card game, which platform is more suitable: SVG or canvas using kinetic.js or oCanvas library? In terms of performance, event handling, and object tracking, which option would be more efficient? Additionally, if there is a bet ...

Matching Figures to their Descriptions

I am currently working on a website and looking to include images with captions that display the price of each item underneath. The issue I'm facing is that the captions are aligning horizontally instead of vertically. Can anyone provide assistance wi ...

modifying the child divs contained in the main div

Hey there! I'm currently using Bootstrap to design an application and I've got the entire form inside a container. However, as I add more child elements within the container div, I would like the parent div to expand automatically to show all the ...

Exploring the Link Between jQuery and HTML

Is it possible to connect between an HTML file and a jQuery file? Here is an example scenario: jquery-test.js: $(document).ready(function(){ $('#inputForm').submit(function(){ $('#inputForm :text:not("#submit")').each(func ...

Exploring the Latest Features: Using Angular 7 with Bootstrap Collapse for Dynamic Aria-Controls

I am currently working on creating my own component to use within an *ngFor loop. I am facing an issue where I need a unique value for my Collapse feature. Right now, when I click on the second main row in the table, the collapse feature only shows the inn ...

Image displayed in tooltip when hovering over

I have been experimenting with displaying an image in a tooltip when hovering over it. I came across a fantastic solution that suits my needs: Codepen CSS: @import "compass"; * { box-sizing: border-box; } .tooltip { position: relative; border-bo ...

Issues with CSS transitions/animations not functioning properly across different browsers

Take a look at this FIDDLE Link. I'm facing an issue where the animation works smoothly in Firefox, but it's not functioning as expected in Chrome. Kindly open the fiddle in both browsers (Chrome and Firefox), hover over the image, and notice t ...

What is the best way to align content in the center when its parent element has a position property

JSBIN Is it possible to center the number row without using JavaScript to calculate its position, considering its parent has a fixed attribute? I've tried using CSS properties like margin and text-align with no success. Any suggestions on achieving t ...

Creating personalized styles for my React components with the help of styled-components

Exploring the power of styled-components for custom styling on child components has been an interesting journey for me. For instance, I recently crafted a unique card component named myCard. Take a look at how it's structured: import React from "rea ...

Tips on automating the process of moving overflowing elements into a dropdown menu

Challenge Description In need of a dynamic navigation bar, I faced the problem of displaying only the first X items on one line and have the remaining items hidden in a "Show more" dropdown. The challenge was to calculate the width of each item accurately ...

After deploying to Firebase, animations suddenly cease to function

After running npm run-script build and deploying my React app to Firebase hosting with firebase deploy, I encountered an issue where my animations stopped working. I'm puzzled as to why this is happening, especially since I've included keyframes ...

Guide on breaking up a string into separate lines

Can someone help me with separating the strings in this line "Add Problem Report \n Add Maintenance Report \n Add Expenses Report \n Add Contract", into new lines? I have tried using br, pre, and \n but it does not seem to work. HTML ...

Google Chrome applying its unique style compositions

I recently started transitioning a website from HTML to Wordpress, and encountered an unexpected challenge with Google Chrome applying unfamiliar styles. Specifically, the issue lies with the positioning of the background image bg.gif. While Internet Explo ...

Python Django: Implementing Proper Spacing in HTML Email Titles

We are currently working on integrating the email feature using Python Django, but we are encountering an issue with extra spaces appearing in the header of the emails as shown in these screenshots: https://i.sstatic.net/EpCca.png https://i.sstatic.net/q6 ...

Is the presence of hidden list items leading to excessive white space?

I have a menu that appears when hovering over a list item. Here is an example: <li> <ul class="topmenu"> <li class="submenu"> <a class="otherIT" href="#" title="Common IT Tasks"><img src="./images/ittasks.png" alt= ...

Having trouble with the JavaScript DOM rewrite for aligning text?

function modifyTextAlignment(){ document.getElementById("th1").style.textAlign = "right"; } #th1{ text-align:left; } <table> <tr><center><th colspan="2" id="th1">List of Important Emergency Contacts</th><center></tr& ...

Is there a way to make a TABLE expand to match the height of its surrounding element? (or, tackling sluggishness in IE with JavaScript)

I am facing a challenge with a web page where I have a table nested inside of a TD tag. Despite the unconventional approach, I need to ensure that the height of the nested table matches the height of the TD cell containing it upon page load. Currently, I a ...

Is it possible to manipulate an Angular #variableName in order to retrieve an ElementRef for an HTML element?

Suppose I have a scenario where I create a button like this: <button #myButton>My Button</button> ...and then use ViewChild in the following way: @ViewChild('myButton', { static: true }) createButton: ElementRef; In this case, creat ...