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

What steps should I take to eliminate the gap in my fixed bottom navbar?

I'm looking to revamp my menu layout by aligning the links with the social media icons and home button on the same line. The links are nested inside a div within the main navbar class (streetworn-demos), while the social media icons and homepage butt ...

Adjust the positioning of divs to account for changes in scale caused

I have 3 div blocks arranged on a web page, with the first one bigger and located on the left side while the other two smaller ones are positioned on the right side. I want their relative positioning to remain fixed across all browsers and zoom levels. Cur ...

Embracing Elegance with jQuery

Is there a way to customize the appearance of my list (<ul>) in the following code snippet? $.widget( "custom.catcomplete", $.ui.autocomplete, { _renderMenu: function( ul, items ) { var self = this, currentCategory = ""; ...

The focus is on the last row that was selected

I've noticed that when I check and select a row in ui-grid, only the last selected row is focused with a background color. Should all rows be automatically painted by default? Thank you. ...

Dynamic Column Image and Text Display

I am working on a website where I am trying to have an image on the left and a paragraph on the right, but when the screen size is reduced, I need the layout to switch so that the image is at the top and the paragraph is at the bottom. Even though I have ...

Adjust all children's height to match that of the tallest child

My nearly completed jQuery slideshow is working well except for one issue - the height of the slideshow remains fixed at the height of the first displayed slide, causing problems. View the issue here: . I have tried different jQuery solutions from StackOve ...

Implementing a dual hover effect on a Div element

I am working with HTML code that includes an image and text <div class="subcontainer1"> <img src="a.png" alt="" class="imgcolumn"> <h3 class="header3">Hello</h3> </div> This setup places the content above the image. ...

Using the ID selector to create a media query

My website is live, and I'm working on making it mobile-friendly. Most of the site works well on mobile, but I'm having trouble centering a jquery lightbox function using media queries. I'm not sure if my syntax is wrong or if there's a ...

Center a vertically aligned element following a heading, with the content dynamically generated and enclosed in a fixed container

My mind is racing with this problem and I'm not sure if it's solvable, but before throwing in the towel, I thought I'd seek help from the Internet Gods. Here's a visual representation of what I'm aiming for: I want the text to al ...

adjust the @page rule size for different classname matches during printing

I am attempting to adjust the @page rule size based on matching classnames, however, my research has only led me to pseudo-classes which do not seem to solve my issue. Here is a snippet of my CSS: @media print { @page { margin: 13.2mm 0mm 12.9mm; ...

Monochrome tabletop solid in one hue

I'm trying to eliminate the space between the columns in my table so it looks solid. Here's the CSS style I've been using: <style> tr, th, td {margin:0;padding:0;border:0;outline:0;font-size:90%;background:transparent;} table{ margin: ...

Pictures without a set width

Hey there! I'm working on a responsive website and facing an issue with image resizing. When I shrink the screen size, the images also shrink along with it. How can I set a fixed width for the images? I tried switching from rem to px but that didn&apo ...

Measuring the space between components

I am looking for a way to dynamically calculate distance on a canvas, similar to the example shown in the figure. I want to be able to drag the highlighted joints in order to adjust the span for calculating distances. Are there any plugins available for th ...

Is there a bug with text rotation in CSS 3?

I'm having trouble understanding how text rotation works in CSS3. For example, when I try to rotate text like you can see here, the rotated text never seems to be in the correct location, despite setting properties like: right: 0; bottom: 0; There ...

Applying a CSS border to a div that perfectly encloses a span, adjusting to the

My goal is to create a nested structure where a div contains a span element like this: ---------- |Sentence| ---------- ----------------- |It's a looooong| |sentence | ----------------- While it works well for short sentences, long ones end u ...

The issue with clip-path not functioning correctly in Safari persists

My list of clipped elements is causing an issue when using the CSS3 clip-path method to clip an image into an SVG path. The clipping works perfectly, but in Safari (V 12.1.4), the position of the clipped element changes with each box. The element ...

Initiate an animation upon reaching a designated element using Jquery scroll event

Hey there! I've been trying to create an animation without using any plugins, but unfortunately, I haven't had much luck so far. Here's the code I've been working on. If anyone can help me figure this out, I'd really appreciate it ...

Setting up a Bootstrap tokenfield for usage with a textarea

I was attempting to set up a tokenfield on a textarea with increased height, but it is showing up as a single-line textbox. How can I modify the tokenfield to function properly with a textarea? <textarea name="f1_email" placeholder="Enter Friends' ...

Updates to the Tailwind file are not appearing on the page after reopening the file

Every time I reopen the project, the file tailwind properties do not get rebuilt and if I add new properties like text-color, they are also not reflected. Any suggestions on how to solve this issue? I need the project to update and take in new properties ...

Customize Cell Styling with Bootstrap Full Calendar CSS

I am attempting to implement a Bootstrap calendar feature where cells are colored green if the timestamp is greater than today's date. This can be achieved by: $checkTime > $today cell.css = green background I came across this code snippet on St ...