Change the text in a CSS checkbox label when it is selected

There is an innovative Pure-CSS accordion that caught my eye, but I have a unique feature in mind that I would like to incorporate. What I am looking for is the ability for the labels to dynamically change based on the state of the checkboxes without relying on JavaScript.

Here's what I envision:

  • If the checkbox is unchecked, the question should display "more info",
  • If the checkbox is checked, it should show "less info" as the question text.

I am open to using various elements to achieve this; the label element does not necessarily have to be the one undergoing changes. However, it is crucial that the solution preserves the functionality allowing users to expand the accordion by either clicking on the text within the label element or the plus sign housed in the class plus. Importantly, the solution must be entirely free from JavaScript.

A quick note: solutions that avoid incorporating the question text with pseudo-elements will be preferred over those that do. The rationale behind this preference is my intention to utilize first-line to style the first line of the question text. Combining pseudo-elements could potentially restrict the use of first-line.

I'm curious and excited to see if this concept can be realized! Thank you for your input! This is the corresponding codepen: http://codepen.io/ihatecoding/pen/oZjVPG

This is the snippet:

@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,800);
body {
  font-family: 'Open Sans';
  font-size: 1.5em;
  background: #eee;
}


.question-content {
  width: 80%;
  padding: 20px;
  margin: 0 auto;
  padding: 0 60px 0 0;
}


.question {
  position: relative;
  background: #8FBC8F;
  margin: 0;
  padding: 10px 10px 10px 50px;
  display: block;
  width:100%;
  cursor: pointer;
}

.answers {
  background: #999;
  padding: 0px 15px;
  margin: 5px 0;
  height: 0;
  overflow: hidden;
  z-index: -1;
  position: relative;
  opacity: 0;
  -webkit-transition: .7s ease;
  -moz-transition: .7s ease;
  -o-transition: .7s ease;
  transition: .7s ease;
}

.questions:checked ~ .answers{
  height: auto;
  opacity: 1;
  padding: 15px;
}

.plus {
  position: absolute;
  margin-left: 10px;
  z-index: 5;
  font-size: 2em;
  line-height: 100%;
  -webkit-user-select: none;    
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
  -webkit-transition: .3s ease;
  -moz-transition: .3s ease;
  -o-transition: .3s ease;
  transition: .3s ease;
  pointer-events: none;
}

.questions:checked ~ .plus {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

.questions {
  display: none;
}
<div class="question-content">
<div>
  <input type="checkbox" id="question1" name="q"  class="questions">
  <div class="plus">+</div>
  <label for="question1" class="question">
    More Info Q1
  </label>
  <div class="answers">
    What if the answer is really long and wraps the whole page and you never want to finish it but you have to because its the answer!
  </div>
</div>

<div>
  <input type="checkbox" id="question2" name="q" class="questions">
  <div class="plus">+</div>
  <label for="question2" class="question">
    More Info Q2
  </label>
  <div class="answers">
    short!
  </div>
</div>
  
<div>
  <input type="checkbox" id="question3" name="q" class="questions">
  <div class="plus">+</div>
  <label for="question3" class="question">
    More Info Q3
  </label>
  <div class="answers">
    This is the answer!
  </div>
</div>
</div>

Answer №1

To achieve this effect, you can utilize the CSS content property and simply eliminate the existing "More Info" text like so:

.questions ~ .question::before{
  content: 'More Info '
}

.questions:checked ~ .question::before{
  content: 'Less Info '
}

By making use of the ::before pseudo elements, the new content will be inserted before your current "Qx" text, giving you the desired visual outcome.

For example:

https://i.stack.imgur.com/Rzy2I.gif

@import url(http://fonts.googleapis.com/css?family=Open+Sans:300,800);
body {
  font-family: 'Open Sans';
  font-size: 1.5em;
  background: #eee;
}

.question-content {
  width: 80%;
  padding: 20px;
  margin: 0 auto;
  padding: 0 60px 0 0;
}

.question {
  position: relative;
  background: #8FBC8F;
  margin: 0;
  padding: 10px 10px 10px 50px;
  display: block;
  width: 100%;
  cursor: pointer;
}

.answers {
  background: #999;
  padding: 0px 15px;
  margin: 5px 0;
  height: 0;
  overflow: hidden;
  z-index: -1;
  position: relative;
  opacity: 0;
  -webkit-transition: .7s ease;
  -moz-transition: .7s ease;
  -o-transition: .7s ease;
  transition: .7s ease;
}

.questions:checked~.answers {
  height: auto;
  opacity: 1;
  padding: 15px;
}

.questions~.question::before {
  content: 'More Info '
}

.questions:checked~.question::before {
  content: 'Less Info '
}

.plus {
  position: absolute;
  margin-left: 10px;
  z-index: 5;
  font-size: 2em;
  line-height: 100%;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  user-select: none;
  -webkit-transition: .3s ease;
  -moz-transition: .3s ease;
  -o-transition: .3s ease;
  transition: .3s ease;
  pointer-events: none;
}

.questions:checked~.plus {
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  transform: rotate(45deg);
}

.questions {
  display: none;
}
<div class="question-content">
  <div>
    <input type="checkbox" id="question1" name="q" class="questions">
    <div class="plus">+</div>
    <label for="question1" class="question">
    Q1
  </label>
    <div class="answers">
      What if the answer is really long and wraps the whole page and you never want to finish it but you have to because its the answer!
    </div>
  </div>

  <div>
    <input type="checkbox" id="question2" name="q" class="questions">
    <div class="plus">+</div>
    <label for="question2" class="question">
    Q2
  </label>
    <div class="answers">
      short!
    </div>
  </div>

  <div>
    <input type="checkbox" id="question3" name="q" class="questions">
    <div class="plus">+</div>
    <label for="question3" class="question">
    Q3
  </label>
    <div class="answers">
      This is the answer!
    </div>
  </div>
</div>

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

Is there a way to showcase an epub format book using only HTML5, CSS, and jQuery?

Can ePub format books be displayed in a web browser using only HTML5, CSS, and jQuery? I would appreciate any suggestions on how to accomplish this. Additionally, it needs to be responsive so that it can work on iPad. While I am aware of this requirement, ...

Prevent the layout from shifting with CSS

I am faced with a challenge regarding a grid of images that are dynamically generated on the screen. The number of images in each row of the grid is dependent on the size of the page. While everything functions properly when the page is at a certain size, ...

Encountering a vast expanse of empty white void

Attempting to create a scrollbar within my content div instead of the entire window seems to be causing a large white space in the content box, and I'm struggling to understand why. Can someone take a look and help me identify the issue? You can view ...

Issue with margins of sections when attempting to activate menu class while scrolling

My Objective: I want to create a website where the menu highlights the section that is currently being viewed as the user scrolls up and down. Progress So Far: I have successfully implemented a functioning menu that changes to "active" when the correspo ...

How to style the first dropdown value in AngularJS to appear bold?

Is there a way to style only the first value in a dropdown list as bold without using jQuery? Here is the code for the dropdown: <div class="col-xs-3"> <select-box id="ad-version-select" options="curItem.stats.version" model="state.version" i ...

Creating an HTML table by populating it with data from a JavaScript array

Imagine you have a JavaScript array structured like this: [ "Heading 1", "Heading 2", "Data 1", "Data 2", "Data 3", "Data 4", ] Your task is to generate an HTML table from this array that looks like the one in this link: https://i.stack.imgur.com/7laUR. ...

Tips for effortlessly enlarging an element

When you click on "#sidebarnav>li", the following happens: 1) The second child of it - <ul> element will expand and its class toggles between "collapse" and "collapse in". 2) "#sidebarnav>li" will receive the "active" class. 3) The "aria-ex ...

What is the best way to incorporate a button in my code that automatically triggers changes at regular intervals?

Is there a way to automatically change the color of my working traffic light in JavaScript on a timed basis, rather than relying solely on button clicks? Here is the current code I am using: <!DOCTYPE html> <html> <head> <style> # ...

JQuery functions for click and hover are not functioning as expected on a div element

I am having trouble with a div that I have declared. The click event is not working as expected, and I also need to use the :hover event in the css, but it isn't functioning either. What could be causing this issue? <div id="info-button" class="in ...

Preserve proportions while resizing the browser window

I needed my code to ensure that the images keep their aspect ratio regardless of the screen size or if the user resizes their browser instead of viewing it full screen. I have some code in place, but I'm unsure how to maintain the current effects of ...

The ion-item is refusing to be centered on the page

I'm having trouble centering an ion-item and it seems slightly off-center. The standard methods like text-align: center and adjusting padding in ion-content don't seem to be working for me. Can someone please assist? https://i.stack.imgur.com/QZ ...

What is the best method for serving static files within a nested EJS view directory?

Assuming I have the directory structure below: |--Views/ | --partial/ | --links.ejs | |--assets/ | --css/ | --styles.css | |--server.js Code for links.ejs <link rel="stylesheet" type="text/css" href="css/forms/switches.css"& ...

Swap the Text for a Button

I've been searching for a solution to this problem, but all I seem to find online is advice on how to "replace button text." What I'm trying to achieve is to have text change into a button when hovered over. I've attempted using fadeIn/fade ...

A chosen class containing a nested class that utilizes the makeStyles function

Can someone explain how to target the 'element' when the root is selected? Here is the makeStyles code snippet: const useStyles = makeStyles(theme => ({ root:{ '&.selected': { } }, element: { } }) And h ...

What is the proper way to encode image URLs for use in CSS?

For the div element in my code, I want to allow users to input a URL that will be applied as a CSS background image, like this: background-image: url("/* user specified URL here*/") I'm concerned about security. How can I properly escape the URL to ...

The background image seems to be malfunctioning

I've been attempting to recreate a similar design to this website, and while most of it is working smoothly, I'm encountering an issue with using ::before to add a background image as desired. Below is the code snippet in question: /* Section ...

Editable content <div>: Cursor position begins prior to the placeholder

Having an issue with a contenteditable div where the placeholder text starts behind the cursor instead of at the cursor position. Any suggestions on how to correct this? <div id="input_box" contenteditable="true" autofocus="autofocus" autocomplete="o ...

Adjust the background image color with CSS styling

I have a collection of icons that I would like to incorporate into my application. These icons primarily consist of glyphicons with a few others mixed in. I want the HTML structure to mimic that of a glyphicon, specifically: <a href="#"> <spa ...

The top value in CSS animation fails to change properly

Can anyone help me figure out why the animation doesn't work when I try to change the vertical position of a form using JQuery? I want the input field to smoothly move to its new position so that users can see it happening. Here is the JsFiddle link: ...

transforming a div container into an inline element using CSS

Hey there! I'm facing an issue with CSS where I am trying to change a div tag into inline. <div id="menu1"> <ul> <a href="#"> <li> one</li> </a> <a href="#"> <li> two</li> </a> <a h ...