The evolution of HTML5 has brought significant advancements in the realm of CSS positioning,

When incorporating relative positioning in CSS, I have observed significant variations in behavior based on the presence of the HTML5 doctype header <!DOCTYPE HTML>. A simple example to illustrate this point is as follows:

<html>
<body>

<img src="test.png" />
<span style="position: relative; top: -10;">TEST</span>

</body>
</html>

This code will display the word TEST positioned 10 pixels higher than its default location. However, once the <!DOCTYPE HTML> declaration is included at the beginning of the document without any other modifications:

<!DOCTYPE HTML>
<html>
<body>

<img src="test.png" />
<span style="position: relative; top: -10;">TEST</span>

</body>
</html>

The relative positioning no longer affects the placement of the word TEST. This inconsistency has been observed across multiple browsers like IE, Chrome, and Firefox on Windows. Furthermore, similar quirky behaviors arise when utilizing absolute positioning with or without the HTML5 doctype header. The question that arises is whether there has been a fundamental shift in how relative and absolute positioning are executed in HTML5.

Answer №1

The concept being discussed here is unrelated to HTML5; it actually pertains to the distinguish quirks mode and standards mode, both of which have been present for a considerable amount of time.

Quirks mode can be activated by various triggers, as demonstrated in your query by the absence of a doctype declaration entirely. In quirks mode, unitless lengths are permissible (and interpreted as pixel lengths), explaining why your relative positioning functions as intended.

In contrast, in standards mode, unitless lengths are not allowed (unless they are zero), causing your top declaration to become invalid CSS and consequently ignored. This principle applies regardless of whether you are utilizing the HTML5 doctype or an HTML 4 or XHTML doctype. Essentially, the HTML5 doctype does not hold any specific significance — its creation was solely aimed at prompting browsers to display content in standards mode.

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

Issue with displaying jQuery 3.5.1 mobile CSS formatting on live server preview using Visual Studio Code

Despite my best efforts, I can't seem to make the jQuery CSS stylesheet work properly. I've been using the live preview extension in VSCode and got the CSS directly from the jQuery website. <!DOCTYPE html> <html> <head> < ...

Transform a three-column layout using floats into a single column layout using a media query in Bootstrap 4

I have integrated Bootstrap 4 into my project along with my custom CSS. In the center of the screen, there is a price sheet displaying three pricing options. I am currently using floats and clears, but the columns are not collapsing as intended at 962px. I ...

Removal of div elements based on checkbox being unchecked - a step-by-step guide

How can I delete elements from a div when the checkbox is unchecked? function removeCampaignName(id) { var campaignDisp = $('#camp' + id).html(); if ($("#campaign-name-disp").text().search("" + campaignDisp) === -1) { ...

I am looking for a method to determine the specific button being clicked while all the buttons are dynamically created through php

I am using php to create multiple divs with the same format but different content. My problem is determining which div a button press belongs to. I need to identify the $row["bar_name"] associated with the pressed button (e.g. if Monday is pressed, I nee ...

Steps for setting a background image for a div

I am facing an issue with 3 horizontally aligned images and a background image. The problem is that when I set the background image for the aligned images, it flows over them instead of staying behind. I want the 3 aligned images to be on top of the backgr ...

HTML TABS: Make the first TAB automatically selected

I've been experimenting with tabbing in HTML using a tutorial I found on W3SCHOOLS. While the source code provided works fine, I'm encountering an issue with automatically selecting the first tab by default. The tutorial doesn't cover this, ...

The PHP code encountered a parsing error due to an unexpected end of file

I encountered an issue: Parse error: syntax error, unexpected end of file in the line This is the code that triggered the error: <html> <?php function login() { // Code for login function } if (lo ...

What is the best way to style an element with two classes in SCSS?

I've always understood that when targeting a div with two classes in SCSS, you should structure it like this: HTML: <div class="item active">...</div> SCSS: .item { &.active { /* add your code here */ } } But what about whe ...

Selectors that can be applied to multiple IDs with identical functions

I have a script for popups that I want to be able to use multiple instances of. For example, I would like to have 5 popups on one page without having to change the JavaScript code. While everything in the script works fine, I am struggling to make it work ...

Tips for removing yellow box decorations

I've noticed a yellow outline appearing on many of my input boxes when I select them. How can I remove this? Css lint is reminding me that: *:focus { outline: none; } should not be used due to outlines should not be hidden unless other visual chang ...

Issue with Bootstrap Nav Pills only functioning properly for the initial option

When using Bootstrap nav pills, only the first one seems to work properly in my case. Even though I have inserted forms for each nav pill, it only shows a blank page. https://i.sstatic.net/QsL1l.png https://i.sstatic.net/Swmtq.png I have PHP code inside t ...

Is there a way to locate an element within innerHTML using getElementById?

Is it possible to achieve the following code snippet? <div id="parent"> <iframe id="myFrame" title="HEY!" srcdoc="<div id='inner'>Hello World!</div>"></iframe> </div> v ...

Adapt to screen size changes by transforming a single row into two separate rows

I am trying to create a two-column row with inputs: <div class="row first-row"> <div class="column col-6"> <div class="form-group"> <label for="usr" >Se ...

I'm looking to replicate the hover effect used on a specific section of the Land Rover website - any tips on how to achieve this

I attempted to modify the cursor behavior on hover, but it only changes in one direction. I am aiming for a similar effect to what is seen on the Land Rover website. ...

Eliminate screen flickering during initial page load

I've been developing a static website using NuxtJS where users can choose between dark mode and default CSS media query selectors. Here is the code snippet for achieving this: <template> <div class="container"> <vertical-nav /> ...

The form-control class in HTML prohibits the hiding of textboxes

The CSS class form-control is causing issues with hiding a textbox in HTML, I attempted the following: <input type="number" name="mfi_nam9" class="text1 form-control" id="mfi_name" hidden> However, it only works when I remove the form-control class ...

Navigate through PNG image - proceed only if the clicked area is transparent

Imagine having an image that looks like this: https://i.sstatic.net/Ab6aW.jpg I am looking to make it so that when I click on the transparent areas of the image, the click should pass through to the element below. However, if I click on a non-transparent ...

Create dynamic web pages using Angular's capability to render HTML, CSS, and

I've encountered an issue while trying to integrate a code snippet received from a post API response into an Angular HTML file. The code works perfectly fine when pasted in the HTML section of Codepen, as shown in the screenshot here. However, when at ...

Instructions for crafting a CSS styling directive for image elements contained within figure elements

Apologies for the simplicity of my inquiry, but despite searching online, I have yet to find a clear explanation. I am struggling to understand how to create a style rule specifically for img elements within figure elements in HTML5 and CSS. I am new to l ...

Having Trouble with jQuery toggleClass

I'm currently working on a project where I have implemented a button that displays a menu when clicked. I am attempting to change the color of the button when it is active, however, the toggleClass function is not behaving as expected. Below is the co ...