Content hidden by spoiler buttons may overlap with other spoiler buttons

I've created some spoilers that reveal information when clicked on.

When I clicked on spoiler1, the content slid underneath the other spoilers strangely.

Here's an example of what happened: http://gyazo.com/4571423534e2442dc960d119c668dfa8

Why is it doing this and how can I adjust it so that the spoilers beneath the one I'm opening slide down below the content?

My code:

<div id="container">
    <div class="spoiler1"><span id="title1">This is a Test</div>
        <span class="hide1">testttttttttttttttttttttt</span><!-- This is what opens after clicking on the spoiler -->
        <br /><br />
    <div class="spoiler2"><span id="title1">This is a Test</div>
        <br /><br />
    <div class="spoiler3"><span id="title1">This is a Test</div>
        <br /><br />
    <div class="spoiler4"><span id="title1">This is a Test</div>
        <br /><br />
        <br /><br />
        </div>

<script type="text/javascript">

$(document).ready(function(){

        $(".hide1").hide();
        $(".spoiler1").show();

    $('.spoiler1').click(function(){
    $(".hide1").slideToggle();
    });

});

</script>

CSS:

#title1 {
    color: #1794c8;
    position: relative;
    top: 10px;
    left: 10px;
    }

.spoiler1{
    font-size: 16px;
    background-color: #c0e6d2;
    border: 1px solid #a7c8b7;
    height: 45px;
    width: 530px;
    position: absolute;
        text-shadow: 0px 1px 0px #f4f4f4;
        filter: dropshadow(color=#fff, offx=0, offy=1);
        z-index:1;

}
.spoiler2{
    font-size: 16px;
    background-color: #c0e6d2;
    border: 1px solid #a7c8b7;
    height: 45px;
    width: 530px;
    position: absolute;
        text-shadow: 0px 1px 0px #f4f4f4;
        filter: dropshadow(color=#fff, offx=0, offy=1);
}
.spoiler3{
    font-size: 16px;
    background-color: #c0e6d2;
    border: 1px solid #a7c8b7;
    height: 45px;
    width: 530px;
    position: absolute;
        text-shadow: 0px 1px 0px #f4f4f4;
        filter: dropshadow(color=#fff, offx=0, offy=1);
}
.spoiler4{
    font-size: 16px;
    background-color: #c0e6d2;
    border: 1px solid #a7c8b7;
    height: 45px;
    width: 530px;
    position: absolute;
        text-shadow: 0px 1px 0px #f4f4f4;
        filter: dropshadow(color=#fff, offx=0, offy=1);
}

Any suggestions on how to fix this issue would be greatly appreciated!

Thank you for your help.

Answer №2

It seems like your HTML code needs some fixing. Make sure to include the closing tag for span in this part of the code:

<div class="reveal1"><span id="header1">Testing This Out</div>

This error might be causing confusion in the DOM and could be the reason for your issue. The same mistake is repeated in all 4 reveal divs.

Answer №3

Look no further, as I have found the perfect solution for you in this fiddle.

Your issue lies within the HTML and CSS structure.

To resolve this, segment your content into containers and style these accordingly.

I've streamlined the classes to enhance clarity.

HTML

<div id="featurelist_2_wrap">
    <div class="spoiler">
        <span class="feature">Networks</span>
        <div class="hide"><span class="featurelist_3-text-margin">We ensure top-notch security with features like secure login access, balance management, and more.<br>
            Our data is safeguarded with hourly SQL backups to prevent any loss.</span></div>
    </div>
     <div class="spoiler"><span class="feature">Networks</span>
        <div class="hide"><span class="featurelist_3-text-margin">We ensure top-notch security with features like secure login access, balance management, and more.<br>
            Our data is safeguarded with hourly SQL backups to prevent any loss.</span></div>
    </div>
    <div class="spoiler"><span class="feature">Networks</span>
        <div class="hide"><span class="featurelist_3-text-margin">We ensure top-notch security with features like secure login access, balance management, and more.<br>
            Our data is safeguarded with hourly SQL backups to prevent any loss.</span></div>
    </div>    

</div>    

CSS

.spoiler {
    position:relative;
    float:left;
}

.feature {
    display:block;
    background-color: #C0E6D2;
    border: 1px solid #A7C8B7;
    font-size: 16px;
    line-height:45px;
    text-shadow: 0 1px 0 #F4F4F4;
    width: 530px;
}

.hide { display:none; }

Javascript

$('.spoiler').click(function() {
    $(this).find('.hide').slideToggle();
})

Answer №4

If you're a new visitor, here's a fun spoiler for you!

HTML

<p>In the classic film Citizen Kane, the mysterious last word spoken by Charles Foster Kane is "Rosebud," which ultimately is revealed to be <span class="spoiler">a sled.</span></p>

CSS

<style>
.reveal { cursor:pointer; }
.spoiler{ display:none; }
</style>

JQUERY

$(document).ready(function() { 

    $('<a class="reveal">&gt;&gt;</a> ').insertBefore('.spoiler');

    $("body").on("click",".reveal" ,function(){
        $(this).parents("p").children("span.spoiler").fadeIn(2500);
        $(this).parents("p").children("a.reveal").fadeOut(600);
    });

}); 

Source: http://css-tricks.com/fade-in-spoiler-revealer/

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

If padding is included, the width of an element will be affected when using rem

I am facing an issue in my project. Whenever I try to insert a custom font-size at the html level, the red badges get deformed when there is padding (p-6) on the first line of code. Can someone assist me with this problem? I am using Tailwind CSS, but even ...

Adjust the color of a contenteditable div once the value matches

I currently have a table with some contenteditable divs: <div contenteditable="true" class="change"> This particular JavaScript code is responsible for changing the color of these divs based on their content when the page loads. However, I am now ...

What is the best way to change the background color of a table cell in HTML and CSS?

Trying to adjust the background image of each data cell to a selected image has been challenging. Using the method displayed below, only the top left corner of the photo appears. The code snippet demonstrates setting the background of each data cell to th ...

Simulating a mobile device screen size using an embedded iframe

Imagine this scenario: What if instead of adjusting the browser window size to showcase a responsive web design, we could load the site into an IFRAME with the dimensions of a mobile screen. Could this concept actually work? Picture having an IFRAME like ...

Is there a way to position the search bar on a new line within the navigation bar when the screen size is at its maximum width for mobile

I have a search bar on my navigation bar, but I am looking to move it to the next line within a maximum width if the screen display is extra small (xs) or for mobile devices. Below is the code snippet: <nav class="navbar navbar-expand-md fixed-top ...

As soon as I hover over my icon to enlarge it, I notice that my entire page starts to

Whenever I hover over my icon, the font size increases causing the div to expand slightly and move the page. This doesn't look great. How can I prevent the div from resizing when hovering over the icon? Here's my code: <div className="bg- ...

Ensuring Form Validity with jQuery for Exclusive Radio Buttons

How can I display an error message when a radio button value is not equal to a specific value? I have a series of yes/no questions and want to show disclaimers under each set of buttons if the value provided is null or different from what is required. Most ...

Automatically scroll to the end of the data retrieved through ajax

I am currently developing a live chat website for my users and I am using ajax to fetch the chat messages. However, I am facing an issue where whenever I enter a new message, it does get added to the database and displayed, but the scroll doesn't auto ...

The jQuery Ajax Error is consistently being triggered

I'm puzzled as to why my custom callback error function keeps getting triggered. When I remove this callback function, the success callback works just fine. Some sources online suggest that it could be an encoding issue, but I don't think that&a ...

"Utilizing React.js to implement white-space styling on numeric values for

I am a beginner in Reactjs and eager to learn and improve. Here is some code I have been working on: there's an <h1> element with the text "test", and beneath it, I want to display numbers vertically like this: 1:1, 1:2, 1:3. However, the CSS do ...

Issue with ellipsis not functioning correctly on dynamic placeholders when they are in focus

When I focus on my dynamic placeholder input, the text-overflow: ellipsis property is lost for some reason. However, it is regained when blurred. Can anyone explain why this is happening? If you want to experiment with it, I have set up a stackblitz: htt ...

Fluid Centered UL with Bullet Points

We have received a unique request from one of our clients. They are asking for the content within a specific <ul> to be centered along with the icons. Centering and adding icons are doable, but the challenge lies in centering the <li> elements ...

Is utilizing fixed-top/fixed-bottom with inner div elements set to a fixed height the most effective strategy?

I am working on developing an app similar to stackoverflow, with fixed menu and footer panels and an inner div that creates browser scroll as needed. I'm wondering if the code below is correct for achieving this setup. The classes fixed-top/fixed-bot ...

Exploring feature detection in jQuery version 1.4

Is there a way to use feature detection to determine if the browser is Firefox, IE (excluding IE6), Opera, Chrome, or Safari? ...

What is the best way to toggle the visibility of forms based on the select element with the use of jQuery and Bootstrap

I have set up a page with 5 different forms, each hidden within a div element using the Bootstrap class .d-none. At the top of the page, there is a select input. My goal is to create a function in jQuery that will hide all forms when an option is selected ...

jQuery toggle function not working properly on first click

Why does my first click not work, but the second one does? This is the code snippet: $(function () { $("#clickme").toggle(function () { $(this).parent().animate({left:'0px'}, {queue: false, duration: 500}); }, function () { ...

Processing requests asynchronously using AJAX

I am facing an issue with my website. Here are the pages causing trouble: /index.php <- session_start() /includes/functions.php /modules/feedback.php <- read some $_SESSION /gui/savefeedback.php <- read some $_SESSIO ...

Encountering error callback in AJAX when executing MySQL update query using PHP

As someone who is just starting out with PHP and backend web development, please excuse me if I make any silly mistakes. My current project involves uploading an image using PHP, utilizing jQuery and AJAX to trigger the PHP function, and then storing the ...

Position a div relative to another div with a static position

I am attempting to create a site that is fullscreen and responsive, but I am having issues with elements in the container overflowing on smaller screens, causing it to not be 100% as desired. I have tried using: top:100%; position:relative; width:100%; he ...

Why is the Ajax request failing to display the branch options in the drop-down list when a company is selected?

Working on a MVC Razor page with .NET Core 7, I encountered an issue where the Ajax request does not return branch options in a drop-down list based on the selected value from the company drop-down list. In other words, I need to cascade the branch drop-d ...