Elements generated from bootstrap's divs are not produced in a single line

I'm attempting to have a div append multiple other divs with the class "card" from Bootstrap, but instead of being created in a horizontal line, they are stacked on top of each other.

I have tried to modify the "card" class by adding display: inline-block, but unfortunately, it did not yield the desired result.

Here is my jQuery code:

let newCard = document.createElement("div"); 
newCard.className = "card";
$( ".cardsContain" ).append(newCard);

And here is the accompanying CSS:

.cardsContain{
        width: 79%;
        margin-left: 9%;
        height: 100%;     
      }
      .card{
        display: inline-block;
         background-image: url('img/facebook.png');
         background-size: 100%;
         width: 100px;
         height: 100px;
      }

The "cardsContain" div is responsible for appending each card.

Is there a way to display these cards in a single line as intended?

Answer №1

It appears that you have integrated jQuery into your code:

<div id="myDiv" class="test"> 1</div>
<button id="fooBtn">Click me</button>

jQuery functionality:

  var fooBtn = $('#fooBtn');

  fooBtn.on('click', function() {
      let newDiv = document.createElement("div"); 
      $(newDiv).addClass('card')
          .html('hello, world!')
          .appendTo($("#myDiv"));        
  });

Custom CSS styling:

.card{
    display: inline-block;       
    width: 100px;
    height: 100px;
}

Answer №2

Check out this demo I put together for you. Whenever you click the 'Add Card' Button, a new card will appear next to the existing ones.

HTML:

<button type="button" class="btn btn-primary" onclick="addCard();">Add Card</button>
<div class="cardsContain row">

Javascript:

    var i = 0;
    function addCard(){
        i++;
        let column = document.createElement("div");
        column.className = "col-3 column" + i;
        $( ".cardsContain" ).append(column);

        let newCard = document.createElement("div"); 
        newCard.className = "card";
        newCard.innerHTML = '<div class="card-body">' + '<h5 class="card-title">Card title</h5>' + '<p class="card-text">Some quick example text to build on the card title and make up the bulk of the cards content.</p>' + '</div>';
        $( ".column" + i ).append(newCard);
     }

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

Steps for deactivating the click event triggered by Bootstrap Collapse API

Currently, I am incorporating Twitter's Bootstrap 'Collapse' plugin in a project. However, I found the functionality of having to click on each parent element to toggle collapsible items quite inconvenient. To address this, I decided to crea ...

use jq to unravel complex json data before importing into pandas

While attempting to convert a JSON file into a pandas dataframe, I came across a helpful solution on Stack Overflow. My JSON file contains numerous attributes, making it time-consuming to define rules for each field manually. Is there a way to automatic ...

When a textarea is marked as readonly, Tinymce automatically sets it to readonly mode

Is it possible to configure tinymce to automatically make the editor read-only when the textarea already has the readonly attribute set? ...

What's the best way to constrain a draggable element within the boundaries of its parent using right and bottom values?

I am currently working on creating a draggable map. I have successfully limited the draggable child for the left and top sides, but I am struggling to do the same for the right and bottom sides. How can I restrict the movement of a draggable child based o ...

Learn how to implement a feature in your chat application that allows users to reply to specific messages, similar to Skype or WhatsApp, using

I am currently working on creating a chatbox for both mobile and desktop websites. However, I have encountered an obstacle in implementing a specific message reply feature similar to Skype and WhatsApp. In this feature, the user can click on the reply butt ...

Adjust the color of a div's background when clicked by solely using CSS

I'm looking to change the color of a specific div when it's clicked on among three different ones. The goal is to indicate which one is active without reverting back to the original color once the mouse is released, similar to a focus effect. I&a ...

Bootstrap form validation solution

Utilizing bootstrap validation to validate a jsp page. The folder structure is as follows: WebContent ├── bootstrap-form-validation ├── js └── pages All three folders are under the web content. If I create another folder called teacher ...

Choosing an element in JQuery based on the value of its style property

I have three divs with the same class but different styles. I need to select only the third one using JQuery. <div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable" tabindex="-1" role="dialog" aria-labelledby="ui-dialog-title-div ...

Ensure that all content in the table rows remains visible, even in a table with unusually lengthy cells

My HTML table is structured like this: +-------------+-------------+----------+ | Single line | Single line | Very, | | | | very | | | | long | | | | text, | | ...

What's the reason for the font weight property in CSS not affecting the font family?

The code structure in my HTML file includes a link to Google Fonts with the following call: <link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet"> Following that, I specify the font family and weight in the CSS section: ...

AngularJS NG-Grid displaying incorrect value for select cell

I'm working on creating a table with a column that needs to be selected based on a value received from the server. The server sends me 4 as the value, but the first option is always selected instead. $scope.lotteryOptions = { data: 'myDa ...

What's the best way to update the fill color of an SVG dynamically?

Is it possible to change the color of an SVG image dynamically without using inline SVG tags? I want to create a code that allows users to specify the source for the SVG tag and a hexadecimal color value, enabling them to customize any SVG image with their ...

Why is AJAX failing to execute PHP file from JavaScript?

The Issue: Hello, I have confirmed that my PHP file is functioning properly. However, the AJAX code connected to my JavaScript function seems to be malfunctioning. Even though the function is triggered, nothing happens as expected. The Script: Check o ...

Transform your image using a stunning zoom-in effect

Currently, I am experimenting with a feature for my website where I want images to switch upon hover. While I have successfully achieved the image switch on hover, I am looking to create smooth transitions between the switches, with the second image being ...

What is the best way to customize multiselection in jqgrid?

jQuery("#grid").jqGrid({ datatype: "local", width:'auto', height: 'auto', multiselect:true, colNames:[ 'no' ], colModel:[ {name:'no', align:'r ...

Adhesive Bottom Navigation, Top Banner, and Full-Height Page Content

Trying to create a header, sticky footer, and content section with 100% height, but facing issues with the middle height. Below is the code and jsfiddles provided. Using HTML 4.0 strict in IE7 without the option to change. jsfiddle without 100% height: ht ...

Place an IconButton component next to each Material UI TableRow

I am trying to include an icon next to the material UI table row component. Similar to the hint icon shown in the screenshot below Here is my attempt so far, but it's not functioning as expected: Check out the code on CodeSandbox https://i.stack.i ...

What is the best way to delete spaces between span elements while preserving the spaces within each individual span tag?

Is there a way to eliminate unexpected spaces that appear between spans? One attempt was made by setting font-size: 0 within the wrapper <div>, which successfully removed spaces not only between <span> tags but also within each <span> ta ...

No longer will popups be blocked

I've been trying to work with jQuery to create a popup. But I'm facing an issue where my code is being blocked by Google. Here is the code snippet: <script type="text/javascript"> $(document).ready(function () { var flag = true; ...

How to center text both horizontally and vertically in HTML

I am struggling with centering a background image along with a 1-line text vertically and horizontally inside a div. Although I have successfully centered the image, the text remains misaligned. I attempted using vertical-align: middle without success. Be ...