The line-height property is failing to apply to individual HTML elements

CSS class overlap includes the property line-height set to 0. Therefore, when utilizing the overlap class, the content within the html tags is expected to overlap.

In the demonstration below, there are two examples. The first example functions correctly. The p tag employed has the overlap class, causing the three lines separated by the <br> tag to overlap with a vertical line spacing of 0.

In the second example beneath the horizontal line, the div contains three distinct p tags each with the overlap class applied individually.

Likewise in the third example, the div storing three diverse tags utilizes the overlap class and behaves similarly to the second example.

The fundamental question raised is: why does the overlap class, specifically the line-height property, fail to function on the second and third divs? What mistake have I made and how can I correct it to ensure the code works seamlessly, eliminating any vertical line spacing between paragraphs causing them to overlap?

The Demo Snippet

<DOCTYPE html>
<html>
    <head>
        <style>
            p.normal {
                font-style: normal;
            }
            
            p.italic {
                font-style: italic;
            }
            
            p.oblique {
                font-style: oblique;
            }
            
            .overlap {
                line-height: 0;
            }
        </style>
    </head>
    <body>
        <hr>
        <!-- #1 zero line-height used, paragraphs overlap -->
        <div>
            <p class="overlap">
                This is a paragraph in normal style.<br>
                This is a paragraph in italic style.<br>
                This is a paragraph in oblique style.<br>
            </p>
        </div>
        <hr>
        <!-- #2 even zero line-height used, paragraphs dont overlap -->
        <div>
            <p class="normal  overlap"> This is a paragraph in normal style.</p>
            <p class="italic  overlap"> This is a paragraph in italic style.</p>
            <p class="oblique overlap"> This is a paragraph in oblique style.</p>
        </div>
        <hr>
        <!-- #3 even zero line-height used, paragraphs dont overlap -->
        <div class="overlap">
            <p class="normal "> This is a paragraph in normal style.</p>
            <p class="italic "> This is a paragraph in italic style.</p>
            <p class="oblique"> This is a paragraph in oblique style.</p>
        </div>
        <hr>
    </body>
</html>

Answer №1

p elements inherit default margins (-top and -bottom) from the browser's default stylesheet, which then affect the height of the surrounding div element.

                p {
                  margin: 0; /* only added this */
                }
                p.normal {
                  font-style: normal;
                }
                p.italic {
                  font-style: italic;
                }
                p.oblique {
                  font-style: oblique;
                }
                .overlap {
                  line-height: 0;
                }
<hr>
<!-- #1 zero line-height used, paragraphs overlap -->
<div>
  <p class="overlap">
    This is a paragraph in normal style.
    <br>This is a paragraph in italic style.
    <br>This is a paragraph in oblique style.
    <br>
  </p>
</div>
<hr>
<!-- #2 even zero line-height used, paragraphs dont overlap -->
<div>
  <p class="normal  overlap">This is a paragraph in normal style.</p>
  <p class="italic  overlap">This is a paragraph in italic style.</p>
  <p class="oblique overlap">This is a paragraph in oblique style.</p>
</div>
<hr>
<!-- #3 even zero line-height used, paragraphs dont overlap -->
<div class="overlap">
  <p class="normal ">This is a paragraph in normal style.</p>
  <p class="italic ">This is a paragraph in italic style.</p>
  <p class="oblique">This is a paragraph in oblique style.</p>
</div>
<hr>

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

Tips for choosing the ancestor's ancestor in CSS

<div id="maincontent"> <div id="content"> <div id="admin"></div> </div> </div> Looking for a CSS rule that will target #maincontent only when #admin is present? I have a background color set in the admi ...

Retrieve all hyperlinks from HTML using C programming

Is there a way to extract all URLs from an HTML document using the C standard library? I attempted to use sscanf() for this purpose, but encountered errors when checking with valgrind. I'm unsure if my code will meet the requirements even after succe ...

Can you tell me the name of this specific page arrangement style?

What is the term for the specific layout style used on Asana's homepage? This layout consists of a single, long page divided into sections that are approximately the size of the viewport. While sometimes it includes parallax scrolling, in this case i ...

PHP code to showcase members in a three by three grid

With the use of li elements, I have created a layout consisting of three rows and three columns. Each li is meant to display a member based on their ID. <li> // Style: margin-bottom:10px; Display first member here... </li> ...

Modify the CSS borders to reflect a selected radio input type

I'm attempting to change the color of a Radio Button to blue when it's selected, but I'm having trouble achieving this. Here is the CSS code I'm using: /* Radio Button Style */ .radio { padding-left: 25px; } .radio label { di ...

Utilizing HTML list elements in conjunction with a switch statement, and connecting the list directly to a database for

Hey there, I'm interested in learning how to access HTML elements and use them within switch statements. <div id="hori"> <ul> <li><a href="#">Aerospace</a></li> <li><a href="#">Automotive</a>< ...

Tips for creating a responsive div that contains both an image and description, including automatically resizing the image to fit the

#content { background-color: #ACAE4C; float: right; display: inline-block; padding: 0; } <div id="content"> <div class="pic"></div> <div class="desc"></div> </div> I need assistan ...

Whenever I adjust the layout of the navigation bar, the edges end up getting clipped

I'm having trouble with the border shape of my navbar. When I try to make it a rounded pill shape, the edges get cut off instead of being properly displayed. https://i.stack.imgur.com/sUN2Y.png Below is the HTML template: <template> <div cl ...

A comprehensive guide on personalizing Bootstrap 4 tooltips to suit your specific needs

I would like to customize the tooltip in Bootstrap 4 based on the screenshot provided below: https://i.stack.imgur.com/wg4Wu.jpg <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta chars ...

Is there a way to prevent the virtual keyboard from covering up input fields on a webview?

I am currently dealing with an issue on my Genero web-app where the webview of an Angular app is not scrolling under an input when it gets the focus. This makes it difficult for users to see what they are typing. Interestingly, loading the page in a regula ...

Looking to add tiered custom attribute select boxes in SnipCart - how can I make it happen?

I am interested in implementing two custom attributes for products, where the options for the second custom attribute are determined by the selection of the first attribute. My specific situation involves selling art in various formats and sizes, with dif ...

VueJS displaying broken PNG images after deploying production build

After running the command npm run build, a dist folder is generated containing static assets such as images. The website is using history mode on vue router. Here's the HTML for the first image: https://i.sstatic.net/ZVArw.png Here's a snapsho ...

How can the background of a div be altered when text is typed into an input field?

I need help with the following code. I want to be able to change the background color of a div with the class "target_bg" from red (default) to green every time someone enters or types text into the input field. <div class="target_bg"></div> & ...

Is there a way to turn off step navigation in bootstrap?

Displayed below is a visual representation of the bootstrap step navigation component. Presently, there is an unseen 'next' button located at the bottom of the page. When this 'next' button is pressed, it transitions from 'step-1 ...

Creating AngularJS variables with dependencies on integer values

Hello, I am a brand new developer and I am currently working on creating an expenses calculator. The goal is to have the sum of inputted integers from a table, each with a default value, become the integer value of another variable. However, I seem to be m ...

Show Pictures Side by Side with Captions Below

I need help organizing multiple images in a row with text underneath for my college project. I've been experimenting with some code but haven't had any success yet. <div class="test"> <p> <a href="https://www.facebook.com" ...

Dropdown colorization

I want to create a dropdown menu in my black navbar, but the text color becomes blue and the background of the pop-up turns white, which clashes with my black background. I need it to be transparent while using Bootstrap v4.1.1 <nav className="navbar n ...

Exploring how enums can be utilized to store categories in Angular applications

My application has enums for category names on both the back- and front-end: export enum CategoryEnum { All = 'All', Category1 = 'Category1', Category2 = 'Category2', Category3 = 'Category3', Cate ...

In a multidimensional array, locate the key corresponding to the specified value

I am facing an issue with my code that involves an array containing various fruits with product ID and price as keys for different values. While I am able to retrieve the correct price, I am struggling to get the name of the chosen product. For instance, ...

Struggling to Align Banner in Dynamic Layout

I am currently working on centering the banner image as the resolution size decreases, rather than having it pushed to the right. The specific page I am addressing can be found here. My approach involves utilizing a width of 100%. <!-- BEGIN T ...