Creating a design where the divs on the sides utilize all the available space using only CSS and HTML

I am trying to create a layout with three <div> elements - one centered at 960px width, and the other two on each side.

My goal is to have the side divs take up all available space except for the 960px occupied by the centered div. I managed to achieve this for the right div using overflow: hidden, but I'm facing difficulty with the left div.

<div id="left">leftt</div>
<div id="center">center</div>
<div id="right">right</div>

<style>
#center{
    width: 960px;
    float: left;
    height: 300px;
    background-color: #bbb;
}
#left{
    float: left;
    height: 300px;
    background-color: #ccc;
    width: ?;
}
#right{
    float: left;
    height: 300px;
    background-color: #ddd;
    width: ?;
}

<style>

This image illustrates what I am aiming for: https://i.stack.imgur.com/CgdLo.png

Answer №1

Utilize css tables for layout design (Even supported by IE8 - caniuse)

FIDDLE

1) Apply display:table to the container div

2) Use display:table-cell on child divs

3) Specify min-width:960px for the center div and width:50% for side divs

CSS

.container
{
    display: table;
    width: 100%;
}
#center{
    min-width: 960px;
    height: 300px;
    background-color: #bbb;
    display: table-cell;
}
#left{

    height: 300px;
    background-color: #ccc;
    display: table-cell;
    width: 50%;
}
#right{

    height: 300px;
    background-color: #ddd;
    display: table-cell;
    width: 50%;
}

Answer №2

When it comes to modern browsers like IE9+, Firefox, and Chrome, the HTML code would look something like this:

<body>
  <div id="left">leftt</div>
  <div id="center">center</div>
  <div id="right">right</div>
</body>

For styling in CSS, you can use the following:

#center{
  width: 960px;
  float: left;
  height: 300px;
  background-color: #bbb;
}
#left{
  float: left;
  height: 300px;
  background-color: #ccc;
  width: 100%;
  width: calc(50% - 480px);
}
#right{
  float: left;
  height: 300px;
  background-color: #ddd;
  width: 100%;
  width: calc(50% - 480px);
}

If you need support for IE8, you can achieve similar results using JQuery:

<script>
  $(function () {
    var documentWidth = $("body").width();
    $("#left").width((documentWidth - 960) / 2);
    $("#right").width((documentWidth - 960) / 2);
  });
</script>

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

Issue with implementing styles on Navbar React component

I'm currently learning Next.js, specifically working with version 10.0.3. In my project, I am using react-bootstrap version 1.4.0 and classnames version 2.2.6. Within my project, I have a component called Navbar that I am trying to style in a certain ...

Tips for arranging div elements in a grid-like matrix layout

I am facing a challenge in arranging multiple rectangular divs into a grid structure with 10 columns and 10 rows. The CSS styles for the top, bottom, left, or right positions must be in percentages to accommodate zoom in and out functionality without overl ...

Can JavaScript be used to upload a file directly to memory for processing before transferring it to the server?

I'm exploring the idea of using a JavaScript encryption library (not Java) to encrypt a file before sending it to the server. Is it feasible to perform this process on the client-side and then upload the encrypted file using JavaScript, storing it in ...

Can you explain the distinction between using .hover and :hover?

Recently, I came across some CSS code that was written by someone else and it contained the following: li.hover, li:hover { } This made me curious - is there a distinction between .hover and :hover in CSS? Could it be possible that certain browsers inte ...

Tips for making nested sliding divs within a parent sliding div

Is it possible to slide a float type div inside another div like this example, but with a white box containing "apple" text sliding inside the black div it's in? I have attempted to recreate the effect using this example. Here is my current JavaScript ...

PHP is not processing the HTML form I submitted

HTML: <form method="post" action="create.php"> Job #: <input type="text" name="jobnum"> <br> Program: <input type="text" name="program"><br /> Ship Date: <input type="text" name="shipdate"><br /> Description: < ...

Click on the header to activate the accordion link with a trigger

I am having an issue with a trigger. My goal is to make the accordions collapse not only by clicking on the link, but also by clicking on the entire header panel. Below is my header's code: <div class="accordion-head" role="tab"> <div cl ...

Is there a way in AngularJS to set all radio buttons to false when one is chosen as true?

When I retrieve true/false string values from the database, I am unable to alter the data. The example I provided is simply a representation of the string values true or false that I receive from the database. My goal is to have a single radio button disp ...

What could be causing my website to function flawlessly in Firefox but not in Chrome or Internet Explorer?

Hey everyone, I'm feeling lost and have been comparing files using DiffNow. I don't have any code to offer because I'm unsure of where to even begin. The website is coded in php/html/mysql/jquery. Here are my main concerns: -Animations are ...

Error: The function execute_script() is expecting between one and two arguments, however three arguments were provided

While attempting to utilize the Selenium method execute_script() in an automated UI test script, I encountered a type error. The error message indicates that there are too many arguments being passed. TypeError: execute_script() takes from 1 to 2 positiona ...

How to achieve divs with see-through text using CSS?

How can I create a CSS (non-HTML5) based website with a filled div that has a cutout revealing an image underneath? There are various overlays and images involved, making the use of static images inconvenient. Additionally, I anticipate needing to scale th ...

The website functions properly with Live Server, but encounters issues when launched from the Files directory

After testing multiple browsers, the site consistently works fine with Live Server. However, when accessed through Firefox or Chrome, my style.css fails to load completely. On Edge, an error message indicates that the file is missing. Below are snippets of ...

Can someone explain why my button icons are not aligning to the center properly?

I have a small issue with the icons I pulled from react-icons - they appear slightly raised within the buttons. How can I position them in the middle? That's my only query, but I can't post it alone due to mostly having code in my post. What an ...

Regex: Identifying all URLs except for those with ".js" or ".css" included

I am currently working on a project where I need to change all the links in an HTML page so that they are not clickable. However, I am having trouble finding the right Regex pattern for the following condition: href="Any_URL" except for those that contain ...

Styling tip: Distance the tooltip from its parent element when scrolling through a list

I have a table inside li tag, and I want to display the description on :hover inside td. However, when scrolling through the list, the description content starts to appear with a distance from its parent. Despite trying various combinations of position se ...

Trouble arises when trying to link IE7 with jQuery

I am currently testing a website with jQuery. There is a plugin that, when hovered over, slides down to reveal another banner, subsequently sliding the banner below it down as well. I have added a link on the bottom banner using z-index, and although it wo ...

Issue with menu height persisting while resizing screen

For my responsive design project, I am implementing a menu that switches to a menu button when the width is less than or equal to 768px. Currently, I am using jQuery to dynamically adjust the height of the menu when the menu button is clicked. The jQuery ...

Tips for adjusting the positioning of a div element in a responsive design

I am currently working on my website and facing a layout issue. In desktop mode, there is a sidebar, but in mobile view, the sidebar goes down under the content displayed on the left side. I would like the sidebar to appear at the top in mobile view, fol ...

What is the best way to include a targeted href link within HTML code?

During the process of creating a website using bootstrap 4, I included the following snippet of HTML code: <form action="https://www.youtube.com/watch?v=pQN-pnXPaVg" target="_blank"> <button>Start</button> </form> Despite li ...

Extension for Chrome

My goal is to develop a Chrome extension that, when a specific button in the extension is clicked, will highlight the current tab. However, I'm encountering some challenges. Currently, I have document.getElementById("button").addEventListen ...