Achieve full-width centered div styling

I am facing a scenario where there are 3 divs, with the first and third div having width specified. I am looking to make the second div fill up the remaining space.

Check out this Fiddle for reference: http://jsfiddle.net/YzUQy/

#div1{
    height:auto;
    text-align:center;
    width:290px;
    left:0;
    float:left;
    border: 1px solid #ccc;
}

#div2{
   float:left;
    overflow: scroll;
    border: 1px solid #ccc;
}

#div3{
    width: 420px;
    border: 1px solid #ccc;
    right:2px;
    padding:10px;
    float:right;
}

Answer №1

To achieve this layout, you can leverage the power of calc():

#div2{
    float:left;
    overflow: scroll;
    border: 1px solid #ccc;
    width: calc(100% - 420px - 20px - 6px - 290px); 
    /* Calculate based on div widths and padding */
}

Check out the demo on JSFiddle

For information on browser support for calc(), visit caniuse

Answer №2

To achieve the desired layout, you can use display:table and display:table-cell. Here is an example implementation: http://jsfiddle.net/YzUQy/4/

Here is the CSS code:

#div1{
    height:auto;
    text-align:center;
    width:290px;
    left:0;
    border: 1px solid #ccc;
}

#div2 {
    overflow: scroll;
    border: 1px solid #ccc;
}

#div3 {
    width: 220px;
    border: 1px solid #ccc;
    right:2px;
    padding:10px;
}

#container {
    display: table;
    width:700px;
}

#container > div {
    display:table-cell;
}

Answer №3

Ensure you have access to this valuable resource for current and future use:

In particular, make sure to bookmark this essential link:

HTML

<div class="content-wrapper">
   <div class="content">
</div>
<div class="left"></div>
<div class="right"></div>

CSS

.content-wrapper {
  float: left;
  width: 100%;
}
.content {
  margin: 0 200px 0 230px; /* set to the widths of right and left columns */
}
.left {
  float: left;
  width: 230px;
  margin-left: -100%;
  background: #C8FC98;
}
.right {
  float: left;
  width: 200px;
  margin-left: -200px;
  background: #FDE95E;
}

Answer №4

To position the center element absolutely, follow these steps:

http://jsfiddle.net/9eGVA/

#container {
    position:relative;
}

#div1{
    height:auto;
    text-align:center;
    width:290px;
    left:0;
    float:left;
    border: 1px solid #ccc;
}

#div2{
   position:absolute;
    overflow: scroll;
    border: 1px solid #333;
    left:292px; /* total width of div1 */
    right:442px; /* total width of div2 */
}

#div3{
    width: 420px;
    border: 1px solid #ccc;
    right:2px;
    padding:10px;
    float:right;
}

Take into account the total width, including padding and borders, for proper positioning.

Answer №5

If you're looking to create a flexible layout, consider using flexbox. Here's an example on how to achieve this: http://jsfiddle.net/fred02138/JDqQu/

<div id="outer">
    <div id="div1">div1</div>
    <div id="div2">div2</div>
    <div id="div3">div3</div>
</div>

Here is the corresponding CSS:

#outer {
    display: -webkit-flex;
   -webkit-flex-direction: row;
    display: flex;
    flex-direction: row;
    width: 100%;    
}

#div1{
    height:auto;
    text-align:center;
    width:290px;
    left:0;
    border: 1px solid #ccc;
}

#div2{
    overflow: scroll;
    border: 1px solid #ccc;
    -webkit-flex: 1 1 auto;
    flex: 1 1 auto;
}

#div3{
    width: 420px;
    border: 1px solid #ccc;
    right:2px;
    padding:10px;
}

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

What steps can I take to prevent my menu items from overlapping in the mobile navigation menu?

I am currently working on creating a mobile menu, but I'm facing an issue where the menu items overlap when hovered over. Instead, I want the menu items to move downwards when hovered upon to prevent the text from overlapping. Below is the HTML code ...

Can a piece of code be created that generates the XPath for a specific element?

I'm interested in finding out if there is a way to automatically generate an XPath by simply selecting an element, eliminating the need for manual updates when changes occur in HTML files on websites. I welcome any suggestions or alternatives that can ...

What is the best way to link the roll button to a specific video URL?

I am currently working on a project that involves assigning specific videos to 6 roll buttons for continuous play. For instance, I want the first roll button to display a yellow dice and the second button to show a blue dice, and so on. As of now, I have ...

Clickable label for checkbox in Android browser is unresponsive

I've been experimenting with creating a responsive CSS menu using the checkbox hack Here's the HTML: <input type="checkbox" id="button" /> <label for="button" onclick>click / touch</label> <div> Change my color! </d ...

The formatting of the datepicker-popup is malfunctioning when the initial value is set within the scope

I have implemented the Angular UI bootstrap date picker popup using a custom directive on Plunker (http://plnkr.co/edit/053VJYm1MpZUiKwFTfrT?p=preview): //Module var userModule = angular.module("userModule",['ui.bootstrap']); //Controller userM ...

Customize your Google Translate experience by choosing your own option values

Is there a way to trigger the same JavaScript calls by clicking on an option value in an HTML select element as with text-based links in the Google Translate widget? Check out this jsfiddle for more information <html> <body> <select i ...

Ways to adjust the div height to match the span height

.headerDesc { width: 100% + 1px; height: 70px; background-color: #4d2765; margin: 0 -8px; } .headerDesc span { padding: 5px; position: absolute; color: white; font-family: 'Arial', sans-serif; text-al ...

Enhance Your Images with Hovering Icons

Is there a way to display an icon checkmark over the main image when a user hovers over it? The icon should appear in the top right corner of the image. <div> <img class="result-image" src="{{$property->photo}}"> <! ...

Eliminate extra spacing in the panel-footer on mobile devices

Trying to eliminate the empty space displayed below the footer in mobile mode, and ensure it stretches to fill the width automatically. I have included some script-related code for reference, but omitted it as it is more logic-based and not design-related. ...

Tips for updating the color of table data when the value exceeds 0

I'm currently working on developing a transaction table that is intended to display debit values in red and credit values in green. However, I would like these colors to only be applied if the value in the table data is greater than 0 via JavaScript. ...

"Mastering the art of text animation: A beginner's guide

If you want to see a cool moving text animation, simply head over to this link: . The text will automatically type out and then clear just like the example below: https://i.sstatic.net/dAZW6.png Ever wondered how to create this kind of text animation? A ...

The table is refusing to align itself in the center

I've been struggling to center the text inside this table despite trying various solutions. Any help would be greatly appreciated. Here's the HTML code I have: <div id="paccount"> <table style="margin: 0 auto;"> <tr&g ...

Customizing Bootstrap styles with a custom CSS file

Currently, in my Django project, I am attempting to customize the base.html file which is utilizing the default bootstrap.min.css by replacing it with a custom.css file like so: base.html: <body id="bootstrap-overrides"> custom.css: #bootstrap-ov ...

How can the Bootstrap Jumbotron element be adjusted to perfectly fill the width of the screen?

Currently, I'm utilizing a jumbotron element on my website and aiming to make it fluid in order to adapt and fit the entire jumbotron to the screen width. However, I seem to be facing difficulty when attempting to increase its width. Below is the cod ...

Communicate through PHP and JavaScript chat to display HTML content in the chat window

I have been attempting to display HTML output in the chat window but instead, it is showing the HTML code. Here are the two files involved in the chat system: chat.js `function chatHeartbeat(){ var itemsfound = 0; if (windowFocus == false) { var ...

Ways to retrieve the text linked to a checkbox

Is there a way to access the text associated with a checkbox using its attribute? For example, in the code below, I want to alert the user with "Selected -- TextXYZ" when the checkbox is clicked. <form id="idForm" class="classForm"> <input type ...

How can JSON data objects be transmitted to the HTML side?

I created a JSON object using Node JS functions, and now I'm looking to transfer this data to the HTML side in order to generate a table with it. However, I'm encountering difficulties sending the JSON object over because of my limited experience ...

I have organized two <h4>, <img>, and <p> tags into individual <div> elements. To prevent overlap, I floated the image and used <br> tags to create separation between the <div> elements. However, they still overlap despite these efforts

Arranging two h4, img, and p tags within separate <div> elements, I attempted to use float for the image and tag for separation. However, they are still overlapping. https://i.sstatic.net/SUxli.png Here is the image displaying the issue. I' ...

What is the best way to synchronize the scale of images with the tempo of a song?

I just started learning CSS, JS, and HTML and I have a question about scaling an image based on a song. Despite searching through YouTube and various forums, I still haven't found a solution. Any help would be greatly appreciated :) Here is the HTML ...

PHP - Dynamically populating form questions based on user input

Currently, I am in the process of developing a fuel calculator form that utilizes PHP and HTML. The form is taking shape nicely, but there is one particular aspect that has me stuck. I want to incorporate a fuel perks feature that requires user input. Spe ...