Multiple Read more issue in css, how do i solve that?


Tags: html,css,html5,css3

Problem :

I am working on a 'read more' content using css. I got a solution as to how do it online. But when am making multiple read more contents, there is a problem.

When clicking on the second 'read more' button, the 1st set of text contents opens up instead of the second set of contents. How do I solve that?

article {
    margin-bottom: 3rem;
    position: relative;
    *zoom: 1;
}

article:before, article:after {
    content: "";
    display: table;
}

article:after { clear: both }

article section:last-of-type {
    display: none;
    visibility: hidden;
}

section {
    -webkit-transition: .125s linear;
    -moz-transition: .125s linear;
    -ms-transition: .125s linear;
    -o-transition: .125s linear;
    transition: .125s linear;
}

input[type=checkbox] {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    width: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
}

[for="read_more"] {
    position: absolute;
    bottom: -3rem;
    left: 0;
    width: 100%;
    text-align: center;
    padding: .65rem;
    box-shadow: inset 1px 1px rgba(0, 0, 0, 0.1), inset -1px -1px rgba(0, 0, 0, 0.1);
}

[for="read_more"]:hover {
    background: rgba(0,0,0,.5);
    color: rgb(255,255,255);
}

[for="read_more"] span:last-of-type {
    display: none;
    visibility: hidden;
}

input[type=checkbox]:checked ~ section {
    display: block;
    visibility: visible;
    width: 100%;
}

input[type=checkbox]:checked ~ figure { width: 100% }

input[type=checkbox]:checked ~ [for="read_more"] span:first-of-type {
    display: none;
    visibility: hidden;
}

input[type=checkbox]:checked ~ [for="read_more"] span:last-of-type {
    display: block;
    visibility: visible;
}
<div>
    <article>
        <input type="checkbox" id="read_more" role="button">
        <label for="read_more" onclick=""><span>Read More</span><span>Hide</span></label>     
            <section>
            <p>Vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id </p>
            </section>    
        	<section>
        	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        	<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est</p>
    		</section>
    </article>​
</div>

<div>
    <article>
        <input type="checkbox" id="read_more" role="button">
        <label for="read_more" onclick=""><span>Read More</span><span>Hide</span></label>     
            <section>
            <p>Vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id </p>
            </section>    
        	<section>
        	<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
        	<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est</p>
    		</section>
    </article>​
</div>

Here is the jsfiddle



Solution :

You need to change the id and for attributes in checkbox and label elements respectively. Something like:

<input type="checkbox" id="read_more1" role="button"/>
<label for="read_more1" onclick=""><span>Read More</span><span>Hide</span></label>

And then in CSS code a possible selector could be [for^="read_more"] instead [for="read_more"].

Check a demo here.


    CSS Howto..

    How do I position a Silverlight Control

    How to control the css for two instance of Google Maps autocomplete on the same page?

    How to disable flow around elements in table (CSS)

    How to keep a very tight control over the order of CSS files in Yii Framework

    How to style
  • s inside
      as a table with custom height?
  • Stylized HR is not showing properly in IE

    How to make this HTML change work? [closed]

    How to make horizontal lines sit beside circle bullets using CSS

    How to reposition CSS-hover popups to stay within a fixed frame?

    Find how an element's css has changed after jQuery “toggle” function has been used

    How to wrap between letter and punctuation to avoid extra blank space in the end of line?

    How to segregate these divs with 'position:absolute' into different groups?

    Multiple css styles applied to one element, how to force one style over the other

    How to select -webkit-filter: drop-shadow with jQuery?

    How to prevent a closing tag from being removed?

    How do you manage css stylesheet files and inclusions in a large ruby on rails application?

    How to add css library to project generated with angular-cli@webpack

    How to set a External SVG color in HTML using CSS?

    Using jquery to swap text, how to revert other text to original?

    How to make this loading overlay effect responsive?

    pure css horizontal accordion: how to have panel open on load

    How to get the scroll bar with CSS overflow on iOS

    How do I control the spacing between my child links in my CSS driven menu dropdown?

    How to set expanded menu to be shut on page load

    how to make a CSS animation end point the right edge of the browser

    How to rotate image in relation to mouse position?

    How to apply “-webkit” css rules using jquery .css

    How to load multiple CSS files within a JS script

    how to make textarea looks like input[text] on form invalid

    How to hide header on scroll down, show on scroll up like linkedin menu