Incorporate details and summary elements for collapsible inline content

Exploring a solution to the following challenge:

Looking for a method to create collapsible buttons (or similar objects) that:

  1. can be displayed in the same line
  2. reveal their content between the current and next line when clicked
  3. are responsive
  4. allow independent styling of title and content

Created a gif to visualize the desired outcome:

https://i.sstatic.net/Rfzu0.gif

Experimented with collapsible objects and details/summary tags so far.

Collapsible objects seem to address only features 2. and 4. As positioning the content manually affects responsiveness, achieving simultaneity is challenging.

Here's some code related to collapsible objects:

// JavaScript function to handle collapsible elements
      var coll = document.getElementsByClassName("collapsible");
      var i;
      
      for (i = 0; i < coll.length; i++) {
        coll[i].addEventListener("click", function() {
          this.classList.toggle("active");
          var content = this.nextElementSibling;
          if (content.style.maxHeight) {
            content.style.maxHeight = null;
          } else {
            content.style.maxHeight = content.scrollHeight + "px";
          }
        });
      }
.collapsible {
  border: none;
  background: none;
  outline:none;
  padding: 0;
  font-size: 1em;
  color: green;
}
.ccontent {
  max-height: 0;
  overflow: hidden;
  background-color: #d3d3d3;
}
Does <button class="collapsible">this</button> work?
      <div class="ccontent">Yes!</div>
      Good job!
      <hr>
      Does <button class="collapsible">this</button> and <button class="collapsible">this</button> work?
      <div class="ccontent">no</div><div class="ccontent">yes</div>
      Ops

Details/summary tags offer easier implementation but pose styling challenges.

Details/summary tags cover features 1. and 3., along with some aspects of feature 4. Combining both methods would ideally satisfy all requirements!

details {
  display: inline;
  color: red;
  padding: 0;
  background-color: #d3d3d3;
  cursor: help;
}
details > summary {
  display: inline;
  background: none;
  color: green;
  list-style: none; 
  outline:none; 
}
details > summary::-webkit-details-marker { 
  display: inline;
  display: none;
}
Does <details><summary>this</summary>so and so</details> work?
      <p>Ops</p>
      <hr>
      Does <details><summary>this</summary>not</details> and <details><summary>this</summary>fully</details> work?
      <p>Ops</p>

In summary, collapsible buttons prioritize features 2 and 4, while details/summary tags cater to features 1 and 3. A combination could potentially fulfill all four requirements!

Is it feasible to achieve all four features using just one element?

Answer №1

Are you finding success with a setup like this?

This method involves absolutely positioning the details element (with no defined top, so it defaults to its natural position) and then applying margin-bottom to the collapsible element along with negative margin-top to the details element.

.container {
  position:relative;
  margin:2em;
}
.details {
  display:none;
}
.collapsible.onactive:active,
.collapsible.onfocus:focus,
.collapsible.ontoggle:focus
{
  margin-bottom:1.5em;
}
.collapsible.ontoggle:focus {
    pointer-events:none;
}

.collapsible.onactive:active + .details, 
.collapsible.onfocus:focus + .details, 
.collapsible.ontoggle:focus + .details 
{
  display:block;
  margin-top:-1.15em;
  position:absolute;
  left:0;
  right:0;
  background:yellow;
}
<div class=container>

    Does 
    <button class="collapsible onactive">on active</button><span class=details>Yes!</span>
    work? Good job!work? Good job!work? Good job!work? Good job!work? Good job!
    <button class="collapsible onfocus">on focus</button><span class=details>Yes!</span>
    work? Good job!work? Good job!work? Good job!work? Good job!work? Good 
    job!work? Good job!work? 
    <button class="collapsible ontoggle">toggle</button><span class=details>Yes!</span>
    Good job!work? Good job!work? Good job!work? Good job!

</div>

Answer №2

Unfortunately, achieving inline details with pure CSS is not feasible due to browsers ignoring details { display: inline; }. I personally consider this behavior a bug.

To work around this limitation, I utilize JavaScript to swap out details with span.details, and summary with span.summary.

This workaround will present closed details as [Spoiler], while open details will appear as [Spoiler: spoiler text].

Without JavaScript enabled, the open [Spoiler] will be displayed as a display:block element.

I have yet to discover an alternative method in CSS to address this issue.

<html>
<head>
  <meta charset="utf-8">
  <title>inline spoiler</title>
  <style>
    /* inline details to show a spoiler text */
    /* CSS limitation: the spoiler is not inline */
    details, span.details {
      /* CSS: this only works for content after <details> as long <details> is closed */
      display: inline;
    }
    details>summary, span.details>span.summary {
      /* remove the triangle left of <summary> */
      display: inline;
      /* <summary> is clickable. show it */
      cursor: pointer;
    }
    // More CSS styles here
  </style>
  <script>
    function makeInlineDetails() {
      Array.from(document.querySelectorAll("details")).map(details => {
        // JavaScript logic here
      });
    }
  </script>
</head>
<body>

<p>
  <button onClick="makeInlineDetails()">makeInlineDetails()</button>
</p>
<div>
  (text before spoiler, text before spoiler, text before spoiler,
  text before spoiler, text before spoiler, text before spoiler,
  text before spoiler, text before spoiler, text before spoiler)
  <details>
    <summary>Spoiler</summary>
    <span>
      (spoiler text, spoiler text, spoiler text,
      spoiler text, spoiler text, spoiler text,
      spoiler text, spoiler text, spoiler text)
    </span>
  </details>
  (text after spoiler, text after spoiler, text after spoiler,
  text after spoiler, text after spoiler, text after spoiler,
  text after spoiler, text after spoiler, text after spoiler)
</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

The Invalid_grant OAuth2 error occurs when attempting to access the Google Drive API using

SOLVED! The issue was resolved by correcting the time on my Linux Server. Google's server was blocking access due to incorrect time settings. I used the following command on my Server to synchronize the time: ntpdate 0.europe.pool.ntp.org Original P ...

Change a date and time structure into just the time using JavaScript

I'm faced with a date and time form as shown below: 2021-06-04 11:23:37.000 I am attempting to convert this form to just the time: Post conversion: 11:23:37 Please note that the original form 2021-06-04 11:23:37.000 remains constant. ...

Utilizing a switch statement for efficiently loading multiple objects within a Three.JS framework

Currently, I am working on a web 3D model viewer that utilizes Three.JS. My goal is to enable users to select different models from a dropdown list using dat.GUI. To achieve this, I have been experimenting with a switch statement, drawing inspiration from ...

Passing data between child components using Vuejs 3.2 for seamless communication within the application

In my chess application, I have a total of 3 components: 1 parent component and 2 child components. The first child component, called Board, is responsible for updating the move and FEN (chess notation). const emit = defineEmits(['fen', 'm ...

CSS background color using over 20 different shades

I am seeking a way to dynamically change the background color of an object based on a specific value. Specifically, I want to create a percentage bar with 100 different background colors possible, corresponding to each percent (excluding when empty). The w ...

The functionality of the dropdown does not seem to be working properly when the checkbox is

So, I have a simple task where if a checkbox is selected, a drop-down should appear. If the checkbox is unselected, the dropdown should not show up. However, it seems like there's an issue with my code. Here's a snippet below to give you an idea ...

What sets apart a post API call from form submission using the post method?

Is it possible to call the payment gateway from Node.js using a POST API call? I understand that traditionally the payment gateway is called through form submission with the method set as post, which redirects to a new page. However, if I use a POST API ...

No content in webpack result file when using express and react

Trying to implement webpack into the basic react comment list tutorial has been a bit of a challenge for me. Everything seems to be functioning properly, but my output file never actually contains any content. Let's take a look at my webpack configur ...

Why are the CSS files not being compiled in Rails' application.scss?

After spending the last 3 hours working hard to include my CSS files in the vendor/stylesheet/ directory in application.scss, I'm still not having any luck. The path to the CSS files is: vendor/assets/stylesheets/ Here is my code in Application.scs ...

Tips for customizing a link element that routes to a React component

App.js: import './App.css'; import logo from './images/logo.png'; import Home from './components/Home'; import About from './components/About'; import Calculators from './components/Calculators'; import Cla ...

Achieve a full-screen width container in Bootstrap 4 without using the container-fluid class

Is there a way to make the container have a 100% width using media queries so that the elements are contained in a larger screen but not contained in a small one, or vice versa? This code is just an example that used to work with Bootstrap v4 alpha 6, but ...

Excluding a Spec File in Your Protractor Configurations

I have a scenario where I have 10 spec files all named *********.test.js. I need to run tests on all 9 of these files, excluding the file named Idontwantyou.test.js. Currently, I am locating my spec files in the config.file using: specs: ['*.test.js ...

Transform Typescript into compiled css files without using any additional tools

Currently, I am working on a monorepo project where the main project relies on another project called components. When running the entire monorepo, the main project utilizes webpack.dev while the components project simply uses the TypeScript compiler. It l ...

Issues arise when attempting to use Bootstrap Dropdowns with an Angular 8 component

I'm working on implementing a dropdown menu using the Bootstrap 4.3 class, but I'm encountering an issue where nothing happens after selecting an option. I am using Angular version 8. <div class="dropdown"> <button class="btn btn-seco ...

Menu overlay conceals company logo

On smaller screen sizes, my hamburger menu is blocking part of the logo. I'm struggling to figure out how to ensure that the logo appears in front of the menu before it is clicked. Here's an example. I attempted setting the background of label . ...

Tips for creating a margin on an HTML button's background color

My bootstrap button has dimensions of 46 x 96px and a border radius of 22px. When hovered, I would like the background color to only extend up to 40 x 90px, without filling the entire button. I understand this can be achieved using a box-shadow, but since ...

I am currently having trouble with req.query not functioning correctly within Next.js for reading query parameters

I am facing an issue while working with a REST API in Next.js 13. I have created the API, which can be accessed at http://localhost:3000/api/portfolio. However, when I try to filter the data using query parameters like http://localhost:3000/api/portfolio?s ...

Implementing a horizontal card layout using a for loop in a Flask application

Currently, I have implemented a for loop to showcase a list of cars in a card layout. However, upon integrating the jinja template, the cards now appear vertically instead of horizontally. Below is the HTML code snippet: {% for car in cars %} < ...

JavaScript heap running out of memory after upgrading from Angular 11 to versions 12, 13, or 14

I need assistance with resolving a JS heap out of memory issue that has been occurring when trying to start the local server ever since migrating from Angular 11 to Angular 12 (or 13 or 14, all versions tested with the same problem). This occurs during th ...

Get rid of the margins on your Wordpress theme

Currently, my Wordpress site is using the Tesseract theme which can be found at (http:// instantiwebs . com) I am interested in removing the left/right margins on all elements, similar to what has been done on this website: . Interestingly enough, they al ...