Increasing and preserving the width value of a div using SCSS?

I am currently working on a div where I need to increase the width based on checkboxes that are selected. The goal is to add to the width as more checkboxes get checked.

Here is an example of the HTML code I'm working with (please excuse any syntax errors, as I am writing this from memory):

<div class="barDiv"></div>

<label for="cb50"><h2>Click here to add 50!</h2></label>
<input type="checkbox" id="cb50">

<label for="cb30"><h2>Click here to add 30!</h2></label>
<input type="checkbox" id="cb30">

This is a sample SCSS code I have been working on:

$add: 0px;

.barDiv {
    width: $add;
    height: 10px;
    background-color: #444;
}

#cb50:checked {
    ~ .barDiv {
        width: $add + 50px;
    }
}

#cb30:checked {
    ~ .barDiv {
        width: $add + 30px;
    }
}

My current setup works individually as expected, but I want the width to be 80px when both checkboxes are checked. However, it just switches between the two widths instead. I have seen this done using LESS and I'm hoping there's a way to achieve this using SCSS as well. Any help on this would be greatly appreciated. Thank you!

Answer №1

  1. To make this setup function as intended, ensure that the .barDiv element follows both checkboxes in the DOM structure (the ~ selector applies to next siblings, not previous ones).
  2. Additionally, you must define the styling when both checkboxes are checked.

Below is an example using pure CSS (remember that Sass is compiled to CSS eventually). I hope this meets your requirements:

.barDiv {
    width: 0;
    height: 10px;
    background-color: #444;
}
#cb50:checked ~ .barDiv {
    width: 50px;
}
#cb30:checked ~ .barDiv {
    width: 30px;
}
#cb50:checked ~ #cb30:checked ~ .barDiv {
    width: 80px;
}
<label for="cb50"><h2>Click me to add 50!</h2></label>
<input type="checkbox" id="cb50">

<label for="cb30"><h2>Click me to add 30!</h2></label>
<input type="checkbox" id="cb30">

<div class="barDiv"></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

Tips for preserving scroll location on Angular components (not the window) when navigating

My current layout setup is like this: https://i.sstatic.net/hOTbe.png In essence <navbar/> <router-outlet/> The issue I'm facing is that the router-outlet has overflow: scroll, making it scrollable (just the outlet section, not the ent ...

Is there a way to retrieve the previous value in an input field's onChange event?

I am working with inputs in a React project and have assigned a function to their onChange event. While I have been able to access the current value, I am now looking for a way to retrieve the previous value as well. The reason I need the old value is bec ...

When a radio button is clicked in Angular7 and it shares the same form control name, both radio buttons are being

Visit this StackBlitz link for more information. places = ['effil tower','new discover'] new FormGroup({place: new FormControl()}); <div *ngIf="places?.length > 0" class="col-12"> <div style=" padding-top: 1e ...

Switch background color multiple times on click using JavaScript loop

Hello amazing people! I have a container with 52 small boxes and one large box containing a letter. The smaller boxes are arranged around the larger one using CSS grid, and when hovered over, they turn light grey. My Objective When any of the 52 small b ...

Padding in submit button value is essential for ensuring proper spacing and

Is there a way to adjust the padding of a submit button value? I have tried using text-align: right;, which works fine for aligning the text to the right. However, when I attempt to use padding-left: 25px;, the text remains centered instead of moving to t ...

Clickable link that directs to a particular tab on a webpage (cbpFWTabs)

I have been utilizing tabs from the codrops Tab Styles Inspiration and I am looking for a way to open specific tabs using URLs. For instance, if I wanted to open tab3, I could link it as www.google.com/index#tab3. Below is the code I am currently using: ...

Retrieving the value of a formControl within a formArray is made possible through a reactive form in

How can I retrieve the value of ItemName in my HTML code? When I attempt to use {{invoiceForm.controls[i].items.controls.itemName.value | json}}, it returns as undefined. <form [formGroup]="invoiceForm"> <div formArrayName="items" *ngFor="let ...

Show links in the sidebar menu when the primary navigation links are hovered over

I am trying to create a dynamic side menu that displays different links depending on which link is hovered over in the top navigation bar. Here is what I have so far: HTML: <ul class="nav navbar-nav"> <li class="nav"><a class="toggle" hr ...

Element with adhesive properties alters its color at a particular location

I need the sticky element to change colors at specific points and then revert back to its original color. The color should be orange initially, green on block 2, and orange again on block 3. For the complete code and to address any issues with jQuery, pl ...

Guidelines on centering an AJAX spinning animation within a parent div

Below is the basic structure of an HTML document for a webpage. <div class="grand-parent"> <div class="header">Heading</div> <div class="parent-div"> <div class="child-div-1"></div> ...

What is the best method for sending PHP data to an HTML file?

Once the user logs in, I aim to direct them to a separate HTML page (a landing page) that greets them with "welcome, (user)." I initially attempted to utilize cookies for this functionality but realized it isn't feasible. How can I achieve this? Here ...

What is the best way to retrieve and display data from a JSON object that has been encoded using json_encode on one page.php, in another page.php?

I have a specific requirement that involves calling JSON data from one PHP page and displaying it on another PHP page. Can someone guide me on how to achieve this? Below is the JSON code that I need to retrieve: <?php include("Connect.php"); $Db = m ...

Struggling to locate text within the confines of the meta viewport

So I'm new to this whole blogging thing and I wanted to make sure my site was responsive. I added the meta viewport tag, but when I tested it out on Google Chrome with different window sizes, my text just disappeared! Here's a snippet of the CSS ...

Is there a restriction on the number of stylesheets per page in Internet Explorer 8?

Discussing the limitations of CSS in a forum thread, a user shared insights: A user mentioned that Internet Explorer has is known to have a restriction of 4096 CSS rules per file. For more information, check out this source. Additionally, there might be ...

The hidden visibility feature only activates when the mouse is in motion

visibility: hidden doesn't take effect until the mouse is moved. const link = document.getElementById("link"); link.onclick = (event) => { link.style.visibility = "hidden"; link.style.pointerEvents = "none"; event ...

Having trouble getting the AJAX script to change the background in Internet Explorer?

I'm currently experiencing an issue with an ajax script. Here is the code I am using: <script type="text/javascript"> $(document).ready(function() { $('#savecolor').click(function(){ var myVar = 'data= ...

Adjusting the size of the snap increments

When using gridstack, I have the ability to resize my widgets. However, I've noticed that when dragging on the widgets' handles, they snap to specific sizes, which seems to be a fixed amount. If I wanted to set the widget's size to something ...

What issues are we facing with submitting this multiple-choice selection?

Why isn't my multi-select form submitting as an array? HTML <div class="container"> <select name="itemsToChoose" id="left" size="8" multiple="multiple"> <option value="1">item1</option> <opti ...

What could be causing my DIV anchor link to appear larger than the image it is supposed to be

Recently, I have rearranged my delete and like buttons within a DIV tag. Interestingly, even though I have meticulously set the width and height of the div tags to match that of the image, they still appear below the "Like" button. Additionally, I've ...

Tips for aligning a drop down list in the center

I need help center-aligning my dropdown list to match the buttons that are also center-aligned. Do you have any suggestions on how I can achieve this using .css? <h4 id="carbCount" class="pageElement">The Carb Count is 0</h4><br /> &l ...