Can an image in CSS trigger the display of a <div> upon clicking it?

Here is an example of my HTML code:

<html>
<head>
<style>
img.settings {
    hight: 100px;
    width: 20px;
}

div.settings {
    position: fixed;
    left: 0px;
    top: 0px;
    right: 100%;
    bottom: 0px;
    background-color: #000000;
}
</style>
</head>
<body>
<div class="head">
    <img class="settings" src="settings.png" />
</div>
<div class="settings"></div>
</body>
</html>

I am looking to modify the div#setting property from right: 100%; to right: 50%; when the image is clicked. Essentially, I want the <div> to be displayed or hidden by clicking the image. Is it possible to achieve this using only CSS?

Answer №1

Without a parent selector, achieving this effect with CSS alone is not possible.

If the elements were siblings, you could assign tabindex="-1" to the image and utilize something similar to:

img.settings:focus + div.settings {
  right: 50%;
}

img.settings {
  height: 100px;
  width: 20px;
}
div.settings {
  position: fixed;
  left: 0px;
  top: 0px;
  right: 100%;
  bottom: 0px;
  background-color: #000000;
}
img.settings:focus + div.settings {
  right: 50%;
}
<img class="settings" src="settings.png" tabindex="-1" alt="[img]" />
<div class="settings"></div>

This effect will only be temporary and will revert when clicking elsewhere.

If you wish to avoid hiding div.settings when clicking on itself, consider:

img.settings:focus + div.settings, div.settings:focus {
  right: 50%;
}

img.settings {
  height: 100px;
  width: 20px;
}
div.settings {
  position: fixed;
  left: 0px;
  top: 0px;
  right: 100%;
  bottom: 0px;
  background-color: #000000;
}
img.settings:focus + div.settings, div.settings:focus {
  right: 50%;
}
<img class="settings" tabindex="-1" src="settings.png" alt="[img]" />
<div class="settings" tabindex="-1"></div>

Answer №2

To achieve this functionality, it is possible to use hacks like the checkbox hack, as mentioned in this article. However, it is recommended to implement this using JavaScript for a more effective solution.

var settingsImage = document.querySelector('img.settings'),
    settingsDiv = document.querySelector('div.settings');

settingsImage.addEventListener('click', function toggleSettingsDiv() {
  settingsDiv.classList.toggle('hidden');
});
img.settings {
    hight: 100px;
    width: 20px;
}

div.settings {
    position: fixed;
    left: 0px;
    top: 0px;
    right: 100%;
    bottom: 0px;
    background-color: #000000;
}
.hidden {
  display: none;
}
<div class="head">
    <img class="settings" src="settings.png" />
</div>
<div class="settings hidden">Lorem Ipsum</div>

It's important to note that jQuery is not necessary to accomplish this task, despite what some may suggest.

Answer №3

It seems unlikely that simple CSS alone can achieve this effect. One possible solution is to use the :active flag, but it only activates while the user is holding down the click.

For a more robust approach, JavaScript can be utilized to add a class to the element upon triggering an onclick event. The recommended method would be to utilize the element.classList feature available here: https://developer.mozilla.org/en-US/docs/Web/API/element.classList

Answer №4

Utilizing JavaScript and implementing the .css() function within the .click() event is a straightforward approach. By following a tutorial, you can easily grasp this concept independently. Feel free to reach out for specific code examples if needed!

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

Does embedding an Iframe for external files from your server enhance the loading speed of the current page compared to directly loading it on the page?

I'm facing an issue with the loading time of a Facebook post on my webpage index.php. The current method of using the embedded post provided by Facebook is taking too long to load. My solution is to create a separate page, post.php, where only the Fac ...

Importing CSS with Node Sass using npm

Instead of inlining css within sass, I attempted to utilize the npm package Node Sass CSS importer. Unfortunately, I am struggling to get it to function properly. I typically use npm as my build tool: "build:css": "node-sass some_path/style.scss some_pa ...

Struggling to get the max-width property to function properly with a Bootstrap multi-level dropdown menu

I have a multi-level Bootstrap dropdown menu that I am customizing based on the design featured in this bootsnipp. However, I am encountering an issue where some of my menu items are too lengthy, so I need to restrict their width to a maximum of 400px. Ad ...

Emails are not responsive to media queries

I've experimented with multiple media queries in an attempt to make this work on both iPhone (landscape/portrait) and desktop, but I'm struggling to achieve the desired outcome on both simultaneously. While some of my classes are functioning cor ...

How come only half of my website is inaccessible when utilizing a sticky navigation menu, and what can be done to remedy this issue?

I've successfully implemented a menu on my website that stays fixed at the top of the page while scrolling. However, there is an issue where I am unable to click on the top 50% of the screen, rendering the website unusable. You can view a test copy of ...

How can I apply bold styling to primary nodes in jsTree using CSS?

I am currently utilizing the in conjunction with asp.net mvc. My objective is to apply bold styling to the text using CSS within the main nodes, specifically those with arrows (refer to the image below). https://i.sstatic.net/xg3uF.png Below is the code ...

Ensure that the text stays aligned at the center within an `<li>` tag, especially when there is an image along with the text within the element

I have a basic list structured like this: <ul> <li>ONE</li> <li>TWO</li> <li>THREE</li> <li>FOUR</li> <li>FIVE</li> <li>SIX</li> <li><hr&g ...

Modifying the opacity of the placeholder in Stripe Element is not possible

The placeholders of Stripe React elements come with a CSS property Opacity: 1. Unfortunately, this property cannot be modified using the style object. However, other ::placeholder CSS properties can be adjusted as needed. Style Object: const iframeStyles ...

Positioning elements on the web page using CSS tiles

Greetings! I am currently working on a website that utilizes tiles similar to this: https://i.sstatic.net/OzKQv.png Everything seems to be in place, except for one tile that is not aligning with the rest. I am struggling to figure out how to position it c ...

Leveraging CSS attribute selectors within JSX using TypeScript

With pure HTML/CSS, I can achieve the following: <div color="red"> red </div> <div color="yellow"> yellow </div> div[color="red"] { color: red; } div[color="yellow"] { color: yellow; ...

Why does a React error keep popping up when trying to set a background-image in my CSS?

I've been working on my React project and I can't figure out why I keep encountering this error. I double-checked the URL paths and made sure they were named correctly, yet the error persists. Here is a snippet of my CSS: background-image: url ...

Adjust the size of horizontal <li> elements dynamically to keep them in a single row

I've been trying to set up a 'gallery' section on one of my pages, with three elements featured. However, I'm running into an issue where resizing the window or viewing on a smaller screen could cause overlaps or force a second row with ...

CSS for when the mouse hovers over an element

Two php files and a js file are involved in this scenario. The issue at hand is that when the div is clicked, it navigates to a new page with an appended # to the URL, causing it to scroll to the specified comment ID. However, instead of scrolling to the c ...

Adjusting the vertical alignment of wrapped divs with jQuery toggle

I am currently working on a project where I have a set of dynamically generated divs, each containing rows of content with three columns. Using jQuery toggle, I am trying to show and hide certain elements within the divs without affecting their layout. Her ...

Elegant rounded bordered sidebar design to highlight selected navigation items using HTML and CSS

I am looking to replicate the sidebar design displayed in the image below by using HTML and CSS. While I have successfully rounded the borders on the left side of a selected link, I am stuck when it comes to rounding the borders on the right side. https:/ ...

In react-native, when the string includes spaces, it will result in a line change

I am currently utilizing react-native in combination with styled-components. Whenever a string is typed into the text and followed by pressing the spacebar, it either results in too many line breaks or creates excessive empty spaces. An example of this i ...

Align text in the center of a static container

I am facing the challenge of aligning four h4 tags in the center of their respective fixed divs, all set to width: 100% to ensure their background-color: rgba(255, 255, 255, 0.5); covers the entire browser width. Despite trying various combinations of CSS ...

Using Cascading Style Sheets (CSS) to make posts appear above an image in a scrolling format

Can anyone guide me on how to show the text above an image contained within a scroll bar, similar to the picture below? Despite trying position property and searching online, I haven't found a solution yet. Your help would be greatly appreciated. ...

Why is the Border Radius hiding the corners of my image?

I've never experienced this issue before, but no matter how much padding I add to the picture, the edges are still slightly covered up. Here is an example image (notice that it doesn't happen for every icon - the third one has a normal round back ...

Sophisticated method in JavaScript to conceal and reveal div elements

My knowledge of front-end web development is strongest in HTML and CSS, but when it comes to JavaScript, I feel like there must be a more efficient way to achieve the functionality I want. On my website, I have a set of <li> items that, when clicked ...