Using Jquery slideToggle is creating additional spacing between inline-block divs

I am struggling with displaying a list of cities in an inline format.

<div class="accordion-container">
        <a href="#" class="accordion-toggle">London</a>
        <div class="accordion-content">
            <p>Information about London</p>
        </div>
    </div>
    

When I click on a city header, I want to use slideToggle to show more information about that city.

accordionContent.slideToggle(250);
    

However, when I do this, it also slides down the city header next to it, creating a large white space area. Is there a way to only slide the divs below the selected city?

Here is a jsfiddle with my code.

Answer №1

To maintain the position of the other header, you can adjust the vertical alignment to top in your CSS:

.accordion-container {
    width: 49%;
    margin: 0 0 20px;
    clear: both;
    display: inline-block;
    vertical-align: top;
}

Check out EXAMPLE 1 for reference.

Alternatively,

If you prefer, you can use position: absolute for the content to break out of its container:

.accordion-content {
    background: #FFF;
    display: none;
    padding: 20px;
    overflow: auto;
    position: absolute;
    z-index: 2;
}

Take a look at EXAMPLE 2 for an illustration.

Answer №2

To achieve the desired layout, it seems like you will have to create two separate columns:

.accordion-container {
    margin: 0 0 20px;
    display: block;
}
.column {
    display: inline-block;
    width: 49%;
    vertical-align: top;
}

http://jsfiddle.net/example123/15/

You may need to rearrange the content to maintain the original order.

If you are programmatically generating this content, you will likely require two loops—one for odd items and another for even items.

Answer №3

I'm not entirely certain about the desired appearance, but one easy solution would be to include vertical-align: top;

.expandable-section {
    width: 50%;
    margin: 0 0 25px;
    clear: both;
    display: inline-block;
    vertical-align: top;
}

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

Is there a way to programmatically click on an href link in Firefox? The method that is typically used in Internet Explorer

http://jsfiddle.net/HVGre/1/ - check out this test link. I have a link in my HTML code that needs to be clickable dynamically. While it functions well with the .click() method in Internet Explorer, it fails in Firefox. Unfortunately, changing the link to ...

Enhancing Forms with Redux and Styled Components

I'm currently working on developing a reusable component that involves using a redux-form <Field /> and styling it with styled-components within the component. The issue I'm facing is that none of the styles are being applied. Here is my ...

Transferring documents using JavaScript

I have been using the following method to upload files to my Laravel backend: setFile(id, e) { let self = this; let reader = new FileReader(); reader.readAsDataURL(e.target.files[0]); reader. ...

Is it true that Firefox fails to display javascript errors on AJAX 'threads'?

Currently, I have a function called test_fn that is being called in two ways: (a) Through an onclick event of a button; or (b) from the success callback within a jQuery AJAX request, as shown here: $.ajax({ type: 'POST', ... succes ...

Pressing the reset button will initiate once the loader has finished

Hello everyone, I'm currently working on creating a button that transforms into a loader when submitted and then reverts back to a button after the form is successfully submitted. I suspect the issue lies within my JavaScript. I'm not sure how ...

Using CSS3 to apply transformations to multiple divs based on their individual attributes

I am currently developing JavaScript code that will enable the use of HTML like the example below: <div class="box" data-vert-speed="7">ContentDiv1</div> <div class="box" data-hori-speed="10">ContentDiv2</div> <di ...

How can I transform this statement into a higher-order function that offers a resource instead of using an object for initialization and destruction?

Starting with this code snippet: convert utilizes svgInjector to start and terminate a resource. export async function convert( serializedSvg: string, svgSourceId: string, containerId: string ): Promise<string> { const svgInjector = new SvgI ...

Classic leading and CSS line-height traditional styling

The CSS specification dictates that line-height should be implemented by dividing the specified value by two and applying the result both above and below a line of text. This significantly differs from the traditional concept of leading, where spacing is ...

Error loading CSS file: 'Invalid MIME type detected, expecting text/css'

Can someone please help me figure out why none of the CSS files I linked are loading on my website? It's strange because the CSS for another page on the same site is loading without any issues. My server is built using express and I'm using ejs ...

What is causing the failure of these "span" elements within an "a" tag in IE7?

Here is a code snippet that I am working with: HTML (version 4.0) <div class="temperatura"> <a href="/link/" class="temperatura_localita"> <span style="padding-left:12px;"> T ...

Formatting your HTML tags in Visual Studio

For instance, I have this particular tag with concise content <div>It is brief</div> However, upon formatting, Visual Web Developer presents it as <div> It is brief</div> which is unattractive. What I desire is <div> ...

Enhance the Search Box with a Vibrant Glow Effect When in Use

I need help adding a glow effect color around a search field when a user clicks on it to type. I want the border to have a shadow and the glow effect to be orange. I have included the CSS below, but I'm unsure of what to change or add. Any assistance ...

The order in which JavaScript is being executed is being reversed

function checkForDuplicate(center, email) { $.ajax({ type: "POST", url: "../staff/staffDA.php", data: "funId=-4&center=" + center + "&email=" + email, success: function (data) { if (data.split('| ...

Incorporate geographical data from a JSON file into my Google Maps application

Hey there, I'm a newbie on this forum and could really use your expertise. I've put together an html5 page with Google maps using the API key from Google (My code is shown below), it's working fine with a central marker in place and loads pe ...

Storing a color palette in a MySQL database from an HTML table

In my project to create a website for sharing quilting patterns, I have encountered an issue with the color changes in the HTML table. Despite allowing users to click and change cell colors, these adjustments are not saved permanently. As a solution, I a ...

The index on ref in MongoDB does not seem to be yielding any improvements

I've encountered a performance issue with my model: const PostSchema = new mongoose.Schema( { _id: mongoose.Schema.Types.ObjectId, workSpace: { type: mongoose.Schema.Types.ObjectId, ref: 'Workspace&apos ...

Mongoose has a tendency to duplicate the mongodb collection within a single instance

For educational purposes, I am developing a simple todo list application using custom routing functionality. When a user enters a custom route, I check if a document with that name exists in the database. If it does not exist, I create one. However, I am e ...

What is the best way to create a fixed contact form on specific pages?

There is a Contact Form 7 on the webpage that I want to make fixed to the right side. Initially, the form is hidden and only the form button (open) is visible. When clicked, the form should open. ...

Determine the total number of hours along with the precise minutes

Could you assist me with calculating minutes? Here is an example: var time_in = '09:15'; var break_out = '12:00'; var break_in = '13:00'; var time_out = '18:00'; var date = '2018-01-31'; var morning = ( ...

Troubles with Vue and localStorage for storing a theme's data

I've been struggling with this issue for a while now while working on a Vue site. I have not been able to find a solution for my specific bug in other discussions. The concept should be straightforward - I want to have a switch that toggles a value b ...