Why is my D3 map not positioning at the center of the page?

Having trouble with centering my D3 map on the webpage. The CSS doesn't seem to apply correctly even though I've tried adding margins to the map class.

I'm not sure why the applied CSS isn't working as expected. Could there be something missing in the D3 code that needs to be adjusted? Any help or guidance would be appreciated. Thank you!

Below is the code snippet:

<!DOCTYPE html>
<html>

<head>
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.1/d3-tip.js'></script>
  <meta charset='utf-8'>
  <meta name="viewport" content='width=device-width, initial-scale=1.0'>
  <title></title>
  <style>
    .d3-tip {
      line-height: 1;
      font-weight: bold;
      padding: 10px;
      background: rgba(0, 0, 0, 0.8);
      color: #fff;
      border-radius: 10px;
    }

    .active {
      fill: rgba(149, 165, 166, 0.8);
    }

    body {
      background: rgba(32, 32, 32, 1);
      color: rgba(255, 255, 255, 0.8);
    }

    h1,
    h2,
    p {
      font-family: "Arial Black", Gadget, sans-serif;
      text-align: center;
    }

    #box {
      border: 10px solid black;
      margin: auto;
      padding: 10px;
      width: 75%;
      border-radius: 10px;
    }

    .map {
      margin-left: auto;
      margin-right: auto;
    }
  </style>
  <script>
    function draw(geo_data) {

      var margin = 0,
        width = 2000 - margin,
        height = 700 - margin;

      var centered;

      var svg = d3.select('#map')
        .append('svg')
        .attr('width', width + margin)
        .attr('height', height + margin)
        .append('g')
        .attr('class', 'map');

      /* Rest of the D3 code follows after this */

    };
  </script>
</head>

<body>

  <div id='box'>
  </div>
  <div id='map'>
    <h2>Hover your mouse to see data on the cities and click a state to zoom in.</h2>
  </div>
  <div id='box'>
  </div>
  <script>
    d3.json('https://raw.githubusercontent.com/dieterholger/US-Gun-Manufacturing-Interactive/master/us_states.json', draw);
  </script>
</body>

Answer №1

Wrap the map inside another container div that matches the width of the box above it (75%). Consider moving the inline styles to a separate CSS class for better organization.

<div style="display:block; margin-left:auto; margin-right:auto; width:75%">
    <div id="map">
    ...
    </div>
</div>

UPDATE

The issue was identified - the css definition for the map element should have an ID selector, not a class selector. Additionally, if using auto margins, a width needs to be specified.

Revise your CSS as shown below:

#map {
  width: 75%;
  margin-left: auto;
  margin-right: auto;
} 

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

How to keep Vuetify component at the top of the v-layout?

https://i.stack.imgur.com/cKdsk.png I am in the process of creating a website using vuetify and nuxt. My goal is to specify the max-width property for the expansion panel UI component (https://vuetifyjs.com/en/components/expansion-panels). Here is my curr ...

Determine the vertical dimension of a child division once it has been integrated into an HTML document

My goal is to create a website with multiple pages without having to recreate the toolbar for each page. To achieve this, I have created a separate HTML file and CSS file specifically for the toolbar. On each page, I simply import the toolbar using the fo ...

Utilize a CSS selector to target all deleted nodes using the MutationObserver in the HTML5 API

Is there a way to retrieve all removed nodes from the DOM that have specific classes using CSS selectors? Below is an example of how it can be done with some complexity, but is there a simpler and more general approach? new MutationObserver(mut =& ...

Toggle between bold and original font styles with Javascript buttons

I am looking to create a button that toggles the text in a text area between bold and its previous state. This button should be able to switch back and forth with one click. function toggleTextBold() { var isBold = false; if (isBold) { // Code t ...

Develop a dynamic button using JavaScript to fetch information from an array

I'm struggling with incorporating this button creation code into my HTML using JavaScript. The code for creating the button element in JS file looks like this: var numery = ['A','B','C']; var numer = document.createE ...

Creating a Navigation Bar using the Flexbox property

As a beginner, I attempted to create a navbar using flex but did not achieve the desired outcome. My goal is to have: Logo Home About Services Contact * { margin: 0; padding: 0; font-family: ...

The children elements inside a parent div with Flexbox are now taking on the height of their container

I am facing an issue with my flexbox grid layout where one column contains a tall image while another column has two shorter images stacked on top of each other. The problem arises when viewing the layout in Internet Explorer, as the height of the smaller ...

Problem encountered when trying to apply background opacity to custom colors using Tailwind CSS and shadcn/ui

While using Tailwind CSS along with the shadcn/ui library for styling buttons, I have encountered an unexpected behavior. The issue arises when trying to add background opacity to a custom color defined in globals.css using HSL values such as: --primary: 3 ...

Enhance user interactivity on your website by incorporating jQuery and CSS for

<table id="tab"> <tr aaa="one" bbb="ooo"><td>xxx</td><</tr> <tr aaa="two" bbb="one"><td>xxx</td><</tr> <tr aaa="three" bbb="one"><td>xxx</td><</tr> ...

Displaying a dynamic progress bar across all elements in fullscreen mode

I am looking to implement a full-screen indeterminate progress bar that overlays all screen elements. Here is my specific use case: When a user fills out a form, including an email field, the email id is checked against a database via ajax. While waiting ...

"Utilizing a background image within a component to completely cover the entirety

Hey everyone, I need some help with a confusing issue. I'm currently working on an AngularJs project and I am struggling to set a background image for a specific component without affecting the entire application. My goal is to have the image cover t ...

Align the input element perfectly in both horizontal and vertical positions

I've been trying to center the <input> element within a black <div>, but my attempts have not been successful. Can you tell me what I'm doing incorrectly? html, body { margin: 0; padding: 0; height: 100%; } header { backgro ...

Lowest value malfunctioning

I have encountered an issue with setting minimum values for 4 things. All 4 things are supposed to have the same minimum value as the first thing, but when moving the slider, everything goes back to normal and works fine. Where could the problem be origina ...

Having trouble getting the carousel slider to function properly on meteor?

I am currently working on my portfolio page using Meteor, and I want to include a carousel slider gallery. The problem I'm facing is that while the first gallery works fine, the second one does not load correctly and just displays a list of pictures. ...

Tips for entering multiple values in an input field

I am attempting to implement a feature where multiple names can be added from an autocomplete drop-down menu to an input field, similar to the example shown below: https://i.sstatic.net/D4hGA.jpg Here is what I aim to create: Upon selecting an item from ...

Animating UL/LI elements using the power of CSS3 transformations

I am interested in creating animations using CSS3 transitions. Let's consider the following UL/LI element: <ul> <li class="green" id="green" style="display: none;">Contents 1</li> <li class="red" id="red" style="display: ...

Customizing popups and tooltips in Material-UI charts

Has anyone had success with customizing the appearance of the popover/tooltip in mui charts? It seems like it should be a simple task, but despite searching extensively, I have only found information on formatting data and nothing about styling the CSS. ...

Jssor's dynamic slider brings a touch of sophistication to preview upcoming images

Incorporating the jssor nearby slider to create a nearly fullscreen display. The goal is to set the opacity of upcoming images to 0.25 when they are not in the main viewport, giving the edges of the upcoming and previous slides a slight transparency. < ...

What is the best way to add multiple classes to an HTML element?

Can a single HTML container have more than one class assigned to it? For example, would the following code work: <article class="column wrapper"> ...

Conceal the scrollable content beneath a stationary transparent header, allowing the background to

Imagine having a background image, fixed header with transparent areas, a content div with semi-transparent background, and dynamic height in a traditional header/content/footer layout. The desired effect is to have the background and content scroll under ...