Accordion styling using CSS

My current issue lies with setting the style for the contents of an accordion. The problem arises when the styles defined in a CSS file do not take effect on the content of my accordion when using the following code along with a separate CSS file:

<script>
    $(document).ready(function() {
    var str = '<h3><a href="#">Device</a></h3>' +
            '<div class="content">'+
                '<div class="block left">' +
                    '<img width=30 height=30/>' +
                '</div>' +
                '<div class="block left">' +
                    '<img width=30 height=30/>' +
                '</div>' +
            '</div>' +
          '<h3><a href="#">VM</a></h3>' +
            '<div class="content">'+
                '<div class="block left" >' +
                    '<img width=30 height=30/>' +
                '</div>' +
                '<div class="block left">' +
                    '<img width=30 height=30/>' +
                '</div>' +
            '</div>';

The relevant code snippet from the CSS file is as follows:

div.toolbox-block {
    width:30px;
    height:30px;
    overflow: hidden;
    background-color: rgba(0,0,0,1);
}
div.left {
    float:left !important;
}

The above code works perfectly if I set the styles inline to the tags within the variable 'str', like this:

str = '<h3><a href="#">Device</a></h3>' +
            '<div class="content">'+
                '<div style="width:30px; height:30px; overflow: hidden; background-color: rgba(0,0,0,1); float: left;">' +
                    '<img width=30 height=30/>' +
                '</div>' +
                '<div style="width:30px; height:30px; overflow: hidden;  background-color: rgba(0,0,0,1);float:left;">' +
                    '<img width=30 height=30/>' +
                '</div>' +
            '</div>' +
          '<h3><a href="#">VM</a></h3>' +
            '<div class="content">'+
                '<div style="width:30px; height:30px; overflow: hidden;  background-color: rgba(0,0,0,1);float:left;">' +
                    '<img width=30 height=30/>' +
                '</div>' +
                '<div style="width:30px; height:28px; overflow: hidden;  background-color: rgba(0,0,0,1);float:left;">' +
                    '<img width=30 height=30/>' +
                '</div>' +
            '</div>';

Answer №1

Have you considered a different approach to including the accordion markup? Instead of dynamically adding it through Javascript, you could include it in your HTML file and initially hide it with CSS. Then, use jQuery on document ready to make it visible.

Here's an example:

<div id="accordion" class="hidden">
    <h3><a href="#">Device</a></h3>
    <div class="content">
        <div class="block left">
            <img width=30 height=30/>
        </div>
        <div class="block left">
            <img width=30 height=30/>
        </div>
    </div>
    <h3><a href="#">VM</a></h3>
    <div class="content">
        <div class="block left">
            <img width=30 height=30/>
        </div>
        <div class="block left">
            <img width=30 height=30/>
        </div>
    </div>
</div>

And your javascript:

<script>
$(document).ready(function() {
    $('#accordion').removeClass('hidden') // removes default CSS class to make it visible
        .accordion(); // turns it into an accordion
});
</script>

This way, the accordion markup is not generated by Javascript and should work with your CSS rules.

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

XPaths are used to verify the accuracy and relevance of text content

Here's a snippet of HTML I need help with: HTML Code: <a id="pINumber" href="csf.ajax?et=p.preparationinsurance">PASS170XX</a> XPATH to identify the above element //*[@id="pINumber"] I am looking to create an XPATH that checks for the ...

How to smoothly scroll a child div when the parent div is set as position sticky in vue JS

My landing page requires a unique feature: when a user enters a specific <section>, that section should become fixed while the elements inside the corresponding <div> start scrolling. Once the inner div has finished scrolling, the parent <se ...

Issue with displaying data using a custom pure pipe and boolean in ngIf condition

For my current web project, I require a friendship/follow feature. There are two roles involved: admins and regular users. Regular users have the ability to follow other users, while admins do not possess this capability. When a user wishes to follow anot ...

Hide jquery scroll bar

I am currently working on a WordPress plugin with the Twenty Thirteen theme. My goal is to display a modal when a div is clicked, and at that time I want to hide the scrollbar on the body of the page. Despite trying the following code snippet, it doesn&ap ...

Embed PHP PDO using AJAX

I've been stuck for days now, unable to solve what should be a simple problem. My goal is to perform a basic insert operation using ajax. Even though I am new to PHP, I have gone through numerous examples, studied POST requests in HTTP and PHP, revie ...

I am looking to create a split div with a diagonal line running through

Is there a way I can create this Mockup using html5 and css3, but also add diagonal lines to divide the div? Any suggestions on how to achieve this? ...

Troubleshooting: Issue encountered while adding jQuery Slideshow plugin to current website

I am currently facing some challenges with a specific feature on my website. I am trying to integrate Jon Raasch's "Simple jQuery Slideshow Plugin" into an existing site that was not originally built by me. However, when viewing the site in IE 9, two ...

What is the most efficient way to transmit an HTML document element from a client to a server in Node JS

I am attempting to capture a snapshot of my client-side document object and send it to the Node.js server. However, when I try to convert it into a string using: JSON.stringify(document.documentElement) I encounter an issue where it becomes an empty obje ...

Personalized Carousel using Ng-Bootstrap, showcasing image and description data fields

I have been working on customizing an Angular Bootstrap Carousel and have managed to successfully change the layout. I now have two columns - with the image on the right and text along with custom arrows on the left. My goal is twofold: First, I am lookin ...

How to achieve divs with see-through text using CSS?

How can I create a CSS (non-HTML5) based website with a filled div that has a cutout revealing an image underneath? There are various overlays and images involved, making the use of static images inconvenient. Additionally, I anticipate needing to scale th ...

Utilizing PayPal as a payment method for an exclusive subscription-based online

Running a membership site involves users registering with their email and password, along with subscribing to a monthly plan. The frontend is built using jquery/ajax while the backend utilizes PHP/Slim. Currently, Stripe has been integrated as a payment me ...

If a single checkbox is selected within a group, then every other checkbox should be deactivated and deselected

Here is the code snippet provided: function listen(element, event, callback) { if (element.attachEvent) { element.attachEvent('on' + event, callback); } else { element.addEventListener(event, callback); } } var form = document.que ...

I am facing an issue where the images (IMG) do not align properly when I test the responsiveness using Chrome

Seeking assistance with a design challenge I encountered. The first image showcases a well-structured table on desktop view, while the second image displays an undesired layout when examined using Chrome DevTools. A link to the problematic layout can be fo ...

Unexpected behavior occurs when attempting to run an HTML string within a Python file

After inputting the following code, I am getting a blank HTML page. Despite including an <h1> and an <a href> tag, only the <title> tag is being executed. Can anyone shed light on why this is happening and offer guidance on how to resolve ...

Responsive slide displays a unique number of items per slide depending

I created a logo slider using Bootstrap, similar to the one showcased here: http://jsfiddle.net/juddlyon/Q2TYv/10/. Currently, each slide in the slider displays 4 logos. I would like to make it responsive so that on smaller screens, only 2 logos are shown ...

Struggling with incorporating HTML5 elements (such as background colors and text colors) into my website using the WordPress editor

My research process began with browsing the internet to find solutions. I stumbled upon this website that provided some insights. Seeking further guidance, I consulted a friend who recommended using specific code, like this: <head> <link rel="st ...

What steps do I need to take to create a basic API that links to a MySQL database?

I'm currently trying to develop an API for PHP that will enable one web server to communicate with another web server. Additionally, I want this API to be able to update MySQL code. Previously, I was utilizing $_GET parameters for this purpose but rea ...

``Is there a way to retrieve only a specific portion of an HTML

Greetings everyone! Here is a snippet of my sample code: <?php $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/x ...

Interact with database using Ajax and Codeigniter (utilizing Bootstrap modal)

My goal is to create an advanced search feature with an interactive button that opens input fields and dropdown menus. To populate these dropdown menus with data from the database without overloading the page, I plan to use AJAX when the user triggers the ...

Create a video player in your HTML code using the class "afterglow", and also link a separate class to it for additional styling

I'm encountering some challenges with a project that involves a JS file for a Bootstrap modal popup and an HTML5 video player. The issue I'm facing is that I am unable to link the class for the video player theme. Can anyone here assist me in ide ...