1

I have a container and three elements inside it. I need to hide .short-post, show .long-post and modify the parent width.

.container, .short-post, .read-more
{
    display: block;
}

.long-post
{
    display: none;
}

.read-more:active > .short-post
{
    display: none;
}

.read-more:active > .long-post
{
    display: block;
}

.read-more:active > .container
{
    /*Previous width = 623px*/
    width: 100%;
}
<div class="container">
    <div class="short-post">
    something
    </div>
    <div class="long-post">
    something bigger
    </div>
    <span class="read-more">
    READ MORE
    </span>
</div>

I know I can't use the selector > but I can't find one that fits my code, I'm reading all references here W3Schools CSS Selectors and testing them all. Is there a way to do what I want with CSS? I know that it's possible with JavaScript.

Thadeu Fernandes
  • 483
  • 6
  • 19
  • With CSS selectors, you [can't go from child to parent](https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector); only the other way round. In other words, styling `.container` depending on one of it's child's `:active` state won't work. – domsson Jul 04 '17 at 14:51
  • @domdom Okay, what about `.read-more` to `.short-post`? Is it possible? – Thadeu Fernandes Jul 04 '17 at 14:55
  • This is straightforward with JS and messy with CSS. – sol Jul 04 '17 at 14:56
  • @DrewKennedy I know, that's why I said I know I can't use. I just wanted to show a demonstration of what I need to do. – Thadeu Fernandes Jul 04 '17 at 14:56
  • @ovokuro that's what I'm trying to accomplish, make the messy thing haha – Thadeu Fernandes Jul 04 '17 at 14:58
  • AFAIK, that won't be possible either as [you can only select the *next*, not the *previous* sibling](https://stackoverflow.com/questions/1817792/is-there-a-previous-sibling-css-selector). – domsson Jul 04 '17 at 14:58
  • I think you're going to need JavaScript. You might be able to use some wonky combination of `~` and `+`, but you might as well use JS since it's super fast anyway. – Drew Kennedy Jul 04 '17 at 14:58

2 Answers2

3

CSS only approach using HTML details element

details {
  position: relative;
  width: 630px;
}

summary {
  display: block;
}

summary::-webkit-details-marker {
  display: none;
}

summary::before {
  content: 'READ MORE';
  position: absolute;
  bottom: -20px;
  cursor: pointer;
}

details[open]>summary {
  font-size: 0;
}


details[open]>summary::before {
  content: 'READ LESS';
  font-size: 16px;
}

details p {
  margin-top: 0px;
}
<details>
  <summary>something</summary>
  <p>something bigger</p>
</details>
sol
  • 19,364
  • 5
  • 33
  • 48
  • 1
    Nice, I didn't know about summary. I've always done it like this https://jsfiddle.net/link2twenty/mdx7x37r/ – Andrew Bone Jul 04 '17 at 15:19
  • 1
    @AndrewBone Yeah that's a nice approach, but I think I'd use JS most of the time to be honest.. – sol Jul 04 '17 at 15:21
1

only with css Read more button just refer this

html { background: white }

* {
    margin: 0;
    padding: 0;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    box-sizing: border-box;
}

body {
    max-width: 480px;
    width: 90%;
    margin: 3em auto;
    font-size: 75%;
    line-height: 1.3rem;
    font-family: sans-serif;
    position: relative;
    *zoom: 1;
}

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

body:after { clear: both }

p { margin-bottom: 1.3rem }

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

.container:before, .container:after {
    content: "";
    display: table;
}

.container:after { clear: both }

.container figure {
    float: right;
    width: 32.5%;
}

.container .short-post {
    float: left;
    width: 62.5%;
}

.container .long-post {
    display: none;
    visibility: hidden;
}

div {
    -webkit-transition: 1s linear;
    -moz-transition: .125s linear;
    -ms-transition: .125s linear;
    -o-transition: .125s linear;
    transition: 1s 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 ~ .short-post, input[type=checkbox]:checked ~ .long-post {
    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 class="container">
<input type="checkbox" id="read_more" role="button">
    <label for="read_more" onclick=""><span>Read More</span><span>Less</span></label>     
    <div class="short-post">
      <p>Owls are a group of birds that belong to the order Strigiformes, constituting 200 extant bird of prey species. Most are solitary and nocturnal, with some exceptions (e.g. the Northern Hawk Owl). </p>
    </div>  
 
<div class="long-post">
    <p>Owls hunt mostly small mammals, insects, and other birds, although a few species specialize in hunting fish. They are found in all regions of the Earth except Antarctica, most of Greenland and some remote islands. Though owls are typically solitary, the literary collective noun for a group of owls is a parliament. Owls are characterized by their small beaks and wide faces, and are divided into two families: the typical owls, Strigidae; and the barn-owls, Tytonidae.</p>
    <p>Owls have large forward-facing eyes and ear-holes; a hawk-like beak; a flat face; and usually a conspicuous circle of feathers, a facial disc, around each eye. The feathers making up this disc can be adjusted in order to sharply focus sounds that come from varying distances onto the owls' asymmetrically placed ear cavities. Most birds of prey sport eyes on the sides of their heads, but the stereoscopic nature of the owl's forward-facing eyes permits the greater sense of depth perception necessary for low-light hunting. Although owls have binocular vision, their large eyes are fixed in their sockets — as are those of other birds — so they must turn their entire head to change views. Owls can rotate their heads and necks as much as 270 degrees in either direction. As owls are farsighted, they are unable to see clearly anything within a few centimeters of their eyes. Caught prey can be felt by owls with the use of filoplumes — like feathers on the beak and feet that act as "feelers". Their far vision, particularly in low light, is exceptionally good.</p>
</div>

</div>
Udhay Titus
  • 5,480
  • 4
  • 21
  • 39