CSS-only "Read More" button failing to function

I am currently utilizing Wordpress and attempting to integrate the following code: https://codepen.io/Idered/pen/AeBgF

HTML:

    .read-more-state {
    display: none;
    }

    .read-more-target {
    opacity: 0;
    max-height: 0;
    font-size: 0;
    transition: .25s ease;
    }

    .read-more-state:checked ~ .read-more-wrap .read-more-target {
    opacity: 1;
    font-size: inherit;
    max-height: 999em;
    }

    .read-more-state ~ .read-more-trigger:before {
    content: 'Show more';
    }

    .read-more-state:checked ~ .read-more-trigger:before {
    content: 'Show less';
    }

    .read-more-trigger {
    cursor: pointer;
    display: inline-block;
    padding: 0 .5em;
    color: #666;
    font-size: .9em;
    line-height: 2;
    border: 1px solid #ddd;
    border-radius: .25em;
    }
<div>
    <input type="checkbox" class="read-more-state" id="post-2" />

    <ul class="read-more-wrap">
    <li>lorem</li>
    <li>lorem 2</li>
    <li class="read-more-target">lorem 3</li>
    <li class="read-more-target">lorem 4</li>
    </ul>
  
    <label for="post-2" class="read-more-trigger"></label>
    </div>

Nevertheless, I am encountering some challenges - a gray line keep showing up (which can be toggled) instead of the show more button - mirroring the issue faced by this user as well

You can view our shared problem here:

Your assistance on this matter would be highly appreciated - thank you in advance.

Answer №1

The issue lies in the hierarchy of your label within your website. The tilde operator ~ only targets sibling elements, not their children or grandchildren. Let me illustrate with an example. Take a look at the code snippet below.

.test ~ .example {
   background:green;
}
<p class="test">Test</p>
<p class="nothing">Nothing</p>
<p class="example">Example</p>
<p><span class="example">Second Level Example</span></p>

The .test class currently selects all sibling elements with the .example class and applies a green background color. We have introduced the .example class for the second level. Your CSS will not target those elements. In your original code, the label is nested under the p, which is why it does not display the content "Show More" for your label. One solution is to remove the p element and keep the label on the same hierarchical level. Alternatively, update your CSS as shown below to target the child elements.

.read-more-state ~ p > label.read-more-trigger:before {
content: 'Show more';
}

.read-more-state:checked ~ p > label.read-more-trigger:before {
content: 'Show less';
}

Here is an implementation of your example similar to the live code.

.read-more-state {
    display: none;
    }

    .read-more-target {
    opacity: 0;
    max-height: 0;
    font-size: 0;
    transition: .25s ease;
    }

    .read-more-state:checked ~ .read-more-wrap .read-more-target {
    opacity: 1;
    font-size: inherit;
    max-height: 999em;
    }

    .read-more-state ~ p > label.read-more-trigger:before {
    content: 'Show more';
    }

    .read-more-state:checked ~ p > label.read-more-trigger:before {
    content: 'Show less';
    }

    .read-more-trigger {
    cursor: pointer;
    display: inline-block;
    padding: 0 .5em;
    color: #666;
    font-size: .9em;
    line-height: 2;
    border: 1px solid #ddd;
    border-radius: .25em;
    }
<div>
    <input type="checkbox" class="read-more-state" id="post-2" />

    <ul class="read-more-wrap">
    <li>lorem</li>
    <li>lorem 2</li>
    <li class="read-more-target">lorem 3</li>
    <li class="read-more-target">lorem 4</li>
    </ul>
  
    <p><label for="post-2" class="read-more-trigger"></label></p>
    </div>

Answer №2

I believe this is what you are looking for :)

.show-more-state {
  display: none;
}

.show-more-target {
  opacity: 0;
  max-height: 0;
  font-size: 0;
  transition: .25s ease;
}

.show-more-state:checked ~ .show-more-wrap .show-more-target {
  opacity: 1;
  font-size: inherit;
  max-height: 999em;
}

.show-more-state ~ .show-more-trigger:before {
  content: 'Display more';
}

.show-more-state:checked ~ .show-more-trigger:before {
  content: 'Display less';
}

.show-more-trigger {
    cursor: pointer;
    display: inline-block;
    padding: 0px 10px;
    color: #fff;
    font-size: .9em;
    line-height: 2;
    border: 1px solid #dea119;
    border-radius: 20px;
    background-color: #b9890c;
    box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.42), 0 0 0 1px rgba(0,0,0,0.08);
}

/* Additional styling */ 
body {
  padding: 2%;
}

p {
  padding: 2%;
  background: #0a0a0a;
  color: #ffffff;
  border: 1px solid #000000;
  border-radius: .25em;
}
p:hover {
    box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.42), 0 0 0 1px rgba(0,0,0,0.08);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <input type="checkbox" class="show-more-state" id="post-1" />

  <p class="show-more-wrap">Lorem ipsum dolor sit amet, consectetur adipisicing elit. <span class="show-more-target">Libero fuga facilis vel consectetur quos sapiente deleniti eveniet dolores tempore eos deserunt officia quis ab? Excepturi vero tempore minus beatae voluptatem!</span></p>
  
  <label for="post-1" class="show-more-trigger"></label>
</div>

<div>
  <input type="checkbox" class="show-more-state" id="post-2" />

  <ul class="show-more-wrap">
    <li>Bootstrap</li>
    <li>Javascript</li>
    <li class="show-more-target">HTML 5</li>
    <li class="show-more-target">Css 3</li>
  </ul>
  
  <label for="post-2" class="show-more-trigger"></label>
</div>

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

Ensure that selected options are unique across multiple selections

Could you help me with a question that involves matching pairs of words in both Russian and English? <div class="form-group" id="question4"> <label for="q4FirstSelectEN">4</label> <div class="row"> <div class="col-lg ...

Creating a consistent visual appearance for disabled inputs in Bootstrap 4 to match regular inputs

In most cases, when an input is disabled in bootstrap 4, it appears greyed-out to show that it's inactive. I am looking into removing this grey background for a different look. ...

I need to figure out how to incorporate a specific feature into my personal list by leveraging Javascript

Looking at the list below, it's a simple one that allows users to add and remove items while also changing their order using buttons. However, I want to enhance this feature by removing the "up" button for the first item and the "down" button for the ...

Steps for including a solid bottom border in a toggled navigation bar that is responsive

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document&l ...

The snap drag position resets to its original placement when dragged for a second time

I am working on creating a unique slider that allows users to click and drag a circle along a curved path. The functionality is almost there, but whenever I drag the circle to a certain position, it snaps back to its original location. var s=Snap(' ...

Contrasting CSS Attribute Selectors and Pseudo-Elements in Input Tags

<input type="text" placeholder="nothing" title="google" required> **CSS Content:** input[type="text"]{ border: 2px solid red; } input::placeholder{ color:green; } input[title="google"]{ background ...

Is there a way for me to obtain the present value of the chosen button in the list below?

I am working with a group of three buttons labeled English, Hindi, and Urdu. How can I retrieve the value of the selected button in a JavaScript function? <div class="btn-group" data-toggle="buttons"> <label class="btn btn-primary active"> ...

Issues with zoom functionality not functioning properly within IE11

I am currently developing an application with Angular that is designed to be compatible with tablets and touch-enabled devices. One of the key features I want to implement is the ability for users to zoom/scale up the app, especially for those with visual ...

What is the best way to target the shadow DOM host element specifically when it is the last child in the selection?

I want to style the shadow DOM host element as the last child. In this particular situation, they are currently all green. I would like them to be all red, with the exception of the final one. class MyCustomElement extends HTMLElement { constructor() ...

Using jQuery, generate a div element and display it in full screen mode

Is it possible to dynamically create a full-screen div using jQuery directly from JavaScript without the need to first define it in the HTML and then make it visible with jQuery or CSS? UPDATE After some troubleshooting, I found a solution to my issue: ...

The z-index of two elements seems to be behaving differently than anticipated

I am encountering an issue with the code provided below, which is a simplified version of a larger problem. Can you explain why, if: .div_A {z-index: 10;} < .div_C {z-index: 20;} the button inside div_C appears behind div_A? I need to understand the ...

What are some techniques for concealing a rendered element retrieved using the fetch API?

Currently, I am grappling with a coding challenge that requires me to extract data from https://jsonplaceholder.typicode.com/users using the fetch API function along with bootstrap and jquery. To display the data in a div element, I utilized the array.map( ...

What is the best way to highlight and extract only the white-coded texts on VSCode to facilitate the process of translating webpages manually?

Currently, I'm engaged in a project where my task involves duplicating an entire website using HTTrack. However, I only need to copy the main page and web pages that are one link deep. Upon copying the website, my next challenge is to translate all of ...

What is the best way to ensure that a div appears below, rather than alongside, another one

I need help positioning my divs on the webpage. Currently, my map div is appearing next to the list instead of below it. The height of the list can vary as it is generated dynamically. I want the map div to be on the right side with the list displayed on t ...

Setting a div's background using JavaScript works flawlessly on jsfiddle.net, but encounters issues when implemented in actual code

contains the files you need. For reference, here is the link to the code on I have attempted to remove all unnecessary elements from the actual project, but it has not made a difference. document.getElementById("image").style.backgroundImage = "url(" + d ...

Setting the height of the text area in a three-column layout cannot be achieved using a percentage value

I'm working on creating a 3-column layout with two text areas. The issue I'm facing is that when I adjust the width of the text areas to create columns, the height shrinks. I need a solution that will be compatible with HTML5 browsers - support f ...

Chrome fails to load webfont upon page initialization

Encountering a problem with loading a WebFont specifically on Chrome version "48.0.2564.82 m (64-bit)". The issue arises from the webfont not being applied upon page load or navigation, unless I manually refresh the page. Below is the snippet of code fro ...

Progress Bar HTML with PHP

Looking for a way to enhance user experience on a website form that utilizes PHP for data submission? How about incorporating a progress bar that visually indicates the form is processing in order to prevent users from clicking the button multiple times af ...

Problem with ngStyle: Error indicating that the expected '}' is missing

I encountered an error in the Chrome console when trying to interpret the code below: <div [ngStyle]="{'width': '100%'; 'height': '100%'; 'background-size': 'cover'; 'background-repeat&ap ...

stop cascading style sheets from being overwritten

In my ASP.NET web forms project, I am utilizing Telerik controls. The Problem In one instance, I need to retrieve HTML data from the database, which includes a lot of CSS, images, and HTML content. Here is an excerpt of my code: <telerik:RadLabel ...