Dimensions of div tags that are nested within each other

I have a container div that contains three inner divs, each with a width of 33%. When they don't have borders, they align perfectly in line.

However, when I add a 1px border to each of the inner divs, it causes them to go out of alignment and pushes the third div below the other two.

Is there a way to keep the three inner divs perfectly aligned within the container while still using borders to highlight each one?

You can view an example demonstrating the issue on JSFiddle by following this link: https://jsfiddle.net/p0yzrL0j/

Second query:

How can I ensure that the sizes of the divs remain fixed so that whenever the window is resized, the divs shrink or expand to fit the window size instead of stacking on top of each other?

Answer №1

Here is a demo I created showcasing the implementation of

box-sizing: border-box;

https://jsfiddle.net/62n4d51e/1/

You have the ability to adjust the border size without affecting the overall width of the element.

Answer №2

If you want to ensure that the total width and height of a box includes any border and padding, you can use:

box-sizing: border-box;

This CSS property is useful for including the border and padding in the overall dimensions of an element. You can find more information about it at this W3C Reference.

Many grid systems utilize universal selectors to apply this style, such as:

*,after,before {
    box-sizing: border-box;
}

Answer №3

Another option is to avoid using "box-sizing: border-box;"

You can utilize the calc() function.

If your boxes have a 1px border, it will add 1px to both the left and right sides. Therefore, you need to subtract 2px from the 33% width.

#stats {
    width: calc(33% - 2px);
}

Answer №4

By utilizing the calc function in CSS, I managed to automatically adjust the width of a div by subtracting border widths from the total width, ensuring it adapts seamlessly to any screen resolution.

.one{
 width:33%;
 height:100px;
 border:1px solid red;
 float:left;
}
.two{
 width:-webkit-calc(34% - 7px);
 width:-moz-calc(34% - 7px);
 height:100px;
 border:1px solid blue;
 float:left;
}
I Just Used widths using calc for one div for subtraction of border widths from total with of divs now it can automatically adjusts for any resolution  
<div class="one">1</div>
<div class="two">2</div>
<div class="one">3</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

Determine the worth of various object attributes and delete them from the list

Currently, my dataset is structured like this: { roof: 'black', door: 'white', windows: 8 }, { roof: 'red', door: 'green', windows: 2 }, { roof: 'black', door: 'green', windows: ...

Header should elegantly glide up and down as the user scrolls through the page

I've implemented a top div on my page that houses contact details and social media links. I've managed to hide it during scrolling and make it reappear when reaching the top of the page. However, I'm hoping to smoothen this transition with a ...

Leveraging AJAX to access a PHP file

I'm currently attempting to access a PHP file that updates a database entry. To ensure that only specific parts of my webpage are updated, I intend to utilize AJAX. Once the PHP file has executed, I aim to modify an image on the triggering page. Coul ...

Ajax is working effectively as the first post is successful and the second post is able to retrieve data

UPDATE: I resolved the issue by relocating my form submitter script from the Header to the bottom of the jamesmsg.php page (the included one). By doing this, the functions are always re-loaded and attached to the "new" form each time the div is refreshed. ...

Incorporating Past Projects into an Angular 2 Website

Some time ago, I built a Javascript game utilizing the HTML canvas element for image rendering. Now that I have a personal website created with Angular 2, I am unsure of how to properly embed my game into my site. Due to Angular 2 removing the script tag ...

Error: The function of _data__WEBPACK_IMPORTED_MODULE_3___default.a.map is not executable

As a newcomer to React, I am exploring how arrays work with components using the map function. However, I recently encountered an error that has me stumped on how to resolve it. I have scoured numerous blogs for answers, but none seem to address my specif ...

Eliminating unnecessary CSS classes

There are multiple references to the specific ".wiki-content" class in the stylesheet: .wiki-content ul,.wiki-content ol,.wiki-content dl { padding-top:0; margin-top:0; } .wiki-content a,.wiki-content a:link,.wiki-content a:visite ...

Formatting Date and Time in the Gridview of my Asp.net Application

I have been using this format to display the date and time in a grid. The issue I am facing is that I cannot retrieve the exact HH:MM from the database. Even though the database shows 11:11, my grid is displaying 11:03 instead. Here is the value stored in ...

Encountered an issue with mapping data from a controller to a view in Angular.js

Currently, my application consists of only three small parts: a service that makes an http call to a .json file, a controller that receives data from the service and sends it to a view. Everything was working fine when I hard coded the data in my service. ...

Unable to view Chart.js on the second tab

I'm currently working on two different charts for a project - a bar chart and a line chart. The bar chart is displayed on the first tab, while the line chart is on the second tab. Interestingly, the bar chart functions properly, and when I point the l ...

Implement an onchange function in the selected jQuery plugin for a WordPress website

Having encountered issues when attempting to implement the onchange function in my JavaScript file, I found it on https://www.w3schools.com/php/php_ajax_database.asp: jQuery(document).ready(function($) { $('select.Nom').chosen({ width:"50%" } ...

What is the best way to send a prop from a parent component to its child using context in React?

Currently, I am working on fetching data in a React application. My goal is to extract an individual value from the response and pass it as a prop in a context from a parent component. The `trendingData` variable holds information retrieved from an API cal ...

Retrieve the file path of an uploaded image in a Razor view using jQuery Ajax

I have been facing challenges while working on an ASP.NET MVC3 application where I am developing a custom image gallery. The browser being targeted is IE9, which rules out the option of using HTML5 File API. All my code resides within a strongly typed Razo ...

PHP Loop to Dynamically Generate Exploded Checkboxes

Is there a way to display data from the table "school_minat" as checkboxes in a checklist by looping through the table "dayaminat"? I have attempted this, however, only one checkbox is displayed. Even though there are 4 data entries with minat_id values ...

Toggle between light and dark mode with React

I am in the process of creating a simple dark/light toggler button using React. Currently, I have developed a toggler component with a state defined as: this.state = {style: './App.css'} I have also created two functions – one that changes thi ...

Determine line-height and adjust positions using JavaScript in the Chrome browser

I am creating a basic text reading application with a line to aid in easy reading. The functionality involves using the up and down arrow keys on the keyboard to adjust the position of a red line based on the line-height property. This is accomplished thr ...

When using the require() function in Node.js, the period "." is not being recognized as part of the command and

I recently encountered a problem while working on my project and reorganizing my files. I noticed that the "." in my requires are not being parsed correctly. Upon running my program, an error message is displayed: Error: Module './src/map/createMa ...

Ways to output HTML from a function

I'm struggling to get my HTML function to work properly, even though it seems like a simple task. For some reason, the content isn't displaying when I return it, but things work fine when using console.log. Could someone please assist me with th ...

Are the CSS flow arrows suffering from poor formatting?

https://i.sstatic.net/ni3vf.pngAfter tinkering with code from three different individuals to create CSS arrows, I fear I may have overcomplicated things. Is there any CSS expert out there who can help me streamline this? I'm struggling to adjust the p ...

What is the best way to extract specific values from these JSON objects?

I'm struggling with extracting the "name" values from JSON code returned by an online API. Despite trying various approaches, such as .people.name, I keep getting either a blank output or "[object Object][object Object][object Object][object Object][o ...