Toggle the visibility of several div elements simultaneously and adjust the dimensions of the remaining div element

My goal is to create an interactive layout with four 200px by 200px div tags. When a user clicks on one of the divs, I want it to expand to 800px by 800px and hide the other three div tags. The expanded div should be able to revert back to its original size when clicked again, allowing the user to interact with each div individually.

CSS:

#tile1-show, #tile2-show, #tile3-show, #tile4-show{
    width: 20%;
    padding-top: 20%;
    margin: 0% 0% 2.5% 2.5%;
    height: auto;
    float: left;
    background-color: red;
    border-radius: 20px;
    border: 2px solid #707070;
}

#tile1-hide, #tile2-hide, #tile3-hide, #tile4-hide {
    width: 40%;
    height: 40%;
    float: left;    
    display: none;
    border-radius: 20px;
    border: 2px solid #707070;
}

Answer №1

To simplify your current code and achieve the desired outcome, consider following these steps: (refer to the demonstration in this JSFiddle link)

Firstly, in the HTML section:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.js"></script>
<div class="tiles"></div>
<div class="tiles"></div>
<div class="tiles"></div>
<div class="tiles"></div>

Then, in the CSS part:

.tiles{
    width: 200px;
    height: 200px;
    padding-top: 20%;
    margin: 0% 0% 2.5% 2.5%;
    height: auto;
    float: left;
    background-color: red;
    border-radius: 20px;
    border: 2px solid #707070;
}
.tiles.active{
    width: 800px;
    height: 800px;
    display: block !important;
}
.tiles.inactive{
    display:none;
}

Lastly, in the jQuery section:

$(document).ready(function(){
    $(".tiles").click(function(){
        if($(this).hasClass("active")){
            $(this).removeClass("active");
            $(".tiles.inactive").removeClass("inactive");
        }
        else{
            $(".tiles").addClass("inactive");   
            $(this).removeClass("inactive").addClass("active");
        }
    });
});

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

Sidebar navigation text shifting during transition

I attempted to modify the CSS and JavaScript, but unfortunately, it had no effect and actually caused more issues. I adjusted the position and display properties in the CSS, but it seems that wasn't the root of the problem. If anyone could offer assis ...

I am attempting to properly arrange my navigation links and logo in alignment

I need help aligning the logo in the exact center while keeping the navigation links as they are. Here is how it currently looks: https://i.stack.imgur.com/qOgVJ.jpg My CSS: .logo { display: inline-block; text-align: center; } na ...

What is the most efficient way to dynamically alter the position of a div element within an HTML document using

In a scenario where a parent div contains multiple child divs, it is required that one particular child div named .div_object_last always stays at the end. Below is an example setup : HTML <div class="div_parent"> <div class="div_object"> ...

Executing SQL Query on Button Click Event

I am working with PHP and trying to execute a SQL query when a button is pressed. However, the issue I'm facing is that nothing happens when I click the button! I checked the developer console but there doesn't seem to be any action detected when ...

Dash: Streamlining the process of updating numerous elements within a callback

I am facing a challenge with my Dash app where I have multiple html.Div components that all look and are laid out similarly. When I click on a checkbox, I want all of these elements to be hidden. The common solution suggested is to create multiple outputs ...

The beauty of Android Studio: Gradients

While working on my Android Studio project, I designed a UI in Figma that included a rectangle with multiple gradients and a solid color. Now I'm wondering how to recreate this exact rectangle in Android Studio. Here is the CSS code for reference: ba ...

Height constraint disregarded by anchor

I am facing an issue with a large textual menu where the active link space is overlapping onto the other menu items. This is causing difficulty in accurately clicking on a link. For reference, you can check out this example: http://jsfiddle.net/dhyz48j3/1 ...

I am puzzled as to why the frames in my code are failing to display

I'm attempting to create a simple two-frame menu and content system, but I've been struggling to display the frames properly. Here's the snippet of code that I'm working with. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http: ...

Exploring the process of setting a background image within a div tag

My goal was to design a clickable box that would redirect users to "#". I initially thought about using area maps within div or p tags, but unfortunately that wouldn't work. Does anyone have any suggestions for alternative solutions? ...

Tips for integrating PHP into a Bootstrap modal dialog

After discovering and modifying a php script designed to process contact form content and display alerts on a Bootstrap 3 modal window, I encountered some issues. While I was able to successfully display errors and show the modal onload without any php con ...

Displaying images in various positions across different browsers

I am currently working on rendering images on a website. The images display perfectly in Firefox and Chrome, but I am encountering issues with Safari (version 7.0.3). Specifically, the image shifts to the bottom right in Safari, whereas I would like it to ...

What is the best way to incorporate an icon into my HTML search bar?

I'm having trouble getting the search icon from font awesome to display properly within my search bar. Here is the code I'm using: <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" rel="stylesheet"/> ...

Initiate an Elementor popup upon form submission (After submit event) using Contact Form 7 in WordPress

I have incorporated Elementor popups and contact form 7 into my WordPress website. Currently, my goal is to activate the Elementor popup after the contact form 7 form has been submitted. Below is what I have tried so far, and there have been no console er ...

Steps to create an automatic submission feature using a combobox in HTML5 and then sending the retrieved data back to the HTML file

Here is the code snippet I've been working on: <strong>Station Name</strong> <!--This portion includes a combobox using HTML5 --> <input type=text list=Stations> <datalist id=Stations> <option>Station1</opt ...

Challenges with resizing floating divs

As a beginner in Web Development, I am tasked with creating a web portfolio for an assignment. In my design layout, I have a navigation bar in a div floated left, and a content div floated right serving as an about me section containing paragraphs and lis ...

Bootstrap's Elegant Adjustable Rows

My task is to showcase 8 images using Bootstrap's grid system. Here is the code I have: <div class="row"> <div class="col-sm-3"> <img class="img-responsive" src="img/img1.png"> </div> <div class="col-sm- ...

Maintain the fixed position of the horizontal scroll bar

I'm facing an issue with a popup window that displays a frameset. Within one of the frames, I'm using jQuery tabs to show some text. The problem arises when trying to display long text or when resizing the window, causing the horizontal scroll t ...

Designing scroll boxes that are not continuous and tracking the time spent in each section

I'm seeking assistance with a unique challenge involving the creation of scroll boxes for use in a Qualtrics survey and tracking the time spent viewing different sections of text within them. Specifically, I aim to design a scroll box featuring two p ...

Personalizing the arrow positioning of the Angular8 date picker (both top and bottom arrow)

I am interested in enhancing the design of the Angular 8 date picker by adding top and bottom arrows instead of the default left and right arrows. Can someone guide me on how to customize it? Check out the Angular 8 date picker here ...

How can you achieve three layers of nested quotes in Dynamic HTML?

Working on an app that utilizes JQuery and JQTouch for iOS. The issue I'm facing involves dynamically generated HTML lists where the user clicks a row that needs to be highlighted. However, achieving this has proven tricky due to nesting 3 sets of quo ...