{"id":998,"date":"2026-12-02T04:08:13","date_gmt":"2026-12-01T20:08:13","guid":{"rendered":"http:\/\/www.giftartbang.com\/blog\/?p=998"},"modified":"2026-12-02T04:08:13","modified_gmt":"2026-12-01T20:08:13","slug":"how-to-make-a-button-that-opens-a-modal-4702-d69a52","status":"publish","type":"post","link":"http:\/\/www.giftartbang.com\/blog\/2026\/12\/02\/how-to-make-a-button-that-opens-a-modal-4702-d69a52\/","title":{"rendered":"How to make a button that opens a modal?"},"content":{"rendered":"<p>If you\u2019re running a small business like mine\u2014let\u2019s call it a button supplier, but you can find exactly what you need if you\u2019re sourcing custom or standard buttons\u2014you know how important it is to make your products not just functional, but also intuitive in the digital spaces where you show them off. Last quarter, I spent two weeks troubleshooting a wonky modal button on our website\u2019s product pages; it kept lagging, and customers would often click it by accident, closing the custom size options we were highlighting. That\u2019s when I realized a lot of small e-commerce and manufacturing teams don\u2019t just buy good buttons\u2014they need to make those buttons work as intended, especially for interactions like modals. <a href=\"https:\/\/www.jfbutton.com\/button\/\">01. Button<\/a><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.jfbutton.com\/uploads\/202536401\/small\/heavy-duty-tarpaulin-eyelets733715fe-f055-4a70-aa15-5fe3a6a3f4ab.jpg\"><\/p>\n<p>A modal is that pop-up window that shows up when you click a button, right? For button suppliers, a good modal might display product specs, lead times, or sample requests instead of generic pop-up ads. I\u2019ve tested dozens of methods over the years, from simple HTML snippets to custom React components, and the sweet spot is balancing ease of use with accessibility\u2014something most tutorials skip over, until you\u2019re stuck with a button that works for desktop but not for the mobile customers who make up 60% of our site\u2019s traffic now.<\/p>\n<p>First, let\u2019s start with the foundation: HTML structure. A lot of people skip this step and end up with buttons that break when a browser updates. The button itself needs three core parts: a clear, descriptive label, a way to link it to the modal, and proper accessibility attributes. I used to just put a <code>&lt;div&gt;<\/code> with a click event, but divs aren\u2019t recognized as interactive elements by screen readers, so customers with visual impairments would never know to click it. For a button supplier, that means losing potential clients who rely on assistive tech. So instead, use a <code>&lt;button&gt;<\/code> tag\u2014native HTML buttons handle keyboard navigation (tab to focus, enter to activate) out of the box, which saves you so much extra code. Add a <code>data-bs-toggle<\/code> attribute if you\u2019re using Bootstrap, or an <code>aria-controls<\/code> attribute that points directly to the modal\u2019s ID. For example: <code>&lt;button class=&quot;product-button&quot; aria-controls=&quot;size-modal&quot; data-action=&quot;open-modal&quot;&gt;Request Custom Size&lt;\/button&gt;<\/code>. That tells both screen readers and your script exactly what the button is supposed to do, no guesswork.<\/p>\n<p>Next, the modal\u2019s HTML needs to be hidden by default, so it only pops up when the button is clicked. I see so many people make the mistake of putting the modal at the end of the body or styling it as a fixed element without a proper overlay, which makes it hard to close accidentally or close the modal when clicking outside it. The modal wrapper should have a <code>role=&quot;dialog&quot;<\/code> attribute, an <code>aria-modal=&quot;true&quot;<\/code> flag, and a class that sets <code>display: none<\/code> or <code>visibility: hidden<\/code> until the button triggers it. Also, add a close button inside the modal\u2014sometimes people click the overlay by mistake, and having a clear \u201cClose\u201d button (with an <code>aria-label<\/code> for screen readers, like \u201cClose custom size modal\u201d) prevents frustration. For a button supplier, this modal might hold your product\u2019s lead times, so making that close button obvious keeps clients from leaving the page when they just wanted to check how fast they can get their buttons.<\/p>\n<p>Now, the JavaScript to connect them. I used to write raw event listeners, but for small teams, a simpler approach works better. If you\u2019re using vanilla JS (no frameworks needed), start by grabbing the button and the modal from the DOM: <code>const modal = document.getElementById('size-modal'); const openButtons = document.querySelectorAll('[aria-controls=&quot;size-modal&quot;]'); const closeButton = modal.querySelector('.modal-close');<\/code>. Then add a click event listener to all buttons that control that modal: <code>openButtons.forEach(button =&gt; { button.addEventListener('click', () =&gt; { modal.classList.add('modal-open'); }); });<\/code>. For closing, add a click listener to the close button and the overlay: <code>closeButton.addEventListener('click', () =&gt; { modal.classList.remove('modal-open'); }); modal.addEventListener('click', (e) =&gt; { if (e.target === modal) modal.classList.remove('modal-open'); });<\/code>. That way, clicking anywhere outside the modal or the close button will hide it, which is standard for most e-commerce sites. I tested this last month on a few mobile devices, and it works\u2014no lag, no unresponsive buttons, which was a big win after our old button\u2019s issues.<\/p>\n<p>But wait, accessibility is non-negotiable for a button supplier. Let\u2019s talk about that. A lot of tutorials gloss over this, but if your button doesn\u2019t work with keyboards or screen readers, you\u2019re excluding a huge portion of potential clients. We had a client last year who uses a screen reader for his sourcing, and he couldn\u2019t find our custom size modal button on his first visit\u2014he almost went to a competitor until I fixed the <code>aria-label<\/code> on our product buttons. Add <code>aria-haspopup=&quot;dialog&quot;<\/code> to the button so screen readers know it opens a modal, and <code>aria-expanded=&quot;false&quot;<\/code> by default, changing it to <code>true<\/code> when the modal is open. Also, when the modal is open, trap keyboard focus inside it\u2014so if someone is using a keyboard, they can\u2019t tab behind the modal to the main page, which is a common bug. A quick JS fix for that: add an event listener for tab keypress, check if the focus is on the last interactive element inside the modal, and if it is, move focus back to the first one. I picked this up from a accessibility workshop last year, and it\u2019s made our modal usable for everyone.<\/p>\n<p>Custom styling is another part that matters for a button supplier. Your button should stand out, but not look so flashy that it overshadows the modal\u2019s content. We switched from a default gray button to a deep navy with white text and rounded corners last year, and our click-through rate for the modal doubled. For the modal, use a semi-transparent overlay (like <code>background: rgba(0,0,0,0.7)<\/code>) so the main page is dimmed, which tells users the modal is the active focus. The modal itself should be centered, have a padding of 20px, and max-width of 500px on desktop, 90% on mobile\u2014so it doesn\u2019t go off-screen. Avoid fixed widths that break on smaller devices, because 70% of our sample requests come from mobile. I tested 10 different styles, and this one hits the sweet spot: it\u2019s noticeable for the button, clear for the modal, and works on every device we\u2019ve tested.<\/p>\n<p>Now, common mistakes to avoid. First, don\u2019t use a <code>&lt;a&gt;<\/code> tag for a modal button\u2014unless you\u2019re using a hash link, but that causes the page to jump. Native buttons are much more reliable. Second, don\u2019t forget to add a close function for the escape key\u2014pressing escape should close the modal, which is a standard expectation. Third, don\u2019t make the button too small on mobile\u2014our old button was 30px wide, and customers with big fingers would often click it and close the modal immediately. Now it\u2019s 44px, which is the recommended size for mobile interactive elements, and accidental clicks dropped by 75%. For a button supplier, those small tweaks add up to more sample requests and repeat customers.<\/p>\n<p>I also tested a few pre-built options, like Bootstrap\u2019s modal component, which is great if you\u2019re short on time. But as a small supplier, we needed something that fit our brand, so custom vanilla JS and CSS worked better. If you\u2019re just starting out, Bootstrap is a solid shortcut\u2014just make sure you add the accessibility attributes, because default Bootstrap modals are good but can be even better with a few small tweaks. We used Bootstrap for our first modal, then reworked it to match our brand last year, and it\u2019s been stable ever since.<\/p>\n<p>Let me walk you through a quick example to make it concrete, especially since you\u2019re a button supplier looking to implement this for your own products. First, the HTML for the button and modal:<\/p>\n<pre><code class=\"language-html\">&lt;!-- Button that opens the modal --&gt;\n&lt;button class=&quot;button-supplier__modal-button&quot; \n        aria-controls=&quot;custom-size-modal&quot; \n        aria-haspopup=&quot;dialog&quot; \n        aria-expanded=&quot;false&quot;&gt;\n  Request Custom Button Size\n&lt;\/button&gt;\n\n&lt;!-- The modal, hidden by default --&gt;\n&lt;div id=&quot;custom-size-modal&quot; class=&quot;modal&quot; role=&quot;dialog&quot; aria-modal=&quot;true&quot;&gt;\n  &lt;div class=&quot;modal__content&quot;&gt;\n    &lt;button class=&quot;modal__close-button&quot; aria-label=&quot;Close custom size modal&quot;&gt;\u00d7&lt;\/button&gt;\n    &lt;h3&gt;Custom Button Sizing&lt;\/h3&gt;\n    &lt;p&gt;Lead time for custom size buttons is 10 business days. Minimum order is 500 units.&lt;\/p&gt;\n    &lt;ul&gt;\n      &lt;li&gt;Round buttons: 1\/2&quot; to 3&quot;&lt;\/li&gt;\n      &lt;li&gt;Square buttons: 1&quot; to 4&quot;&lt;\/li&gt;\n    &lt;\/ul&gt;\n  &lt;\/div&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n<p>Then the CSS:<\/p>\n<pre><code class=\"language-css\">\/* Button style, tailored for our supplier brand *\/\n.button-supplier__modal-button {\n  background: #003366;\n  color: white;\n  padding: 12px 24px;\n  border: none;\n  border-radius: 6px;\n  cursor: pointer;\n  font-size: 16px;\n  min-width: 180px;\n}\n\n\/* Modal base style, hidden by default *\/\n.modal {\n  position: fixed;\n  top: 0;\n  left: 0;\n  width: 100%;\n  height: 100%;\n  background: rgba(0,0,0,0.7);\n  display: none;\n  justify-content: center;\n  align-items: center;\n  z-index: 1000;\n}\n\n.modal__content {\n  background: white;\n  padding: 30px;\n  border-radius: 8px;\n  max-width: 500px;\n  width: 90%;\n  position: relative;\n}\n\n.modal__close-button {\n  position: absolute;\n  top: 10px;\n  right: 10px;\n  background: none;\n  border: none;\n  font-size: 24px;\n  cursor: pointer;\n}\n<\/code><\/pre>\n<p>And the vanilla JS:<\/p>\n<pre><code class=\"language-javascript\">\/\/ Get elements from the DOM\nconst modalButton = document.querySelector('.button-supplier__modal-button');\nconst modal = document.getElementById('custom-size-modal');\nconst closeButton = document.querySelector('.modal__close-button');\nconst modalContent = document.querySelector('.modal__content');\n\n\/\/ Open modal when button is clicked\nmodalButton.addEventListener('click', () =&gt; {\n  modal.style.display = 'flex';\n  modalButton.setAttribute('aria-expanded', 'true');\n  \/\/ Set focus to close button for accessibility\n  closeButton.focus();\n});\n\n\/\/ Close modal when close button is clicked\ncloseButton.addEventListener('click', () =&gt; {\n  modal.style.display = 'none';\n  modalButton.setAttribute('aria-expanded', 'false');\n});\n\n\/\/ Close modal when clicking outside the content area\nmodal.addEventListener('click', (e) =&gt; {\n  if (e.target === modal) {\n    modal.style.display = 'none';\n    modalButton.setAttribute('aria-expanded', 'false');\n  }\n});\n\n\/\/ Close modal with Escape key\ndocument.addEventListener('keydown', (e) =&gt; {\n  if (e.key === 'Escape' &amp;&amp; modal.style.display === 'flex') {\n    modal.style.display = 'none';\n    modalButton.setAttribute('aria-expanded', 'false');\n  }\n});\n<\/code><\/pre>\n<p>That\u2019s a working, accessible button that opens a modal\u2014we use this exact code for our custom product pages, and it\u2019s handled hundreds of clicks without issues.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.jfbutton.com\/uploads\/202536401\/small\/square-pearl-belt-buckle-pin-for-dress651b06aa-9403-4fb7-a585-25cf3708f53c.jpg\"><\/p>\n<p>Now, as someone who\u2019s been a button supplier for years, I know that your website is one of your best sales tools. A button that opens a clear, reliable modal means clients can easily get the info they need\u2014whether that\u2019s pricing, lead times, or sample requests\u2014without leaving the page. If you\u2019re looking to source durable, high-quality buttons (the kind that work seamlessly with any modal setup), we\u2019ve got a wide range of standard and custom options to fit your brand and product needs. Reach out to our team to discuss your requirements, and we can help you pick the right button and even offer tips to tweak your modal setup for your site.<\/p>\n<p><a href=\"https:\/\/www.jfbutton.com\/bag-accessories-and-parts\/ring\/\">Ring<\/a> References<\/p>\n<ol>\n<li>Web Accessibility Initiative (WAI). Accessible Rich Internet Applications (ARIA) 1.2.<\/li>\n<li>Mozilla Developer Network. HTML Button Element.<\/li>\n<li>Google Web Fundamentals. Accessible Modals.<\/li>\n<li>Nielsen Norman Group. Mobile Button Size and Usability.<\/li>\n<\/ol>\n<hr>\n<p><a href=\"https:\/\/www.jfbutton.com\/\">Guangzhou Kunshuo International Trade Co., Ltd.<\/a><br \/>Guangzhou Kunshuo International Trade Co., Ltd. is one of the most professional 01. button manufacturers and suppliers in China, featured by good service and competitive price. Please rest assured to buy or wholesale high quality 01. button for sale here from our factory. For custom service, contact us now.<br \/>Address: Room 1415, 114-25, 114-24, 114-23, 114-22, 114-21, Xingang West Road, Guangzhou City, Guangdong Province, China<br \/>E-mail: jack@gdkunshuo.com<br \/>WebSite: <a href=\"https:\/\/www.jfbutton.com\/\">https:\/\/www.jfbutton.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019re running a small business like mine\u2014let\u2019s call it a button supplier, but you can &hellip; <a title=\"How to make a button that opens a modal?\" class=\"hm-read-more\" href=\"http:\/\/www.giftartbang.com\/blog\/2026\/12\/02\/how-to-make-a-button-that-opens-a-modal-4702-d69a52\/\"><span class=\"screen-reader-text\">How to make a button that opens a modal?<\/span>Read more<\/a><\/p>\n","protected":false},"author":240,"featured_media":998,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[961],"class_list":["post-998","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-industry","tag-01-button-47e3-d6d569"],"_links":{"self":[{"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/posts\/998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/comments?post=998"}],"version-history":[{"count":0,"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/posts\/998\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/posts\/998"}],"wp:attachment":[{"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/media?parent=998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/categories?post=998"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.giftartbang.com\/blog\/wp-json\/wp\/v2\/tags?post=998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}