Dividing a container into sections using percentages with margins? Ensure the total adds up to

Looking to create a grid with div/section in percentage format, while maintaining consistent margins throughout. See an example here:

I have code that works with margin set to 1px, but I want to adjust it to use a margin of 1em or around 10px. How can I achieve this?

Here is the HTML:

<html>
    <head>
        <title>Title</title>
        <link rel="stylesheet" type="text/css" media="all" href="default.css" /> 
    </head>
    <body>
    <div class="box small"></div>
    <div class="box small"></div>
    <div class="box small"></div>
    <div class="clear"></div>
    <div class="box small"></div>
    <div class="box large"></div>
    </body>
</html>

And the CSS:

.box {
    height:50px;
    margin:1em;
    background:#000;
    float:left;
}
.small {
    width:33%;
}
.large {
    width:66%;
}
.clear{
    clear:both;
}

Answer №1

Instead of using margins, consider using transparent or white borders in conjunction with the box-sizing property.

.box {
    height: 50px;
    border: 1em solid #fff;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    background: #000;
    float: left;
}

By implementing this approach, the width specified for the large (66%) and small (33%) boxes will account for the border width as well. Be sure to adjust the border color to blend seamlessly with your current background.

Answer №2

Check out the box-sizing attribute for more information.

box-sizing: border-box

The specified width and height (as well as min/max properties) on this element determine the border box of the element. Essentially, any padding or border defined on the element is contained within this specified width and height. The actual dimensions of the content are calculated by subtracting the widths of the borders and paddings from the set 'width' and 'height' values.

However, it's important to note that this concept only applies to padding, not margin. Padding is typically used in frameworks like Twitter Bootstrap to create space between elements.

Answer №3

Make sure to take a look at the link below.

Visit this page

.BigBox{ width:40%; height:225px;}
.SmallBox{ width:20%;height:225px;}
.InnerBox{ padding:10px; margin:0px 10px 0px 10px; border:solid 1px #666666;}

Best of luck!

Answer №4

To accommodate the margin added to each element, it's necessary to slightly decrease the widths:

.small {
    width:24%;
}
.large {
    width:57%;
}

http://jsfiddle.net/7AbCd/

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

Using jQuery's .on() method to dynamically add classes

Why is this method not functioning as expected? My understanding was that by assigning a class to the trigger element when it opens, I could then use that class to close it when the trigger is clicked again. // Trigger for menu $('.menu-trigger' ...

Retrieve the value of the input type="file" when multiple files have been selected

Similar Query: Extracting file names from a multi-file upload control using JavaScript With HTML5 input type="file", users can select multiple files by adding the attribute multiple="multiple" : <input type="file" multiple="multiple" /> My que ...

Tips for determining the actions of a node prior to its inception

Is there a way to automatically run scripts on specific elements after their creation? For example, using a jQuery plugin like autoresize to expand the height of a textarea. If I use $('.textarea').autosize(), only the current textareas will be a ...

Why aren't CSS media queries functioning in the existing stylesheet?

I have a good grasp on responsive design and CSS techniques. However, I am encountering an issue where the media query for mobile dimensions is not working as expected. When monitoring the applied styles in Chrome, only the min-device-width styles are be ...

Preventing users from selecting past dates in HTML input date field

I need help with disabling past dates in an HTML date input field. Even though I am able to restrict selecting past dates on the date picker, I can still save data if I manually type in a previous date (e.g., 12/08/2020). $(document).ready(function() { ...

The align-content property in CSS Flex does not function properly when used alongside an inline height declaration

I encountered an issue with the CSS Flex property align-content. It seems to not work properly in a container where the height is declared inline. However, once I move the inline height declaration to a separate CSS class, everything works fine. Both Chrom ...

Experiencing difficulties with the alignment of Bootstrap columns

I'm encountering an issue with displaying 3 Bootstrap columns in the ContentArea. Even though I can locate them using Developer tools, they seem to be positioned at the bottom of the ContentArea, making them not visible. I've attempted adjusting ...

Storing a temporary value within an ng-repeat iteration in AngularJS

A unique and interesting query arises when dealing with random value generation. Here is a snippet of code that showcases how to achieve this: function randomize() { return function (input) { if (input !== null && input !== undefined & ...

Add a div element only if there is a background image present

I am facing a situation where I need to display an image as the background of a div only if it is available. If the image is not present, I do not want the div to be visible at all. I am working with django templates and here is my current approach: {% if ...

Dimensional adjustments for Semantic-ui dropdowns

Currently, we are attempting to transform bootstrap into semantic-ui. <select id="sayfaArama" class="ui search dropdown"> <option value="">Searching in pages...</option> </select> Take a look at this image I have encountered ...

Discover the secret to permanently adding a video icon to your images

I am struggling to permanently place a video icon on an image without having both elements appear separately. Essentially, I want to overlay a video icon onto an image seamlessly without the need for external tools like Photoshop. More Clearly: I have ...

Using CSS to apply hidden box shadow to nth child element

I am encountering a challenge with using the CSS selector :nth-child(...) in combination with the box-shadow effect. The intended outcome is as follows: The even-numbered div elements within a specific container should have alternating background colors. ...

Resizing a column in the Bootstrap CSS grid causes adjacent columns to move

I am currently working with a bootstrap grid that includes an expand component. The issue I am facing is that when I expand one column, the other columns in the same row also shift. Is there a way to make each column independent of the others? expand co ...

Set up two separate tables with sufficient space in between each other

Is there a way to align these two tables next to each other with some space between them? Currently, they appear one below the other. How can I make them sit beside each other with spacing in between? If anyone knows how to do this, please help me out. &l ...

How to maintain the original size of an image when setting it as a background with CSS

My challenge is using a large image as a background, as it always resizes automatically and cannot display the full height of the image. How should I handle this issue? One approach is to set the height to match the image's height. However, this mean ...

Issue with firebase.auth() method not triggering onAuthStateChanged after user login/logout操作

My code looks like this: var config = { apiKey: "xxxxx", authDomain: "xxxxx", databaseURL: "xxxxx", projectId: "xxxxx", storageBucket: "xxxxx", messagingSenderId: "xxxxx" }; firebase.initializeApp(config); $("#l ...

Information disappearing upon returning to a previous screen

Currently, I am developing a web application using AngularJS and HTML. As part of the application, I created a dashboard on the home screen (referred to as Screen A) that displays a list of clients with a summary. When a client is clicked, it navigates t ...

Eliminating a decimal point from the Document Object Model

I've got some HTML code that's being generated through PHP. It includes a number with two decimal places within a span element that has the class "amount": <span class="amount">200.00</span> My goal is to find a way to use a client- ...

The Chrome equivalent of -moz-user-focus

We have a custom model dialog control that we use for all popups on our web pages. When this dialog is initialized, we use jQuery expose to gray out the rest of the page. To prevent selection on the grayed-out area, we apply the following styles to the mas ...

How can the presence of a CSS rule prevent another from being applied?

My attempt to create this HTML file in Chrome posed some challenges: <style> br { }​ [required] { border-width: 3px; border-color: red; } </style> <input required /> The entire content of the file is shown above. However, I noti ...