<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>css on luis angel ortega</title><link>https://luisangel.me/tags/css/</link><description>Recent Css on luis angel ortega</description><generator>Hugo</generator><language>en-US</language><managingEditor>hey@luisangel.me (Luis Angel Ortega)</managingEditor><webMaster>hey@luisangel.me (Luis Angel Ortega)</webMaster><copyright>2025 Luis Angel Ortega Holguin All rights reserved</copyright><lastBuildDate>Sat, 05 Nov 2022 11:53:11 -0500</lastBuildDate><atom:link href="https://luisangel.me/tags/css/index.xml" rel="self" type="application/rss+xml"/><item><title>Dropdowns and Toggles with Pure CSS</title><link>https://luisangel.me/post/2022/11/05/dropdowns-and-toggles-with-css/</link><pubDate>Sat, 05 Nov 2022 11:53:11 -0500</pubDate><author>hey@luisangel.me (Luis Angel Ortega)</author><guid>https://luisangel.me/post/2022/11/05/dropdowns-and-toggles-with-css/</guid><description>&lt;p>We could imagine the technologies of the platform on which I work, &lt;a href="https://www.joinbuildit.com/">Build It&lt;/a>, as a dish; where Ruby on Rails is our main dish but is accompanied by a good portion of JavaScript through &lt;a href="https://stimulus.hotwired.dev/">Stimulus JS&lt;/a>, and while this brings the functionality and that magical feeling of a single page application (SPA), there are things we want to keep simple instead of using a sledgehammer to kill an ant, as a university professor used to say. Therefore, we decided to take a page from &lt;a href="https://apple.com/mx">Apple&lt;/a> when handling dropdown menus or dropdowns and do it through pure CSS and HTML; without requiring a line of JavaScript.&lt;/p>
&lt;h2 id="the-pillars">The Pillars&lt;/h2>
&lt;p>Our two pillars in the view will be &lt;code>&amp;lt;input&amp;gt;&lt;/code> elements of checkbox type and &lt;code>&amp;lt;label&amp;gt;&lt;/code> tags. We will form our basic skeleton as follows:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-html" data-lang="html">&lt;span style="display:flex;">&lt;span>&amp;lt;&lt;span style="color:#ca9ee6">input&lt;/span> &lt;span style="color:#8caaee">id&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">=&lt;/span>&lt;span style="color:#a6d189">&amp;#34;toggle&amp;#34;&lt;/span> &lt;span style="color:#8caaee">type&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">=&lt;/span>&lt;span style="color:#a6d189">&amp;#34;checkbox&amp;#34;&lt;/span>&amp;gt;&amp;lt;/&lt;span style="color:#ca9ee6">input&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&amp;lt;&lt;span style="color:#ca9ee6">label&lt;/span> &lt;span style="color:#8caaee">for&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">=&lt;/span>&lt;span style="color:#a6d189">&amp;#34;toggle&amp;#34;&lt;/span>&amp;gt;&amp;lt;/&lt;span style="color:#ca9ee6">label&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We will be using the checked property of checkbox-type inputs to control the content we show and when we show it. For this, we will be relying on the &lt;code>&amp;lt;label&amp;gt;&lt;/code> element since by linking it through its &lt;code>for&lt;/code> property it will also be affected by the class change when the checked property of our checkbox is present.&lt;/p>
&lt;p>All our code will really be inside &lt;code>&amp;lt;label&amp;gt;&lt;/code> in two sections: container and toggle as shown below.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-html" data-lang="html">&lt;span style="display:flex;">&lt;span>&amp;lt;&lt;span style="color:#ca9ee6">input&lt;/span> &lt;span style="color:#8caaee">id&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">=&lt;/span>&lt;span style="color:#a6d189">&amp;#34;toggle&amp;#34;&lt;/span> &lt;span style="color:#8caaee">type&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">=&lt;/span>&lt;span style="color:#a6d189">&amp;#34;checkbox&amp;#34;&lt;/span>&amp;gt;&amp;lt;/&lt;span style="color:#ca9ee6">input&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&amp;lt;&lt;span style="color:#ca9ee6">label&lt;/span> &lt;span style="color:#8caaee">for&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">=&lt;/span>&lt;span style="color:#a6d189">&amp;#34;toggle&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#ca9ee6">div&lt;/span> &lt;span style="color:#8caaee">class&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">=&lt;/span>&lt;span style="color:#a6d189">&amp;#34;toggle&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#737994;font-style:italic">&amp;lt;!-- The element with which the user will interact --&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;/&lt;span style="color:#ca9ee6">div&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;&lt;span style="color:#ca9ee6">div&lt;/span> &lt;span style="color:#8caaee">class&lt;/span>&lt;span style="color:#99d1db;font-weight:bold">=&lt;/span>&lt;span style="color:#a6d189">&amp;#34;container&amp;#34;&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#737994;font-style:italic">&amp;lt;!-- What we want to show--&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &amp;lt;/&lt;span style="color:#ca9ee6">div&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&amp;lt;/&lt;span style="color:#ca9ee6">label&lt;/span>&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="the-magic">The Magic&lt;/h2>
&lt;p>Now that we have our HTML skeleton ready, it&amp;rsquo;s time to add the CSS that will give our component the magical touch:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#c6d0f5;background-color:#303446;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-css" data-lang="css">&lt;span style="display:flex;">&lt;span>#&lt;span style="color:#ef9f76">toggle&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">display&lt;/span>: &lt;span style="color:#ef9f76">none&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>.&lt;span style="color:#e5c890">container&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">display&lt;/span>: &lt;span style="color:#ef9f76">none&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>#&lt;span style="color:#ef9f76">toggle&lt;/span>:&lt;span style="color:#8caaee;font-weight:bold">checked&lt;/span> &lt;span style="color:#99d1db;font-weight:bold">+&lt;/span> &lt;span style="color:#ca9ee6">label&lt;/span> .&lt;span style="color:#e5c890">container&lt;/span> { 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#ca9ee6">display&lt;/span>: &lt;span style="color:#ef9f76">inherit&lt;/span>; 
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Our input with the toggle id will never be shown so that we do not have the checkbox present on our page, what we will show and with which the user interacts is what is inside our div with toggle class. Once they click on our toggle, the input will have the value of checked and with &lt;code>#toggle:checked+label&lt;/code> we affect the style of our &lt;code>&amp;lt;label&amp;gt;&lt;/code> to obtain a result like the following&lt;/p>
&lt;p>&lt;img src="https://luisangel.me/images/post/dropdowns-toggle-css-1.gif" alt="Demo 1">&lt;/p>
&lt;p>And with a little more style (courtesy of Thulio Philipe) we can have results like this&lt;/p>
&lt;p>&lt;img src="https://luisangel.me/images/post/dropdowns-toggle-css-2.gif" alt="Demo 2">&lt;/p>
&lt;p>Or as the examples we have inside Build It&lt;/p>
&lt;p>&lt;img src="https://luisangel.me/images/post/dropdowns-toggle-css-3.gif" alt="Demo 3">&lt;/p>
&lt;p>A small popover that is always present, giving vital information to the user&lt;/p>
&lt;p>&lt;img src="https://luisangel.me/images/post/dropdowns-toggle-css-4.gif" alt="Demo 4">&lt;/p>
&lt;p>Or a filter menu that can be shown easily.&lt;/p>
&lt;p>As we can see, modern CSS allows us to make interactive pages without the need for a script for it; giving us a new solution to a problem with endless ways to solve it, so it will be necessary to consider the needs of the project and what is most suitable.&lt;/p>
&lt;p>Finally, I leave you &lt;a href="https://github.com/LinkSake/toggle-dropdown-css">a repository&lt;/a> with a couple of examples seen in this article for future reference and I invite you to visit &lt;a href="https://www.joinbuildit.com/early_access/client">the platform&lt;/a> and especially &lt;a href="https://www.joinbuildit.com/u/luis-ortega-160">my profile&lt;/a>, where we can connect for future projects.&lt;/p></description></item></channel></rss>