There is a single div sandwiched between two others, with each of the surrounding divs reaching the edge of the browser

Require a small space between the middle div and the two on each side. The middle bar should have a fixed width.

Here is my attempt so far, but it seems messy:

<!DOCTYPE html>
<html>
<head>
<style>

#container {
    width: auto;
    height: 50px;
    margin-left: auto;
    margin-right: auto;
    margin-top: 50px;
}
#mainBar {
    position: relative;
    z-index: -1;
    width: 406px;
    height: 50px;
    margin-left: auto;
    margin-right: auto;
    background-color: white;
}
#mainBarInner {
    position: relative;
    z-index: 1;
    width: 400px;
    height: 50px;
    margin-left: auto;
    margin-right: auto;
    background-color: red;
}
#leftBar {
    position: absolute;
    z-index: -2;
    width: 40%;
    height: 50px;
    left: 0px;
    background-color: red;
}
#rightBar {
    position: absolute;
    z-index: -2;
    width: 40%;
    height: 50px;
    top: 50px;
    right: 0px;
    background-color: red;
}

</style>
</head>
<body>

<div id="container">
<div id="leftBar"></div>
<div id="mainBar"><div id="mainBarInner"></div></div>
<div id="rightBar"></div>
</div>

</body>
</html>

Is there a better way to achieve this without aligning the divs on both sides and under the middle one?

Demo file: FIDDLE DEMO

Answer №1

If you're looking for a responsive design, one way to achieve this is by using percentages for the width of elements. Adding in a Reset.css file can help ensure consistent styling, and adjusting margins and padding manually can provide additional customization. Feel free to adjust these percentages to suit your preferences. Check out this JSFiddle demo for reference.

<head>
<style type="text/css">

#container {
width: 100%;
height: 50px;
margin-top: 50px;
}
#mainBar {
width: 39.5%;
height: 50px;
display: inline-block;
background-color: red;
}
#leftBar, #rightBar {
display: inline-block;
width: 29.5%;
height: 50px;
background-color: red;
}

</style>
</head>
<body>

<div id="container">
<div id="leftBar"></div>
<div id="mainBar"></div>
<div id="rightBar"></div>
</div>

</body>

Consider how you want the middle and side bars to behave at different screen sizes and heights. Should they disappear, slide above or below, or adjust by percentage? Do you want the middle bar to have a fixed width or be a percentage on smaller screens while having a max-width on larger screens? Providing more specific details will allow us to offer targeted solutions.

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

Using AJAX to submit a form and retrieve response data in Javascript

After successfully getting everything to post correctly, I encountered a problem with this script. It keeps loading the content into a new page. Could it be related to the way my php file returns it using "echo(json_encode($return_receipt));"? <s ...

Resolving formatting challenges in R markdown with Blastula

Dealing with formatting problems while trying to include an R markdown file in an email body. As a CSS novice, I'm struggling to find a solution and it's becoming quite frustrating. The table has the following CSS styling: <!DOCTYPE html ...

JavaScript detecting improper XHTML syntax

Is there an effective method to detect malformed XHTML within a string using JavaScript? Given that my page allows user-generated XHTML (from trusted users) to be inserted into the DOM, I aim to identify unclosed or overly closed tags. If found, I intend ...

AngularJS Gallery Showcase

Trying to showcase a collection of videos in a modal using angular.js, I decided to implement the solution from . However, I encountered difficulty in integrating my scope variables into the "html5gallery" class. <div class="html5gallery" data-skin="ho ...

Retrieve the chosen option from a Select Dropdown and display it in a modal before carrying out the form submission

I need help figuring out how to send a warning message when a "delete" button is clicked on my webpage. I want the message to appear in a modal and display the selected value from a dropdown menu. Here is the code I have so far: <form action="elim ...

switch from material ui lists on/off

Trying to learn React through coding, I've encountered an issue with displaying the 'StarBorder' icon next to folders when clicked. Currently, clicking on any folder displays the 'StarBorder' icon for all folders. Any tips on how t ...

Learn how to smoothly scroll to the top of a div by clicking on a team name

CSS Code .hover-div { position:absolute; margin-top:-150px; visibility:hidden; transition:all 0.5s linear 0s; } .team_hover:hover + .hover-div { margin-top:0px; visibility:visible; } .hover-div:hover { margin-top:0px; visi ...

How can I ensure that the div remains fixed on scroll and appears directly below the navigation bar, rather than at the top of the page?

There is a div (#callback) that loads asynchronously via ajax upon submit, for updating or deleting an item. It appears as the green message box at the top of the page. Click here to see image and this is how it looks when "scrolling": Check out the scrol ...

Dealing with JSON data in the format of `(Object object)` requires a specific approach

I originally encountered object object when attempting to display JSON API data in HTML. I then used keyvalue in *ngFor which allowed me to display the object, but I am wondering how I can access and display the entire JSON data? Here are the relevant cod ...

Unable to reset iframe style height in IE8 using JavaScript

I am encountering an issue with resetting the height of an iframe using JavaScript. Here is the code snippet I am working with: var urlpxExt = document.getElementById('urlPx'); urlpxExt.style.height = "200px"; While this approach works well in m ...

Modify the body tag upon loading the page for Bootstrap 2.0

I'm currently utilizing bootstrap on my website, and I have implemented scrollspy with bootstrap. However, I am facing an issue where adding scrollspy to other pages causes my links to behave unexpectedly. Is there a way to simply incorporate the foll ...

Is there a way to track and detect alterations to an element using JavaScript or jQuery

Can I detect the addition of a specific CSS class to an element without having to create a new event? ...

Having trouble with the $push operator in Mongo DB?

I am facing an issue while attempting to store data in an array within an object in a Mongo model by utilizing the req.params method. Unfortunately, I encounter the following error: MongoError: cannot use the part (friends of friends.username) to traverse ...

Exploring the functionalities of jQuery and Local Storage: Understanding the Selector's various states

I am currently developing a new feature for font customization on a website. The drop-down menu offers several font options to choose from. When a user clicks on one of these options, the following code is executed: jQuery(function($) { if (localStorage.g ...

What is preventing my boolean from being altered?

Creating a basic calculator that can handle single-digit arithmetic. The current code is incomplete and redundant, so please avoid commenting on that. <!doctype html> <html> <head> <title>JavaScript Learning Zone</title> ...

Stop the css animation from playing

I created a slideshow using HTML and CSS, and I'm looking to implement a feature where the slideshow pauses when my mouse hovers over it. I attempted to achieve this by using -webkit-animation-play-state: paused;, but unfortunately, it only pauses one ...

Change the color of this element and input field background

Having trouble with setting the background color of an input field to red in this code: $(document).ready(function(){ $(".abc").focus(function(){ $(this).attr('background', 'red'); $("label").text('Insert tex ...

The select2 jQuery plugin is malfunctioning following an AJAX request to update the HTML content

I have implemented an AJAX function to change the content of a HTML div. Within this changed content, there is a select box with the 'select2' jQuery class. However, after the AJAX load, the select2 jQuery plugin does not work properly. Here is ...

Text Fade-in Animation with CSS 3

Could someone assist me in creating an animation similar to a marquee using CSS "animation" and "keyframes"? I attempted to move text from top to bottom, but encountered an issue where the text would only restart its movement from the top once it reached ...

Utilize pandas.read_html to extract image alt text from web pages

Can you extract the text from the img alt attribute using pandas.read_html? The webpage I am trying to scrape has replaced some text with images, and the original text is now stored in the alt attribute of those images. For example: <td> <div>. ...