A div in the center, flanked by two divs that stretch all the way to the edges of the

Looking for a more efficient way to create a centered div with extended divs on either side reaching the page margin.

Currently, I am using two divs with 49% width each, and then placing a centered div on top of them to achieve the desired effect.

However, this method creates an awkward space below.

HTML:    
<div id="green"></div>
<div id="red"></div>
<div id="blue"></div>

CSS:
#green {
height:25em;
width:49.99%;
background-color:#132a10;
display:inline-block;
margin:0;
float:left;
opacity:.47;
}
#red {
height:25em;
width:49%;
background-color:#400120;
display:inline-block;
margin:0;
opacity:.4;
}
#blue {
background-color:#436a97;
position:relative;
bottom:26em;
width:22em;
height:27em;
margin-right:auto;
margin-left:auto;
z-index:1;
opacity:;
}

http://jsfiddle.net/JFA2z/5/

Answer №1

The spacing issue arises due to the use of relative positioning on the blue div. When an element is positioned relatively, its original position still occupies space and the element is moved relative to that position. To resolve this, you can utilize absolute positioning while adhering to these guidelines:

#blue {
    background-color:#436a97;
    position:absolute;
    width:6em;
    height:7em;
    margin:auto;
    top:0;
    left:0;
    right:0;
}

View the example on jsFiddle

Answer №2

Ensure uniform height for all divs and adjust the top margin of the blue div to -16px

<html>
<head>
<style>
#green
{
 height:7em;
 width:49.99%;
 background-color:#132a10;
 display:inline-block;
 margin:0;
 float:left;
 opacity:.47;
}
#red
{
 height:7em;
 width:49%;
 background-color:#400120;
 display:inline-block;
 margin:0;
 opacity:.4;
}
#blue
{
 background-color:#436a97;
 position:relative;
 bottom:6em;
 width:6em;
 height:7em;  /*edit*/
 margin-right:auto;
 margin-left:auto;
 margin-top:-16px;  /*edit*/
 z-index:1;
 opacity:;
}
</style>
</head>
<body>
  <div id="green"></div>
  <div id="red"></div>
  <div id="blue"></div>
</body>
</html>

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

A guide on detecting overflow in a React functional component

I am in search of a way to determine if a div contains overflowing text and display a "show more" link if it does. While researching, I came across an insightful Stack Overflow answer on checking for overflow in a div. The answer suggests implementing a fu ...

ways to modify hash URLs using JavaScript

I am looking to add separate hash links for my Facebook and Instagram social media pages. I also want to change the text color to blue for Facebook and yellow for Instagram. How can I achieve this using JavaScript? let socialMediaData = [{ s_media: &apo ...

What is the best way to implement a day timer feature using JavaScript?

I am looking for a timer that can automatically change the rows in an HTML table every day. For example, if it is Day 11, 12, or 25 and the month is February at 8 AM, the rows should display "Hello!". function time() { var xdate = new Date(); var ...

How can I reference a local file in HTML using Node.js?

I am currently setting up a file to run app.js var express = require('express') var app = express(); var router = express.Router(); var path = __dirname + '/views/'; router.get("/",function(req,res){ res.sendFile(path + "index.html" ...

Is there a way for me to align my div with my header on the same line?

HTML: <h2> RAN shares false news story about Hershey's Oil Commitment</h2> <div class="date"> <div class="number">27</div> <div class="month">Oct</div> </div> <p>The Rainforest Action Netwo ...

MUI: Issue with pseudo element appearing cropped outside of Paper container

I am facing an issue where a red arrow pseudo element '::before' is partially cut off outside its container '.MuiPaper-root'. I need the arrow to remain visible, any suggestions on how to fix this? Here is the relevant code snippet and ...

The checkboxes do not appear to be updating visually when the "checked" property is toggled programmatically

Check out this example on JSFiddle for a demonstration. Clicking on a checkbox should cause all the neighboring checkboxes to toggle on or off. Even though the "checked" property is toggling, there doesn't seem to be any visual change. n.prop("chec ...

Column Alignment Problem in Bootstrap

I'm currently working on a blog template project that resembles the design of this particular page However, I've encountered an issue with aligning the grids in a way that meets my requirements. To showcase my problem, I have shared my version o ...

How to Add Catchy Titles to Your Menu Items on Silverstripe CMS?

Hey there! I'm currently working with Silverstripe CMS using the "Simple" template. I'm interested in finding out how to add subtitles to my menu items. Here's the current structure of my navigation template: <nav class="primary"> &l ...

Collapse input horizontally using the display property set to flex

At the top of a mobile page, I have implemented two search boxes: https://i.sstatic.net/gCfOG.png When a user clicks inside one of the boxes, the desired behavior is as follows: The selected box expands The other box collapses horizontally A close ico ...

Artwork - Circular design disappears without warning

While working on a clock project purely for enjoyment, I noticed that the minute arc disappears from the canvas as soon as a new minute begins. Any idea why this is happening? Check out the clock in action: https://jsfiddle.net/y0bson6f/ HTML <canvas ...

What causes the odd gaps at the bottom of boxes in a Pure CSS Masonry layout?

Issue with implementing Pure CSS Masonry layout. Using position: relative for the boxes and position: absolute for the content inside each box is causing gaps below each box. Struggling to find a solution, view the code on this codepen: http://codepen.io/k ...

Display properties of a JSON object using VUEJS

How can I efficiently read JSON data in my VueJS code? Here is the code snippet: <template> {{data}} </template> <script> import axios from 'axios'; export default { data() { return { data: null }; }, mou ...

modify data when parsing a file / utilizing loops in shell scripts

I have a log file that has the following format, <tr><td>AAA-application-01 <br> Quality_gate_failed_reason:xxxxxxx </td></tr> <tr><td>AAA-application-02 <br> Quality_gate_failed_reason:xxxxxxx </td>&l ...

What is the best way to track down the source of a CSS rule?

A website built on .asp was passed down to me, and I was tasked with reorganizing forms in tables to the sidebar. Most pages were successfully updated, except for one stubborn page that seems to be ignoring my CSS changes and using values from an unknown s ...

A guide to mastering Controllers in AngularJS

Trying to set up a basic page with a controller but encountering difficulties. The HTML code is straightforward, with an Angular script included, but the functionality isn't working as expected. The HTML snippet: <!DOCTYPE html> <html ng-ap ...

What could be causing my jQuery to only toggle the first arrow in my HTML?

I am facing an issue with a series of divs generated from data, each containing an arrow that should toggle an expandable section. Strangely, only the first div is functioning properly: toggling the arrow icon and revealing the content upon click. When th ...

Tips on creating a keypress function for a div element with the contenteditable attribute

Hey there, I have managed to create a textarea within a div by using -webkit-appearance. However, I am currently struggling to add an event to this div on key press. I have been trying my best to figure it out but seem to be missing something. If you coul ...

JavaScript-driven bootstrap 4 collapsing functionality

I have created a Bootstrap 4 page with collapsible accordion tables. Each table has a fa-plus-circle icon that changes to a fa-minus-circle icon when clicked. However, I am unable to change it back to the plus-circle icon when clicked again. Can someone ...

Database information displayed in a text area with spaces before each entry

I have implemented a text area for members to update their about me profile. However, I am facing an issue where extra spaces appear at the beginning before the first word when they go to edit it. <textarea rows="15" cols="25" name="aboutme" id="aboutm ...