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

Challenge with Express Helmet: CSS fails to load properly on iOS Safari browser

After successfully adding Helmet to my Node/Express/EJS project and configuring my CSP to allow inline scripts, styles, and certain external sources on my Windows laptop in Opera, Chrome & Edge, I encountered a problem when connecting via iOS Safari on mob ...

Avoid creating a line break directly after the header, however allow for a line break

I find myself in a scenario where I require a paragraph that has a specific format: An Emphasized Title Some relevant information regarding the subject... I am looking to insert a new line before the title, ensuring it seamlessly integrates with the re ...

Show button once modal has been closed

I have a 'start' button that triggers a modal window with an embedded video. Once the user closes the modal, I want to show a 'redeem' button after a delay of 6 seconds. HTML: <a id="video-start" onclick="openVideoModal()"><i ...

Creating a visually appealing table in HTML or experimenting with a tableless design can greatly enhance your website's layout

Presenting data with headers in HTML has been a challenge for me. Below is the portion of the header I'm having difficulty with. Although I've successfully rotated the text, the issue I'm facing now is text overlap. Here's the code st ...

Top div click causes bottom div to slide

I'm currently working on a project that involves using CSS and jQuery, but I've encountered an issue that I haven't been able to solve. Here is the demo link: . When the second div is clicked, why does the third div slide? How can this prob ...

Is it advisable to initiate an AJAX call and allow the browser to cancel the request if needed?

When an AJAX request is made, it typically appears in the network tab in Chrome. However, if a client-based redirect occurs at the same time, the AJAX request may be cancelled. But does this mean that the request still reaches the server and executes as ...

The grayscale effect is not functioning on the default web browser of an Android device

I have implemented a grayscale effect for an image using the following CSS code snippet: img.grayscale { filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\'><filter id=\'grayscale\&apo ...

The anchor tag <a> is configured for inline display but is unexpectedly occupying the entire space

I am currently incorporating Bootstrap into my HTML, however, the <a> tag is behaving like a block display despite the fact that I have specified the CSS to be display:inline; <!doctype html> <html> <head> <link rel="styleshee ...

Adjust div elements to fit the size of the window

I'm currently working on designing a new web application's skeleton layout. I have come across some issues, particularly with CSS layouts and DIV. 1) Boxes 1 and 2 in the first column do not align with boxes 3 and 4 in the second and third colum ...

Tips for arranging images in a horizontal layout using FlatList in React Native

Is there a way to display feed images horizontally instead of vertically in FlatList? I've tried wrapping the images in a view with flex-direction set to row, as well as adding horizontal={true} to the FlatList, but nothing seems to work. Any suggesti ...

Sleek CSS Slider with Image Transformation on HoverThe requested task has been

I am currently working with a client on the website . One of the features on the site is a slider at the top that allows for image swapping using pure CSS. However, the client recently requested that the images change when hovering over radio buttons or au ...

What steps can I take to have the text extend beyond the background at both the top and bottom edges?

I am attempting to replicate this design using CSS: So far, this is what I have achieved: .container{ height: 30px; margin-bottom: 10px; } .redLine{ background-color: red; opacity: 0.5; height: 20px; border-radius: 5px; text-align: ce ...

Tips on maintaining the parent's scope in CoffeeScript while making an AJAX call

I need to iterate through an object in CoffeeScript and make an AJAX call for each item in the object using jQuery. However, I'm facing an issue with losing the reference to the initial context in the callback function of the AJAX call. The context al ...

Navigating through Expression Engine and utilizing conditional statements

Would greatly appreciate some assistance with this issue. I am working on an Expression Engine website and encountering difficulties with simple conditionals for navigation active states. Despite having a color state designated within the styles, there s ...

Is there a way to automatically add a div to the window as the user scrolls and then hide the div when they scroll back to the

Seeking assistance with creating a function that will add a 'sticky' class to the menu when the user scrolls down to the middle, and then append a div when the 'sticky' class is present. Currently facing issues where the div keeps appen ...

Leverage CSS styling for database values within a PHP framework

How can I style MYSQL database values in HTML using CSS? My vegetarian database row has 'Y' for yes and 'N' for no. I want to apply different styles to these values using CSS as I display them on my PHP-based page. if ($result1->num_ ...

Having trouble with jQuery AJAX - page continually refreshes upon submitting and data isn't being transmitted

I've been working on creating a dynamic AJAX function using PHP and MySQL to update database records without the need for page refresh or navigation. However, I haven't had much success with it yet. Here's the code I have on the page with t ...

Having trouble with sending a post request through ajax

Whenever I try to initiate an Ajax request upon clicking a button, the request never seems to get executed. Below is the snippet of my HTML code : <!DOCTYPE html> <html lang="en"> <head> <title>User Form</title> ...

Exploring the process of transferring a jQuery array from a Rails controller to a PostgreSQL array column

For my project, I successfully pass a JavaScript array to a Rails controller using AJAX. The JavaScript array consists of upload image names. Within my postgresql database, there is an array column called "images." In the Rails controller, I attempted the ...

Passing a message from a Struts2 action to jQuery

When using jQuery Ajax to call a Struts2 action, I have the following setup: $.ajax ({ url: 'callAction.action', type: 'POST', data: data, dataType: 'string', success: function (data ...