Artifacts Gallery Guilds Search Wiki Login Register

Welcome, Guest. Please login or register. - Thinking of joining?
Autumn, 20 Nov 2026 - @49.21
Forum Activity: Four Stars Random | Recent Posts | Guild Recents
News: :ha: :pc: Hello Melonland! :pc: :happy: Guild Events: November 200 words RPG jam
+  MelonLand Forum
|-+  Da Web Revival
| |-+  ✁ ∙ Web Crafting
| | |-+  ♺ ∙ Snippets & Materials
| | | |-+  Tutorial: Sidebar accessed by toggle-button on small screens (Popover API)


« previous next »
Pages: [1] Print Embed
Author Topic: Tutorial: Sidebar accessed by toggle-button on small screens (Popover API)  (Read 261 times)
Dan Q
Hero Member ⚓︎
*****
View Profile WWWArt


I have no idea what I am doing
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Caught by RubbereonThe Worm is creeping around Melonland profiles!Dan Q Cruisin'I DIDN'T meet Dan Q on Melonland!Visited on Melon's 10th Anniversary!
« on: Autumn, 04 Nov 2026 @513 » Toggle Font Embed

Dynamic Responsive Sidebars (with no JS!)

Inspired by something @fairyrune mentioned in the Wednesday Website Club this week, I thought I'd have a go  at making a web page that:

- Had a "main" column and a sidebar, but
- On narrow (e.g. mobile) screens, the sidebar disappeared and
- Could be toggled by clicking a button,
- Using only HTML + (minimal) CSS



Here's the Everyone Page I came up with to demonstrate the concept. But as usual, I'll be talking you through it step by step!

I'm using the Popover API. You can achieve something very similar with <details>/<summary> elements if you like, but for a "popover" effect the CSS is harder, especially if you want to animate it later! The Popover API is well-supported by all modern browsers, but if you really care about the very old ones, <details>/<summary> might be a safer choice.

HTML

Let's start with some HTML. I'll need some kind of "container"/wrapper that I can use to lay out my inner "main" section and "sidebar", and it should contain my main section and sidebar. It also needs to contain a button that toggles the sidebar (we'll hide this with CSS when it's not needed):


Code
<div id="container"><!-- the outer wrapper -->

  <main>
    (main content)
  </main>

  <!-- note `popovertarget="sidebar"`; this is what makes the popup work! -->
  <button id="sidebar-button" popovertarget="sidebar">
    Show Sidebar
  </button>
  <!-- the button can be anywhere, e.g. in your <main> or whatever if you prefer! -->

  <!-- note `popover`; this is what makes this a popover; the id matches the popovertarget from above -->
  <div id="sidebar" popover>
    <aside><!-- this INNER wrapper is all that will remain in desktop mode -->
      (sidebar content)
    </aside>
  </div>

</div>

If you try it, this already "just works": the sidebar is hidden until you click the toggle button. There's a lot of magic styling you can do with this, using CSS (e.g. blurring or fading the background "behind" the popover, animating its appearance and disappearance etc.) but I'm looking only to do an absolute minimal implementation.

CSS

Because what we've got already WORKS on mobile, we only need to implement the changes that affect it in desktop mode. This is a good approach in-general, because it means that mobiles (which tend to have weaker processors) have less CSS to apply!

Here's my CSS to "shift" the sidebar to being an actual-sidebar when on desktop:


Code
/* If we're on a screen wider than 800px, then... */
@media(width > 800px) {
    
  /* 1. Hide the button that toggles the sidebar (we don't need it!) */
  #sidebar-button {
    display: none;
  }
  
  /* 2. Lay out the content using a grid, flexbox, absolute positioning, or whatever you like!
   *    I'm going to have a 300px sidebar and use the rest of the space for my main content.
   */
  #container {
    display: grid;
    grid-template-columns: 1fr 300px;
  }
  
  /* Here's the important bit! Instead of displaying the sidebar "as normal", which includes its
   * `popover` attribute, we only display its CONTENTS (i.e. the inner <aside> we added). This
   * effectively disables the popover functionality, and the <aside> can be positioned however
   * we like (in our case, in our grid). */
  #sidebar {
    display: contents;
  }
}

That's literally all there is to it! Probably barely any more HTML than you'd write for a desktop-only layout, plus three CSS selectors and four CSS directives. Give it a go on the everyone site and see how it works for you.

« Last Edit: Autumn, 04 Nov 2026 @520 by Dan Q » Logged

https://danq.me/_q26t/badges/dan-q-88x31-lighter.gif https://danq.me/_q26t/badges/dan-q-88x31-peekaboo-scroller.gif https://beige-buttons.danq.dev/beige-buttons-88x31.gif https://embed-html.danq.dev/embed-html-88x31.gif https://origami.danq.dev/88x31.gif https://skelly.monster/dem-bones-88x31.gif

Artifact Swap: Pink ham-ham rangerPolyamorousI met Dan Q on Melonland!shoeJoined 2025!cinnamoroll
mall-made manners
Casual Poster ⚓︎
*
View Profile WWWArt


in look for being edgy, I found only copying...
⛺︎ My Room
RSS: RSS

Guild Memberships:
Artifacts:
Moon Cheesepin - by mall-made mannersJoined 2026!
« Reply #1 on: Autumn, 11 Nov 2026 @964 » Embed

it is "cheating", but it can also be somewhat done with
Code
columns: (num)px
and hiding elements (maybe https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp)
.

Logged

Quote
in look for being edgy, I found only copying...

Artifact Swap: Clippy just wanted to helpMelonalbum by newgeneMcmxcixfllorabeat time pin
Pages: [1] Print Embed 
« previous next »
 

Melonking.Net © Always and ever was! SMF © 2021 | Privacy Notice | Send Feedback | Supporters ♥ Guide | Rules | Whos a Mod? | RSS | WAP | Mobile


MelonLand Badges and Other Melon Sites!

MelonLand Project! Visit the MelonLand Forum! Support the Forum
Visit Melonking.Net! Visit the Gif Gallery! Pixel Sea TamaNOTchi
@000 Melon
Land
Forum Art Hub Chat Webring Explore Random Recent Tenement Arcade Want to Login or Join ? Web Craft Guide Graphic Catalogue Wiki Help I'm Lost! Link Land Newsletters Image Stream Minecraft: Online
Zap!
Passport Sites tamaNOTchi Asatte Add your site?
Aero Archive Onio Cafe 32 Bit Cafe Status Cafe