Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input hidden or div with display:none css? [closed]

Tags:

html

css

In our project we both use input type=hidden elements and divs with display:none css for storing some data on html page without showing it to user.

So i wonder which way is more suitable in both performance and code integrity and html semantics?

like image 983
LostMohican Avatar asked Nov 19 '11 20:11

LostMohican


People also ask

How do I show display none in CSS?

getElementById("element"). style. display = "none"; To show an element, set the style display property to “block”.

What is the difference between display none and visibility hidden?

Difference between display:none and visiblity: hiddenvisibility:hidden hides the element, but it still takes up space in the layout. display:none removes the element from the document. It does not take up any space.

Why is display none still taking up space?

display: none means it will be hidden AND removed from the flow of the website. visibility: hidden means it will be hidden, BUT will remain in the flow of the website. Essentially, visibility: hidden will not show the element, but will retain the space it takes up.


2 Answers

The hidden input will be hidden regardless of the styling rules of the document (CSS), this may make it better performance wise but I don't have the data to show this. Having said that, input controls are meant to be submitted as part of a form.

There are other methods as well, like the HTML5 custom data attributes or using a script tag:

<!-- Custom data attribute -->
<div id="product" data-id="42">
    <h1>Product name</h1>
</div>

<!-- JSON data embedded in a script tag -->
<script type="application/json">
    { "id": 42 }
</script>
like image 130
Michiel van Oosterhout Avatar answered Oct 17 '22 11:10

Michiel van Oosterhout


There are many factors involved in performance. But unless you are storing a ton of data the performance differences are likely indistinguishable.

What's best for code integrity and semantics is dependent upon the data and how you are using it.

There are many options for storing data:

  • Storing data in a hidden input which is great for use with forms:

    <input name="mydata" type="hidden" value="some data" />

  • Storing data in a hidden html tags which is often frowned upon by search engines because of it's abuse in trying to improve SEO ranking:

    <div id="mydata" style="display:none"> some data </div>

  • Storing data in javascript which is great for quick easy access:

    <script type="text/javascript"> var data.id = 123; var data.list = ["Yes","No","Maybe"]; </script>

  • Storing data in a META Tag which I've used on a rare occasion: http://code.lancepollard.com/complete-list-of-html-meta-tags#create-custom-meta-tags

    <meta name="mydata" content="some data"/>

  • Storing data in a cookie which as long as cookies haven't been disabled this option allows for the data to expire: http://www.w3schools.com/js/js_cookies.asp

  • HTML5 has an SQLite database:

    http://html5doctor.com/introducing-web-sql-databases/

  • HTML5 specs allows for custom data attributes

    http://html5doctor.com/html5-custom-data-attributes/

like image 4
Micah Avatar answered Oct 17 '22 09:10

Micah