Objects that incorporate a mixture of relative and absolute positioning properties

I am looking to display elements that are relatively positioned next to each other but absolutely positioned above everything else. You can view the desired rendering here and my attempt at achieving it in the fiddle below (or this codepen). Thank you!

.row {
  display: flex;
}

.box {
  height: 100px;
  width: 100px;
  background: yellow;
  border: 2px solid red;
  margin: 5px;
}

.tallBox {
  height: 150px;
  width: 100px;
  background: orange;
  border: 2px solid green;
  margin: 5px;
}

.bar {
  height: 100px;
  width: 442px;
  background: pink;
  border: 2px solid red;
  margin: 5px;
}

.group {
  display: flex;
  // position: absolute;
}

.gap {
  height: 100px;
}
<h2>Proposal: </h2>
<img src="https://i.sstatic.net/5OZMg.png">
<div class="gap"></div>

<h2>Current rendering without changing any position properties:</h2>
<section class="row">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</section>
<section class="row">
  <div class="bar"></div>
</section>

<div class="gap"></div>

<h2>Attempt:</h2>
<section class="row special">
  <div class="box"></div>
  <div class="group">
    <div class="tallBox"></div>
    <div class="tallBox"></div>
  </div>
  <div class="box"></div>
</section>
<section class="row">
  <div class="bar"></div>
</section>

Answer №1

If you want to achieve a certain layout, you may want to explore using negative margins instead of relying on the position:absolute property.

.row {
  display: flex;
}

.box {
  height: 100px;
  width: 100px;
  background: yellow;
  border: 2px solid red;
  margin: 5px;
}

.tallBox {
  height: 150px;
  width: 100px;
  background: orange;
  border: 2px solid green;
  margin: 5px;
  margin-bottom:-50px;
}

.bar {
  height: 100px;
  width: 442px;
  background: pink;
  border: 2px solid red;
  margin: 5px;
}

.group {
  display: flex;
  z-index:2;
}

.gap {
  height: 100px;
}
<section class="row special">
  <div class="box"></div>
  <div class="group">
    <div class="tallBox"></div>
    <div class="tallBox"></div>
  </div>
  <div class="box"></div>
</section>
<section class="row">
  <div class="bar"></div>
</section>

Answer №2

Below is the solution I found for my query:

.container {
  display: flex;
}

.box {
  height: 100px;
  width: 100px;
  background: yellow;
  border: 2px solid red;
  margin: 5px;
}

.tallBox {
  height: 150px;
  background: orange;
  border: 2px solid green;
  width: 100px;
  position: absolute
}

.tallBox-container {
  positin: relative;
  width: 100px;
  margin: 5px;
}

.bar {
  height: 100px;
  width: 442px;
  background: pink;
  border: 2px solid red;
  margin: 5px;
}


.group {
  display: flex;
  // position: absolute;
}

.gap {
  height: 100px;
}
<section class="container special">
  <div class="box"></div>
  <div class="group">
    <div class="tallBox-container">   
      <div class="tallBox"></div>
    </div>
    <div class="tallBox-container"> 
      <div class="tallBox"></div>
    </div>
  </div>
  <div class="box"></div>
</section>
<section class="container">
  <div class="bar"></div>
</section>

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

Run a function continuously while a key is being held down

I am currently working on a Javascript program that will move a div up and down based on key inputs. The idea is to continuously update the 'top' style value of the div while a specific key is pressed. While the basic function works, I am struggl ...

Steps for creating an SQL query using a selection

I am looking for a solution to incorporate the selection made from an HTML select element as a variable in my SQL query. Essentially, if a user chooses "iPhone" from the list, I want the query to be something like select * where name = iPhone. I am unsure ...

Make the checkbox font-weight bold when it is checked

Attempting to apply custom CSS to enhance the appearance of checkboxes using input and label elements. The goal is to make the font-weight bold when a user clicks on the checkbox, and then revert it back to regular if clicked again. Currently stuck on this ...

What is the process for invoking a websocket from an HTML client?

I have created a WCF Service using netHttpBinding binding and it is hosted on IIS 8 (Windows Server 2012). The interfaces for the service are as follows: [ServiceContract(CallbackContract = typeof(IDuplexCallbackContract))] public interface IHelloWebSocke ...

What are some strategies to enhance the performance of JavaScript pagination to ensure it functions effectively for varying numbers of pages?

Currently, I am working on building Vanilla Javascript Pagination. I encountered an issue where the pagination seems to work flawlessly only when there are precisely 7 pages. If the page count exceeds or falls below 7, some minor bugs start to surface. I h ...

What is the best way to change the background color of a specific link in the top navigation bar in WordPress?

I'm currently facing a challenge while attempting to create a single link within a WordPress top navigation menu that has a unique background color. Although my approach seems to work on most pages, I've encountered issues where it doesn't ...

innerhtml+= Learn how to retrieve data using ajax and perform a specific action

I am a beginner trying to learn here... I am trying to add some events from dynamically loaded content but it always returns the same ID. My issue is with using innerHTML+= and calling it with AJAX like this.. $.ajax({ url: 'get ...

Challenges with creating a responsive layout for a Bootstrap timeline

I've been utilizing a Bootstrap-based sample timeline, and while it's functional overall, some layout elements become misaligned when resizing the window. I've made adjustments to the code so that all timeline items are positioned to the rig ...

Utilizing html2canvas in pdf-lib using JavaScript

Hey there, I have a few questions concerning the usage of html2canvas. Currently, I am delving into JavaScript and working on a platform aimed at generating budgets that can be downloaded in PDF format. My goal is to utilize html2canvas to capture a dynami ...

UTF-8 encoding experiencing issues on select Android mobile devices

Our new website, powered by Wordpress, is up and running but some users are experiencing issues with displaying Swedish special characters. It seems to be happening specifically on Android devices, regardless of the browser being used. However, I have not ...

What is the best way to position the button in line with multiple dropdown menus that include labels?

My interface consists of 3 dropdowns and a submit button, each with a label. Initially, I struggled to align the button with the dropdowns themselves (not the labels), so I resorted to adding a placeholder label and matching its color with the background. ...

While continuing to input text, make sure to highlight a specific element from the search results list

Currently, I am using a customized search feature in my React.js application. I am looking for a way to focus on an element within the search results so that I can navigate using arrow keys. At the same time, I need to keep the input field focused to conti ...

Adapting CSS styles according to the height of the viewport

Goldman Sachs has an interesting webpage located here. One feature that caught my eye is the header that appears as you scroll down, with squares changing from white to blue based on the section of the page. I'm curious about how they achieved this ef ...

Creating an HTML template in an Angular service and utilizing it as an HTMLTemplateRef

I'm currently working on a major project that has a specific requirement. As part of this project, I am utilizing a library which allows me to open dialog (modal) popups. In order to do so, I need to configure some options for the modal opening proce ...

The problem encountered when transferring a file from a local file system to a PHP server

I'm having trouble uploading a file from my local file system to a remote server using PHP. I'm using the move_uploaded_file function, but when I select a file from my local file system, it seems to be looking for the file on the remote server, ...

Update the YouTube API inside the iframe

I have successfully implemented the iframe API to embed a YouTube video on my website. loadAPIPls('WQBUy6t3f0U'); function loadAPIPls(idYTB) { loadYouTubeAPI(); window.onYouTubeIframeAPIReady = function() { createYouTubePlayer(idYTB ...

I'm curious if there is a specific jQuery event that triggers right before an element loses focus or right before the next element gains focus

I am facing a situation where I have two input elements in a form that are closely connected. The first element triggers an ajax call to check for existing data in the database when the user tries to move away from it, while the second element opens a moda ...

Conceal a different CSS class if a certain CSS class is present on the page

I am currently working on organizing my page to distinguish between purchased and unsold products. For the items that have been bought, I am using the CSS class "winning_bid" within a div element. My goal is to hide another div with the class "price" if th ...

Positioning elements in a header using CSS techniques

I am currently working on designing a navigation menu header that includes a logo, along with some buttons aligned to the left side of the logo. However, I am facing an issue where these buttons appear at the bottom of the logo instead of aligning to the t ...

`Place text to the right side of the image`

Is there a way to align all three lines of text to the right of an image without wrapping on the second line? If you have any suggestions, please let me know! Check out this JSFiddle for reference. CSS: img { min-width:75px; height:90px; ve ...