Search code examples
htmlcsselement

Remove HTML element completely just by using CSS?


Just like removeAttribute in JavaScript, after which the element won't be visible in the source.


Solution

  • You cannot remove an element from the DOM tree using CSS. You can only prevent it from being rendered in the layout with display: none; doing so does not prevent it from responding to events or cause it to be ignored by CSS selectors such as + and :nth-child(). You won't be able to interact with an element that's not there so you wouldn't be able to trigger events the usual way, but its "essence" remains, so to speak.