The combination of absolute positioning and a canvas

Can someone help me understand why the span is positioned at the bottom of the root span instead of at the top? Here's a link to my Plunker:

/* Styles go here */

span {
  background-color :#808080;
}

canvas {
  background-color:#800000;
}
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <span style="position:relative">
      <canvas width="100" height="100">
      </canvas>
      <span style="position:absolute; left:0px; top:0px;">after</span>
    </span>
  </body>

</html>

I was expecting the "after" string to appear at the top of the dark-red area, not at the bottom. I've set the outer span's position to relative, which should act as the anchor for the child elements with absolute positioning.

The inner span is set to position absolute with top=0px, left=0px; so it should be placed at the upper left corner of the outer span.

However, the outer span doesn't seem to enclose the canvas, which I find confusing. Could someone explain why this is happening? This might be the reason why the inner span is not positioned at the top left corner as expected.

Answer №1

This issue arises because the canvas is contained within a span element that has a display:inline property, causing it (the span) to not adjust its size according to the canvas. As a result, the canvas overflows the span.

To illustrate the problem, consider the following:

/* CSS styles */

span {
  background-color: #808080;
  border: 1px solid black;
}

canvas {
  background-color: rgba(100, 0, 0, 0.5);
}
<span style="position:relative;">
      <canvas width="100" height="100">
      </canvas>
      <span style="position:absolute; left:0px; top:0px;">after</span>
    </span>

Resolving this issue involves changing the outer span element to have a display:inline-block property, which effectively resolves the problem.

Take a look at the updated solution below:

/* CSS styles */

span {
  background-color: #808080;
  border: 1px solid black;
}

canvas {
  background-color: rgba(100, 0, 0, 0.5);
  display: block;
}
<span style="position:relative;display:inline-block;">
      <canvas width="100" height="100">
      </canvas>
      <span style="position:absolute; left:0px; top:0px;">after</span>
    </span>

Answer №2

The issue arises because you have placed a block element (canvas) inside an inline element (span). This causes the span element not to stretch to accommodate its block content. To resolve this, simply add display: inline-block to your span element.

It is worth noting that although this solution will work, nesting block-level elements inside inline elements is not considered valid according to W3C standards. It might be a good idea to replace the span with a div instead.

Additionally, when setting values to 0, remember not to add units. For example, use left: 0; top: 0; instead of left: 0px; top: 0px;

Here is the corrected example: https://plnkr.co/edit/MAfzT9A3uJwHAN8YtpKF?p=preview

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

pop-up window that shows chosen choices using javascript or ajax

I have a specific HTML code that allows users to select multiple options. I would like these selected options to be displayed in a popup or a div in real-time as the user makes their selections. I have attempted using a selectbox with checkboxes, but extra ...

adjust the width of an element based on the size of characters within it

I need to ensure that the width of a div is equivalent to 10 characters If one character is equal to 2px, then I want the width to be 20px, even if the text is only 4 characters long. <div class="row"> <div class="key">City</div> ...

What could possibly be causing the 500 server error in PHP?

It's frustrating - every time a user tries to enter their email and password, an error 500 message pops up on the server! But strangely enough, upon refreshing the page, everything seems to work just fine... So why does the server throw an error on th ...

Adding the BETA symbol to your Bootstrap navbar is simple and can be done by

Is there a way to include a "BETA" symbol at the top of the "Brand Name" on my Navbar, similar to the "TM" symbol? How can this be achieved? ...

Using TypeScript, apply an event to every element within an array of elements through iteration

I have written the code snippet below, however I am encountering an issue where every element alerts the index of the last iteration. For instance, if there are 24 items in the elements array, each element will alert "Changed row 23" on change. I underst ...

Unable to locate the value property of a null object

I'm currently working on creating a Shopping Cart using HTML, CSS, JavaScript, and JQuery. The idea is that when you click "Add to Cart" for the orange item, most of the HTML elements will disappear, leaving only the table displaying the Shopping Cart ...

Retrieving xml data using ajax

Hey there, I'm working with an HTML file that looks like this: <!DOCTYPE html> <html> <style> table, th, td { border: 1px solid black; border-collapse: collapse; } th, td { padding: 5px; } </style> & ...

Troubleshooting the issue with the htmlFor attribute

I have encountered an issue with creating radio buttons and labels using JavaScript. Despite adding the 'for' attribute in the label using 'htmlFor', it does not apply to the actual DOM Element. This results in the label not selecting t ...

Expanding Two HTML Elements to Fill the Screen Width Using HTML/CSS

This is the current setup: The screenshot spans the width of my screen. The image is aligned to the left side, while the Level information section remains fixed to the right of the image. This layout meets my requirements. However, I would like the border ...

Why is my md-button in Angular Material displaying as full-width?

Just a quick question, I created a simple page to experiment with Angular Material. On medium-sized devices, there is a button that toggles the side navigation, but for some reason, it expands to full width. There's no CSS or Material directives causi ...

Is it possible to ensure uniformity in the appearance of all images with MVC Image Handling?

I'm currently developing an MVC 4 application that allows users to upload images. These uploaded images are then displayed in a different view. Users can upload images up to 10MB in size, which are then resized to 800 x ? pixels using the WebImage (Sy ...

The Jquery watermark extension in IE9 is capable of transmitting the watermark as the primary value of the form

I have integrated a plugin into my project. The plugin can be downloaded from this link. However, I am facing an issue where the watermark value is being submitted as the textbox's value in Internet Explorer 9. How can I resolve this problem? Intere ...

Ways to conceal HTML tags within a text box

Currently, I am utilizing PHP to extract content from a text file and display it in a textbox. However, I am interested in finding a way to conceal the HTML tags within the textbox (as shown in the image) without deleting them, making them invisible to use ...

Avoiding the parsing of JSON strings in JavaScript

var data = Sheet : [ {"Cell_Address":"A1","Cell_Type":"1","Cell_Value":"Name"}, {"Cell_Address":"B1","Cell_Type":"1","Cell_Value":"Phone no."}, {"Cell_Address":"C1","Cell_Type":"1","Cell_Value":"Currency"}, {"Cell_Address":"D1","Cell_Type":"1","Cell_Value ...

Issue with Jquery Date picker functionality on Master Page not functioning as expected

My issue is with the master page <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- ...

Positioning CSS for a Responsive Button

Currently, I am working on making my modal responsive. However, I am encountering an issue with the positioning of the "SAVE" button. The button is not staying in the desired position but instead disappears. Below is the snippet of my HTML and CSS: .dele ...

What is the best way to connect my 'Projects' folder to app.js within a React application?

import "bootstrap/dist/css/bootstrap.min.css"; import Navbar from './components/Navbar'; import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import Home from './components/Home'; import Project ...

Steps for generating a unique division element for every javascript response

How can I dynamically create a new div for each response in JavaScript? The message is in JSON format containing all the messages sent and received. This is my JavaScript code: $.get("MessageServlet", function (responseJson) { $.each(responseJ ...

Creating a virtual roulette wheel with JavaScript

I'm currently working on creating a roulette wheel using JavaScript. While researching, I came across this example: , but I wasn't satisfied with the aesthetics. Considering that my roulette will only have a few options, I was thinking of using ...

how to position one div above another using css

I currently have a page layout with 7 different div elements structured like this; <div id="container"> <div id="head"></div> <div id="A"> <div id="A1">A1</div> <div id="A2"></div> ...