Tips for Styling "Opened" Elements within the <Summary><Details> Web Design in HTML using CSS? (a color coding challenge)

I've dedicated a significant amount of time crafting my ultimate HTML FAQ with detailed summaries styled in CSS.

There's one aspect that continues to elude me - achieving the perfect orange background color for expanded topics:

My goal is for a selected topic to remain highlighted in orange even after clicking, regardless of the mouse movement as depicted in the image below.

The issue arises when the mouse moves away causing the background of the selected topic to revert to white.

If I assign the details an orange color, all items end up turning orange, thus defeating the purpose of highlighting specific ones.

I desire closed items to have a white background and opened ones to display in orange.

What could I possibly be missing?

Bonus: Could you please streamline my CSS code by eliminating unnecessary elements, creating a more elegant and clutter-free design?

https://i.sstatic.net/VZ1NF.png

html, body {background: #EEE}
article{display:block;
    margin: 40px auto;
    width: 700px;
    }


details{
    background: white;
    overflow: hidden;
    outline: 1px solid black;
    padding: 20px;
    margin-bottom: 1px;
    }

details[open] summary:after {content: "\25BC"}
details:hover {background: #FFD700;}
details[open] summary {
    color: white;
    background: #FFD700;
    margin-bottom: 0px;
    font-weight: bold;
    transition: all .5s ease;
    }

summary {
    cursor: pointer;
    user-select: none;
    display:block;
    padding: 20px;
    margin:-20px;
    }

summary:after {
    content: "\25C1";
    float:right;
    margin-right: 0px; /* Arrow margin */
    width: 20px;
    }
/*details summary::marker {display:none} hides the standard > sign.  Apparently not needed anymore? */
/*summary:focus {outline:0 !important}; hides blue border in chrome. Apparently not needed anymore? */
<article>

<details><summary>First Topic</summary>
Additional explanation additional explanation<br>
Additional explanation additional explanation
</details>

<details><summary>Second Topic</summary>
Additional explanation additional explanation<br>
Additonal explanation additional explanation
</details>

<details><summary>Third Topic</summary>
Additional explanation additional explanation<br>
Additional explanation additional explanation
</details>

</article>

Answer №1

You have applied the background only to the details element on hover, but you should also apply it to the details element with the open attribute

html, body {background: #EEE}
article{display:block;
    margin: 40px auto;
    width: 700px;
    }


details{
    background: white;
    overflow: hidden;
    outline: 1px solid black;
    padding: 20px;
    margin-bottom: 1px;
    }

details[open] summary:after {content: "\25BC"}
details:hover, details[open] {background: #FFD700;}
details[open] summary {
    color: white;
    background: #FFD700;
    margin-bottom: 0px;
    font-weight: bold;
    transition: all .5s ease;
    }

summary {
    cursor: pointer;
    user-select: none;
    display:block;
    padding: 20px;
    margin:-20px;
    }

summary:after {
    content: "\25C1";
    float:right;
    margin-right: 0px; /* Arrow margin */
    width: 20px;
    }

/*details summary::marker {display:none} hides the standard > sign.  Apparently not needed anymore? */
/*summary:focus {outline:0 !important}  hides blue border in chrome. Apparently not needed anymore? */
<article>

<details><summary>First Topic</summary>
Extra explanation extra explanation<br>
Extra explanation extra explanation
</details>

<details><summary>Second Topic</summary>
Extra explanation extra explanation<br>
Extra explanation extra explanation
</details>

<details><summary>Third Topic</summary>
Extra explanation extra explanation<br>
Extra explanation extra explanation
</details>

</article>

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

Customizing Mouse Pointers in CSS

My goal is to customize the click pointer on a website using CSS. I have successfully changed the default cursor, but now I am seeking help in changing different 'versions' of the cursor. Here's my current attempt at customizing the pointe ...

Unable to Achieve Full Height with Vuetify Container

Challenge: I'm facing an issue with my <v-container> component not consistently spanning the entire height of the application. Despite trying various methods such as using the fill-height property, setting height: 100%;, height: 100vh;, and expe ...

How to use CSS to dynamically adjust the maximum width of an overflow container based on its children's content

I have a container with an overflowing <div> that displays multiple <ul> <li> lists. Each list always contains the same number of bordered <li> items, resembling a table. However, there may be instances where a <ul> has only ...

Exploring the world of web scraping by using Python to loop through HTML elements

Created a Python script to extract information from the website regarding its asset listings. So far, I have successfully retrieved the name of the first coin "Bitcoin," but I'm unable to get the ticker. With 27 pages on the site, each containing 40 ...

disable border-collapse styling in ASP.NET DataList

Currently, I am new to the world of ASP.NET and I am facing a challenge in styling a DataList. Lately, I have developed a fondness for rounded corners on borders, and I am attempting to incorporate this effect into all my pages by implementing it across al ...

What is the best way to locate an element using text in xpath?

Having an issue with this HTML source code: <td class="specs_title"> Processortype <a href="#" class="info-link"> <img src="x.jpg" title="" height="16" alt="" width="16" /> <span class="info-popup"> <span class="hd">Processor ...

I am looking to use flexbox and Vue.js to organize my sent messages in blue on the right and received messages in yellow on the left. How can I achieve this grouping effectively?

I've successfully built a messenger system using Vue.js with a Laravel backend. My goal is to display messages from myself (Jim, the logged in user) on the right side in blue and messages from Debbie (the recipient) on the left side in yellow, similar ...

Enhance your website with footer animations using jQuery!

I am implementing a sticky footer on my website to keep the footer at the bottom using CSS. I want the footer to initially be collapsed and expand when the user clicks on a button, swapping the "expand" link with a different container div inside the footer ...

Revealing CSS elements moving from the left to the right

On my website, I have implemented two different menu bars. The first one, in black, stays fixed at the top of the page. The second one, in orange, becomes sticky once it reaches the black bar. At the same time, a small logo image is revealed, previously hi ...

What is the best method for eliminating tiny spaces between images that are aligned horizontally?

Check out this link for more details: . I am looking to eliminate the slim vertical gaps between the images in html5 without resorting to using the table cellpadding="0" cellspacing="0". Any ideas on how to achieve this? ...

The method WebKitBrowser.StringByEvaluatingJavaScriptFromString does not provide any output

After running all JavaScript, I need to retrieve the HTML content. However, I am facing an issue using WebKit.NET where the method WebKitBrowser.StringByEvaluatingJavaScriptFromString does not return anything, even with a simple alert(). When I try passi ...

Steps to make a sub Post or page on WordPress

Seeking to add sub posts within my website's main posts. The URL structure for the main post will be: site.com/post-title I envision sub posts following this format: site.com/post-title/subpost1 site.com/post-title/subpost2 site.com/post-title/ ...

How can you style a two-item list in Material-UI React to display the items side by side?

I have a list that contains two items: 'label' and 'value'. This is the current layout: https://i.stack.imgur.com/HufX7.png How can I rearrange the items on the right to be positioned next to the label on the left? https://i.stack.im ...

How can I modify the border color of a Material Ui TextField component?

I've been struggling to figure out how to customize the color of the TextField border. I want to change it from the default grey to black. Below is a simplified version of my UpdatePage.js file: const useStyles = makeStyles(() => ({ updatePage ...

Exploring the World of Print CSS, Embracing Bootstrap, and Master

In continuation of my previous query, I am facing an issue with setting up a filemaker foreach loop to display a group of images along with their names and IDs, accompanied by checkboxes. Upon checking the relevant checkboxes, the corresponding images are ...

Is it necessary for every single product to have its very own webpage?

As a newcomer to the world of web development, I'm uncertain about the standard practices. Currently, I am working on creating a small e-commerce website with a wide range of products. Is it recommended to give each item its own dedicated page? Furthe ...

It appears that the JS function is not being triggered

I am facing an issue with my JavaScript code where a ball is not showing up even though it should. I have an array with only one element, and when that element matches with "F2", the ball is supposed to appear. I suspect there might be a problem with my us ...

Various settings in JQuery UI Slider

Does anyone know how to customize the steps in a jQuery UI slider? I want to set specific values as steps, like 0, 1200, 2000, 2200, and 3000. Any suggestions on how to achieve this? const rangeSliderInit = () => { const valueArray = [0, 400, 1000, 1 ...

AngularJS templateUrl changes on page refresh``

I am currently using an angular packet template that consists of 11 pages, each with a corresponding button. The route code for this template is as follows: state('app.pagelayouts.fixedsidebar1', { url: "/fixed-sidebar", templateUrl: "assets ...

Angular click event to compute a function and display the result on a separate page

Currently, I am developing a BMI app using Ionic Angular (latest version). The objective is to create a button that collects input data from fields and triggers a method to validate the inputs. Once validated, the app should display the results page with t ...