What is the reason behind the inability of vertical margins to collapse?

Review the code snippet below:

<!doctype html>
<html>
<style>
  div.compact-inverse {
    border: 1px solid black;
  }

  div.compact-inverse > input {
    display: inline-block;
    width: 16px;
    height: 16px;
    margin: 12px 0;
  }
  
  div.compact-inverse > label {
    display: inline;
    margin: 12px 0;
    padding-left: 18px;
    border: 1px solid red;
    background-color: pink;
  }
  
  div.compact-inverse > p {
    display: block;
    margin: 12px 0;
    padding-left: 34px;
    border: 1px solid blue;
    background-color: cyan;
  }
</style>
<body>
  <form>
    <div class='compact-inverse'>
      <input type='checkbox' name='delete_imported' /><label>Delete originals</label>
      <p>Original files will be deleted after the import when possible.</p>
    </div>
  </form>
</body>
</html>

The screenshot shows that the vertical margin between the first row (input with label) and the second row (paragraph) is stacked rather than collapsed.

Can you identify why this is happening?

Answer №1

According to the official documentation:

Vertical margins that are next to each other collapse

Additionally,

Margins are considered adjoining only if … they both belong to block-level boxes that are part of the same block formatting context

The label and input elements function as inline elements, meaning they do not create block-level boxes.

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

What could be causing the malfunction of slideToggle in this situation?

I have been troubleshooting this issue all night and I am completely stuck. Can anyone figure out why the navigation menu is not opening? Here is the HTML code: <ul id="nav"> <li><a href="item1.html">item1</a></li> & ...

An alternative way to adjust font size without having control over the HTML code

Is there a way to adjust the font size in a web page when only the div or iFrame is editable, not the actual HTML content? Our website allows users to upload their own ebooks, resulting in uncontrolled and complex HTML content for each chapter. We want to ...

Utilizing the power of ui-views alongside angularjs ui Tabs to handle inappropriate user input

I recently updated the markup for some forms to utilize the angularJS ui Tabs component. The new markup structure is as follows: <div class="col-lg-3 col-md-3 panel-container"> <tabset vertical="true" type="pills"> ...

How can I ensure the MatBottomSheet aligns perfectly with the top toolbar?

If we intend for the MatBottomSheet to appear with its top aligned perfectly against the top toolbar, what is the most effective approach to achieve this? Refer to the demo provided in the documentation. View the demonstration here In this stackblitz ex ...

Need a guide to identify HTML elements with a particular attribute?

I attempted to find this information online, but I wasn't successful. Suppose I am interested in knowing all the HTML elements that contain a "background" attribute. I'm aware that some web browsers may not support certain elements. Off the top ...

Should HTML input arrays be named as [] or [0]?

I am looking to implement multiple forms with a single submit (save) button. <?php for ($i = 0; $i < count($c); $i++): ?> <div> <input type="radio" name="gender[<?php echo $i ?>]" id="gender_<?php echo $ ...

Random variable without repetition in SASS

I have a unique project where I am tasked with incorporating a list of 5 specific colors into 10 different div elements. The challenge is that each color must be used twice and in a completely random order without any repetition of colors. Although utilizi ...

Please submit the form triggered by a JavaScript event

I am looking to create a form that can be submitted when a user presses a key, such as tab or enter, which would cause them to lose focus. Additionally, clicking outside of the text field should also trigger a submit action. However, if the user clicks on ...

Arranging unrelated divs in alignment

http://codepen.io/anon/pen/Gxbfu <-- Here is the specific portion of the website that needs alignment. My goal is to have Onyx Design perfectly aligned with the right side of the navbar, or to have the navbar extend up to the end of "Onyx Design". The ...

When the oncuechange event is triggered, it initiates a smooth fade-in/fade-out animation within the HTML P tag

Just starting out with web development and learning JavaScript. Trying to create a webpage that displays lyrics synced with an audio file inside a p tag. The subtitles are sourced from a vet file extension and I am using the "cuechange" event in JavaScript ...

Div takes on the opacity of its parent element

I want to implement a modal popup using the Ajaxtoolkit's modalpopupextender, and so far it's working perfectly. While the popup is displayed, I need to dim out the rest of the page. To achieve this, I applied the modalPopup CSS class to the pop ...

Creating a web layout or template using HTML

Currently in the process of developing a website, my goal is to construct html templates that include placeholders for injecting pages/content, menus, and other elements. While I admire AngularJS for its flexibility in achieving this, I fear it may be too ...

"Place text at the center of a div that is rotated

I'm facing the challenge of centering a heading both vertically and horizontally within a div that has been rotated 45 degrees (transform:rotate(45deg);). Due to this rotation, I have attempted to counteract it by rotating the heading in the opposite ...

Top method for individually choosing multiple selections from a foreach loop output

Need help with a loop: foreach ($userItems_get as $item => $value) { if ($value['prefab'] == 'wearable') { echo $value['name'] . "</br>"; echo "<img src=\"{$value['image_invent ...

Is there a way to add a disappearing large space that vanishes when a line break occurs?

I am currently working on a website where I aim to have the name and airport code displayed in a centered title above an image of the airport. https://i.sstatic.net/Vwi2V.png The name and code will be separated by an em space: div.terminal-silhouette- ...

Images will only be displayed if the optional html parameter is included; otherwise, they will be hidden

In my parent component, there is an image displayed at a specific path (note: the image is already stored in my project). This path can optionally have additional parameters. If the For instance, The image is visible (image) when the HTML path is: ww ...

What is the method for creating a hover line that perfectly mirrors the length of text above it?

I'm interested in creating a hover effect where a line appears above my text when hovering over it. The twist is, I want the hover line to match the length of each individual text. This effect will be applied to the navigation section of my HTML. Her ...

Do you have any tips on designing a mobile-friendly navigation bar?

Struggling with creating a responsive navbar using Bootstrap 5? The navbar-brand is causing issues with letters overflowing over other elements, and the toggle-icon is not aligning properly. Check out my Codepen for reference: https://codepen.io/namename12 ...

Is it necessary for me to employ MetaTag http-equiv in conjunction with DTD XHTML 1.0 Transitional//EN?

Currently using Asp.net for my project. In the document, I have included the following: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> I am wondering if it is necessary to a ...

What is the process to replace the image in these templates?

I recently downloaded a template from the internet and am in the process of developing a website. Can someone please guide me on how to change the icons highlighted in the black and red boxes as shown in the image? I am using Visual Studio and ASP.NET fo ...