Fluid designs in CSS with static padding and margins

Is there a method that allows for the incorporation of pixel width paddings or margins in a fluid column design, without requiring additional html markup?

To better explain, consider a basic html/css layout like this:

<style>
    .cont{width:500px;height:200px;border:1px solid black;}
    .col{float:left;}
    #foo{background-color:blue;width:10%;}
    #bar{background-color:yellow;width:30%;}
    #baz{background-color:red;width:60%;}
</style>
<div class="cont">
    <div class="col" id="foo">Foo</div>
    <div class="col" id="bar">Bar</div>
    <div class="col" id="baz">Baz</div>
</div>

This setup works fine (ignoring any potential display bugs). However, once padding of 10px is added to the col class, the floats no longer align correctly. The same issue arises if a 3px border is applied to the col class. While negative margin techniques can counteract the box model's added pixels, they do not adjust the <div> width. Essentially, the goal is to maintain a 10% width with 10px of that percentage dedicated to padding (or margin).

Answer №1

To achieve the desired layout without additional html markup in pre-CSS3, you can utilize an extra wrapper for each div. The width property does not account for padding and borders by default. To avoid negative margins, add a wrapper with the following CSS:

.padder {border: 3px solid green; padding: 10px;}

The corresponding HTML structure would be:

<div class="cont">
    <div class="col" id="foo"><div class="padder">Foo</div></div>
    <div class="col" id="bar"><div class="padder">Bar</div></div>
    <div class="col" id="baz"><div class="padder">Baz</div></div>
</div>

If you need compatibility with CSS3, consider using bobince's solution which involves using box-sizing.

Answer №2

To achieve this, you can utilize the box-sizing property to alter how width is calculated, including padding (and border—similar to the box model in Quirks Mode without its associated issues).

box-sizing is a CSS3 style that is currently in the proposal stage. However, it may not be compatible with older or lesser-known browsers and might require prefixing on certain browsers.

.col{
    float:left; padding: 10px;
    box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box;
}

Unfortunately, box-sizing does not function in IE versions prior to IE8. To avoid resorting to Quirks Mode for those browsers—and it's strongly recommended not to do so—you could consider using fix scripts/behaviors that attempt to implement box-sizing on IE6-7.

If these solutions are insufficient, the fallback option is to use the wrapper method suggested by Scott. This method should work universally across all platforms.

When working with percentages that sum up to 100% and liquid layouts, exercise caution. Rounding discrepancies may occur if the pixel width does not evenly divide by the specified percentages. WebKit usually rounds down while IE6-7 rounds to the nearest value, potentially causing floats to wrap unexpectedly due to a one or two-pixel difference.

Answer №3

One alternative is to manually compute the width, margin, and padding.

This can be done as the container has a set width.

Answer №4

If you want to streamline your web design process and avoid a multitude of issues, consider exploring 960 grids (960.gs), Blueprint () or YUI (http://developer.yahoo.com/yui/grids/). These frameworks have carefully resolved common design problems by allowing you to simply define a percentage or number of grids, with the framework handling the rest of the heavy lifting. YUI and 960 even offer a convenient generator tool so you can bypass tedious manual work. Additionally, many of these CSS frameworks include a css "reset" feature that rectifies IE flaws and standardizes fonts, spacing, and other elements that tend to cause frustration for UI designers.

I recently completed a regulatory compliance update across thousands of webpages with diverse designs for a prominent international bank. We opted for YUI at the onset and I quickly developed an appreciation for its straightforward "grids" function due to its effortless implementation. The framework has efficiently managed sites attracting over 1 million unique visitors daily without any hiccups or noticeable lag. Once you become familiar with how it functions, you can lay out a website within minutes, eliminating concerns about floats, padding, and more. In my personal projects, I leverage both Blueprint and 960 for similar reasons.

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

The Chrome Keyboard on Android seamlessly passes words to the next input field when the focus changes in JavaScript

When using two input fields, pressing the enter key on the first field in Chrome (49) on Android (6.0.1) carries over the last word typed to the new input due to the right-bottom button on the standard keyboard. Is there a way to prevent this behavior or a ...

Text beyond the body tag

My current setup includes: html, body { height: 100%; } section { width: 100%; height: 100%; } However, I have noticed that any content appearing below the .full class is not being included within the body tag. Is there a way to address ...

Size of text Including line breaks

I have developed a function that calculates the maximum size of text: function CalculateMaxTextSize(width, height, text) { var ourText = $('span').css('font-weight', 'bold').text(text); ...

Stop the inclusion of the scrollbar in the mat-drawer-inner-container within the Angular mat-drawer Component

Background Story: Working on designing a screen layout that includes the use of a mat-drawer to display a custom component. The challenge arises when the custom component gets nested inside a div (with class="mat-drawer-inner-container") automatically adde ...

Is it true that nested CSS IDs function correctly in Firefox, but not in IE8?

My HTML is structured like this: <div id="content"> <div id="asection"> <h1>Some Text</h1> </div> </div> The CSS properties I'm using are as follows: h1 { color:#873C62; font-size:32px; line-heigh ...

Step-by-step guide to adding a skew overlay to your video

I am experimenting with creating a skewed overlay on a video playing in the background at full width. Currently, the skew overlay is functioning perfectly. What if I want it to appear in the bottom-right corner instead of the top-left corner? Would I need ...

Angular click event to compute a function and display the result on a separate page

Currently, I am developing a BMI app using Ionic Angular (latest version). The objective is to create a button that collects input data from fields and triggers a method to validate the inputs. Once validated, the app should display the results page with t ...

Is the first part of the URL in Express susceptible to injections when using two-level URLs?

I am currently utilizing Node.js and Express for my project. When dealing with a single-level URL like: /estonia All scripts and styles are loading correctly. However, when it comes to a two-level URL such as: /estonia/tallinn The scripts and styles i ...

Can I create interactive stacked shapes with CSS and/or JavaScript?

Trying to explain this may be a challenge, so please bear with me. I need to create an "upvote" feature for a website. The number of upvotes is adjustable in the system settings. The upvote controls should resemble green chevrons pointing upwards. For exa ...

An error in syntax is being triggered by the Php file being accessed through the <script src="list.php"></script>

We're facing an unusual problem with our online ordering platform script that we purchased a few months ago. It had been functioning perfectly for several months, but now we're encountering an issue! The website page is not loading, and when we c ...

Displaying live data from an XMLHttpRequest in a Vue component in real-time

I'm currently working on implementing lazy loading for a list of posts fetched from the WordPress REST API. My goal is to load additional news stories upon clicking an HTML element. However, I'm facing issues with accessing the original Vue inst ...

The CSS file was not applied because the mime type did not match (Error code: SEC711

I am currently working on a JSP-Servlet application. I have successfully deployed the application using Eclipse with the default Tomcat 7 server. However, upon deployment, I noticed that the UI renders properly in Chrome and Firefox but encounters issues ...

html5 drag and drop issue with data set

Hello, I'm currently working on enhancing my order form with some innovative drag and drop features. However, I've encountered an issue with the dataset js object. When I attach a function to the ondragstart event and attempt to access event.data ...

Changing the color of an SVG as I scroll to the bottom of the page

On my React page, I am looking to change the color of an SVG element from black to white at the bottom of the page. Is there a React-specific method for accomplishing this task? The div container located at the bottom of the page is not the parent div fo ...

Looking for an API to retrieve random quotes and images from a website?

Greetings, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS along with the basics of JS. Level of Expertise: Beginner During my exploration, I stumbled upon this intriguing website - . It stands out a ...

Add a button at the base of a row within Bootstrap

I've been struggling to position a button at the bottom of the column, but so far, I haven't had any luck. After reading this post, I added the CSS classes d-flex, flex-column, and mt-auto, but they don't seem to be working as expected. Can ...

How to Implement Filtering in AngularJS ng-repeat

While developing my app, I came across a challenge with the filter in my ng-repeat. Initially, I could search by productCode or name. Then, a new requirement emerged - "Filter by SubcategoryId." The issue lies in filtering strictly by subCategoryId, while ...

What's the best way to customize the color of the text "labels" in the Form components of a basic React JS module?

I have a React component named "Login.js" that utilizes forms and renders the following:- return ( <div className="form-container"> <Form onSubmit={onSubmit} noValidate className={loading ? 'loading' : ''}&g ...

Establish a foundation grid structure within the elements of the list

I'm facing an issue while trying to implement a numbered list, where each li contains a segment of the Foundation 5.5.3 grid. The problem arises when there is unwanted space above the child grid columns within the li elements. Example: https://i.sst ...

I am unable to display the content even after setting `display: block` using the `.show()`

Hello there, I have attached my javascript and html code. While in debug mode, I can see that the CSS property 'display: none' changes to 'display: block', but for some reason, the popupEventForm does not open up. Any suggestions on why ...