0

I would like to include/positioning a button in the header of a webpage.

It would be helpful if you could tell me how I can place a button in the header. When I tried to position that button in the header, it does not overlap it.

2
  • 1
    Hard to help without seeing any HTML, or what you even tried Commented Nov 21, 2023 at 8:59
  • Please provide enough code so others can better understand or reproduce the problem. Commented Nov 21, 2023 at 19:13

1 Answer 1

0

It is unclear what you mean by your request to add "a button in the header of a webpage". In this example, I added a button at the very top of the page. The button is set to position:absolute so it will overlap other content as you requested. (If this doesn't work, add button.style.zIndex = 999; to the code.)

const button = document.createElement('button');
button.textContent = "I am a button!";
button.style.position = 'absolute';
button.addEventListener('click', ev => {
  // randomly change the button's text color when it is clicked
  button.style.color = "#" + (Math.round(Math.random()*2**24)).toString(16)
});
document.body.insertBefore(button, document.body.firstChild);
<p>This is a <em>very</em> simple HTML body.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,
  sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

If you want to insert it into a menu at the top of a page, you'll have to figure out which DOM element to add it to. You'll probably want to clone its style as well. I can help further if you specify the website and where you want your button. If you can't share the whole site (e.g. it requires a login or a VPN), you can paste a snippet (barring legal hurdles; keep it short to qualify for copyright fair use).

If you're adding the button to a dynamically-added parent, you'll need to wait for it to be loaded.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.