Nihilus.JS
Nihilus.JS

Reputation: 95

Create a CSS button to show x amount of text when clicked and change the size of the div tag

I have a project I'm doing for a friend. I want to display some text and when a "learn more" button is clicked, the remainder of the text will appear. I've split the visible and non-visible text into separate

tags and given them a class name - there are 3 of them.

  <div class="column">
    <h2> BCI </h2>

    <p class="visible_text">
        The Better Cotton
        Initiative (BCI) is a global
        non-profit organisation
        cemented around being
        the largest cotton sustainability
        programme in the
        world.
    </p>

    <p class="non_visible_text">
        The Better Cotton Initiative (BCI) is a global non-profit organisation
        cemented around being the largest cotton sustainability
        programme in the world. Striving towards better global cotton
        production, in terms of bettering the lives of those who produce
        it, better for the environment it grows in and better for the
        sector’s future.
        Thanks to the help of several partners, the BCI provides training
        on more sustainable farming practices to more than two million
        cotton farmers in 21 countries. In the 2017-18 cotton season,
        licensed BCI Farmers produced more than five million metric
        tonnes of ‘Better Cotton’ – that accounts for around 19% of
        global cotton production!
    </p>


    <button class="learnBtn">
        <img src="images/LearnMore.png" alt="Learn More Button">
    </button>
</div>

So when the button is clicked I want the text in the class "non_visible_text" to display below the "visible_text".

Any ideas?

Upvotes: 3

Views: 2240

Answers (3)

G-Cyrillus
G-Cyrillus

Reputation: 105873

If you look for a css option, you need an input(type=checkbox) before the element you want to update style wether it is checked or not and a label linked to it via the for attribute, so it can stand anywhere:

possible example :

#more {/* hide this one */
  position: absolute;
  right: 100vw;
}

.non_visible_text {
  display: none; 
}

label[for="more"] {/* use a label linked to the checkbox, style it like a button if you wish */
  -moz-appearance: button;
  appearance: button;
}

label[for="more"]:after {
  content: ' more.'/* update text */
}

#more:checked~.non_visible_text {
  display: block;/* show hidden tag */
}

#more:checked~label[for="more"]:after {
  content: ' less.'/* update text */
}
<div class="column">
 <h2>HTML Ipsum Presents</h2>

  <p class="visible_text">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
  <input type="checkbox" id="more" />
  <p class="non_visible_text">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>


  <label class="learnBtn" for="more">
        see 
    </label>
</div>

Here is also a javascript method upon the text content from your button:

var myButton    = document.querySelector(".learnBtn");
var text2toggle = document.querySelector(".non_visible_text");

myButton.addEventListener("click", function() {
  if ((myButton.textContent === "see more")) {
    text2toggle.style.display = "block";
    myButton.textContent = "see less";
  } else {
    text2toggle.style.display = "none";
    myButton.textContent = "see more";
  }
});
.non_visible_text {
  display: none
}
<div class="column">
  <h2>HTML Ipsum Presents</h2>

  <p class="visible_text">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
    Mauris placerat eleifend leo.</p>
  <p class="non_visible_text">Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est.
    Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus
    lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor,
    facilisis luctus, metus</p>


  <button type="text" class="learnBtn">see more</button>
</div>

Upvotes: 1

shreyasm-dev
shreyasm-dev

Reputation: 2834

It took me a long time, here's the code. (Modified your code to work with toggle button)

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.visible {
	visibility: hidden;
}

.less::after {
	content: ' Less'
}

.more::after {
	content: ' More'
}
</style>
</head>
<body>
<h2> BCI </h2>
<p class="visible_text">
        The Better Cotton
        Initiative (BCI) is a global
        non-profit organisation
        cemented around being
        the largest cotton sustainability
        programme in the
        world.
    </p>

<button onclick="element.classList.toggle('visible'); element2.classList.toggle('more');" id='toggleBTN' class='less'>Learn</button>

<p id="toggleP" style='display:block'>
        The Better Cotton Initiative (BCI) is a global non-profit organisation
        cemented around being the largest cotton sustainability
        programme in the world. Striving towards better global cotton
        production, in terms of bettering the lives of those who produce
        it, better for the environment it grows in and better for the
        sector’s future.
        Thanks to the help of several partners, the BCI provides training
        on more sustainable farming practices to more than two million
        cotton farmers in 21 countries. In the 2017-18 cotton season,
        licensed BCI Farmers produced more than five million metric
        tonnes of ‘Better Cotton’ – that accounts for around 19% of
        global cotton production!
</p>

<script>
   var element = document.getElementById("toggleP");
   var element2 = document.getElementById('toggleBTN');
</script>

</body>
</html>

Upvotes: 0

simonwo
simonwo

Reputation: 3391

One technique you could use is making use of the :target CSS class – I use this to display stuff without having to use Javascript.

:target rules will apply whenever the id of an element matches the fragment of the URL, so anything after the #. Then, change your button into a link to #id:

.non_visible_text {
  display: none;
}

.non_visible_text:target {
  display: inline;
}

.non_visible_text:target + .visible_text {
  display: none;
}
    <p id="read-more" class="non_visible_text">
      longer text
    </p>
    
    <p class="visible_text">
      short text
    </p>


    <a href="#read-more" class="learnBtn">
        <img src="images/LearnMore.png" alt="Learn More Button">
    </a>

Upvotes: 0

Related Questions