Activating several divs using transition

Is there a way to trigger 2 or more divs with one transition? For example, when clicking on one box, can I make the width and height of both boxes change to 400px simultaneously? Ideally, I would like to achieve this using only HTML and CSS. If not, are there any other options available? Thank you!

<!DOCTYPE html>

<html>
<head>
<style>

    .box1{
        width:200px;
        height:200px;
        background-color:red;
    }

    .box2{
        width:200px;
        height:200px;
        background-color:black;
    }


</style>
</head>

<body>

<div class="box1"></div>
<div class="box2"></div>

</body>
</html>

Answer №1

Here is the HTML structure:

<div class="container">
<div class="box"></div>
<div class="box"></div>

This CSS code will style the boxes:

.box {
  width:100px;
  height:100px;
  background:red;
  display:inline-block;
}

.expanded {
  width:200px;
  height:200px;
  transition: height 1s linear, width 1s linear;
}

You can view the demo on jsfiddle: http://jsfiddle.net/fBETk/6/

By clicking on one box, both boxes will increase in size simultaneously.

If you prefer not to use JavaScript, you can utilize the "checkbox hack" technique like this:

<label for="toggleBtn">
<input type="checkbox" id="toggleBtn">
<div class="box"></div> 
<div class="box"></div>
</label>

For the corresponding CSS styling:

.box {
    width:100px;
    height:100px;
    background:red;
    display:inline-block;
    transition: all 1s linear;
}

input[type=checkbox] {
    display:none;
}

input[type=checkbox]:checked ~ .box{
    width:200px;
    height:200px;
}
label {
    display: block;
}

Demo using the checkbox hack on jsfiddle: http://jsfiddle.net/fBETk/9/

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

What is the solution for accessing properties of a non-object?

In my referral system, everything is working perfectly. I would like to include the name, username, or email address of the referral in the page's HTML, for example: {$ username}. Currently, in my controller, the "redirect" function is working fine. B ...

What is the process for setting data to the value attribute in an input tag, retrieved from a firebase database?

I utilized the following code snippet to retrieve data from Firebase database and store it in the Name variable. var userId = localStorage.getItem('keyName'); var dbRefName = firebase.database().ref() .child(& ...

Using CSS to position two divs at the bottom-right corner of the page by using the `position: absolute` property

Currently working on creating an uncomplicated chatbox with a header using CSS. My aim is to position the chat at the bottom and right of the page. I've experimented with the following: float: right; bottom: 0; position: absolute; Although this alig ...

"Encountered an HTTP 400 error when trying to retrieve content from a Persian URL using file

As a beginner, I am facing an issue with a URL that contains Persian language characters. For instance: http://tabnak.ir/fa/news/577155/ویدیوی-درگیری-نیروهای-سیا-و-پنتاگون-در-سوریه-با-همدیگر-ویدیوهایی ...

Incorporating text onto an HTML webpage

Context: My program reads a file and displays it on an HTML page. The code below utilizes regex to extract errors from the file. Instead of using console.log for debugging, is there a way to display the results on the HTML page? When attempting: document ...

Angular ng-repeat can sometimes lead to annoying flickering on the webpage

I have a code snippet that displays a list of thumbnails: <div class ="channel" ng-repeat ="channel in UIModel.channels" ng-class ="{evenchannel: ($index % 2) == 0, oddchannel: ($index % 2) == 1}"> <img class="channel-img" ng-src ="da ...

Assistance required with scrollable tab content in Chakra UI

When implementing the Tabs component in Chakra UI version 2, I encountered an issue where the content overflows instead of being scrollable: function App() { return ( <ChakraProvider> <Box width={300} height={300} ...

Centering Text and Image in React Horizontally

I'm attempting to achieve a layout similar to the one shown in the image below. Currently, my image is positioned at the top with the text below it. I would like to arrange it so that the text appears on the right side of the image. Please take a loo ...

Creating a custom grid layout with Bootstrap 4

Take a look at the image link provided below. https://i.sstatic.net/yhbx9.jpg I am attempting to use boostrap4 to create a grid layout similar to the one shown in the image. On desktop, the first column should be col-5 width and the other 3 equal. On mob ...

Positioning a Form with Sliding Out Effects Using CSS

Hello, I recently came across a tutorial online that helped me create a form slide effect on my website. However, the issue I'm facing is that the form is positioned to the left and I would like it to be placed on the right instead. For reference, h ...

leveraging jquery for delaying, subsequently fading out, and ultimately removing

I am currently working on a website that has a dialog feature which I am trying to make functional by following these steps: The dialog is created using .append() method, then it is set to wait for 5 seconds before fading out and being removed. Despite my ...

What would you suggest as a more effective method for preventing content overlap during multi uploads?

Recently, I was working on a Wordpress site and used the Ninja Forms plugin to create a form. One of the challenges I encountered was styling the file upload button. After some research, I came across a tutorial that provided a great solution that worked a ...

Implementing a Vertical Scrollbar for tbody Using Bootstrap Table Styles

My goal is to display a vertical scrollbar within the tbody. After researching, I found suggestions to apply display:block to both the tbody and thead, as shown here Unfortunately, this solution does not work when the table has Bootstrap's table tabl ...

Tips on creating a React Vite Typescript website that utilizes the entire height and ensures a minimum width

I'm currently working on building a portfolio webpage with React, Vite, and Typescript, but I'm facing an issue where the content is not taking up the full height of the page. I have multiple pages with different lengths of content that I want to ...

Choose a random cell in Jquery every second

I am searching for a method to choose one div out of sixteen and change its CSS backgrounds. This selection needs to occur every second, so a new div is selected from the group each second. I am struggling with implementing the "every second" functionalit ...

What happens when browsers encounter unfamiliar attributes in HTML code

Direct and to the point: if I were to execute: <div id = "thisid" class = "a plethora of classes" mother = "your mother" levelofhotness = "over 9000"> I am aware that Chrome and Firefox (the browsers I typically test with) simply disregard these a ...

Make sure to look for the sub-division mouse-over feature

I'm struggling to phrase this question correctly. Please help me rephrase it. I am trying to detect a mouse-over event on a sub-division, but it's not functioning as expected. Here is the code I am currently using: $('#mc').hover(func ...

The slider components have an endless loop feature that only runs once before coming to a

I'm working on creating an infinite slider that loops continuously without the need for navigation buttons. Instead, I plan to allow users to control the slider using touch gestures (although this functionality is not yet implemented in the code snipp ...

There is a significant spacing issue between the header and main category when viewed on a mobile device

Experiencing issues with element distances on different screen sizes? While the website looks fine on normal screens, there's a noticeable gap between the header and main sections on mobile devices. See below for the provided HTML and CSS code. ...

I am perplexed by the fact that the Div is nested within the "a" element

My recent code validation revealed an error related to the use of a div element within an a element in the context of a carousel. The error message from the validator was: Element div not allowed as child of element a in this context. (Suppressing further ...