Keep one div attached to the edge of another div to ensure they remain conjoined when resizing

Is there a way to center a Square within the border of another div? I have attempted the following method:

.container {
  position: relative;
  background-color: blue;
}

.square {
  position: absolute;
  height: 50px;
  width: 50px;
  border-radius: 0%;
  background-color: yellow;
  left: -5%;
  top: 25%;
}

.logoOnTheRight {
  max-height: 100px;
  max-width: 100px;
}

.logoOnTheRight img {
  width: 100%;
}

body {
  padding: 50px;
}
<div class="container d-flex flex-row-reverse">
  <div class="square"></div>
           <div class="logoOnTheRight p-2">
    <img src="https://i.imgur.com/sPuqd31g.jpg" />
  </div>
</div>

When in fullscreen mode, the Square aligns with the border as expected. However, upon resizing the page, the Square's position changes and it no longer stays on the border. I've observed that specifying the width of the container cont in pixels resolves this issue. But I want the container cont to be 100% width. How can I ensure that the Square remains on the border of cont during page resizing?

Answer №1

When utilizing position: absolute, you are essentially positioning an element independently without any correlation to other elements on the page.

If your goal is to position an element relative to another element, it's advisable to avoid using position: absolute. Instead, you can align these two elements to the left/right using flexbox as demonstrated below. For customized alignment of items, CSS adjustments are necessary to achieve the desired layout. It's recommended to avoid using position: absolute entirely for responsiveness.

Although there are certain scenarios where position: absolute is required, this particular situation does not call for it

In the provided example, I utilized display: flex in conjunction with align-items: center; to centrally align the items within the box. For more insights on flexbox, refer to this detailed guide at CSS Tricks.

To shift the box to the left, you can employ transform: translateX(-50%);

.cont {
  position: relative;
  background-color: blue;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.square {
  height: 50px;
  width: 50px;
  border-radius: 0%;
  transform: translateX(-50%);
  background-color: yellow;
}

.logoOnTheRight {
  max-height: 100px;
  max-width: 100px;
}

.logoOnTheRight img {
  width: 100%;
}

body {
  padding: 50px;
}
<div class="cont d-flex flex-row-reverse">
  <div class="square"></div>
  <div class="logoOnTheRight p-2">
    <img src="https://i.imgur.com/sPuqd31g.jpg" />
  </div>
</div>

Answer №2

Give this a shot

.box {
    position: absolute;
    height: 50px;
    width: 50px;
    border-radius: 0%;
    background-color: yellow;
    left: -25px;
    top: 25%;
}

Try replacing % with px for the left property

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

Aligning a Material UI button to the far right of a row

I am struggling to align a React material-ui button to the right-most of the page and despite trying to use the justify="flex-end" attribute within the following code, it doesn't seem to be working: <Grid item justify="flex-end" ...

Managing an iframes website using buttons and links: Tips and tricks

I am trying to create a website that includes an iframe for navigating through external websites using previous and next buttons. I also want to display the links on a sidebar so that users can click on them to automatically load the site in the iframe. I ...

Ways to ensure a form label is accessible via keyboard navigation

My HTML5 form collects user information and processes it upon submission. With the help of the jQuery Validation Plugin, input validation is performed. In case of errors, an error-message-container div is displayed at the top of the screen, listing all err ...

What is the best way to send various variables using an onclick event?

My action plan was as follows: $(document).on("click", "#deleteMe", function (){ var id = $(this).data("id"); console.log(id); // the function responsible for deleting the event using its id belongs here }); The HTML for my delete button lo ...

Draggable slider not functioning properly with linear interpolation

Recently, I've been delving into the world of "linear interpolation" and its application in creating easing effects. However, while the easing functionality of the draggable slider seems to be operational, I'm encountering an issue. The slider re ...

The functionality of the Delete and Edit buttons has been disabled

As I am developing a CRUD (Create, Read, Update, Delete) application that involves managing a list of students and their grades, I have encountered issues while trying to implement the delete and edit functionalities on the front end. Specifically, I am st ...

Customizing the HTMLElement class to modify particular attributes

Is there a way to modify the behavior of an HTMLElement's scrollTop property by adding some extra logic before updating the actual value? The common approach seems to be deleting the original property and using Object.defineProperty(): delete element. ...

What is the method for adding an attribute without a value to an HtmlGenericControl in C#

In my C#/ASP.net project, I am creating a new HtmlGenericControl and I need to add an attribute to the control without assigning it a value. This means having no ="" after the attribute name. oHtmlGenericControl = new HtmlGenericControl("MyHtmlTag"); oHtm ...

Building adaptable divs using bootstrap technology

I'm playing around with bootstrap and here is the code I came up with: <!doctype html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet ...

Sleek dialog sliding animation with Svelte

I'm struggling with a svelte component that I have and I'm trying to implement a slide down animation when it closes. The slide up animation is functioning correctly, but for some reason the slide down animation is not working. Does anyone have a ...

Tips for automatically displaying user text based on its natural direction

Looking for a way to properly display user inputted text in Unicode based on its general direction, rather than defaulting to left-to-right. An issue arises when dealing with Arabic text as shown below, where the English word (4th) is split apart: اُر ...

Looking for a replacement for EO.Pdf to convert HTML to PDF in C#? Check out wkhtmltopdf!

Currently, I am in the process of creating an HTML catalog for movies, with plans to convert it into a PDF format. While using EO.Pdf initially showed promise with a small test sample, issues arose when attempting to run it against the entire list of movie ...

What is the process of displaying data from a MySQL database on a label using PHP?

I am seeking a way to retrieve form labels from my Mysql database in order to efficiently display them in any language using select statement. Although I have attempted this method, it has proven difficult due to the various languages involved. A ...

Prevent the tab key from exiting the input field and instead direct it to a designated element on the webpage

I have customized a select menu for user interaction, but I am facing issues with normal keyboard behaviors not working as expected. Specifically, I want to enable tab functionality to navigate to my styled select menu just like when clicking tab to cycle ...

Trouble pausing CSS slide using -webkit-animation-play-state:paused; finding it ineffective

Below is the code snippet in question: .picTransition .item { position: absolute; left: 0; right: 0; opacity: 0; -webkit-animation: picTransition 56s linear infinite; -moz-animation: picTransition 56s linear infinite; -ms-animation: ...

Issue with Website Rendering: Safari 4 exhibits display glitch showing temporary content before showing a blank white screen

Currently, I'm in the process of developing a specialized rails application. However, there seems to be an unusual rendering error that has been reported by Safari 4 users. It's quite peculiar because the page appears briefly but quickly disappea ...

An issue has occurred while parsing the XML data, causing the document to not be properly formatted. As a result

Hey everyone, I'm currently working on an HTML/JavaScript project and have encountered a problem. There's a button in my code that is supposed to redirect to another page on my website, but when I click it, I receive the following error: XML Pars ...

Storing website data for localization purposes on the web platform

I am currently working on a project to develop a website that displays the selected language page upon first visit. The idea is that when a user clicks on a language name, such as French, it will be stored in web/local storage. Then, when the user returns ...

Seeking assistance with creating a form that is centered on the page

As a newcomer here, I am in the process of learning new things. My current challenge is to create a center form. Here is what I have experimented with so far: <!doctype html> <html lang="en> <head> <meta charset="utf-8"> &l ...

The responsive navigation bar yielded an unforeseen outcome

Looking to create a responsive navigation bar similar to the one shown here My code successfully shows and hides the navigation items, but it's not updating the burger icon No console errors or warnings are present This is my HTML: <nav> ...