How can I get my Chart.JS to center within a div?

B. Clay Shannon

I'm creating a pie chart using Chart.JS like so:

<canvas id="top10ItemsChart" width="320" height="320"></canvas>

..and then, of course, providing it data and such. But it displays in the div wherein it is ensconced aligned to the left. I prefer it centered (more-or-less, anyway) within the div. I tried all of the following, but none of them make it budge; it stays magnetized to the left side.

<canvas id="top10ItemsChart" margin-left="80px" width="320" height="320"></canvas>

<canvas id="top10ItemsChart" padding-left="80px" width="320" height="320"></canvas>

<canvas id="top10ItemsChart" left="80px" width="320" height="320"></canvas>

A centering would be perfect, but I would be satisfied with providing it a hard value, as I try to do above.

The entire html is:

<div class="col-md-6">
    <div class="topleft">
        <h2 class="sectiontext">Top 10 Items</h2>
        <br/>
        <canvas id="top10ItemsChart" width="320" height="320"></canvas>
    </div>
</div>
Howzieky

There are two things here that are important to not mix up: HTML Attributes, and CSS tags/properties. An attribute is the class="topleft" part of <div class="topleft"> (you can see the full list here). A CSS tag is what holds things like display: block;. You can edit CSS tags with an HTML attribute, so if you want to set the margin for something, you would create an attribute called 'style' and put the css tag inside, like <div style="margin: 10%;">. You were trying to use attributes to edit CSS tags, which is why you couldn't change padding or margins. There is some overlap between css tags and html attributes, such as height and width, which is why you could still edit the height and width with attributes.

For your needs, I would definitely use CSS. From what I understand, you want .topleft (css selector for anything with class="topleft", in case you are new to this) to be stuck to the left side of .col-md-6, and you want everything inside of .topleft to be centered. If that is the case, then give .topleft the tags text-align:center; to set its contents to be centered, and float:left; to keep .topleft floating to the left of .col-md-6. Also give .col-md-6 the tag display:inline-block; to keep things looking nice.

<div class="col-md-6" style="display:inline-block">
  <div class="topleft" style="text-align:center; float:left;">
    <h2 class="sectiontext">Top 10 Items</h2>
    <br/>
    <canvas id="top10ItemsChart" width="320" height="320"></canvas>
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make my text be vertically aligned within a DIV?

From Dev

How can I center an ActivityIndicator within the screen?

From Dev

How can I make two of my lines in Chart JS thicker

From Dev

How can I get everything within a div using Nokogiri?

From Dev

How can I get everything within a div using Nokogiri?

From Dev

How can I center a div inside a div?

From Dev

I can't get div to center in HTML

From Dev

How can I center my ul li list inside my div?

From Dev

How can I make my div's svg background image scale dynamically and stay in the center of the div

From Dev

How can I stop my center div from changing position after I SlideUp another divs?

From Dev

How can I stop my center div from changing position after I SlideUp another divs?

From Dev

How can I center my button on radiogroup?

From Dev

How can i center these images on my screen

From Dev

How can I center this datepicker in my form?

From Dev

How can i center an inner div?

From Dev

How can I center vertically a div in bootstrap?

From Dev

How can i center an inner div?

From Dev

How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?

From Dev

How do I center this horizontal list within a fixed div?

From Dev

How do I center an element within a div with another element nearby?

From Dev

How can I vertically center all children of my div using css?

From Dev

How can i center the slideshow in my website to be in the center of the screen?

From Dev

I can't align the div to the center of my webpage

From Dev

Google Pie Chart: How can I put the tooltip in front of my central div

From Dev

Center type accurately within my headline div

From Dev

How can I make my div spawn within 400 px of either side using jquery?

From Dev

How can I center individual images within their own background boxes?

From Dev

How can I center individual images within their own background boxes?

From Dev

How can I center the text within a <p> tag vertically?

Related Related

  1. 1

    How can I make my text be vertically aligned within a DIV?

  2. 2

    How can I center an ActivityIndicator within the screen?

  3. 3

    How can I make two of my lines in Chart JS thicker

  4. 4

    How can I get everything within a div using Nokogiri?

  5. 5

    How can I get everything within a div using Nokogiri?

  6. 6

    How can I center a div inside a div?

  7. 7

    I can't get div to center in HTML

  8. 8

    How can I center my ul li list inside my div?

  9. 9

    How can I make my div's svg background image scale dynamically and stay in the center of the div

  10. 10

    How can I stop my center div from changing position after I SlideUp another divs?

  11. 11

    How can I stop my center div from changing position after I SlideUp another divs?

  12. 12

    How can I center my button on radiogroup?

  13. 13

    How can i center these images on my screen

  14. 14

    How can I center this datepicker in my form?

  15. 15

    How can i center an inner div?

  16. 16

    How can I center vertically a div in bootstrap?

  17. 17

    How can i center an inner div?

  18. 18

    How can I get my Chart.JS bar chart to stack two data values together on each bar, and print a calculated value on each bar?

  19. 19

    How do I center this horizontal list within a fixed div?

  20. 20

    How do I center an element within a div with another element nearby?

  21. 21

    How can I vertically center all children of my div using css?

  22. 22

    How can i center the slideshow in my website to be in the center of the screen?

  23. 23

    I can't align the div to the center of my webpage

  24. 24

    Google Pie Chart: How can I put the tooltip in front of my central div

  25. 25

    Center type accurately within my headline div

  26. 26

    How can I make my div spawn within 400 px of either side using jquery?

  27. 27

    How can I center individual images within their own background boxes?

  28. 28

    How can I center individual images within their own background boxes?

  29. 29

    How can I center the text within a <p> tag vertically?

HotTag

Archive