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

Identify when a click occurs outside specific elements

I've been searching for solutions to address this issue, but so far nothing has worked. Here is the JavaScript code I am using: var specifiedElement = document.getElementById('a'); document.addEventListener('click', function(eve ...

Identify compatibility with the background-attachment property set to fixed

I've been searching for an updated answer on this topic, but I couldn't find one so I wanted to confirm. My goal is to achieve a parallax effect using background-attachment: fixed, however I've noticed that it doesn't work on mobile ph ...

The CSS 'top' attribute is without impact

As I begin my journey with CSS, I am eager to grasp some of its behaviors. In the CSS file, the following code is defined: #spa { position : absolute; top : 8px; left : 8px; bottom : 8px; right : 8px; min-height : 500px; min-width : 500px ...

Ways to retrieve the position of hyperlinks within a div element

<div class="nav"> <a href="#item1">item1</a> <a href="#item2">item2</a> <a href="#item3">item3</a> </div> Looking at the HTML structure provided above, I am seeking to determine the index of the hyp ...

Aligning the .left position to make the border of a <div> synchronized

When working on my Jquery program, I encountered an issue with borders not aligning properly within the container. Specifically, in the execution (where the container is represented by a white box), the left border extends beyond the position().left proper ...

Modifying the content within a DIV element

I want to make changes to my DIV. <div id="main"> <div id="one"> <div class="red"> ... </div> <img class="avatar" src="img/avatar1.jpg"/> <span class="name"> John < ...

Switching the typeface within the content data

Recently, I incorporated this code: <div data-content="Reach" class="main-image"> along with the following CSS styling: .main-image:before { content: attr(data-content); I am now wondering if there is a method to adjust the font size and ...

Discovering content between HTML tags using Python Regular Expressions

Can someone help me with extracting the content between HTML tags shown here: <div class="td-wrap"> <a href="/universities/university-cambridge" class="uni-link">University of Cambridge </a> </div> ...

Can you explain the purpose of using href="#" in web development?

I've noticed that numerous websites use links that have href="#". Can you explain what this means and how it is typically used? ...

Incorporating fresh components and newly defined attributes in Angular

Is there a way for me to click on a new component button, specify a name, description, select type, and add attributes such as default value and type? I need all this information to be saved and for the new button to appear in the drag-and-drop section. ...

Tips for aligning numbers in a monospaced ordered list

Is there a way to align the numbers in a numbered list within a table using monospace font so that they are at the same level as bullets on a bullet list? Simplest example: ol { font-family: monospace; } ul, ol { margin-top: 10px; margin-bottom: ...

Is there a way to position two <div> elements side by side so that they are the same width and height without causing any scrolling?

I am in the process of creating a basic HTML page with two sections, each containing content. However, the last content within the second .right div is extending to the bottom of the page, causing it to become scrollable. I attempted to create another div ...

The Material UI dialog box popped up against an unexpected gray backdrop

Currently, I am using Material UI in conjunction with React to create a dialog that appears when a button is tapped. This button is located within a table, which is displayed over a Paper component. The problem arises when I utilize the dialog with its def ...

Is it possible to verify if a bearer token has expired within an Angular application?

Is there a secure and efficient way to determine when a bearer token has expired in my Angular application? This is the issue I am facing: If I keep the app open in my browser, someone could potentially access sensitive information on my screen since I am ...

Include a parent class within the style tags in your CSS code

Currently, I am facing an issue with a web application that I am developing. On the left side of the page, there is an editable area, and on the right side, there is a live preview. The live preview area contains an HTML file with specific fields that can ...

Theme.breakpoints.down not being acknowledged by MUI breakpoints

The Challenge: Implement a hamburger menu to replace the navMenu on tablet and smaller screens After successfully compiling in VS code terminal, encountering an error in the browser: Error Message: TypeError: Cannot read properties of undefined (reading ...

Table Header Stays Put Without Shrinking or Expanding with Window Adjustment

I have a sticky table header that stays at the top when scrolling down on my web page. To achieve this effect, I followed the solution provided on css-tricks.com/persistent-headers/. However, I encountered an issue where the sticky table header does not ...

Material UI defaults remain unchanged despite any emotional influence

I'm currently experimenting with custom styling on a MaterialUI Typography component using the code snippet below: const StyledTitleTypography = styled(Typography)` color: 'black'; font-weight: 'bold'; `; <StyledTitleTypogr ...

Terminate a browser tab with the click of an HTML button

I'm facing an issue with my HTML button - I need it to close the tab upon clicking. Unfortunately, the common methods seem to no longer work on newer versions of Chrome and Firefox. I've tried using these two solutions but they did not work: & ...

Production environment poses a challenge as Rails 4 struggles to locate fonts

Situation: I am facing an issue with my Rails 4.0.0 application deployed using capistrano, where the asset precompilation does not work properly on my production server. Challenge: Despite successfully adding a font and using it with @font-face locally, t ...