Make a native bookmark button with contrast, keyboard focus and a programmatic pressed state.
A button has a job: it performs an action when a person activates it. The appearance helps people recognise the action and see when the control is ready, focused, or pressed. This tutorial builds a simple bookmark toggle for a sample article. You will style it with a gradient and shadow, give keyboard users a visible outline, and connect the visual state to a native button’s pressed state.
Use the element that already supplies the interaction
Start from the HTML button element. It has built-in keyboard activation and a focus target. A link is the right control when the action is navigation to another address. Use the link’s actual destination so a visitor can open it in a new tab or inspect where it goes. Giving a generic element a click handler does not automatically supply all the keyboard behaviour of a button. It also makes it easier to omit information that assistive technology needs.
Choose a label that names the action. A bookmark control can keep the same name and expose whether it is pressed. A normal action button, such as “Download report,” does not need a pressed state because it does not toggle an ongoing condition. Set the type explicitly when you put a button inside a form. A button that opens local help or changes a display preference should normally have the ordinary button type so it does not inadvertently submit the form.
Build a working bookmark toggle
Paste the following into the HTML editor. Use the button twice and verify that the status changes both times. This is a local demonstration. It makes no request and does not save anything when you reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bookmark button practice</title>
<style>
body { margin: 2rem auto; max-width: 38rem; padding: 0 1rem; font: 1rem/1.6 system-ui; color: #10244a; background: #eff8ff; }
button { font: inherit; font-weight: 700; cursor: pointer; min-height: 44px; padding: .6rem 1rem; border: 2px solid #003571; border-radius: .6rem; color: white; background: #0053ad linear-gradient(120deg,#074694,#0053ad); box-shadow: 0 4px 10px #12274b32; transition: transform 150ms ease, box-shadow 150ms ease; }
button:hover { box-shadow: 0 5px 14px #12274b55; }
button:active { transform: translateY(1px); }
button:focus-visible { outline: 3px solid #8a3d00; outline-offset: 4px; }
button[aria-pressed="true"] { background: #176324; border-color: #164c22; }
@media (prefers-reduced-motion: reduce) { button { transition: none; } button:active { transform: none; } }
</style>
</head>
<body>
<main>
<h1>Keep this reading example handy</h1>
<p>Practice using the bookmark control with a mouse or keyboard.</p>
<button type="button" id="bookmark" aria-pressed="false">Bookmark article</button>
<p id="status" role="status">Not bookmarked.</p>
</main>
<script>
const button = document.querySelector('#bookmark');
const status = document.querySelector('#status');
button.addEventListener('click', () => {
const pressed = button.getAttribute('aria-pressed') !== 'true';
button.setAttribute('aria-pressed', String(pressed));
status.textContent = pressed ? 'Bookmarked.' : 'Not bookmarked.';
});
</script>
</body>
</html>
Try it in the HTML editor
The border and label still identify the button without its shadow. The colour change reinforces the pressed state, and the status text explains what happened without relying on colour. The pressed attribute is tied to the same Boolean value that updates the status. Keeping those changes together helps prevent the visual state and accessible state from disagreeing.
Choose a background with enough contrast
A gradient can vary across the full button. Check the label against the lightest relevant point as well as the darker points. If the light and dark ends would require different text colours, the simplest fix is to bring their contrast closer together. A shadow can separate the control from the surface around it, but it cannot make white text readable against a pale gradient behind the letters. Start with a solid colour that works, then add the gradient within the same readable range.
The shadow offset and blur are deliberately small. Making a shadow larger on hover provides gentle feedback without moving the control in the layout. Avoid animating a heavy blur on many controls at once. A transition on a specific property is easier to maintain than a transition on every property. The example respects reduced-motion preferences. Remove the movement entirely if it does not help people understand the action.
Keep keyboard and touch feedback visible
Use Tab to reach the button. An outline should appear outside the border and remain visible while the control has keyboard focus. Press Space or Enter and check that the label remains readable after the action. If a surrounding card clips the outline, adjust the layout or use an appropriate inner focus treatment. Removing the outline because it is clipped takes the feedback away rather than fixing its geometry.
Hover styling gives some feedback to pointer users but is not a dependable interaction on a touchscreen. The user should be able to see and operate the control even if hover never occurs. Provide enough target area and space around it to avoid accidental taps. Try the sample at narrow width with a longer label, because a translated phrase can occupy much more space. Let the text wrap when needed instead of giving the button a fixed height that clips it.
Verify state transitions and common mistakes
Change the pressed colours to another palette, then click twice. Confirm that the indicator returns to its original appearance and the status returns to the initial message. Inspect the native button if only one of those changes occurs. A selector that looks for the wrong attribute value will not match even when the script sets the value correctly. If nothing happens under keyboard activation, check that your listener is attached to click on the real button and not to a pointer-only event on its wrapper.
For a temporary unavailable action, a disabled form control usually cannot receive keyboard focus. If a visitor needs to know why an action is unavailable, explain the reason near the control in visible text. An explanation hidden in a hover tooltip is easy to miss. Re-enable the action only when the requirement is met, and verify that the layout and label still communicate what the button will do.
Finish by changing one design decision at a time. Choose a readable background, a shape consistent with the rest of your site, and a distinct keyboard outline. Then check the final label at several widths and test both activation methods. Use the CSS editor to compare two designs without changing the working example. For native behaviour and attribute details see MDN’s button reference.
Make repeated buttons consistent without making every action look the same
Start with a shared rule for typography, spacing, and focus. The primary action can use a stronger background, while a secondary action can use a plain surface with a visible border. Both should remain recognisable and both should give usable keyboard feedback. If the page shows several strong buttons next to one another, ask whether each action genuinely carries the same weight. A person filling out a form should be able to find the next step without comparing a row of visually identical calls to action.
A long label can reveal layout problems that the sample label does not. Change the button text to describe a longer action and place it beside a second control. Test at phone width and then enlarge the text. Let the controls wrap before they become cramped. If a label does wrap, check that the text remains easy to scan and that the surrounding controls do not move under the pointer during activation. Reserve a stable location for a loading message when an action is slow.
In the sample, reloading clears the pressed state because it is a browser-only exercise. In a real bookmark feature the application might either save the state on the device or store it in an account. Choose that behaviour deliberately and state it accurately. If saving fails, show a helpful error and make sure the visual state reflects what actually happened. A button that looks pressed while a request failed gives the reader a misleading impression of what was saved.
Inspect the result without relying on a screenshot
Try turning colours off mentally and look for another way to tell the states apart. The text below the button is one such clue. Next use the browser accessibility inspector to read the accessible name and pressed state before and after activation. Then try the control without a mouse. These checks cover information that a visual screenshot cannot show. If you add an icon, let it reinforce the label rather than replacing readable words unless the icon has a clear accessible name. Use the smallest set of styles and states that communicates the action reliably across those tests.
Keep that checklist beside the component so you can repeat it when the shared styles change.