Home > front end >  Which has more specificity between hidden attribute in HTML or display property in CSS? And how?
Which has more specificity between hidden attribute in HTML or display property in CSS? And how?

Time:09-29

Also what is the best way to hide the elements from the DOM so that the attacker won't be able to change the css property or html attribute in order to access the element. I know we can use React or Angular to develop website and it is easier to hide or display elements. But I want to know in pure HTML & JS what is the best way?

CodePudding user response:

Anyone can just use the browser console and find all elements with for example:

document.querySelectorAll('*');

It does not matter if elements are hidden with CSS.
Even if you encrypt your HTML you will have to decrypt it to show it to the browser. Then the above code still finds all the elements.

Any code you have can be deactivated by setting a breakpoint and rewriting it in-browser using the developer tools.

Even if you replace document.querySelectorAll and all like them with an empty function, developers can still just add jQuery or any DOM querying engine and find your elements that way.

Any code you can use to hide or show elements can just be executed using the browser console if someone spends the time understanding your code.
How else would you debug or test it?

Angular, Vue etc. does remove elements from the DOM but you should never expect this to be a security feature! A hacker can easily set a breakpoint anywhere in your code, inspect API results from the Network panel, go into the components' code to find out what HTML they would be rendering and much more I haven't started to mention.

To implement security you want to only have in the browser what the user needs to see.

There is no way around it.
DOM, stylings, scripts, assets, etc. can always be accessed using developer tools.

As for the question in your question title:

  • style attribute styles have a higher specificity than CSS from file (or style tags)
  • CSS from file (or style tags) with !important has higher specificity than styles from the style attribute
  • style attribute styles with !important have the highest specificty

So !important just overrides specificity if you want to look at it that way. Other than that you should read about CSS Specificity.

CodePudding user response:

Both are same. If you store your value from html hidden or css hide. Anyone can find out them.

So if you are using html , js & css and want to pass value as hidden than disable developer tool and shortkey to open it by this way you can protect your data or else use any encryption method for that.

  • Related