IMDTouchKnowledge BaseShop main store

How-to

How to Style a Pressed Button State on a Touch HMI

HMI and touchscreens Published 5 min read

Short answer

The pressed look on a browser-based HMI is the CSS :active pseudo-class, styled like any other state rule. One test decides everything else: how long the highlight lasts. Feedback only during contact is :active or the browser tap highlight; feedback stuck after release is a :hover rule matched by touch, which belongs inside @media (hover: hover).

On this page
  1. Style the pressed state with :active
  2. Decide the mechanism before writing more CSS
  3. Keep hover rules away from touch panels
  4. Control the browser tap highlight
  5. Verify the change on the panel
  6. A worked example with the timings
  7. Questions asked

The pressed look while a finger is on the control is the CSS :active pseudo-class, and you style it the same way you style :hover: write button:active with the appearance you want. Before writing anything, run one deciding test — how long the current highlight lasts. Feedback that exists only while the finger is down is :active, or the browser's own tap-highlight overlay, which the nonstandard -webkit-tap-highlight-color property controls. Feedback still present after release is a :hover rule the touch matched, and the fix is to wrap that rule in @media (hover: hover). Get this one decision right and the same stylesheet behaves on the Windows development PC and on the touch panel.

Style the pressed state with :active #

The :active pseudo-class represents an element being activated by the user. On a touch panel that window runs from touch-down to touch-up: the style is visible the whole time the finger is on the control and disappears the instant it lifts — exactly the pressed cue an operator screen wants.

/* Illustrative selector and colors: replace `button` with the
   widget selector you inspected, and the values with your scheme. */
button:active {
  background: #1e6bb8;
  border-color: #144a7d;
}

Give the active appearance its own rule instead of copying hover, so it reads the same whether the control is touched or clicked. Apply it in the stylesheet the project already loads; in WebIQ Designer that is the widget's CSS stylesheet, and if a style name you expect is missing there, see missing names in WebIQ Designer IO styling before assuming your rule was ignored.

Decide the mechanism before writing more CSS #

One observation separates the four mechanisms that can put a rectangle on a touched widget. Hold the control for 2 seconds, release, watch for 5 seconds, then touch a second control. Those timings are test durations you can check with a stopwatch, not browser limits.

What you see What produces it Where the fix goes
Highlight only while the finger is down the :active state restyle :active
Highlight stays after release, clears when you touch another control a :hover rule the touch matched @media (hover: hover)
Highlight clears only when focus moves, even by keyboard focus indication :focus-visible
A blue or gray box on every tapped element, images included, with no matching rule in the stylesheet the browser tap-highlight overlay -webkit-tap-highlight-color

Keep hover rules away from touch panels #

A mouse can point at a control without pressing it; a finger cannot, so the browser synthesizes mouse-like behavior when the finger lands, and a :hover rule can match — then stay matched until the next touch elsewhere. That is why a stylesheet that looks correct in Windows leaves a colored rectangle on the panel. Hover is not the only mouse behavior synthesized from touch, either: long contact can arrive as a right-click, as CODESYS WebVisu long touch opening the right-click menu shows.

The fix is to move the hover declarations into a media query that only hover-capable devices match:

/* Illustrative selector: substitute your widget or container class. */
@media (hover: hover) {
  button:hover {
    /* the existing hover appearance moves here, unchanged */
  }
}

button:focus-visible {
  outline: 2px solid #1e6bb8; /* illustrative value */
}

Nothing is deleted: a PC with a mouse still shows hover, and the panel stops matching the rule. For focus feedback, :focus-visible lets the browser show a visible focus indicator when it is appropriate — commonly during keyboard navigation — instead of after every touch. The full diagnosis of the stuck-after-release case on a WebIQ panel is in why a WebIQ button stays highlighted after release.

Control the browser tap highlight #

When no stylesheet rule produces the box — it appears over images and containers that carry no hover or active styling at all — it is the browser's own tap-highlight overlay. The property that recolors or suppresses it is -webkit-tap-highlight-color, written with one leading hyphen. It is nonstandard: support and exact behavior depend on the browser engine in the panel, so prove it on the panel, not only on the desk.

/* Test globally first, then scope to the affected controls. */
body {
  -webkit-tap-highlight-color: unset;
}

unset is a CSS-wide reset keyword, not an instruction to make the highlight invisible; its result follows the property's inheritance and initial value. If suppressing the overlay is the goal, test transparent on the affected selectors:

/* Illustrative selectors: the ones you confirmed own the box. */
button,
.image-widget {
  -webkit-tap-highlight-color: transparent;
}

Two one-line cautions: a name with two leading hyphens (--webkit-tap-highlight-color) declares a custom property and never touches the overlay; and do not suppress the overlay without keeping the :active cue, or the operator gets no confirmation that a press registered.

Verify the change on the panel #

  1. Load the revised stylesheet, reload the application on the panel, and keep the same revision on the PC for comparison.
  2. Hold the test control for 2 s: the pressed appearance must be visible for the whole contact.
  3. Release and watch 5 s: the appearance must be gone.
  4. Touch a second control: no styling may remain on, or transfer from, the first.
  5. Tab to the control if the panel has a keyboard: the focus indicator must appear through :focus-visible.
  6. Press each control type — button, container, image — and confirm the bound action still executes.
  7. If a box survives all of that, disable one suspected declaration at a time, in the browser inspection tools where the panel offers them and otherwise by redeploying with one rule commented out, until the box's owner is identified.

A worked example with the timings #

Scenario: an HMI application shows no appearance change under a Windows mouse, but every tapped widget on the panel — buttons, containers, images — picks up a blue rectangle.

The test run, on one button:

  • Hold 2 s: the blue rectangle is present during contact.
  • Release; 5 s later it is still there, so it is not :active alone.
  • Touch a container: the blue moves to the container and clears from the button — a latched hover.
  • Inspect the widget's matched rules: a :hover background on the widget class.

The fix: that rule moves inside @media (hover: hover), a button:active rule supplies the pressed cue, and because the box also appeared on images with no matched rule, the confirmed selectors also get -webkit-tap-highlight-color: transparent.

The acceptance arithmetic: 3 control types (button, container, image) × 4 checks each (visible during a 2 s hold, cleared within 5 s of release, no transfer to a second control, bound action still runs) = 12 checks. All 12 must pass; a failing image case sends you back to the tap-highlight branch even when the buttons are clean. A pressed cue also confirms the hit area, which matters more as controls crowd — see how to size HMI touch buttons in a dense button matrix.

Questions asked

Why does the highlight stay on the button after I lift my finger?

A finger cannot hover, so when it lands the browser synthesizes mouse-like behavior and a :hover rule can match, then stay matched until you touch another control. Move the hover declarations into @media (hover: hover) so they apply only where the primary input can hover; the touch panel then never matches them.

Does -webkit-tap-highlight-color work on every touch panel?

No. It is a nonstandard property, so support and behavior depend on the browser engine in the panel, and it must be proven on the panel itself. Write the name with one leading hyphen: a name starting with two hyphens declares a custom property and has no effect on the overlay. Keep the :active style as the activation cue either way.

What is the difference between :active and :focus on a touch panel?

:active spans the activation itself, from touch-down to touch-up. :focus persists after the control is used until focus moves elsewhere. Style :active for the pressed look, and use :focus-visible when the visible focus indicator should appear mainly for keyboard navigation rather than after every touch.

Can I keep the hover effect for operators who use a mouse?

Yes. Wrapping the hover rule in @media (hover: hover) does not delete it; it only stops touch panels from matching it. A PC with a mouse still shows the hover appearance, and the panel gets its feedback from the :active style instead.

Will touch-action remove the blue rectangle?

No. touch-action controls the browser default gesture handling on an element, such as panning and zooming. It changes what the touch does, not what the control looks like, so it belongs in the discussion only when scrolling or zooming interferes with button presses.

Keep reading

  • Troubleshooting

    Why a WebIQ button stays highlighted after touch release

    The blue left on a WebIQ iq-button after touch release is the widget's default active-state animation, not the variable-driven stylesheet fill. Reproduce the latch with Chrome's touch simulation, then apply the vendor's documented fix for color change on button press on touch panels, or adopt the cx-Visuals package with its required cx-tools dependency. Leave the tag binding and style condition untouched, and verify on the physical panel.

    HMI and touchscreens

  • How-to

    How to Size HMI Touch Buttons in a Dense Button Matrix

    On a graphics HMI there is no fixed touch-cell grid: the button you draw is the touch target. Lay out all 42 elements — 32 matrix buttons, 5 feedback displays, 5 navigation keys — with at least a 6 mm gap between touch areas, keep any pixel deadband small, then press every target on the actual terminal before sign-off.

    HMI and touchscreens

  • Troubleshooting

    Why Gas Dispenser Screens Show the Wrong Prompt or Overlapping Text

    A stale prompt such as LIFT LEVER on a lever-less handle comes from a mismatch between the installed hardware and the configured message mapping, not from a communications fault. Correct the profile first, then screen composition, and touch the communications path only where diagnostics show stale or rejected data.

    HMI and touchscreens