The CSS border specification is important when determining the height of content

Recently, while working on some CSS code, I encountered an interesting issue. I had wrapped an article within a div and assigned percentage widths to both elements, as well as added padding to the div. Strangely enough, the padding did not seem to be applied until I added a border to the article. When I commented out the border, the height of my article suddenly shrank. What could possibly be causing this unexpected behavior?

<style>
body{
    margin:100px;
}
.content{
width:100%;   
padding:.9746589%;
background:green;
   }
  .content>article{
width:100%;
background:yellow;  
border:1px solid red;
 }

</style>
        <div class="content" role="main">
            <article>

                <p>This is my text!</p>
            </article>
        </div>

Answer №1

The default margins within the p element are interacting with the margins of the article element, causing them to collapse. However, when a border is added to the article, this collapse is prevented and the article contains the p element along with its default margins.

Although the .9746589% padding is present in both scenarios, placing it on .content shields it from the margin collapse between the article and the contained p element. If you were to remove the border as well as the padding, the margins would collapse across all elements, resulting in the green background disappearing completely. Essentially, just like the border prevents margin collapse between the article and p, the padding also blocks margin collapse between .content and the article along with its contents.

Furthermore, it's important to note that margins only collapse vertically; therefore, adjusting the width will not have any impact on this behavior.

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

Customizing the HTMLElement class to modify particular attributes

Is there a way to modify the behavior of an HTMLElement's scrollTop property by adding some extra logic before updating the actual value? The common approach seems to be deleting the original property and using Object.defineProperty(): delete element. ...

Is there a way to ensure that Google ads can be opened in a new tab when clicked, instead of opening within the current tab?

I recently created a Google ad and placed it on my website. However, I noticed that when users click on the ad, sometimes it opens in the same tab rather than a new one. This can lead to users losing track of my website. Can anyone explain why this incons ...

Is it possible to gather information by scraping after submitting a form?

I'm currently working on a project for my class and as a beginner, I might make mistakes with some of the technical terms. Please bear with me. My project involves creating an interactive journey planner specifically for my city's public transit ...

Is AsciiEffect.js compatible with CSS3DRenderer.js in combination with three.js/WebGL?

Having trouble using the AsciiEffect.js and CSS3DRenderer.js together, wondering if it's possible without tweaking... here's the concept var W = window.innerWidth, H = window.innerHeight; renderer = new THREE.CSS3DRenderer(); effect = new THREE. ...

Developing a form using jQuery/ajax

Is there a way to update the Contact Us form in Div without redirecting to another thank you page after submission? I want to replace the contact form in that Div with a "Thank You" message upon successful validation. Any suggestions or demo/download code ...

Design a circular text element using Tailwind CSS

Just starting out as a web developer and running into some issues with Tailwind CSS. Here's what I'm trying to achieve: https://ibb.co/KL8cDR2 This is the code I have right now: <div class="w-1/2 h-10 rounded-full bg-gray-400" style ...

The issue with the FileEntry.file() method arises when attempting to open an HTML file directly

I have been developing a drag-and-drop upload feature. In the drop event, I utilize e.dataTransfer.items to extract handles for all files and folders, then use the items[n].webkitGetAsEntry() method to obtain the FileEntry object. Lastly, I try to access t ...

show a different text when hovering over a table cell

I am working with a table where one column displays the employee ID, and I would like to show their names when hovering over it. Can someone assist me with this task? ...

What is the process for implementing the sticky table header jQuery plugin?

I am looking to implement a sticky header on all tables in my web application, which is built on PHP. As the amount of data continues to grow, search results are fetching more records that may not be visible. I am primarily a PHP programmer and do not have ...

What is the best way to make the text align with the top of its designated space?

I've been experimenting with different CSS properties like vertical-align, line-height, and more. This example should give you an idea of my goal. I added a small red arrow to indicate where I intend for the text to be shifted upwards. ...

Various height divs are arranged in two columns and float accordingly

My goal is to have two columns that stack divs of varying heights in the order they appear. These divs are created dynamically, causing some challenges with alignment when floated at 50% width. For example, div #4 may end up being significantly taller tha ...

Incorporate Multiple Choice Queries into your PHP Online Form

My PHP web form consists of text fields, dropdowns, and radio buttons. When submitted, the information is sent to me via email. I am trying to implement a multiple choice question with checkboxes. Although I can create the form field, I'm struggling t ...

The inconsistency of Selenium's StaleElementReferenceException error and the variability of pageload completion codes is causing issues with clicking on elements

Big shoutout to the amazing stackoverflow community for always providing assistance. Lately, I've been grappling with the frustrating "StaleElementReferenceException" issue and haven't found a universal solution yet. Some helpful members have rec ...

Retrieving and storing information from a form without the need to submit it

I have been given the task of creating a load/save feature for a complex form. The goal is to allow users to save their progress and resume working on it at a later time. After much consideration, I have decided to implement server-side storage by saving ...

How should white space be managed in Bootstrap 4 cards when incorporating responsive column classes - wrap elements inside divs or apply classes directly?

In Bootstrap 4, when applying a responsive column class like "col-sm-8" directly to a "div" with the class "card", whitespace is added between the card border and content. This can be seen in the highlighted yellow area in the image below. https://i.sstat ...

increased space between tables in HTML email design for optimal display in Gmail inbox

Here is the link to my code: http://jsfiddle.net/user1212/G86KE/4/ I am experiencing an issue in Gmail where there is extra white space between 2 tables inside the same cell. I have attempted using display:block; margin:0; padding:0; line-height:0; How ...

Visual Studio is not recognizing at-rules

Currently implementing code found at this URL: This code includes: @-webkit-keyframes moveclouds { 0% {margin-left: 1000px;} 100% {margin-left: -1000px;} } @-moz-keyframes moveclouds { 0% {margin-left: 1000px;} 100% {margin-left: -1000px;} } @-o-keyframe ...

The functionality of the Bootstrap carousel appears to be malfunctioning

My attempt at creating a carousel has hit a snag - it seems to be stuck on the first image and the next and previous buttons aren't functioning. I've gone over the code multiple times, but I just can't pinpoint the issue. Any assistance woul ...

Getting the text from a Selenium element can become tricky when dealing with product discounts that alter the element

(Programming) I am working on selecting random products from a website, some with discounts and some without. For instance: How do I retrieve product 748 (without discount)? or How do I retrieve product 419 (with discount)? When the product ha ...

Tips for incorporating a Font Awesome icon <i> within a select dropdown and customizing its appearance

I am facing an issue while trying to insert an icon into a select option. The error message I received is: Warning: validateDOMNesting(...): cannot appear as a child of <option> To indicate that certain fields are required, I am using asterisk ic ...