Having trouble with CSS :before pseudo-element and sibling selector?

Looking for a CSS solution to change the appearance of a triangle when the .hide class is activated within my markup.

I attempted using a pseudo-element like so:

summary:before  {
    content: '\25BC';
}
summary:before   + .hide  {
    content: '\25BA';
}

However, this method did not work as expected. The arrow failed to update despite seeing summary:before + .hide in DevTools.

Any suggestions on achieving the desired effect without resorting to JavaScript, relying solely on CSS?

var $div = $('summary');

$div.on('click', function() {
  $('ul').toggleClass('hide');
});
.wrapper {
  width: 300px;
  height: 300px;
  background-color: #ecf0f1;
}
.details {
  outline: none;
  background-color: #95a5a6;
  cursor: pointer;
  position: relative;
}
summary {
  outline: none;
  margin-left: 30px;
}
summary:before {
  content: '\25BA';
  font-size: 12px;
  position: absolute;
  top: 2px;
  left: 13px;
}
summary:before + ul.hide {
  content: '\25BC';
  font-size: 12px;
  position: absolute;
  top: 2px;
  left: 13px;
}
.hide {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="details">
    <summary>Sample</summary>
    <ul class="hide">
      <li>
        <input type="radio" checked/>
        <label>Label 1</label>
      </li>
      <li>
        <input type="radio" />
        <label>Label 2</label>
      </li>
      <li>
        <input type="radio" />
        <label>Label 3</label>
      </li>
    </ul>
  </div>
</div>

Answer №1

In CSS, it's not possible to select the previous element directly. The workaround is to add a class to the div and toggle it.

Take a look at the solution below.

var $div = $('summary');

$div.on('click', function() {
  $('ul').toggleClass('hide');
  $(this).toggleClass('collapse');
});
.wrapper {
  width: 300px;
  height: 300px;
  background-color: #ecf0f1;
}
.details {
  outline: none;
  background-color: #95a5a6;
  cursor: pointer;
  position: relative;
}
summary {
  outline: none;
  margin-left: 30px;
}
summary:before {
  content: '\25BA';
  font-size: 12px;
  position: absolute;
  top: 2px;
  left: 13px;
}
summary.collapse:before {
  content: '\25BC';
}
.hide {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="details">
    <summary>Sample</summary>
    <ul class="hide">
      <li>
        <input type="radio" checked/>
        <label>Label 1</label>
      </li>
      <li>
        <input type="radio" />
        <label>Label 2</label>
      </li>
      <li>
        <input type="radio" />
        <label>Label 3</label>
      </li>
    </ul>
  </div>
</div>

Answer №2

You've created 2 errors in your code.

First mistake: You duplicated the styles for your pseudo classes in two different locations.

Second mistake: Instead of applying the 'hidden' class directly to the sibling, it should be added to the parent element so that all children can be accessed.

You used the class 'hidden' when it should actually be

'expand'</code. This is because by default, your menu is closed and expands when the user clicks on the details box. I hope that clarifies things.</p>

<p><strong>I have created two codepens for you to refer to.</strong></p>

<p>In the first codepen, we expand the ul list by toggling between <code>display:block
and display:none, triggered by the jQuery class.

Example 1


In the second codepen, we use only jQuery to expand the ul list with a smooth slide animation. Additionally, the ul is hidden by default.

Example 2

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

Unbinding or undoing an 'onclick' event when another 'onclick' event is triggered

I am facing an issue where clicking on elements with 'onclick' functions works as expected, but when I click on a different element with another 'onclick' function, the first one remains active. What I actually want is for the previous ...

Perform the same actions on every element within the ul li

I'm facing an issue with my unordered list, where each list item contains a span element with an image inside. My goal is to set the background-image of each span to be the same as the image it contains, while also setting the opacity of the image to ...

IE rendering issue: jQuery image slideshow captions lack transparency

I recently created a slideshow using jQuery that features fading images with corresponding captions in individual divs. While the captions are meant to have a transparent appearance, I have encountered an issue specifically in Internet Explorer where this ...

Center contents of Bootstrap row vertically

I have a simple property grid layout in Bootstrap 3 structured as follows: <div class="row"> <div class="col-md-3"> <div id="propGrid"> <div class="row pgTable"> <div class="col col-md-12"> <d ...

The dropdown menu is unable to open directly below the main menu item

How can I make the submenu open to the left when hovering over a menu point, instead of directly under it? I have tried adjusting the position attributes but haven't found the right solution yet. The closest I got was by changing the display property ...

What's the best way to modify the style property of a button when it's clicked in

I am working with a react element that I need to hide when a button is clicked. The styles for the element are set in the constructor like this: constructor(props) { super(props); this.state = { display: 'block' }; this. ...

Ways to retrieve a JSON element and incorporate it into a CSS styling

Can someone assist me in converting a JSON object for use in CSS? I've attempted to do this using Vue.js, creating a map legend with v-for to generate the legend. However, if there is an alternative method that allows me to take a JSON object and ins ...

What is the best way to keep the navbar contained within the parent div (hero image) no matter the size of the screen?

After working on adjusting my website layout for different screen sizes, I encountered an issue with the navbar overflowing the parent image when viewed on my laptop. Despite trying both relative and absolute positioning for the .navbar element and setting ...

I want to use flexbox to center three divs on my webpage

I am trying to align 3 divs inside a flex container in the center of the page with equal distance between them and also from the edge of the page. .container { display : flex; width : 60%; } .para { width : 33%; padding : 1em; borde ...

Can the top header stay fixed to the top of the screen even when scrolling down?

Code snippet: http://jsfiddle.net/R3G2K/1/ In my project, there are multiple divs with content and each div has a header. My goal is to make the last header that goes out of viewport "sticky" or fixed at the top. I have explored various solutions for thi ...

Configure UL element as a dropdown on mobile devices and expand it to full width on desktop screens

Circumstances This HTML code is intended to create a horizontal <ul> element that spans the entire width of the <div class="list__wrapper"> container on desktop screens with a minimum width of 1024px. However, for mobile devices with a maximum ...

Vertical centering not functioning as expected due to issues with margin-top and margin-bottom

I'm having trouble centering my red block in the middle of the webpage, with the footer block at the bottom and the white block between them. Oddly enough, margin-top doesn't seem to be working for me, but left and right are functioning fine. Al ...

The dialog box is not taking up the full width of the browser window

I'm facing an issue with a dialog box that only occupies a portion of the browser width, despite having a width set to 100%. The backdrop, however, extends across the entire width. //App.js import React from "react"; import ConfirmationDial ...

Make the Angular Modal scrollable except for when a specific div is being click dragged

I am working on an Ionic app that uses AngularJS. One issue I encountered is with a modal popup that can sometimes extend beyond the visible area. To address this, we applied overflow: hidden to its CSS class. However, there is a specific functionality i ...

Designing Checkboxes and Radio Buttons

I am seeking a way to customize the appearance of checked and unchecked checkboxes using images without modifying the HTML structure. I would prefer not to add labels, classes, or IDs to the elements. The provided example only works in Webkit browsers, s ...

Is it possible to have dual images as the background for an HTML div?

Is it possible to use two images for a DIV background in CSS? I have one image that is just the background color and another that is a logo. Here's the code snippet I am currently using: .header{ background-attachment: scroll; background-clip ...

Having trouble with implementing a static URL in CSS

I'm struggling to incorporate a static file into my CSS as a background image for my website. The static URL is functioning properly when tested in HTML, but it's not working when implemented in CSS. HTML: {% extends 'base.html' %} { ...

Toggle the visibility of items based on the user's login status

Below is the code snippet for when the user is logged out: <div class="fusion-secondary-main-menu"> <div class="fusion-row"> <?php avada_main_menu(); ?> <?php avada_mobile_menu_search( ...

Using Jquery to set values for css properties

I've recently started using jquery and I have a task related to a drop-down menu that I'm struggling with: if (scrnwidth <= 761) { if (display was block) { //Defaultly testi has display:none property. testi = m ...

How can we create a touch and mouse-friendly horizontal scrolling navigation pattern?

I am trying to implement horizontal scrolling for video testimonials in a single row on a responsive website, but I am encountering an issue. The width of the container class is larger than col-12, which leaves space and causes horizontal scroll on the pag ...