Arranging 3 floated divs at the top of the page

I am attempting to get 3 divs that cover the entire width of the page to align at the top. The goal is to have:

Left Div| Center Div | Right Div

with both the left and right divs occupying 25% of the width and the center occupying 50%. I am using float to align the left and right divs to the sides of the screen. However, my issue arises when I set the center div to 50%, as the right div no longer aligns at the top. Setting the center div to 45% width achieves the desired effect in terms of aligning the tops, but the center div does not utilize all available space.

Below is a simple HTML snippet illustrating the problem:

<div class="parent">
<div class="title-bar">Title goes Here</div>
<div class="sidecontent left">Some content</div>
<div class="content">More content and more...</div>
<div class="sidecontent right">Some Content</div>
</div>

CSS:

    .title-bar {
    color: @color;
    background-color: @title-color;
    padding: 5px;
    font-family: @font-family;
    margin-bottom: 10px;
    font-size: 20px;
    text-align: center;
    border: 1px solid #D5D5D5;
    border-radius: 5px;
    }
.left {
    float: left;
}
.right {
    float: right;
}
.sidecontent {
    width: 25%;
    border: 1px solid #D5D5D5;
    display: inline-block;
    //padding: 5px;
    vertical-align: top;
    height: 500px;
    background-color: red;
    overflow-y: auto;
}
.content {
    float: left;
    display: inline-block;
    vertical-align: top;
    width: 50%;
}

Here is a jsFiddle demo showcasing the issue: http://jsfiddle.net/s6vqC/

Any assistance would be greatly appreciated as I am new to CSS. Thank you.

Answer №1

Applying a border to the left sidebar will increase its width by 25% + 1px. This means that when you add together the percentages and the border size, the total width will exceed 100% of the screen's width by just one pixel. So, practically it would be like this: 25% + 50% + 25% + border:1px = 100% + 1px = full screen width + 1px

Answer №2

@Chanckjh is absolutely correct, the cause of the misalignment is indeed the border.

To solve this issue, you can utilize the CSS property box-sizing: border-box; to ensure that the border does not affect the width of the elements.

For a visual example, check out this demonstration: http://jsfiddle.net/s6vqC/1/

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

Leveraging perl's CGI.pm within various subroutines

I am currently working on a school project that requires me to write a Perl CGI script to build a social networking site. While I have successfully created multiple subroutines for different pages, I am encountering an issue. When I try to execute each su ...

What is the best way to adjust the positioning of my hyperlinks in a CSS drop-down menu to be centered on

I have implemented a dropdown menu on an asp.net webform using an external style sheet and html hyperlinks. I am currently working on getting the menu to align in the center of the webpage, where the rest of the content is already centered. Take a look at ...

I have managed to update the Immutable and Primitive Data-Types in JS. Now, the question arises - are these truly Primitives or are the concepts in

In JavaScript, there are 4 primitive data types that store values directly: String, Number, Boolean, and Symbol. I am excluding undefined and null from this list as they are special data types with unique characteristics. One key feature of primitives is ...

What is the best way to pass a scope variable into an ng-if directive within an li element

Is there a way to pass a scope variable in ng-if within an li element? I have one li element that should change based on the scope variable. For example, if the value is true, it should display a different li. HTML code <h4 class="font-weight-normal mb ...

Anchor tag remains in fixed location in HTML

Here's a link that I need help with: <a id="readmore" href="#">READ MORE</a> Currently, when the user clicks on this hyperlink, I am using Jquery to toggle the content below it to show/hide the content. However, I have encountered an ...

Define the css settings for 'html, body' in Meteor for a particular page or route

I've been working on customizing the background CSS settings for a specific route or page in my Meteor app. To achieve this, I utilized Template.template.rendered/destroyed along with jQuery selectors to inject the desired CSS once the template is ren ...

Is a non-traditional fading effect possible with scrolling?

I have implemented a code snippet to adjust the opacity of an element based on scrolling: $(window).scroll(function () { this = myfadingdiv var height = $(this).height(); var offset = $(this).offset().top; var opacity = (height - homeTop + ...

Converting LaTeX input into HTML and generating a PDF output

Looking for a ruby gem that can handle latex formatting to html and pdf, including bibtex bibliography integration. Currently using textile in my rails app, but wishing to explore latex for more advanced formatting options. Preference for utilizing *.bib ...

Is there a way to override the padding of a container?

Working with Wordpress for the first time has been quite a challenge. Adjusting to everything is causing me some major headaches. So, I've got this container div and added a lazy 10px padding. However, for the theme I'm attempting to develop, I ...

Spinning text within a responsive bootstrap card

I have a card containing text that is not wrapping properly within the card due to rotation using CSS. <div class="card border-info"> <div id="heading_card" class="card-header"> a very long string of text which is not g ...

Bringing audio to life in a dynamic AJAX chat environment

In my AJAX chat, I am utilizing the Audio class to play a sound when a new message is received. Below is my code snippet: if ( ! $.browser.msie || ( $.browser.msie && parseFloat(jQuery.browser.version) >= 9 ) ) { //var notif = new Audio(&apo ...

What are some creative ways to implement the useState function as a toggle switch in React?

import React, { useEffect, useState } from "react"; import './Menu.css' export default function Menu() { const [classes, setClasses] = useState('container') return ( <div> <p>Click the Me ...

What technologies are utilized to create this price chart in the JavaScript visualization?

Does anyone know how this historical data price graph is created? I've heard of JS libraries like that can generate visualizations, but I'm not very familiar with JS. Any tips on how to create a similar visualization would be greatly appreciate ...

Tips for turning off specific row elements within a formGroup in Angular

Hey there, I'm looking to disable a specific row that is selected through a checkbox. I've tried the code below but it's not working, so I could use some help with it. Thanks in advance! HTML file code: <form [formGroup]="myFormGroup"& ...

Tips for avoiding incorrect data entry in input fields

Is there a way to prevent the input type number from having an undefined value? For instance, like shown in the image below: https://i.sstatic.net/z1pZH.png Any ideas on how to restrict the input to only accept plus or minus values? ...

The datepicker fails to appear when the input field is hidden based on the category that is

I am currently developing a project utilizing Laravel and VueJs. Within the project, I have implemented a dropdown menu as shown below. <select class="form-control" v-model="form.type" @change="typeChange()"> <option value="1">Repair</o ...

Tricks to customize Liferay core CSS

Greetings! I am looking to customize a date picker CSS using a hook in Liferay. Link to relevant Liferay Documentation However, in the Liferay IDE, when creating a new hook, there doesn't seem to be an option for CSS customization in the liferay-hoo ...

Unselect a text field using jQuery

I have developed a custom search script using jQuery that interacts with a PHP file to display results in an HTML div. The script allows users to choose the search type through tabs, and when a search is performed, a URL is generated in the format of #type ...

Struggling with developing a straightforward application with Angular-Material

My goal is to develop an application that utilizes the Angular Material navigation bar, as showcased in this example. Being relatively new to AngularJS, I'm facing an issue where my app loads but only displays a blank page. Below is the code snippet ...