How to Make a No Button That Runs Away (HTML, CSS, JS + No-Code)
How to make a No button that runs away: a short HTML, CSS and JavaScript version that works on phones, stays on screen and never covers Yes, plus a free no-code option.
To make a No button that runs away, you move the button to a new random spot on the screen whenever someone's pointer gets close to it or they try to tap it. For example, a "Will you be my Valentine?" page can listen for the pointer touching the No button and instantly jump it somewhere else, so the only button left to press is Yes.
Below you'll find a small, working version in plain HTML, CSS and JavaScript that also handles phones, stays on screen and never covers the Yes button. If you'd rather skip the code, there's a no-code way at the end that takes about a minute.
How does a runaway No button work?
A runaway No button is a normal button that gets a new position every time someone tries to reach it. The page listens for a "pointer is here" event, picks a random spot inside the visible screen, and moves the button there before the click can land.
That's the whole trick. Everything else is polish: making it work on touchscreens, keeping it on screen, keeping it away from Yes, and making sure it's still fun rather than frustrating.
Most tutorials and GitHub projects you'll find use the mouseover event and Math.random() with the window size. That works on a laptop, but it tends to break in three places:
- Phones: there's no hover on a touchscreen, so a mouseover-only button can simply be tapped.
- Edges: if you use the full window width, the button can land half off screen or under the browser bar.
- Overlap: a random spot can land right on top of Yes, which looks broken and can block the answer you actually want.
The version below fixes all three.
Build it in 4 steps (HTML, CSS & JS)
You need one HTML file with two buttons, a few lines of CSS and about 30 lines of JavaScript. Our blog doesn't support code boxes, so each snippet is written as plain text below. Copy it into a file called index.html, in this order.
- Add the question and two buttons inside the body.
- Add a little CSS so the No button can move smoothly.
- Add the JavaScript that picks a safe new spot.
- Open the file in your browser, test it on your phone, then share it.
Step 1: The HTML
Inside your body tag, add a heading and two buttons with ids so the script can find them:
<h1>Will you be my Valentine?</h1> <button id="yes">Yes</button> <button id="no">No</button>
Give Yes the friendlier style. It's the one you want pressed, so make it big and bright.
Step 2: The CSS
Inside a style tag, add a smooth glide for the No button, but only for people who haven't asked their device to reduce motion:
@media (prefers-reduced-motion: no-preference) { #no { transition: left 0.2s ease, top 0.2s ease; } }
#no { touch-action: manipulation; }
The touch-action line stops a quick double tap from zooming the page on some phones.
Step 3: The JavaScript
Inside a script tag at the bottom of the body, grab both buttons and set a little padding so the button never hugs the edge:
const no = document.getElementById("no"); const yes = document.getElementById("yes"); const PAD = 12;
Next, a helper that checks whether two boxes overlap:
function overlaps(a, b) { return !(a.right < b.left || a.left > b.right || a.bottom < b.top || a.top > b.bottom); }
Now the main function. It measures the button, works out the largest left and top values that still keep it fully on screen, and tries random spots until it finds one that doesn't touch Yes (with a limit, so it can never loop forever):
function dodge() { const w = no.offsetWidth, h = no.offsetHeight; const maxX = window.innerWidth - w - PAD; const maxY = window.innerHeight - h - PAD; const y = yes.getBoundingClientRect(); let x, top, tries = 0; do { x = PAD + Math.random() * (maxX - PAD); top = PAD + Math.random() * (maxY - PAD); tries++; } while (tries < 25 && overlaps({ left: x - PAD, top: top - PAD, right: x + w + PAD, bottom: top + h + PAD }, y)); no.style.position = "fixed"; no.style.left = x + "px"; no.style.top = top + "px"; }
Finally, hook it up. Pointer events cover mouse, pen and touch with one listener, and the click listener catches anything that slips through, including someone pressing Enter on the keyboard:
no.addEventListener("pointerenter", dodge); no.addEventListener("pointerdown", (e) => { e.preventDefault(); dodge(); }); no.addEventListener("click", (e) => { e.preventDefault(); dodge(); }); window.addEventListener("resize", () => { if (no.style.position === "fixed") dodge(); }); yes.addEventListener("click", () => { document.body.innerHTML = "<h1>Yay! See you on the 14th 💖</h1>"; });
Step 4: Test it on a real phone
Open the file on your laptop first, then on your phone. On a phone, the button jumps the moment a finger lands on it, because pointerdown fires before the tap finishes. Rotate the phone too: the resize listener moves the button back into view if it ended up off screen.
Why does my No button still get clicked on mobile?
Usually because the code only listens for mouseover or mouseenter, which phones never fire before a tap. Switch to pointerdown (or touchstart) and call preventDefault, like in Step 3, so the button moves the instant a finger touches it.
Two other common causes:
- The button is inside a container with overflow hidden or a transform. A fixed-position element inside a transformed parent is positioned relative to that parent, not the screen. Keep the No button outside any transformed wrapper.
- The page scrolls. If your page is taller than the screen, use position fixed (as above) so the random spot is based on what's visible, not the whole page.
How do you keep the No button from covering the Yes button?
Check the new spot against the Yes button's box before you move. The overlaps() helper in Step 3 compares the two rectangles, adds a bit of padding, and picks again if they touch.
The tries limit matters on very small screens. If there's barely any room, 25 attempts is plenty to find a free spot most of the time, and the loop still stops instead of freezing the page.
Ideas to make your runaway button more fun
Once the basic dodge works, small touches make it feel playful instead of glitchy. Pick one or two; piling on everything gets chaotic fast.
Change the No text each time
Keep a short list like "No", "Are you sure?", "Think again", "Pretty please?" and "Wrong button", and swap the button text on each dodge. It turns the chase into a little conversation.
Shrink No, grow Yes
On each attempt, make No slightly smaller and Yes slightly bigger. Cap the Yes size so it still fits on a narrow phone screen.
Count the attempts
Show "You tried to say no 7 times" on the Yes screen. People love seeing their own score, and it's a nice screenshot moment.
Let it give up eventually
After a dozen or so attempts, let the No button stop running and turn into a second Yes. It ends the joke on a laugh instead of leaving someone stuck.
Respect reduced motion and keyboards
The CSS in Step 2 already turns off the glide for people who prefer less motion, and the click listener makes sure keyboard users get the same joke. Test with the Tab key once before you send it.
Questions that work well with a runaway No button
The best questions are short, clearly playful, and fit on one line on a phone. Here are some originals to start with. Each "Make this one" link opens it in the Only Yes editor, ready to send.
- "Will you be my Valentine?" Classic for a reason. Make this one
- "Will you be my Valentine? (There is only one right answer)" Make this one
- "Will you be my Valentine again this year?" Sweet for a long-term partner. Make this one
- "Will you be my Valentine even though I already ate your chocolate?" Make this one
- "Be my Valentine? I promise to share the fries." Make this one
- "Will you be my long-distance Valentine?" Make this one
- "Will you be my Galentine?" For your best friend. Make this one
- "Will you be my Valentine, or do I need to ask nicer?" Make this one
The same trick works far beyond Valentine's Day. People use it to invite friends to a party, to find a study buddy, or as a silly way to say sorry for something small.
Is a runaway No button okay to send?
Yes, as long as both of you know it's a joke. The button is a bit of fun; it isn't a real way to get someone's answer.
Send it to someone you already have an easy, joking relationship with, and keep the real conversation separate. If they reply "haha, but honestly, no," that's the answer that counts, and the right move is to thank them and let it go. A runaway button should make someone laugh, never feel cornered.
It's also worth skipping for big, serious questions. A first ask-out with someone you barely know usually lands better as a simple, honest message. Our guide on how to ask someone out over text has plenty of low-pressure examples.
How do I share my runaway button page?
A file on your laptop only works for you, so you'll need to put it online. Free static hosts like GitHub Pages or Netlify can serve a single index.html file, and you then send the link.
A few things to check before you send it:
- Open the link on your phone in the same app they'll use (WhatsApp, iMessage, Instagram DMs), since in-app browsers sometimes behave differently.
- Make sure the page has a title, so the link preview doesn't just show "index.html".
- Remove any test text or your own name in the code if you're sharing the repository publicly.
A playful way to send it with Only Yes
If you'd rather skip the code, there's a ready-made version. Only Yes (onlyyes.fun) is a free website for making a yes-or-no question page where the No button runs away. You type your question, pick an occasion, and get a link to send, with no sign-up and no paywall. The No button dodges, teleports and shrinks until they tap Yes, and then they land on a celebration screen where you can add photos, music or a note.
Start a page in the editor, or browse the Valentine version on the will you be my Valentine page. If you want the full walkthrough of the no-code route, read is there a website where the No button runs away, and for a round-up of everything Valentine-specific, see our free Valentine yes-or-no website guide.
Frequently asked questions
What is the code for a No button that runs away?
The core is a function that sets the button to position fixed and gives it a random left and top value within the visible window, called whenever the pointer enters the button or presses down on it. The full version above adds checks so it stays on screen and never covers Yes. It's plain JavaScript, so it works without any library.
Can I make a runaway No button without coding?
Yes. You can use Only Yes, which builds the page for you: write your question, choose an occasion and send the link. It's free and doesn't need an account.
Does the runaway No button work on iPhone and Android?
It does if you listen for pointer or touch events, not just mouse hover. Using pointerdown with preventDefault, like in the snippet above, moves the button the moment a finger touches it on both iPhone and Android browsers.
Why does my No button go off the screen?
Usually because the random position uses the full window size without subtracting the button's own width and height. Take the button's size and a small padding away from window.innerWidth and window.innerHeight before picking a spot, and re-run the move when the window resizes.
Is it mean to send a page where you can't say no?
It's fine as a joke between people who are already comfortable with each other, and the fun is in the chase, not the "forced" answer. In real life, people can always say no, so keep a real way for them to answer and take that answer seriously.
Can I use a runaway button for things other than Valentine's Day?
Definitely. It works for party invites, asking a friend to be your gym or study buddy, a playful promposal, or a light-hearted sorry for something small. Only Yes has presets for all of these, plus a custom option for anything else.
A little inspiration
Valentine Website for Your Boyfriend: Free, Made in a Minute
A free Valentine website for your boyfriend, written for him: questions that fit his humor, texts to send with the link, and a note for after he taps Yes.
Read the guideHow to Ask Someone to Be Your Valentine (25+ Messages)
How to ask someone to be your Valentine, by stage: crush, new date, partner, long distance or friend. 25+ texts, card lines and what to do if they say no.
Read the guideIs There a Website Where the No Button Runs Away? Yes, Here's How
Is there a website where the No button runs away? Yes. Only Yes is free, needs no sign-up and works for Valentines, dates, parties and more. Here's how it works, with questions to copy.
Read the guide