Achieving overlapping div layouts with CSS styling

Displayed above are 4 different divs: red, green, and blue. I am seeking to have the blue div positioned directly below the green div, which contains the text "JACKSON". Additionally, I do not want the green div to expand beyond its content, nor do I want the red div to expand at all. The blue div should be centered according to the text "JACKSON", with a higher z-index than the other divs. My goal is to achieve this using only css.

Please note that the divs will not have specific pixel values for width and height.

https://i.sstatic.net/QhhEV.jpg

Answer №1

To center the blue div next to the JACKSON div, you can utilize CSS3 transforms. This method ensures that regardless of the size of the green divs, the blue div will always be centered (remember to include other necessary css prefixes):

HTML

<div class="red">
  <div class="green">
    DAVID
  </div>
  <div class="green">
    JACKSON
    <div class="blue">
      WHO IS DAVID AND JACKSON?
    </div>
  </div>
</div>

CSS

.red, .green, .blue{
  box-sizing: border-box;
  display: inline-block;
  padding: 5px;
}

.red{
  border: 2px solid red;
}

.green{
  border: 2px solid green;
  position: relative;
}

.blue{
  border: 2px solid blue;
  display: block;
  position: absolute;
  transform: translate(-50%, 100%);
  left: 50%;
  top: 0;
  white-space: nowrap;
}

View example on jsfiddle

Answer №2

Here is a simple CSS code snippet that you can customize to suit your needs.

To create an "overlap" effect with your div, you can adjust the margin property or combine it with the position property along with values like top, left, right, or bottom.

.red, .green, .blue {border: solid 4px; display: inline-block;}
.red {border-color: red;}
.green {border-color: green; margin-bottom: 1em;}
.blue {border-color: blue; margin-left: 2em; position: relative; top:-1em;}
<div class="red">
  <div class="green">David</div>
  <div class="green">Jackson</div>
</div>
<br />
<div class="blue">Who are David and Jackson?</div>

Answer №3

Do you think something like this could work for your needs?

.red {
  width: auto;
  display: inline-block;
  position: relative;
  padding: 5px;
  border: 2px solid red;
}

.green {
  width: auto;
  display: inline-block;
  vertical-align:top;
  padding: 5px;
  border: 2px solid green;
}
.blue {
  width: 35%;
  display: block;
  padding: 5px;
  border: 2px solid blue;
  margin-top: -10px;
  text-align:center;
  z-index:1;
  position:relative;
}
<div class="red">
  <div class="green">David</div>
  <div class="green">Jackson</div>
</div>
<div class="blue">Who are David and Jackson?</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

Switching off toggle does not switch back to primary theme when clicking again for the second time

When I click the button for the first time, it changes the theme. However, when I click it a second time, nothing happens; it seems like it's stuck on the second theme forever. It should change themes with every click. Can someone please help me with ...

Selenium is unable to activate the button element by clicking

I have been using Selenium with Python and attempting to click a button, but it simply isn't working. Here is the relevant HTML: <div class="nmMym"> <button class="sqdOP yWX7d _8A5w5 " type="button"> ...

navigation bar directing to the wrong destination

My landing page has attached pages like landing/gothere and /another. The issue arises when I try to navigate from /another to landing/gothere. I fetch landing information using an API, but there's a delay when I click on the navbar link. The page loa ...

Using a for loop, how can the property value be set in another object?

One challenge that I am facing is setting object property values. The object in question looks like this: const newPlan = { name: '', description: '', number: '', monday: { breakfast: '', ...

What is the best way to add the SO-style <kbd> effect to my presentation made with HTML?

I'm currently utilizing reveal.js to construct an HTML-based presentation. My goal is to incorporate keyboard symbols like the ones found on SO and Github using the <kbd>SPACE</kbd> syntax, resulting in this glyph: SPACE. Despite attempti ...

"To ensure consistent alignment, position list items with varying heights at the bottom of the

Is there a way to align a series of li items to the bottom of their parent, the ul, without using any unconventional methods? The challenge lies in the fact that these li items need to have a float: left property and vary in height and width. Below is my ...

Content from PHP's unlink() function continues to persistently display

I run an Angular front end along with a PHP back end for my website. I utilize PHP's unlink function to remove specific images from Angular's assets folder: $fileToDelete = "../src/assets/images/".$name; unlink($fileToDelete) or die("Error delet ...

I would like to know the method for inserting an HTML element in between the opening and closing tags of another HTML element using

Recently, I came across a coding challenge involving a textbox. <input type="text></input> The task was to insert a new span element between the input tags using jQuery, as shown below: <input type="text><span>New span element< ...

the stacking context of elements with z-index and relative positioning

I am currently working on a modal that is toggled using JavaScript. Within the modal, I am dynamically adding an image using JavaScript. Additionally, there will be a div element overlaying the image to simulate cropping (extract coordinates from the imag ...

Using percentages for sizing and margins in CSS

Looking to create a visually appealing page that always fills the entire screen? Check out this code snippet that does just that: #posts{ width:100%; height:auto; background:#666; } .entry{ float:left; margin-left: 4%; margin-top:10 ...

Python script using Selenium to only print elements in an HTML page that have a specific class value and print only certain elements based on

I am currently developing a script to automatically test all the free proxies available on The website contains a comprehensive list of all available proxies, and I have successfully made my script extract and display them. However, I only want to display ...

Is there an issue with this html code?

Can you help me troubleshoot this HTML code snippet I have? <html> <head> <script type="text/javascript" language="JavaScript1.3" src="js/euDock.2.0.js"></script> <script type="text/javascript" language="JavaScript1.3" ...

"There seems to be an issue with the KeyListener function in

The error I'm encountering is: index.html:12 Uncaught TypeError: Cannot read property 'addEventListener' of null I'm unsure about what went wrong. The intention behind the code was to store the result of the selected radio button into a ...

Ways to dynamically manipulate HTML elements in Angular 5

Recently, I've been attempting to programmatically transform an HTML element. Strangely, when I update the transform value in the console tab, it changes successfully, but for some reason it doesn't reflect in the element tab of the browser. onD ...

The password-protected HTML blog remains hidden until the correct entry is entered into the password box, causing

After successfully creating a password redirect page where entering the correct password redirects you to another URL (psswrdtest.tumblr.com with the password as correctpsswrd, redirecting to google.com*), I attempted to improve it by making the password p ...

What could be causing my HTML button to malfunction when attempting to navigate to a different section of the webpage?

Just starting out and developing my website. My hosting provider is IPage, but I'm running into an issue. When I click on a button to switch to another section of the site, it's not working as expected. Here's the code snippet: <button on ...

What is the best way to target and style anchor links in CSS that are not empty?

Hi there! I am on the hunt for a CSS selector that will target all anchor links that are not empty and contain content. I currently have a selector to identify all links with an anchor attribute, like in this example: a[href*=\#] { background-co ...

Issues with executing google.script.run in Google Web App

I am encountering an issue with the function I have defined in my .gs file: function doGet(e) { return HtmlService.createHtmlOutputFromFile('index') } function goToMaster() { const ss = SpreadsheetApp.getActiveSpreadsheet() const sheet = s ...

Utilize CSS block display for ::before and ::after pseudo elements within a div container

I have a CSS file that contains the following code snippet: .loader { width: 250px; height: 50px; line-height: 50px; text-align: center; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-fam ...

What are some effective ways of using the parent, before, and children selectors?

<table> <tr><td><input type="text" value="123"></td><td><input class="here" type="text"></td></tr> <tr><td><input type="text" value="333"></td><td><input class=" ...