Back to buttons

Live preview

Preview settings

Changes appear as you type. Use Tab to test keyboard focus.

⌘ / Ctrl + S saves locally

Question Box & Button Randomly Change Position

HTML, CSS or Tailwind, and a little JavaScript. One button or a group of up to six.

By @md-rosh02

Remix button

Button submission checks

Checks will appear after the preview updates.

Buttons only: no full pages, forms, cards or navigation. Put scripts in the JavaScript tab. Tailwind uses utilities from HTML and JavaScript; use complete class names. Custom CSS supports plain CSS, without @apply, imports or plugins.

Accessibility quick check

Waiting for the rendered preview…

Try Tab, Enter and Space in the preview. Check the focus ring, hover and disabled states, then enable Reduce motion. These checks are guidance, not an accessibility certification.

Explore freely here. Remix to publish your version with credit to the creator.

About Question Box & Button Randomly Change Position

Question Box & Button Randomly Change Position is an animated CSS button by @md-rosh02. Preview the interaction, customize its HTML, styles and JavaScript, or download the files with creator credit.

Effects and categories
Icon
Using this example
Copy the HTML or download index.html, style.css and app.js. Test keyboard focus, motion and contrast in your own project.

Usage, Tailwind and licensing questions

Read the source files

HTML

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Question Box & Button Randomly Change Position </title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css">
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <div class="wrapper">
        <i class="fa-solid fa-circle-question"></i>
        <h2 class="question">Do you love me ?</h2>
        <div class="btn-group">
            <button class="yes-btn">Yes</button>
            <button class="no-btn">No</button>
        </div>
    </div>

    <script src="script.js"></script>
    
</body>

</html>

CSS


@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@600;700&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

*::selection {
    background: #7d2ae8;
    color: #fff;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #7d2ae8;
}

.wrapper {
    position: relative;
    width: 600px;
    height: 400px;
    background: #fff;
    border-radius: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

i {
    font-size: 7.5em;
    color: #7d2ae8;
    border: 5px solid transparent;
    outline: 3px solid #7d2ae8;
    border-radius: 50%;
}

h2 {
    font-size: 3em;
    color: #7d2ae8;
    margin: 15px 0;
}

.btn-group {
    width: 100%;
    height: 40px;
    display: flex;
    justify-content: center;
    margin-top: 15px;
}

button {
    position: absolute;
    width: 150px;
    height: inherit;
    font-size: 1.2em;
    color: #fff;
    font-weight: 600;
    border-radius: 30px;
    border: 2px solid #7d2ae8;
    outline: none;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .3);
}

button:nth-child(1) {
    margin-left: -200px;
    background: #7d2ae8;
}

button:nth-child(2) {
    margin-right: -200px;
    color: #7d2ae8;
}

JavaScript

const wrapper = document.querySelector('.wrapper');
const question = document.querySelector('.question');
const yesBtn = document.querySelector('.yes-btn');
const noBtn = document.querySelector('.no-btn');

const wrapperRect = wrapper.getBoundingClientRect();
const noBtnRect = noBtn.getBoundingClientRect();

yesBtn.addEventListener('click', () => {
    question.innerHTML = 'I Love You Too :)';
});

noBtn.addEventListener('mouseover', () => {
    const i = Math.floor(Math.random() * (wrapperRect.width - noBtnRect.width)) + 1;
    const j = Math.floor(Math.random() * (wrapperRect.height - noBtnRect.height)) + 1;

    noBtn.style.left = i + 'px';
    noBtn.style.top = j + 'px';
});