Is there a way to eliminate the :target pseudo-class from the entirety of a page using just HTML and CSS?

For an assignment, I have created a menu with a list of items positioned on the left side of the screen. The requirement is that when a menu item is clicked, it should move outside the menu using only CSS/HTML.

I managed to achieve this by utilizing the :target pseudo-class along with the href tag. However, I encountered an issue where I couldn't revert the menu back to its original position since a menu item always remains targeted and stays outside the menu.

Initially, I thought clicking on the targeted element again would remove the pseudo-class, but that did not work as expected.

I believe the optimal solution to return the menu to its initial layout is to un-target the current item without selecting another one.

Here is the HTML structure:

<a class="card" id="card1" href="#card1">
          <div class="avatar"></div>
          <div class="info">
            <p>Leah Shapiro</p>
            <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cca0a9ada4e2bfa4adbca5bea38caba1ada5a0e2afa3a1">[email protected]</a></p>
          </div>
        </a>

        <a class="card" id="card2" href="#card2">
          <div class="avatar"></div>
          <div class="info">
            <p>Rob Been</p>
            <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f737a7e77316c77727077596c70727">[email protected]</a></p>
          </div>
        </a>

        <a class="card" id="card3" href="#card3">
          <div class="avatar"></div>
          <div class="info">
            <p>Peter Hayes</p>
            <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e9858c8881c79ae788899099869883828d8916"><abbr title="Email blocked due to scraping protection"></abbr></a></p>
          </div>
        </a>
        

The CSS styling for the menu:

.list {
  height: 100%;
  width: 200px;
  background: #dddddd;
  float: left;
}

.list .card {
  border: solid;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 55px;
}

:target {
  z-index: 1000;
  background-color: red;
  position: absolute;
  left: 300px;
  top: 200px;
}

Link to the codepen demo: https://codepen.io/maydanachi/pen/QXPYvy

Although I found various JavaScript solutions for this issue, the rules specify that only CSS/HTML should be used.

Answer №1

Instead of using :target, opt for :focus

body,
html {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
  position: relative;
}

.screen {
  height: 100%;
  width: calc(100% - 200px);
  background-color: tomato;
  float: right;
}

.list {
  height: 100%;
  width: 200px;
  background: #dddddd;
  float: left;
}

.list .card {
  border: solid;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 55px;
}

.list a {
  text-decoration: none;
}

:focus {
  z-index: 1000;
  background-color: red;
  position: absolute;
  left: 300px;
  top: 200px;
}

.list .card:hover {
  cursor: pointer;
  background-color: rgba(143, 143, 143, 0.8);
}

.list .card:active {
  background-color: teal;
}

.list .avatar {
  height: 48px;
  width: 48px;
  border-radius: 50%;
  background-color: #ccc;
  user-select: none;
}

.list .info {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  user-select: none;
}

.list .info p {
  margin: 0;
  padding-left: 5px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="./style.css" />
</head>

<body>
  <div class="list">

    <a class="card" id="card1" href="#card1">
      <div class="avatar"></div>
      <div class="info">
        <p>Leah Shapiro</p>
        <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b5bcb8b1f7aab1b8a9b0abb699beb4b8b0b5f7bab6b4">[email protected]</a>>
      </div>
    </a>

    <a class="card" id="card2" href="#card2">
      <div class="avatar"></div>
      <div class="info">
        <p>Rob Been</p>
        <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="660a03070e48150e07160f140926010b070f0a4805090b">[email protected]</a>>
      </div>
    </a>

    <a class="card" id="card3" href="#card3">
      <div class="avatar"></div>
      <div class="info">
        <p>Peter Hayes</p>
        <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e222b2f26603d262f3e273c210e29232f2722602d2123">[email protected]</a>>
      </div>
    </a>

    <a class="card" id="card4" href="#card4">
      <div class="avatar"></div>
      <div class="info">
        <p>Dave Catching</p>
        <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87ebe2e6efa9f4efe6f7eef5e8c7e0eae6eeeba9e4e8ea">[email protected]</a>>
      </div>
    </a>

    <a class="card" id="card5" href="#card5">
      <div class="avatar"></div>
      <div class="info">
        <p>Josh Homme</p>
        <p><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="147871757c3a677c75647d667b547379757d783a777b79">[email protected]</a>>
      </div>
    </a>

  </div>
  <div class="screen"></div>

</body>

</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating JavaScript unit tests into an established website

I am facing a challenge with testing my JavaScript functions in the context of a specific webpage. These functions are tightly integrated with the UI elements on the page, so I need to be able to unit-test the entire webpage and not just the functions them ...

Refresh all fields for various products with a single click of a button

Our Magento multi-vendor site allows vendors to easily manage their products. For each single product, vendors can view and update information such as price, selling price, quantity, and more from their vendor account. We have implemented a feature where ...

Methods to disregard class prefix in css document

When I inspect elements in the Chrome console, I notice styles applied to certain divs/buttons like this: /* Sample Chrome browser styles */ .ItemClass1-0-3-171.ItemClass2-0-3-173: { background-color: "red" } I'm wondering how I can def ...

having difficulty in configuring the precise CSS for the login page

I am struggling to replicate a specific login page design. The design I want can be found at this link: https://i.sstatic.net/smEWd.jpg However, my current implementation looks like this: https://i.sstatic.net/DvpEL.jpg import React, { useState } from ...

I want my website to display tech bargains in a unique way - whenever I showcase 9 items, the next item I add will automatically show up below the ninth product rather than

I've been working on a website that showcases tech deals, and I've encountered an issue where if I insert a product after the ninth one, it doesn't align properly. Instead of appearing to the left of the ninth product, it displays below it. ...

The interaction of a horizontal scroll bar on a CSS Grid layout triggers the appearance of a vertical scroll bar

I am facing a challenge with my HTML layout. In Chrome, when the horizontal scroll bar is visible, a vertical scroll bar also appears. However, in Firefox, everything works fine. This issue seems to be related to the height of the div not auto-scaling prop ...

iPhone has trouble accessing alternative forms of content

<object width="300" height="100" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param value="5352f5c8e96c251fb9d79890f2294608.swf" name="movie"> <!--[if !IE]>--> <object width="300" height="100" data="5352f5c8e96 ...

Extract image styles from a different webpage

My website is located at: I'm attempting to replicate the testimonial photo effect that can be seen on this website: https://i.sstatic.net/70Jqd.png What CSS code do I need to include in order to make the image with the class "client-pic" circula ...

Are section elements aware of their positional order in the document structure?

For the Terms of Service page, I decided to incorporate both the ol element and the section element for two specific reasons: Ensuring each item is guaranteed in sequential order Allowing each part to be sectioned off individually ol { list-style ...

Tips for transitioning from an old link to a new link within an MVC framework

Is there a way to redirect an old link to a new link in MVC? In Google search results, my old URL was cached as www.abcd.com/product?id=64 however, my new URL is now www.abcd.com/product/sample How can I set up a redirect so that when a user clicks on th ...

A javascript text slider that dynamically updates text content sourced from PHP

Currently, I am in the process of developing a project that involves creating a dynamic slideshow of testimonials for my website. To achieve this, I am utilizing PHP and MySQL for the database backend. My goal is to use pure JavaScript to efficiently showc ...

Issue within the application causing a sudden shutdown

I have been encountering an application error in my android phonegap app. The issue arises when I transition from one page to another page, resulting in the following message: "A network error occurred.(file:///android_asset/www/home/home.html?userid=91 ...

What is the purpose of the `hidden` class for the `<hr>` element?

I've been tasked with updating a webpage that was passed down to me. One thing I'm working on is adding a print.css file, as it didn't have one before. The original developer included a... <hr class="hidden" /> The main css file has ...

Struggling with CSS, seeking improvements to align with my previous coding style

After spending a considerable amount of time working on my game, I made the decision to change my navigation bars to buttons. However, I'm facing issues while trying to finalize the style that I had previously. Here is the old code fiddle: https://js ...

Fade out one div and then fade in a new one using Jquery

Hey there! I'm looking to create a smooth transition effect for my website where the content fades out when clicking on a menu item, and then fades in with the new content based on the link provided. Here's an example: . So basically, when I clic ...

Interactive Code Playground for JS/JQuery, HTML, and CSS

I am in the process of developing my unique code for a real-time html/js/jquery/css editor. I have successfully integrated css and html, but I am facing challenges with getting jquery or javascript to function properly. Below is an excerpt of the code caus ...

JQuery's ajax method removes HTML tags in XML content

Hey there, I'm currently working on populating my webpage elements dynamically using data from an XML file. The issue I'm facing is that when I make use of JQuery's .ajax method to fetch the XML file, it removes all my HTML tags. For instan ...

Challenges with parsing JSON using jQuery

I am attempting to retrieve data from a page that returns JSON in order to store it in an array. The current code is functional, but I am encountering difficulties when trying to pass the variable (which should contain the content) into the jQuery.parseJSO ...

JQuery displays 'undefined' on checkbox loaded via Ajax

Currently, I am utilizing a checkbox to activate my select Option tag. The select option tag and checkbox are both loaded via ajax. While the select option works perfectly, the checkbox displays as undefined. However, it functions properly in enabling my d ...

What is the best way to center a Bootstrap Navbar and set a fixed width that is neither responsive nor static?

I just started working on my webpage school project and I've managed to create a Navbar that looks decent. However, I want to resize it to be centered and have a specific width. Can anyone guide me on how to achieve this? Thank you! <!doctyp ...