Trouble Aligning a CSS3 Canvas Within a Div

I'm facing a dilemma with my HTML code:

<div id=test>
<canvas  id="pic" width="300" height="250"></canvas>
</div>

Whenever I attempt to adjust the width and height using CSS (by removing the width and height attributes from the HTML element) like so:

#pic{
width:300px;
height:250px;
}

The image ends up being stretched - why is this happening?

When I add additional CSS properties to the div, such as:

#test{
float:right;
border:solid red;
border-radius: 10px;
box-shadow: 5px 5px 20px #888;
}

The canvas aligns tightly to the top left corner, and for some reason, the border created around the canvas appears slightly larger than the canvas itself.

I've tried various adjustments with margins and padding to move the canvas inside the "box," but have been unsuccessful in achieving the desired alignment.

Is there a way to center the canvas within the "box" or alternatively reduce the size of the box?

New Addition

To address the initial issue, here's the complete code snippet:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>

      <script>
          function myImage() {
              var context = document.getElementById('pic').getContext('2d');

              var img = new Image();
              img.src = "saturn-400x300.jpg";

              img.onload = function () {

                      context.drawImage(img, 0, 0, 300, 250);
                  }
              
          }

    </script>
    
    <style>
        #pic {
        width:300px;
        height:250px;
        }

    </style>

</head>
   
  
    
<body onload="myImage()">

    <div id="test">
<canvas  id="pic" width="300" height="250"></canvas>
</div>


</body>
</html>

With the current setup utilizing inline attributes, the image appears fine. Here's how it looks:

https://i.sstatic.net/wENX4.jpg

However, upon removing the inline attributes and retaining only the styles, the layout appears like this:

https://i.sstatic.net/KC9HZ.jpg

Regarding the secondary issue, I retained the inline attributes for explanation purposes. I added the following code to the style tag of the div:

#test{
float:right;
border:solid red;
border-radius: 10px;
box-shadow: 5px 5px 20px #888;
}

The gap between the red border and the canvas is what I am trying to minimize.

Answer №1

Consider implementing a reset css file to combat any browser default issues that may be causing display errors. Test your website in multiple browsers to pinpoint and address any inconsistencies.

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

Strategies for avoiding text selection interference with onMouseMove event

I am in the process of adding a "resize handle" to adjust the width of my left navigation panel. This handle, represented by a div, triggers an onMouseDown() event that calculates the necessary widths and applies them to the relevant elements during subseq ...

What is the best way to implement a sliding menu when clicked using HTML and CSS?

In my HTML page, there is a side menu bar displaying a list of dashboards using Django. I want this left-side menu to show only partially when logged in and fully expand after clicking on it. The HTML code snippet for the sidebar looks like: <div cl ...

Achieving a consistent fixed width for tbody even when scrollbar is present

thead{ width: calc(100% - 17px); display:block; } tbody{ display:block; overflow-y: auto; height:100px; } http://jsfiddle.net/mkgBx/ Is there a way to adjust the width of the tbody element to match that of the thead element plus the scrollbar? Currently ...

Stopping Accordion Title from Moving Vertically in Material-UI

Just finished creating this accordion which will be used as a menu item. However, I've encountered an issue where clicking on the Main title causes the accordion summary to move downward vertically. Any suggestions on how to keep the Main tile fixed ...

Creating a layout where three divs are lined up next to each other, each with a height that spans

I am currently designing a website layout and I am working on setting up the CSS rules for the layout as shown in the image below: My goal is to have the left and right divs extend to the height of the content div. However, I am not very familiar with the ...

The arrow icon that I included in my bootstrap project seems to have disappeared from my view

While trying to enhance my footer, I attempted to include an arrow-up-circle icon. However, after adding the code, the icon doesn't appear. Here is the html snippet I used: <html> <head> <link href="https://cd ...

CSS3 Marquee Effect - puzzled

After trying numerous solutions for the marquee effect, I find myself at a dead end. Even webkit examples have failed me, as the demos don't seem to work either. My browser of choice is Chrome, but my website needs to function properly in both Chrome ...

Unexpected changes are observed in the size of DIV elements when adjusting the SVG viewBox

My aim is to make the <svg> element have the same dimensions for the viewBox as the <div> it is inside. This means that the viewBox values need to match the dimensions of the containing <div>. Here is an example: <div style="width ...

Aligning the right menu of Ant Design from the Layout component

How can I align the menu to the right in antd? https://i.sstatic.net/xmh6E.png Please Note: The image has been edited using Photoshop and is not real. I attempted to declare a className = "Menu" and set .Menu {align-items: right;} but it didn't wor ...

"Implementing Bootstrap CSS styles for a pair of specific classes

I am working with a div element that has classes of .col-xs-12 .col-md-4 and .left. My goal is to ensure that when the application is opened on a computer, it uses the CSS styles for .col-md-4 and .left, and when accessed on a smartphone, it only applies t ...

Mastering @media queries to dynamically alter widths in prop styled components

I have a unique component that utilizes an object with its props for styling. const CustomSection = ({ sectionDescription, }) => { return ( <Text {...sectionDescription} content={intl.formatMessage({ id: &apos ...

Modify the color of drop-down menu links

Recently, I set up a drop-down menu containing links. Initially, when hovering over the names in the menu, they would change color and display the drop-down. I had successfully made all the names golden, with the hovering name turning black while display ...

What could be causing certain items in my Bootstrap navbar to be hidden on mobile screens?

When I view my website on mobile devices, only the logo appears in the navbar, not the other elements. Can you help me troubleshoot this issue? NavBar HTML Code: .navbar-logo { width: 93.75px; height: 23.5px; margin-left: 10rem; } .navba ...

What is the best way to ensure that my transitionend event is triggered?

I'm currently exploring the <progress> element and attempting to incorporate a CSS transition to achieve a smooth progress bar effect as its value increases. Despite my efforts, I can't seem to trigger JS after the transitionend event occur ...

Is it possible to customize the color of an SVG image within Next.js using next/image?

import Image from 'next/image' ... <Image src="/emotion.svg" alt="emtion" width={50} height={50} /> I am trying to change the color of the SVG using next/image. However, applying the following CSS rule does not seem ...

What is the most effective method for generating dial images through programming?

Currently, I am in the process of creating a website that makes use of variously sized and styled dials to indicate progress. The filled portion of the dial represents how close an item is to being 100% complete. I am seeking a universal solution that wil ...

I'm trying to display hidden forms on a webpage when a button is clicked using the DojoToolkit, but I'm having trouble figuring out what's going wrong with my code

Currently, I am trying to grasp the concepts of Dojotoolkit and my objective is to display a form when a button is clicked. Upon reviewing other examples, my code seems correct to me; however, there appears to be something crucial that I am overlooking but ...

The div element is not resizing properly when the phone is in landscape mode

I have set a background photo to take up 100% of the height. However, when I view the site on a mobile phone in landscape mode using Chrome or Firefox, the background image does not fill the entire space. In desktop and portrait mobile modes, everything ...

The color of the Angular md-switch .md-thumb is not showing up properly when switching the toggle

In Safari, the md-switch displays a yellow section on the md-thumb when toggled. However, other browsers handle the switch without any issues. The md-bar appears fine, but the md-thumb is yellow. I have tried setting everything to the blue color I am using ...

IE compatibility mode causing ckeditor dropdown to be hidden

When using the CKEditor editor bar inside a green div with another div below it, I noticed that when clicking on "font" or any other option that opens a dropdown menu, it gets hidden behind the bottom div. This issue seems to occur in browsers like Chrome ...