Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Is there a way to style/design a programatical button in JavaScript?

I implemented a Mapbox map on my website. On top of the Mapview I would like to add a button on the lower part of the page/map. As I couldn't figure out how to lay the button on top of the map (an HTML-button would just be below the map) I solved the problem with a programatical button in JavaScript.

function setupMap(center){
    const map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/streets-v11',
        center: center,
        zoom: 14
    });
    

const nav = new mapboxgl.NavigationControl();
map.addControl(nav);
var button = document.body.appendChild(document.createElement('button'));
button.style = 'z-index:10;position:absolute;top:10px;right:10px;';
button.textContent = 'Calculate Consumption';

Now I would like to design/change the style of the button. Is there a way to do that? And if not, is there a way to put a normal HTML button on top of the map?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

There are multiple ways to change the style of the button, best way is by setting an id to getting ahold of the button using it

function setupMap(center){
const map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11',
    center: center,
    zoom: 14
});


const nav = new mapboxgl.NavigationControl();
map.addControl(nav);
var button = document.body.appendChild(document.createElement('button'));
button.style = 'z-index:10;position:absolute;top:10px;right:10px;';
button.textContent = 'Calculate Consumption';
button.id = 'map-button';

then later do that

const mapButton = document.getElementById('map-button');
mapButton.classList.add('map-button--edited');

and add your styles in the class you added map-button--edited in this case

about 4 years ago · Juan Pablo Isaza Report

0

If you wanted the style you said above than add this to your button.style :

border-radius: 5px;background-color: white; padding: 10px;font-size: 20px;

The changed code will be like this :

function setupMap(center){
    const map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/mapbox/streets-v11',
        center: center,
        zoom: 14
    });
    

const nav = new mapboxgl.NavigationControl();
map.addControl(nav);
var button = document.body.appendChild(document.createElement('button'));
button.style = 'z-index:10;position:absolute;top:10px;right:10px;border-radius: 5px;background-color: white; padding: 10px;font-size: 20px;';
button.textContent = 'Calculate Consumption';

The value of border-radius padding font-size can be changed according to need

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!