Tips for controlling the placement of divs on a webpage

I need to align 3 divs in a specific way on my webpage. One should be on the left side taking up 25% of the window, one on the right side also taking up 25%, and the last one in the center taking up the remaining space. I'm struggling with how to achieve this layout.

Here is what I've attempted so far :

html:

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

css:

#left {
    border: 2px solid blue;
    position: absolute;
    left: 0px;
    top: 0px;
    bottom: 0px;
    width: 25%;
}

#right {
    border: 2px solid blue;
    position: absolute;
    right: 0px;
    top: 0px;
    bottom: 0px;
    width: 25%;
}

#center {
    border: 2px solid red;
    position: absolute;
    right: 25%;
    top: 0px;
    bottom: 0px;
    width: 50%;
}

http://jsfiddle.net/Elfayer/VLjvK/

Answer №2

Currently, the borders are adding to the width of the div elements.

To fix this issue, add the following CSS code:

* {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

JSFiddle

It is recommended to avoid using absolute positioning as the main layout method. You can explore more flexible solutions on this website.

Learn Layout

Answer №3

Give this a try.. I made some adjustments by removing the absolute positioning.

        #left,#right, #center {
            height: 100%;
            float: left;
        }

        #left {
            background-color: blue;
            width: 25%;
        }

        #right {
            background-color: orange;
            width: 25%;
        }

        #center {
            background-color: pink;
            width: 50%;
        }

Answer №4

The sidebars on the left and right can now have variable widths, while the main content will fill up the remaining space. Each div element has its own padding and margin to adjust as needed.

Check out this example on JSFiddle for more details.

Here's how the code looks:

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

And here's the CSS styling:

div {
    box-sizing: border-box;
}

#left {
    border: 2px solid blue;
    width: 25%;
    float: left;
}

#right {
    border: 2px solid blue;
    width: 25%;
    float: right;
}
#center {
    border: 2px solid red;
}

Answer №5

Is this what you're looking for?

DEMO

I made adjustments by replacing the absolute positioning with relative.

Additionally, I included display:inline-block and tweaked the div percentages to allow for different border widths.

Answer №6

Here is a suggestion for you:

div {
    padding:0px;
    margin:0px;
    float:left;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

#left {
    border: 2px solid blue;
    width: 25%;
}

#right {
    border: 2px solid blue;
    width: 25%;
}
#center {
    border: 2px solid red;
    width: 50%;
}

Answer №7

Check out this demo: http://jsfiddle.net/VLjvK/8/

#left {
    width: 25%;
    float: left;
    background-color: blue;
}
#right {
    width: 25%;
    float: right;
    background-color: blue;
}
#center {
    width: 50%;
    float: left;
    background-color: red;
}

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

Interactive search tool with multiple fields using HTML and JavaScript

I need help developing a search box for structured data, where I want to implement two types of typeahead searches: one for fields and another for values within those fields. The image below illustrates what I am aiming for. https://i.sstatic.net/MRsJ2.png ...

What is the best way to navigate between elements using AJAX?

My experience in Ajax is minimal. I've managed to create two <li> elements with <a> elements inside, each linking to different HTML files. My goal is for clicking on a link to automatically load the corresponding HTML file without refreshi ...

Step-by-step guide on displaying a window containing text when hovering over a CSS class

As a beginner in css, html, and js, I created an html page with a table of athletes. Each row is identified by the css class "athlete-name", like this: <div id="message-hide" style="display: none"> Hello world! </div> <t ...

The z-index of two elements seems to be behaving differently than anticipated

I am encountering an issue with the code provided below, which is a simplified version of a larger problem. Can you explain why, if: .div_A {z-index: 10;} < .div_C {z-index: 20;} the button inside div_C appears behind div_A? I need to understand the ...

Substitute the division

Can I make the old div change its position and move to the side when I add a new div? And can all the old divs be hidden when adding four new divs? This is for my full news website and I want this applied to all four pages. First page: https://i.sstatic. ...

Show a collection of elements containing attributes in JSX

I have a React component where I am displaying data from an array called pkgData: <div className="mainApp"> <div className="pkgsList"> {pkgData.map((p) => <h3 key={p.name}>{p.name}</h3> ...

Switching the class from "untoggled" items to the selected item in React

Recently, I developed a toggle component known as Accordion. Within this component, I am iterating through an array of objects and displaying them in the following format: {object.map((o) => ( <Accordion key={o.id} title={o.question} className="i ...

Unique mechanism for tracking clicks in ASP and C#

Is there a preferred method for showing a click counter on an HTML page when working with a .aspx file that connects to a .cs file pulling data from a SQL server, without relying on an external text file for tracking purposes? I'm trying to avoid usin ...

Check to see if two words are identical in PHP

Could you please check the word I've written in the text field? If the two words are the same, I should receive a message saying "They are the same." <?php function array_random($arr, $num = 1) { shuffle($arr); $r = array(); for ($i = ...

CSS Opacity Issue

Why is the article transparent? Despite my efforts to search online for similar instances, I am surprised at not finding anything and feeling quite puzzled about what steps to take next. body { background-color: #000000; opacity: 0.8; background-i ...

Failure of Outlook 2007, 2010, and 2013 to Recognize Conditional CSS

I am currently working on a design for a responsive email. The layout is functioning well in all email clients tested using Litmus, except for Outlook 07/10/13 due to its challenging word rendering engine versions. To ensure correct display across differen ...

Utilizing jQuery AJAX to efficiently handle branching based on the result received

I've successfully set up my first AJAX call with jQuery. The only thing left to do is to check the result from the PHP page for any database errors and display an error message if necessary. Here's the current Javascript code: <script type=" ...

Mysterious purple squares mysteriously popping up around elements in a WordPress website utilizing CSS Bootstrap 4

While browsing certain websites on specific browsers that utilize Bootstrap 4 as a framework within a WordPress site, I've noticed some elements displaying strange purple squares when visited. Any ideas on the cause of this issue and how it can be res ...

What is the reason behind MySQL rejecting float values?

inquiry in .php $que_cinstructors = " INSERT INTO course_instructors ( usn, i1, i2, i3, i4, i5, i6, i7, i8, i9 ) VALUES ( :usn, :i1, :i2, :i3, :i4, :i5, :i6, :i7, :i8, :i9 )"; $query_params3 = array( ':usn' => ...

Conceal anchor links using jQuery by targeting their title attribute

Looking at the html below <li class="static"> <a title="blog" class="static menu-item" href="http://google.com">Blog</a> </li> <li class="static"> <a title="profile" class="static menu-item" href="http://profile.com"> ...

Insert information into the <div> element

Sorry if this is a repeated question, but I'm struggling to locate the solution. Is there a way to use jQuery to add content within a div, depending on its current class? For instance, adding "CODE" to a div such as: <div "CODE" class="class_one" ...

Can someone help me locate the selector for using .click in Nightmare.js?

I am currently using Nightmare.js to automate the scraping of a webpage. As a sysadmin, my understanding of Node.js and CSS is limited. So far, I have been able to successfully use Nightmare to input and click on certain buttons in order to log in. I iden ...

Prevent anchor link click and drag functionality on an HTML page / Swipe through a container containing links

Is there a way to prevent clicking and dragging on a link in a webpage? When you click the left mouse button on a link and drag it, you can unintentionally move the link or open a new tab. I am looking for a way to disable this behavior using JavaScript o ...

Is it possible for images to align vertically instead of horizontally in IE?

Encountering an issue with image alignment in Internet Explorer - they are aligning vertically instead of horizontally, while functioning correctly in all other browsers. Moreover, in IE8 and 7, the images are not being displayed at all. Is there a workaro ...

Send the user to a specified destination

Currently, I am working on creating a form that allows users to input a location and have the page redirect to that location after submission. However, I am facing difficulty in determining how to set the value for action="" when I do not know what the loc ...