I aim to arrange three div elements in a single row with varying widths - placing one on the left, another in the center, and the last

I am trying to have three div elements in one row with different widths - one on the left, one in the center and one on the right.

  • The left div should be 160px
  • The center div should be 640px
  • The right div should be 160px

My goal is for them to appear separate when viewed on a wide screen - one on the left, one in the center and one on the right.

However, when the user resizes the browser window or views the page on a smaller resolution, I want the divs to come closer together without collapsing (they should take up at least 960px altogether - 160 + 640 + 160).

So far, I have managed to achieve this with the help of StackOverflow and Google:

Html :

<div id="main">
  <div id="leftDiv">left</div>
  <div id="centerDiv">center</div>
  <div id="rightDiv">right</div>
</div>

CSS:

#main {
    overflow:hidden;
}

#main div {
    width:33%;
    float:left;
}

#leftDiv
{
    width:160px;
}
}

#centerDiv {
    text-align:center;
    width:640px;
}

#rightDiv {
    text-align:right;
    width:160px;
}

If you can provide any further assistance, it would be greatly appreciated. Thank you in advance.

Answer №1

After following this method, the elements need to be reorganized as shown below:

<div id="main">
  <div id="leftDiv">left</div>
  <div id="rightDiv">right</div>
  <div id="centerDiv">center</div>
</div>

To ensure that the centerDiv remains at the center of the layout, you can utilize the auto value for the left/right margin.

Additionally, setting a minimum width for the container element (#main) is recommended to prevent layout collapse.

#main {
  min-width: 960px; /* minimum width: 160px + 640px + 160px */
}

#leftDiv  { float: left;  width:160px; }
#rightDiv { float: right; width:160px; }

#centerDiv {
  width:640px;
  margin: 0 auto;
}

VIEW WORKING DEMO

Answer №2

When dealing with this, my recommendation would be to utilize the power of Twitter Bootstrap. Simply follow these steps:
css:

#main {
   min-width:960px;
}

/* It’s also advisable to have your columns scale accordingly unless there is a maximum width in place */

#leftDiv, #rightDiv {
   width: 160px;
}

#centerDiv {
   width: 640px; 
}


html:

<div id='main' class='row'>
     <div class='col-md-2' id='leftDiv'></div>
     <div class='col-md-8' id='centerDiv'></div>
     <div class='col-md-2' id='rightDiv'></div>
</div>

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

Utilizing AngularJS, implement ng-form and ng-repeat to seamlessly handle input validation and submission actions

Angular 1.6.2 I am experimenting with iterating over elements of a collection inputted by the user, checking their validity, and enabling the submit action if validation passes. I have tried using ng-form for this purpose and have come up with two differ ...

The system considers the resource as a script, yet it was transmitted with the MIME type of text

I am encountering an issue when trying to integrate AngularJS/JavaScript into my HTML document. Each time I try to load my index.html file, the following error message appears: Resource interpreted as Script but transferred with MIME type text/html: "http ...

Material selection through span in three.js is not functional

Initially, the span element was functioning properly for me. However, after setting up materialsLib and initMaterialSelectionMenus based on the documentation and examples, the span stopped working and now the model is not visible. Any assistance would be g ...

When the open button is clicked, the Div will toggle between open and closed states

Recently, some of my questions have not been well-received, which makes me feel a bit disheartened. It's important to remember to be kind when providing feedback. I've noticed that some people downvote without offering constructive criticism, whi ...

Basic inquiries concerning Vue.js and JavaScript

Hey there, I recently developed a small app to practice my Vue skills. However, there are a few features that I would like to implement but I'm not sure how to do it just yet. <div class="container" id="app"> <div class="row"> <d ...

The Material UI Grid item shifted upwards as a result of the image loading

While working on a project using the Material UI grid system with create-react-app, I encountered an issue with two adjacent grid items. Initially, they display as intended: However, upon loading the page, there is a brief moment where the text on the rig ...

Guide to bringing API information into a Material UI Card

I've been working on a Recipe app that leverages data from the edamame API. I successfully imported Material UI cards into my .js file to pull data from the API and stored it in a const named recipes. However, I'm facing difficulties in getting t ...

Unforeseen truncation issues within a table causing ellipsis to appear unexpectedly

I'm facing an issue with truncating text inside a table, and it's not working as expected. Can someone help me understand what I might be doing wrong? Edit: I need to ensure that the table width remains max-width: 100%; <script src="http ...

How can you efficiently update another ng-model value based on a select input in AngularJS using ng-change?

<select data-ng-init="selectedItem='previewWidth = 1920; previewHeight = 1080'" data-ng-model="selectedItem" data-ng-change="GetNewData(); {{selectedItem}}"> <option value="previewWidth = 1920; previe ...

Troubles with jQuery mobile functionality when utilizing hyperlink urls within a table

Currently, I am utilizing jQuery mobile to populate a table using ajax. One of the fields in the table is a hyperlink (href) that has a class assigned to it to trigger an alert on the screen. However, the alert does not appear when clicked: I have attempt ...

I successfully managed to ensure that the splash screen is only displayed upon the initial page load. However, I am now encountering an issue where it fails to load again after I close the page. Is there any possible solution

After successfully implementing a splash screen that only plays once on page load, I encountered an issue. When I close the tab and revisit the page, the splash screen no longer plays. Is there a way to fix this problem? Perhaps by setting a time limit for ...

Unable to concentrate on HTML input elements

TL;DR just click on this link I'm having a problem with my input elements - they seem to be on strike and not working properly. I've simplified the code to focus on the variables, and while I found some solutions that work, I believe there' ...

The initial menu option stands out due to its unique appearance, created using the :first-child

I am currently designing a website menu and I would like the .current-menu-item - current item, to have a different appearance. However, I want the first item in this menu to have rounded corners (top left, bottom left). How can I achieve this using the :f ...

How do I modify the sidebar width with CSS code?

I am trying to adjust the width of the right sidebar on my website. The current layout leaves too much empty space on the right side and makes it appear crowded next to the main content and images in the center. Despite exploring various solutions provide ...

Utilize various addMethods on a field in the event a radio button is chosen through the jQuery validator plugin

When a specific radio button value is selected, I need to apply different methods to a single field. If "true" is selected, method X should be applied. If "false" is selected, method Y should be used on the same field. I attempted to achieve this with a ...

The issue with material-ui 0.15.2 is that it applies extra vendor-prefixed styles on the server side but not on the client side, resulting in warnings in React 15

I have set up my project with the following configurations: react is at version 15.2.1 material-ui is at version 0.15.2 I am using express and universal-router for server-side rendering Every time I include a material-ui component, I encounter this erro ...

Combining the Powers of Magento and Wordpress: Can I manually edit a page or the CSS?

I am currently faced with the challenge of editing a webpage that was not originally created by me. I have been tasked with understanding someone else's code in order to make alterations. Specifically, I am attempting to change the font color on the h ...

The Ajax request is successful on one server but fails on another

I have implemented an ajax call to the UPS address validation webservice. The call is successful when made from my application using this domain: http://serverone.org/addrvalidator. However, if I try using a different domain or an IP address instead of th ...

Monitoring user clicks using JavaScript code implemented

Currently, I am utilizing a JavaScript file to load on certain pages in order to track various events. Within this JavaScript file, there is a collection of selectors that have listeners attached to them. When these selectors are clicked, the tracking AP ...

Looking to transform a list into JSON format using JavaScript?

I have a collection that looks like this: <ol class="BasketballPlayers"> <li id="1">Player: LeBron, TotalPoints: 28753, MVP: true</li> <li id="2">Player: Steph, TotalPoints: 17670, MVP: true< ...