The CSS command for displaying additional content is not functioning within table elements

I'm attempting to add a 'read more' arrow to the third column, which should expand the text in the first column when clicked. I have the following code, and it works fine outside of a table but not inside one. Where am I going wrong? I prefer not to use jQuery or JavaScript.

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

.read-more-target {
  opacity: 0;
  display: none;
}

.read-more-state:checked ~ .read-more-wrap .read-more-target {
  opacity: 1;
  display: block;
}

.read-more-state ~ .read-more-trigger:before {
  width: 0;
  height: 0;
  border-top: .6rem solid red;
  border-right: .6rem solid transparent;
  border-left: .6rem solid transparent;
  display: inline-block;
  content: '';
}

.read-more-state:checked ~ .read-more-trigger:before {
  transform: rotate(180deg);
}

.read-more-trigger {
  cursor: pointer;
  display: inline-block;
}

td {
  border:1px solid black;
}
<article>
    <h4>Lorem ipsum</h4>
    <input type="checkbox" class="read-more-state" id="post-1" />
    <div class="read-more-wrap">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim doloremque voluptate maiores fugiat nam ab dolorum magni eos libero laudantium, eum cupiditate atque repellendus debitis quisquam blanditiis, quis modi aliquid?</p>
        <span class="read-more-target" id="post-1">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis architecto quia modi. Optio ducimus amet aliquam, recusandae ad, cupiditate harum minima sint repellat tenetur dolores, soluta quidem est veniam reprehenderit?</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id iusto ratione error blanditiis non, commodi sequi. Quas, id. Atque harum hic quia totam qui laborum. Molestias laborum, tempora ratione aperiam.</p>
        </span>
    </div>
    <label for="post-1" class="read-more-trigger"></label>
</article>

<hr>

<table>
    <tr>
        <input type="checkbox" class="read-more-state" id="post-2" />
        <td class="read-more-wrap">Lorem ipsum dolor sit amet.
            <p class="read-more-target" id="post-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A accusantium, aperiam aut eaque eos esse fugit id, illo impedit ipsa iure mollitia natus.</p>
        </td>
        <td>100 pts</td>
        <td><label for="post-2" class="read-more-trigger">???</label></td>
    </tr>
</table>

Answer №1

Encountered the problem. Placing an <input /> directly under the <tr> is not valid. Adjust the code accordingly. Moreover, using <p> inside a <span> is not recommended. Change it to <div>:

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

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

.read-more-state:checked + table .read-more-wrap .read-more-target {
  display: block;
}

.read-more-trigger:before {
  width: 0;
  height: 0;
  border-top: .6rem solid red;
  border-right: .6rem solid transparent;
  border-left: .6rem solid transparent;
  display: inline-block;
  content: '';
}

.read-more-state:checked + table .read-more-trigger:before {
  transform: rotate(180deg);
}

.read-more-trigger {
  cursor: pointer;
  display: inline-block;
}

td {
  border:1px solid black;
}
<input type="checkbox" class="read-more-state" id="post-2" />
<table>
  <tr>
    <td>
      <div class="read-more-wrap">
        <p>Lorem ipsum dolor sit amet.</p>
        <p class="read-more-target" id="post-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A accusantium, aperiam aut eaque eos esse fugit id, illo impedit ipsa iure mollitia natus.</p>
      </div>
    </td>
    <td>100 pts</td>
    <td>
      <label for="post-2" class="read-more-trigger"></label>
    </td>
  </tr>
</table>

Answer №2

Special thanks to @Praveen Kumar for providing me with this ultimate code snippet. Since CSS lacks a parent selector, I wasn't able to simply rotate the 'read-more' triangle by 180 degrees using only the input state.

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

.read-more-target {
  opacity: 0;
  display: none;
}

.read-more-state:checked ~ .read-more-wrap .read-more-target {
  opacity: 1;
  display: block;
}

.read-more-trigger:before {
  width: 0;
  height: 0;
  border-top: .6rem solid red;
  border-right: .6rem solid transparent;
  border-left: .6rem solid transparent;
  display: inline-block;
  content: '';
}

.read-more-state:checked ~ .read-more-trigger:before {
  transform: rotate(180deg);
}

.read-more-trigger {
  cursor: pointer;
  display: inline-block;
}

td {
  border:1px solid black;
  width: 10rem;
  vertical-align:top;
}
<article>
  <h4>CSS 'read-more' table version</h4>
  <input type="checkbox" class="read-more-state" id="post-1" />
  <div class="read-more-wrap">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Enim doloremque voluptate maiores fugiat nam ab dolorum magni eos libero laudantium, eum cupiditate atque repellendus debitis quisquam blanditiis, quis modi aliquid?</p>
    <span class="read-more-target" id="post-1">
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Veritatis architecto quia modi. Optio ducimus amet aliquam, recusandae ad, cupiditate harum minima sint repellat tenetur dolores, soluta quidem est veniam reprehenderit?</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Id iusto ratione error blanditiis non, commodi sequi. Quas, id. Atque harum hic quia totam qui laborum. Molestias laborum, tempora ratione aperiam.</p>
    </span>
  </div>
  <label for="post-1" class="read-more-trigger"></label>
</article>

<hr>

<table>
  <tr>
    <td>
      <input type="checkbox" class="read-more-state" id="post-2" />
      <span class="read-more-wrap">
        Lorem ipsum dolor sit amet.
        <span class="read-more-target" id="post-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A accusantium, aperiam aut eaque eos esse fugit id, illo impedit ipsa iure mollitia natus.</span>
      </span>
    </td>
    <td>100 pts</td>
    <td>
      <label for="post-2" class="read-more-trigger"></label>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="read-more-state" id="post-3" />
      <span class="read-more-wrap">
        Lorem ipsum dolor sit amet.
        <span class="read-more-target" id="post-3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A accusantium, aperiam aut eaque eos esse fugit id, illo impedit ipsa iure mollitia natus.</span>
      </span>
    </td>
    <td>100 pts</td>
    <td>
      <label for="post-3" class="read-more-trigger"></label>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="read-more-state" id="post-4" />
      <div class="read-more-wrap">
        Lorem ipsum dolor sit amet.
        <span class="read-more-target" id="post-4">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A accusantium, aperiam aut eaque eos esse fugit id, illo impedit ipsa iure mollitia natus.</span>
      </div>
    </td>
    <td>100 pts</td>
    <td>
      <label for="post-4" class="read-more-trigger"></label>
    </td>
  </tr>
</table>

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

Tips for adjusting border colors based on the current time

Trying to change the border color based on the opening hours. For example, green border from 10am to 9:29pm, yellow from 9:30pm to 9:44pm, and orange from 9:45pm until closing at 10pm. The issue is that the colors change at incorrect times beyond midnight. ...

Aligning elements vertically in Bootstrap 4 with the power of flexbox

Seeking assistance with a frustrating issue in Bootstrap 4: I tried to implement the align-self-start flexbox alignment using the code from this page . However, the example provided on that page does not seem to work. Is there a simpler way to achieve the ...

Achieving a Transparent Flash overlay on a website without hindering its usability (attention, interaction, form submissions, etc.)

Currently, we are attempting to overlay a transparent flash on top of an iframe which loads external websites. Is there a method to configure the page in a way that allows the transparent flash to be displayed while still allowing interaction with the und ...

Is there a way to repurpose a function to work with both ids and classes?

My current code is affecting all elements instead of just the intended one. I've experimented with classes and ids, ruling out those as potential issues. I'm hoping for my JavaScript to target only the selected element, not all of them. Check ou ...

Is there a way to retrieve the background URL from CSS using Selenium Webdriver?

I am attempting to verify the URL of an image within a div element's background property in the CSS for the webpage. However, when I retrieve the CssValue and display it, it appears to be blank. The HTML structure for the div is as follows: <div ...

When hovering over the bootstrap dropdown, the main dropdown remains a clickable link

How can I create a bootstrap dropdown menu that activates on hover, while still allowing the main button to link to a different page when clicked? Below is the HTML code: <div class="body-wrap"> <div class="container"> <nav class="navbar n ...

Issues with Media Query in Mobile Device Footer not Resolving

Despite searching through numerous pages on Stack, I have not been able to find a solution. I am attempting to remove the footer on mobile devices using a media query. Unfortunately, it does not seem to be effective on the device, although it works when th ...

Create a minimalist Vue table design that enforces a maximum of 2 or 3 columns within

Is there a way to make my v-simple table with v-chips and v-badges align correctly in two or three column peer cell format? Here is the table for reference: Currently, I only have scoped styling applied: <style scoped> .time-slot-x-small { border- ...

Combining Node and React: Using CSS Class Names with Hyphens

Currently, my project utilizes react-starter-kit and includes several react components, each with its own styling file (scss). Every component imports the styling file at the top like this: import s from './Header.scss'; When it comes to using ...

Changing the background color completely

Need help with CSS! I have two divs, one with a background color of #000 and the other with a transparent PNG image as the background. I want to override the background color with transparency from the PNG file so that the background doesn't cover th ...

Duplicate label issue with 'No files chosen' message on Bootstrap form control

After migrating to webpack for managing our assets (js, scss, ...), everything seemed to be working smoothly. However, we encountered a minor issue where the input label behind the file-selector-button is overflowing with its neighbor, as shown in the exam ...

Tips for personalizing the FileField in wtforms to function as an image button

I'm attempting to display an image from my project's directory as an icon instead of the standard "choose file" button. Unfortunately, my attempts thus far have been unsuccessful. Not only is the image not loading, but the original button is only ...

The window.addEventListener function is failing to work properly on mobile devices

Hey there! I am facing an issue in my JS code. I wrote this code because I want the menu to close when a visitor clicks on another div (not the menu) if it is open. The code seems to be working fine in developer tools on Chrome or Firefox, but it's no ...

Enhance the appearance of the navbar on mobile devices by customizing the styling in Bootstrap 5

Hi everyone, this is my first time posting a question here so please be gentle :) I recently implemented a script to change the CSS of my navbar when scrolling. window.addEventListener("scroll", function () { let header = document.querySelector(".navbar") ...

What is the best way to include an arrow in a dropdown menu?

I've been working on restyling a Select box and I want to add a CSS arrow that rotates as the Select box expands. However, I'm struggling to align the arrow properly with the Select box. I've tried using code from the internet, but it hasn&a ...

When viewing a page in IE 9 within a frame, not all CSS styles are properly rendered, causing it to enter Quirks mode

When a web page is loaded within a frame, not all of its styles are applied. The specific stylesheet used for the div elements is as follows: div.super{ position:absolute; font-family: Helvetica; font-size: 0.75em; padding:4px; overflo ...

hiding html elements by using the display property set to none instead of physically removing

I am currently utilizing an if-else statement to display different HTML structures. As a result, the entire HTML is being rendered twice. Is there a way we can utilize 'display: none' instead? I attempted to use it in th ...

Adjust the height of the div container and implement a vertical scroll feature on the fixed element

I created a fiddle and was hoping to enable scrolling, but I have been unable to find a solution. http://jsfiddle.net/sq181h3h/3/ I tried both of these options, but nothing seems to be working: #league_chat { overflow-y:scroll; } #league_chat { ...

Update to the viewport meta tag in iOS 7

Anyone experiencing problems with the viewport tag after updating to iOS7? I've noticed a white margin on the right side of some sites. Adjusting the initial scale to 0.1 fixed it for iPhone but made it tiny on iPad 3, which is expected due to the low ...

How can I display a button (hyperlink) on a new line using CSS or jQuery?

I am looking to create a new line after the last input of my form. On this new line, I want to add a "remove-link". This is my HTML: <div class="subform dynamic-form"> <label for="id_delivery_set-0-price"> Price: </label> <inp ...