What is the reason behind adding an extra block in overlapping P tags?

I've encountered a puzzling situation in my code. I have two P tags with borders applied to them, leading me to believe that there should be two light blue blocks displayed. However, upon running the code, I am seeing three blocks instead of the expected two. Can someone please explain why this is happening? Any help would be greatly appreciated. Thank you for taking the time to read.

p {
            margin: auto;
            text-align: center;
            background: lightblue;
            height: 70px;
            width: 200px;
            border: solid 1px black;
            line-height: 70px;
       }
<html>
    
              <head>
                <meta charset="utf-8" http-equiv="content-type">
                <style media="screen">
                  p {
                    margin: auto;
                    text-align: center;
                    background: lightblue;
                    height: 70px;
                    width: 200px;
                    border: solid 1px black;
                    line-height: 70px;
                  }
            
                </style>
              </head>
            
              <body>
                <p><p>dddddddddddoo</p></p>
              </body>
            
        </html>

Answer №1

Try using your browser's element inspector to explore the actual DOM structure. Here is what you may find:

<p></p>
<p>dddddddddddoo</p>
<p></p>

An incorrectly nested p within another p tag can lead to unexpected behavior in the rendering of the webpage. The browser may automatically close the first p tag upon encountering the second one, creating an additional p element due to the invalid markup.

Answer №2

You should avoid nesting a <p> tag inside another <p> tag as it is not valid HTML syntax. When the browser encounters this issue,

It may replace your code with:

<p> </p>
<p>dddddddddddoo<p>
<p></p>

Answer №3

substitute

<body>
    <p><p>dddddddddddoo</p></p>
</body>

with

<body>
    <p></p>
    <p>dddddddddddoo</p>
</body>

Answer №4

Remember, <p> elements cannot be nested. The purpose of the <p> element is to represent a paragraph, and it cannot contain any block-level elements (including another <p> tag). When you check your browser's inspection tool, you will notice there are 3 separate paragraphs.

On the other hand, <div> serves as a versatile container where content can either be inline (like with <span>) or block-level. This means that <div> can hold <p>, but not vice versa.

To see this concept in action, take a look at the updated fiddle: https://jsfiddle.net/862afpo5/1/

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

Including a Textbox completely disrupts the Menu design

I have been working on a project involving ASP and C#. Within the project, I am utilizing a masterpage for the navigation bar located at the top of my pages. It appears as follows: https://i.sstatic.net/Qb1P8.png The navigation bar includes standard nav ...

Creating a JavaScript array in Rails 4 based on current data without the need to reload the webpage

Currently in my rails 4 app, I am working on a unique tags validation using jquery validate to ensure that tags are not duplicated before they can be added to an item. The tag list structure is as follows: <div id="taglist"> <span class="label ...

The hyperlink within the dropdown menu in HTML code is malfunctioning

I'm currently working on my personal website using HTML and CSS with textedit. I've successfully created functional links for the main navigation menu headings, but I'm encountering issues with the dropdown options. Whenever I try to click o ...

Tips for using jQuery dropdown menus

I am currently in the process of creating a prototype for a quick dropdown menu using jQuery. However, I have reached the limits of my knowledge on jQuery and could use some assistance in solving my issue. My objective is to have a dropdown animate down w ...

The Carousel feature in Bootstrap 4 is malfunctioning on the website

I've inserted a carousel into my webpage but it's not functioning as intended. I'm attempting to use this carousel code from "codepen.io/decibeldesign/details/bMrQKN" and the full webpage code can be found at "paste.ofcode.org/saJPkuhPPixFmX ...

Utilize Python Selenium to interact with Cookie-Consent pop-ups

Attempting to scrape a webpage using Python and selenium, but encountering difficulty when trying to click the "OK" Button for cookie consent as it cannot be located: View Picture of Consent Dialog Visit Website Here This locator works: driver.find_eleme ...

Rules of HTML tables extend to the border size

Check out this HTML code snippet: div { border: 2px solid red; height: 50vh; width: 50vw; } table { border: 1px solid; table-layout: fixed; } <div> <table rules="cols"> <tr> <th>Name</th> < ...

Ways to emphasize the current tab on the site's navigation bar?

Here's my question: I have a menu with different items, and I want to make sure that the active tab is highlighted when users switch to another page. I noticed that Stack Overflow uses the following CSS: .nav { float: left; font-size: 1 ...

Tips for inserting a line break in a script

Hello, I need some assistance with creating a typewriter effect using JS, CSS, and HTML. Everything seems to be working fine except when I try to add a new line of text, it doesn't display properly. var str = "<p>I want to put text here then ...

Using jquery/offset to position a div causes it to float, but it does not take

<script type="text/javascript"> $(document).ready(function () { var top = $('#rt_outer').offset().top - parseFloat($('#rt_outer').css('marginTop').replace(/auto/, 0)); $(window).scroll(function (event) { // det ...

Adjusting the header and unordered list on resize

I need help with my portfolio website. I can't seem to figure out how to make my header and navigation menu stack on top of each other when the page is small, and then go back to their normal layout when the page is larger. Below is the HTML code I&a ...

What is the best way to add an image to a SVG placeholder?

This is all unfamiliar territory for me. I am experimenting with the Bootstrap template named "Carousel" as my starting point, utilizing Bootstrap 5.3.2. Here is the link to the Carousel example Everything seems fine so far. However, I am struggling to l ...

Issue arises when applying both overflow-x:scroll and justify-content:center

Encountering a problem with using overflow-x: scroll and justify-content: center on a flex parent container. Here is my code snippet: The issue: the first flex child item is not visible as it is cropped on the left side. Please refer to the screenshot and ...

Preloading and rendering an image onto a canvas in a Vue single-page application is not functioning as expected

I am working on a Vue 3 SPA where I am manipulating canvas elements. To preload my image, I am using the function provided below: async preloadLogo () { return new Promise( (resolve) => { var logo_img_temp = new Image(); const logo_s ...

How come only the individual top, bottom, left and right paddings function correctly while the combined padding does not respond?

Seeking to customize a button using SCSS file that is connected to HTML. This button is part of a flex layout design. The element's size is set at 83.333×16px. The box-sizing property is defined as: box-sizing: border-box; Adding padding with s ...

Can a button be connected to both navigate to another webpage and trigger a modal to appear on it simultaneously?

Is there a way to connect a button to a modal on a different page so that when the button is clicked, it opens another page (in a new tab with target 0) with the modal already displayed? ...

Does the Fileupload jQuery event function differently when uploading from a mobile device compared to a desktop computer?

I am encountering an issue with this jQuery function. It works perfectly when I upload a file from my desktop computer, but it doesn't seem to fire when I try uploading a file from my mobile phone. Can anyone identify what might be causing this proble ...

Added the .active state to the menu navigation successfully, however the active color was not implemented

My primary navigation menu is managed by WordPress and can be seen below. I aimed to modify the active state when a user clicks on a link in the menu by adding background-color and color for styling purposes. Although changing the background-color upon cl ...

Admin template system for CodeIgniter

In order to give the Administrator the ability to customize the Admin Dashboard Layout and provide an option for them to upload their own layouts or "Premium" layouts, I need to implement a solution. I have considered one approach: Utilizing CSS (allowin ...

The Bootstrap Hugo Documentation website is experiencing difficulties and displaying the error message "failure to render 'page'"

Currently, I have bootstraps' source code downloaded on my Mac along with all the necessary node packages installed. Upon attempting to run hugo --cleanDestinationDir --printUnusedTemplates (hugo is properly installed), I encountered the following err ...