D3: Incorporating elegance and sophistication to DIV elements leads to the styles being omitted

I'm trying to create a windows-like interface for my d3-based project, but I've encountered a strange issue. When I add the class attribute to a DIV element and then attempt to control the window position using left and right, they seem to be ignored by d3 and the style tag becomes empty for the window DIV.

I need a universal function that can add and control window positions.

Here is a link to the working fiddle

Below is the code snippet:

var addWindow = function(parent, aClass, x, y, width, height) {
    var aWindow = d3.select(parent)
        .append("div")
        .attr("class", aClass)
        .style("top", y)
        .style("left", x)
        .style("width", width)
        .style("height", height);

    aWindow.append("div")
        .attr("class", "window-header-3d")
        .text("List");

    return aWindow;
}

persons_listbox = addWindow(".dia_body", "window-3d", 30, 30, 200, 300);

//persons list
persons_list = persons_listbox.append("ul").attr("class", "window-list-3d")

persons_list.append("li").attr("class", "window-item-3d").text("11111");
persons_list.append("li").attr("class", "window-item-3d").text("12111");
persons_list.append("li").attr("class", "window-item-3d").text("13111");
body {
  background-color:rgba(50,50,50,1);   
}

.window-header-3d {
  width:100%;
  height:30px;
  border-radius:5px;
  background-color:rgba(250,250,250,1);
  z-index:1000;
  position:relative;
  padding-top:5px;
  padding-bottom:5px;
  text-align:center;
  margin-bottom:10px;
  border-bottom-right-radius: 0px;
  border-bottom-left-radius: 0px;
}

.window-item-3d {
  /*background-color: rgba(255, 255, 255, 1);*/
}

.window-list-3d {
  list-style: none;
  overflow-y: scroll;
  padding-left: 10px;
}

.window-3d {
  position:absolute;
  border:1px #ddd solid;
  border-radius:5px;
  background-color:rgba(255,255,255,0.5);
  z-index:1000;
}
<script src="https://d3js.org/d3.v3.min.js"></script>
<div class="dia_body"></div>

The solution was simple - I just forgot to add px for values. The updated fiddle demonstrates how it works now.

Answer №1

Don't overlook the "px" in your code:

Let's fix it by adding the missing "px":
var newDiv = d3.select(parent)
            .append("div")
            .attr("class",newClass)
            // <div style="top:30px; left:40px; width:50px; height:50px;"></div>
            .style("top",y + "px")
            .style("left",x + "px")
            .style("width",width + "px")
            .style("height",height + "px");

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

What could be the reason for the incorrect parsing of this JSON data?

Issue arising due to unparseable JSON in JavaScript and jQuery API. Here is the code snippet: JSON parsing issue in JavaScript: // Code sample: alert(data); // output is an object alert(data.horas[0].hora; // returns undefined The JSON structure: {"hor ...

Error encountered during AJAX POST request: NETWORK_ERR code XMLHttpRequest Exception 101 was raised while using an Android device

Here is the ajax post code that I am using: $.ajax({ type: "POST", url: "http://sampleurl", data: { 'email':$('#email').val(), 'password':$('#password').val(), }, cache: false, ...

Bootstrap 4 Nav Table of Contents with Expand and Collapse Feature

Currently, I am encountering an issue with implementing a button to expand and collapse a "table of contents" in Bootstrap 4. The code I have so far can be viewed here: https://codepen.io/nht910/pen/RwwwyKB Code Snippet: <div class="main-wrapper col- ...

What kind of influence will JQuery have on the world of Web design and

With the rising popularity of JQuery in the tech world, it's no surprise that there are currently 8,470 questions tagged with JQuery on Stack Overflow. As the 10th most popular topic on the site, it's clear that JQuery is a force to be reckoned w ...

Testing properties of a rendered SVG component in React with Mocha

My current task involves writing unit tests for a method that is responsible for rendering two distinct SVG <line/> elements, which are commonly referred to as 'wicks'. The input parameters for this method include: {x1, x2, y1, y2}. This pa ...

Is there a way to position a ListItem (using React) in the lower left corner in this specific case?

import * as React from 'react'; import { styled, useTheme } from '@mui/material/styles'; import Box from '@mui/material/Box'; import MuiDrawer from '@mui/material/Drawer'; import MuiAppBar from '@mui/material/Ap ...

Troubleshooting Vue 3: Dealing with Synchronization Issues Between Keyboard Input and

I am currently working on a Vue 3 autocomplete component and I've come across a strange issue that I can't quite figure out. The component emits two events: "update:search" to update the search variable in the parent component, and "change" whic ...

Issue with text editor saving random html tags causing disruption to the website's layout

In my ASP.net application, I have implemented a text editor that allows users to upload articles on my site. However, I am experiencing an issue where if the author copies HTML text and pastes it into the editor, it disrupts the overall content of the si ...

AngularJS error: Cannot find provider for $scopeProvider due to $scope not being injected in the service

I've come across a puzzling issue while working with AngularJS and dependency injection that seems to be common among many developers. When I attempt to inject a service into a controller in my AngularJS app, I encounter an error stating: Unknown prov ...

What are the steps to resolve the DOMException error "Failed to execute 'open' on 'XMLHttpRequest': Invalid URL" in Angular HttpClient when making requests to a third-party server or API?

I am encountering the following issue: DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL while trying to perform a GET request to debounce.com import{HttpClient} from '@angular/common/http'; export {T ...

How can you make nested JSON values optional in Joi Validation?

As I work on my API, I encounter a nested JSON structure that serves as the payload for the endpoint. Here is an example of what it looks like: {"item_id":"1245", "item_name":"asdffd", "item_Code":"1244", "attributes":[{"id":"it1","value":"1"},{"id":"it2" ...

How can I restrict validation to trigger only when a field loses focus?

Is there a way to validate the input tag for time format 00:00:00 after exiting the field, rather than during user typing? Any suggestions on how to accomplish this? <label for="operativeToTime">Operative to time</label> <input name ...

`Great issue with parsing JSON using JQuery`

Check out this function: $(".menu_tree img.edit").click(function() { id = this.id; lang = '<?=$lang_id?>'; var body_width = $("body").width(); var body_height = $("body").height(); $("#shadow").w ...

Decoding "multipart/form-data" in .NET using C#

Have a question regarding .NET/C#? I am looking for a way to parse input post data in "multipart/form-data" format to extract the username and password without having to write my own parsing code. Does anyone know how to achieve this? The input post data ...

Discovering with jQuery

Can we actually change the background color of '.preview-inner' to yellow from "#change_color"? <div id = "form-container> <input class="color-picker" id="change_color" type="text"> <div class="replacer"> <div class= ...

Updating div content dynamically with Jquery and PHP variable

How can I continuously update a div with the current date and time using PHP variable and Jquery? Here is my PHP file containing the variable date: <?php $date = date('d/m/Y H:i:s'); ?> And here's the code in my HTML file: <!DOCT ...

Deploy a web application using JavaScript and HTML to Heroku platform

I noticed that when I visit the Heroku dashboard, there is an option to create an app using Node.js, but not JavaScript. This raises a question for me - if I want to upload my locally created JavaScript/HTML5 app to Heroku, do I need to select Node.js or ...

What is the best way to show real-time values using chart js?

Just recently, I've delved into the world of web development and am eager to learn how to integrate chart js for real-time value display. Could anyone offer advice or guide me through the process? var data = []; var temp = []; async f ...

Aligning a bootstrap 4 navbar with a mega menu at the center

Trying to center the mega menu in Bootstrap4 led me to this: I attempted this approach initially: <nav class="navbar navbar-light bg-light navbar-expand-lg sticky-top" id="myNavbar"> <button class="navbar-toggler" ...

ES6 Error: import statement not allowed in this context

Encountering an error while setting up the Javascript Development environment. Preference for using import over require. npm install babel-register babel-preset-env --save-dev Utilized Babel. import express from 'express'; ...