Building CSS only interactive components from scratch - Part I (modal window)
Tags: beginners, css, career, tutorial
Published: 2020-05-29 20:01:16

---
title: Building CSS only interactive components from scratch - Part I (modal window)
published: true
description: Awesome CSS Only Responsive Modal Window
tags: beginner, css, career, tutorial
cover_image: https://i.imgur.com/0NIHd7R.jpg
---

<meta name="monetization" content="$ilp.uphold.com/7QaxJkDwRZED">

*Each post about <strong>Building CSS Only interactive components </strong> is stand alone, so you don't need to understand part 1 to understand the part 2. Feel free to check it in the order you want, need or like.
Also a basic understanding about how CSS works is required to understand the workarounds of this posts. If you want to level up your CSS acknowledgement, you can visit <a href="https://dev.to/joelbonetr/complete-css-guide-for-beginners-1gjc" target="_blank">this post </a>.
<hr>
<ol>
<li><a href="https://dev.to/joelbonetr/build-your-own-css-only-components-part-i-4b83" target="_blank">Building CSS Only interactive components from scratch PART 1</a></li>
<li><a href="https://dev.to/joelbonetr/building-css-interactive-components-from-scratch-part-ii-4c22" target="_blank">Building CSS Only interactive components from scratch PART 2</a></li>
<li><a href="https://dev.to/joelbonetr/building-css-only-interactive-components-from-scratch-part-iii-tabs-4d13" target="_blank">Building CSS Only interactive components from scratch PART 3</a></li>
</ol>
<hr>

Hi there, 

I'll make a post series about components that usually people code using javascript but... well, without javascript. Using CSS Only to make them more efficient to load and render.

The main point on this post and for the task I'm showing is the usage of the <strong>:target </strong> pseudo-selector.

Without further delay let's take a dive into it.

The HTML code:

<pre>

&lt;div id="open-modal" class="modal-window">
  &lt;div class="modal-content">
    &lt;a href="#!" title="Close" class="modal-close"> X &lt;/a>
    &lt;h2>Yaaay!&lt;/h2>
    &lt;div>
      &lt;p class="bold">A nice responsive css only modal.&lt;/p>
    &lt;/div>
    &lt;div>
      &lt;p>
        You can close this dialog by clicking the close button at the bottom, the cross at the top right or the backdrop.
      &lt;/p>
      &lt;p>
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti dolore repellat nulla sunt, sed libero voluptatem facilis saepe iste dolores et dignissimos ea fugiat sit quae aliquam nisi corporis tempora.
      &lt;/p>
    &lt;/div>
    &lt;div class="modal-footer">
      &lt;a href="#!" title="Close" class="btn modal-close"> Close &lt;/a>
    &lt;/div>
  &lt;/div>
&lt;/div>
&lt;a class="modal-backdrop" href="#!">&lt;/a>
&lt;a class="btn modal-btn" href="#open-modal" name="a">
  Open Modal
&lt;/a>

</pre>
<small>Remember that Dev.to renders html "as is" and i didn't find a way to avoid it so I put white spaces plus a hyphen on each html tag as an ugly way to reach it.</small>


Now we have the structure which is:
<ul>
  <li> a modal window</li>
    <ul>
      <li> a modal content </li>
        <ul>
          <li>a close cross icon ( &times; )</li>
          <li> a modal title (actually a h2 tag because you will (or should) get an h1 already on main content. </li>
          <li> some content (you can put whatever here) </li>
        </ul>
      <li> a modal footer </li>
  </ul>
  <li> a modal backdrop (this darken background that usualy shows behind the modal window) </li>
  <li>the button that will trigger the modal.</li>
</ul>


At this point it only seems a mess if you try to render it because of course, we need <strong>styles</strong>.

<strong> <code>!important:</code> All styles here will be written in SASS/SCSS which is preferred due to maintainability of it versus plain CSS.</strong>

step one:
<pre>
.modal-btn { 
  font-size: 20px; 
  font-weight: bold; 
  text-decoration: none; 
  color: #282828; 
  border-radius: .4em; 
  border: 1px solid lightblue; 
  height: fit-content; 
  padding: 5px 15px;
  &:hover, &:hover p {
    background: lightblue;
  }
}
</pre>

This only gives a little shape to the "open modal" button, you can build what you like but it needs to be a link html tag ( &lt;a> ) below you'll see why.

<pre>
.modal-window { 
  position: fixed; 
  left: 50%; 
  top: 50%; 
  right: 50%; 
  bottom: 50%; 
  transform: translate(-50%, -50%); 
  z-index: 999;
  visibility: hidden; 
  opacity: 0; 
  pointer-events: none; 
  transition: opacity 0.25s;
</pre>

Here we set the modal-window as hidden, setting it fixed to the middle of the screen, disabling the pointer-events (must not be necessary but depending the layout could cause a miss-click on a hidden element so it's preferred) and applying a transition that will apply on opacity change.

*Note that i didn't close the <code>.modal-window</code> selector, this is because we will add more content here. In fact, all the code that left will be wrapped inside <code>.modal-window</code> selector

<pre>
  &:target { 
    visibility: visible; 
    opacity: 1; 
    pointer-events: auto; 
    +.modal-backdrop { 
      background-color: rgba(0, 0, 0, 0.75); 
      position: fixed; 
      top: 0; 
      right: 0; 
      bottom: 0; 
      left: 0; 
      z-index: 990;
    }
  }
</pre>

Now I'm using <ode>:target</code> pseudo-selector inside <code>.modal-window</code>. The & means an addition, so it will translate into:
<code> .modal-window:target </code> when parsed into plain CSS.

<strong>What means target pseudo-selector?</strong>

An element will be targeted (:target = true) when a hash with it's ID is on the URL.

Example:
<code> Base URL: </code>
https://myawesomeproject.com/
<code> Base URL + hash </code>
https://myawesomeproject.com/#about-us

This means that, if there's an html element with the id attribute with "about-us" as value (i.e. <code> div id="about-us" </code> ), it will be targeted.

Now you can understand why the "open modal" button must be a link tag instead a button, and why the href attribute on it is the same than the ID of the modal-window.

After this point, let's take it back to the main thread. What I'm doing when .modal-window is :target?

Making .modal-window visible, increasing the opacity to 1 (which means 100%) and enabling all pointer-events on it.

In addition, we need to show the backdrop too, so there's the sibling selector + pointing to the <code>.modal-backdrop</code>, where we set some properties to make it visible and positioned behind the modal (z-index 990 while .modal-window has 999).

Note that backdrop is an &lt;a> tag too, that points to #! as well as close button on modal-footer and the &times; on the top right of the modal.


<pre>
  &>div.modal-content { 
    width: 900px; 
    max-width: 90vw; 
    position: fixed; 
    top: 50%; 
    left: 50%; 
    transform: translate(-50%, -50%); 
    padding: 10px 30px; 
    background: #fff; 
    max-height: 90vh; 
    overflow-y: auto;
  }
</pre>

Properties for the specific content container on the modal.
Fixed to the centre, with max height and width properties and scrollable if needed.

<pre>
 .modal-close:not(.btn) { 
    position: fixed; 
    right: 15px; 
    top: 5px; 
    font-size: 1.2rem; 
    text-decoration: none; 
  }
  .modal-close, .modal-close:visited { 
    color: #212121; 
  }
  .btn.modal-close { 
    font-size: 1.2rem; 
    text-decoration: none; 
    border: 1px solid #333; 
    padding: 10px 15px; 
    background: #fff; 
    border-radius: .4em; 
  }
  .btn.modal-close:hover { 
    background: #f0f0f0; 
  }

</pre>

These are styles for the &times; and the close buttons (actually there are links, for the same reason I told you before).
As you can see the close &lt;a> tags have href="#!". This removes any target without reloading the page (which will happen with a void href attribute).


<pre>
  .modal-footer { 
    width: 100%; 
    display: flex; 
    justify-content: flex-end; 
    margin: 15px 0px 0px 5px;
  }
}
</pre>

Finally the modal-footer and the closing bracket for <code>.modal-window</code>, with the close button aligned to the right and with a bit of margin.

All together will look like this:

<pre>
.modal-btn { 
  font-size: 20px; 
  font-weight: lighter; 
  text-decoration: none; 
  color: #282828; 
  border-radius: .4em; 
  border: 1px solid lightblue; 
  height: fit-content; 
  padding: 5px 15px;
  &:hover, &:hover p {
    background: lightblue;  
  }
}
.modal-window { 
  position: fixed; 
  left: 50%; 
  top: 50%; 
  right: 50%; 
  bottom: 50%; 
  transform: translate(-50%, -50%); 
  z-index: 999;
  visibility: hidden; 
  opacity: 0; 
  pointer-events: none; 
  transition: opacity .25s;
  &:target { 
    visibility: visible; 
    opacity: 1; 
    pointer-events: auto; 
    +.modal-backdrop { 
      background-color: rgba(0, 0, 0, 0.75); 
      position: fixed; 
      top: 0; 
      right: 0; 
      bottom: 0; 
      left: 0; 
      z-index: 990;
    }
  }
  &>div.modal-content { 
    width: 900px; 
    max-width: 90vw; 
    position: fixed; 
    top: 50%; left: 50%; 
    transform: translate(-50%, -50%); 
    padding: 10px 30px; 
    background: #fff; 
    max-height: 90vh; 
    overflow-y: auto;
  }
  .modal-close:not(.btn) { 
    position: fixed; 
    right: 15px; 
    top: 5px; 
    font-size: 1.2rem; 
    text-decoration: none; 
  }
  .modal-close, .modal-close:visited { 
    color: #212121; 
  }
  .btn.modal-close { 
    font-size: 1.2rem; 
    text-decoration: none; 
    border: 1px solid #333; 
    padding: 10px 15px; 
    background: #fff; 
    border-radius: .4em; 
  }
  .btn.modal-close:hover { 
    background: #f0f0f0; 
  }
  .modal-footer { 
    width: 100%; 
    display: flex; 
    justify-content: flex-end; 
    margin: 15px 0px 0px 5px;
  }
}

</pre>


There are loads of styles that you can edit as you like to add your own flavour.


<strong> What about responsiveness?</strong>
There are two properties for <code>div.modal-content</code> that are doing the trick:

<pre>
    width: 900px; 
    max-width: 90vw;
    max-height: 90vh;
</pre>

This will make the modal show at 900px by default on desktop and 90% of window height on any resolution as maximum (default height is auto, it will not grow more than needed vertically, but if it grows more than 90% of viewport height, it will cause a natural and vertical scroll.
When you are on a device with a width less than 900px, max-width will take effect, avoiding horizontal scroll. Setting the modal max-width at 90% of viewport width.

{% codepen https://codepen.io/joelbonetr/pen/GRpVJzK %}


<hr>

You can find the complete code <a target="_blank" href="https://codepen.io/joelbonetr/pen/GRpVJzK">here on codepen</a>. Fork it to use codepen as playground for editing it. If you already have a codepen account simply press Control + S (Command + S on Mac) and it will be forked into your dashboard.


<strong>Footnotes</strong>

Hope you learnt something with this article, I'll try to post next parts ASAP with new components that usually you find done with javascript, but without javascript. Stay tunned!


Best regards