methods to "hide" content

methods to "hide" content

Administrator

Thread Starter
LV
0
 
well-known master
Jan
88
1
Credits
3,050
Points
436
Staff member
Owner
In HTML, CSS, and JavaScript, there are several methods for “hiding” content, including the toggle approach. Here’s a comprehensive list of commonly used methods, along with detailed, professional explanations, including code examples and how-tos:

1. Using the HTML hidden attribute
Description: The hidden attribute is HTML5’s built-in way to hide an element. By default, elements with this attribute have display: none in CSS.
Toggle: Can be changed with JavaScript by adding/removing the hidden attribute.
Pros: Simple, supported by all modern browsers, and semantic.
Cons: No built-in animations, only on/off.
Code Example:

HTML:
<div id="content" hidden>Konten tersembunyi</div>
<button onclick="toggleHidden()">Toggle</button>

<script>
function toggleHidden() {
    const element = document.getElementById('content');
    element.hidden = !element.hidden; // Toggle atribut hidden
}
</script>

Explanation:

hidden makes an element invisible and takes up no space in the layout.
JavaScript changes the hidden state from true to false and vice versa.

2. Using CSS display: none
Description: The CSS display: none property hides an element completely from the layout (takes no space).
Toggle: Overridden with display: block (or other values such as flex, inline) via JavaScript or CSS class.
Pros: Full control over visibility, broad compatibility.
Cons: No live animation transitions.
Code Example:

HTML:
<style>
.hidden {
    display: none;
}
</style>

<div id="content" class="hidden">Konten tersembunyi</div>
<button onclick="toggleDisplay()">Toggle</button>

<script>
function toggleDisplay() {
    const element = document.getElementById('content');
    element.classList.toggle('hidden'); // Toggle kelas hidden
}
</script>


Explanation:

display: none removes the element from rendering.
classList.toggle() adds/removes the hidden class to toggle between hidden and visible.


3. Using CSS visibility: hidden
Description: The visibility: hidden property hides an element but preserves its space in the layout.
Toggle: Replaced with visibility: visible.
Pros: Good if you want to keep the space occupied.
Cons: Doesn't affect layout, less flexible than display.
Code Example:

HTML:
<style>
.hidden {
    visibility: hidden;
}
</style>

<div id="content" class="hidden">Konten tersembunyi</div>
<button onclick="toggleVisibility()">Toggle</button>

<script>
function toggleVisibility() {
    const element = document.getElementById('content');
    element.classList.toggle('hidden');
}
</script>

Explanation:

The element is hidden but its space remains (e.g., an empty box).
Toggle is done by changing the CSS class.


4. Using CSS opacity with Transitions
Description: The opacity property sets transparency (0 = hidden, 1 = visible) and can be combined with transitions for animation.
Toggle: Change the opacity value between 0 and 1.
Advantages: Supports smooth animations, preserves layout space.
Disadvantages: Elements are still clickable even though they are transparent unless other properties such as pointer-events are added.
Code Example:


HTML:
<style>
.hidden {
    opacity: 0;
    transition: opacity 0.3s ease;
}
.visible {
    opacity: 1;
    transition: opacity 0.3s ease;
}
</style>

<div id="content" class="hidden">Konten tersembunyi</div>
<button onclick="toggleOpacity()">Toggle</button>

<script>
function toggleOpacity() {
    const element = document.getElementById('content');
    element.classList.toggle('hidden');
    element.classList.toggle('visible');
}
</script>


Explanation:

opacity: 0 makes the element invisible, opacity: 1 makes it visible.
transition adds a fade animation for 0.3 seconds.


5. Using CSS height or max-height with Animation
Description: Set the height or max-height to 0 to hide, then to the original value to show, often used for accordion effects.
Toggle: Change the height/max-height value.
Pros: Smooth collapse/expand animations.
Cons: Need to know the original height of the element (unless using max-height).
Example Code:


HTML:
<style>
.hidden {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.5s ease;
}
.visible {
    max-height: 1000px; /* Nilai besar untuk mendukung konten dinamis */
    transition: max-height 0.5s ease;
}
</style>

<div id="content" class="hidden">Konten tersembunyi</div>
<button onclick="toggleHeight()">Toggle</button>

<script>
function toggleHeight() {
    const element = document.getElementById('content');
    element.classList.toggle('hidden');
    element.classList.toggle('visible');
}
</script>

Explanation:

max-height: 0 hides the content by compressing the height.
overflow: hidden prevents the content from being visible when cropped.
Transitions provide animation effects.


6. Using position: absolute or left/top
Description: Moves an element off-screen using position: absolute or coordinates such as left: -9999px.
Toggle: Returns it to its normal position (e.g., left: 0).
Pros: Doesn't affect layout.
Cons: Elements are still in the DOM and can affect performance if there are many.
Code Example:

HTML:
<style>
.hidden {
    position: absolute;
    left: -9999px;
}
.visible {
    position: static;
}
</style>

<div id="content" class="hidden">Konten tersembunyi</div>
<button onclick="togglePosition()">Toggle</button>

<script>
function togglePosition() {
    const element = document.getElementById('content');
    element.classList.toggle('hidden');
    element.classList.toggle('visible');
}
</script>


Explanation:

left: -9999px moves the element far off-screen.
position: static returns to its normal position.
7. Using transform (Translate)
Description: Use transform: translate to move an element off-screen with animation.
Toggle: Return to its original position (translate(0, 0)).
Pros: Good performance due to GPU-accelerated, great for animations.
Cons: Elements are still in the DOM.
Code Example:

HTML:
<style>
.hidden {
    transform: translateY(-100%);
    transition: transform 0.3s ease;
}
.visible {
    transform: translateY(0);
    transition: transform 0.3s ease;
}
</style>

<div id="content" class="hidden">Konten tersembunyi</div>
<button onclick="toggleTransform()">Toggle</button>

<script>
function toggleTransform() {
    const element = document.getElementById('content');
    element.classList.toggle('hidden');
    element.classList.toggle('visible');
}
</script>

Explanation:

translateY(-100%) shifts the element up as far as its own height.
The transition provides a sliding effect.

Professional Recommendations
For Simple Toggle: Use display: none or hidden because it is simple and efficient.
For Animation: Use opacity or max-height for smooth fade/collapse effects.
For Performance: Use transform because it is GPU optimized.
XenForo Context: If you want to implement it in XenForo, integrate it into a template (e.g. post_macros) with JavaScript in <xf:js>.


Conclusion and Comparison​

methodHiding Animation SpaceAnimasiInteractivityUse Case
hiddennononoSimple, fast
display: nonenononoFull control of visibility
visibilityyesnonoPreserve space
opacityYesYesYes (click)Fade effect
height/max-heightnoYesnoAccordion/collapse
positionnononoOff-screen without animation
transformnoYesnoModern sliding animation






(meme2 7) (f15)
 

Facebook Comment

Search Google Web Search Google Images Search Google Voice Search Google Videos Search Maps Translate Word
🔍
GE
Google Experiment
Visit Google Experiment
⋮
CS
Code Saya
Visit Code Saya
⋮
SK
Sekolah Koding
Visit Sekolah Koding
⋮
HF
Huggingface
Visit Huggingface
⋮
SL
Sololearn
Visit Sololearn
⋮
LM
Learnx
Visit Learnxinmyminutes
⋮
w3s
W3schools
Visit w3schools
⋮
CW
Codewars
Visit Codewars
⋮
GG
Geeksforgeeks
Visit Geeksforgeeks
⋮
EXC
Exercism
Visit Exercism
⋮
EDX
EDX
Visit EDX
⋮
Add Shortcut
WhatsApp

Customer Service

CS 1

Mr.Ibnu

Loading jadwal...

Hubungi via WhatsApp
CS 2

Mr.Husein

Loading jadwal...

Hubungi via WhatsApp
CS 3

Mr.Hasan

Loading jadwal...

Hubungi via WhatsApp
Top Bottom

Translate Website

Upload Custom Icon

coding Coding
programmer Programmer
webmaster Webmaster
banners.png Banners
Home
What's New Resources Media Gallery
Record your forum activity and save it as MP4
> INITIALIZING ISE CONNECTION...
> BYPASSING SECURITY PROTOCOLS...
> ESTABLISHING WORMHOLE LINK...
> Starting XenForo engine... [DONE]
> Calibrating singularity Hosting parameters... [DONE]
> Establishing Forum Style... [DONE]
> The Programmer Community ready in 3... 2... 1.