Adding additional elements to a div in a horizontal orientation

I am currently working on a project where I need to display bars and restaurants based on specific filter criteria. I have a container div called .resultsContainer where all the results will be displayed. While I can easily append more divs (.barContainer) to this container, I am facing challenges when it comes to appending information to these divs. My goal is to have each .barContainer (.infoContainer) hold details such as pictures, names, and addresses horizontally without any margins in between them. However, no matter how hard I try, the divs are stacked vertically due to the full width taken up by margins even when set to zero. I attempted to use createDocumentFragment() function to stack child elements together before appending them to .barContainer, but the layout still isn't as desired. I believe there might be a way to dynamically create an HTML template for displaying the information, but being new to web coding, I'm struggling to find the right solution. In my code, I intend to include picture, address, and name in the infoContainer wherein each of them should occupy one third of the parent div's width and be displayed next to each other seamlessly without any spacing. Moreover, they should have different background colors for distinction.

HTML:

<body>
    <button type="button" id="myBtn" onclick="appendBars()">Append</button>     
    <div id="resultsContainer"></div>
</body>

JS:

function appendBars(){

    var barContainer = document.createElement("div");
    barContainer.className = "barContainer"

    var infoContainer = document.createElement("div");
    infoContainer.className = "infoContainer";

    var barPicture = document.createElement("div");
    barPicture.className = "barPicture";

    var barName = document.createElement("div");
    barName.className = "barName";

    var barAddress = document.createElement("div");
    barAddress.className = "barAddress";

    var fragment = document.createDocumentFragment();

    fragment.appendChild(barPicture);
    fragment.appendChild(barName);
    fragment.appendChild(barAddress);
    infoContainer.appendChild(fragment);

    barContainer.appendChild(infoContainer);

    document.getElementById("resultsContainer").appendChild(barContainer);

}

CSS:

#resultsContainer{
    float: right;
    background: red;
    width: 620px;
    height: 100%;
    overflow-x: auto;
}

.barContainer {
    background-color: white;
    width: 100%;
    height: 200px;
    border-top: 0px;
    border-right: 0px;
    border-bottom: 1px;
    border-style: solid;
    border-color: grey;
}


.infoContainer{
    width: 620px;
    height: 180px;
    background-color: white;
    padding: 10px;
}

.barPicture{
    width: 200px;
    height: 180px;
    margin: 0;
    background-color: yellow;
}

.barName{
    background-color: blue;
    margin: 0px;
    width: 200px;
    height: 180px;
}

.barAddress{
    background-color: green;
    margin: 0px;
    width: 200px;
    height: 180px;
}

Answer №1

To ensure that your elements only take up the necessary space, consider setting them to inline-block. By default, a div element is block-level and will always occupy the entire row regardless of the specified width.

If you want to learn more about the differences between block and inline elements, check out this resource.

function appendBars(){

    var barContainer = document.createElement("div");
    barContainer.className = "barContainer"

    var infoContainer = document.createElement("div");
    infoContainer.className = "infoContainer";

    var barPicture = document.createElement("div");
    barPicture.className = "barPicture";

    var barName = document.createElement("div");
    barName.className = "barName";

    var barAddress = document.createElement("div");
    barAddress.className = "barAddress";

    var fragment = document.createDocumentFragment();

    fragment.appendChild(barPicture);
    fragment.appendChild(barName);
    fragment.appendChild(barAddress);
    infoContainer.appendChild(fragment);

    barContainer.appendChild(infoContainer);

    document.getElementById("resultsContainer").appendChild(barContainer);

}
#resultsContainer{
    float: right;
    background: red;
    width: 620px;
    height: 100%;
    overflow-x: auto;
}

.barContainer {
    background-color: white;
    width: 100%;
    height: 200px;
    border-top: 0px;
    border-right: 0px;
    border-bottom: 1px;
    border-style: solid;
    border-color: grey;
}


.infoContainer{
    width: 620px;
    height: 180px;
    background-color: white;
    padding: 10px;
}

.barPicture{
    width: 200px;
    height: 180px;
    margin: 0;
    background-color: yellow;
    display:inline-block
}

.barName{
    background-color: blue;
    margin: 0px;
    width: 200px;
    height: 180px;
    display:inline-block
}

.barAddress{
    background-color: green;
    margin: 0px;
    width: 200px;
    height: 180px;
    display:inline-block
}
<button type="button" id="myBtn" onclick="appendBars()">Append</button>     
    <div id="resultsContainer"></div>

Answer №2

To optimize the layout of your divs, you can utilize the display: flex property on the .infoContainer class. This will ensure that the div elements are aligned in a row. Additionally, consider using display: inline-block; for the .barPicture and .barName classes, although be cautious as it may lead to overflow-related issues.

Experiment with .infoContainer {display: flex;}:

function appendBars(){

    var barContainer = document.createElement("div");
    barContainer.className = "barContainer"

    var infoContainer = document.createElement("div");
    infoContainer.className = "infoContainer";

    var barPicture = document.createElement("div");
    barPicture.className = "barPicture";

    var barName = document.createElement("div");
    barName.className = "barName";

    var barAddress = document.createTextNode("div");
    barAddress.className = "barAddress";

    var fragment = document.createDocumentFragment();

    fragment.appendChild(barPicture);
    fragment.appendChild(barName);
    fragment.appendChild(barAddress);
    infoContainer.appendChild(fragment);

    barContainer.appendChild(infoContainer);

    document.getElementById("resultsContainer").appendChild(barContainer);

}
#resultsContainer{
    float: right;
    background: red;
    width: 620px;
    height: 100%;
    overflow-x: auto;
}

.barContainer {
    background-color: white;
    width: 100%;
    height: 200px;
    border-top: 0px;
    border-right: 0px;
    border-bottom: 1px;
    border-style: solid;
    border-color: grey;
}


.infoContainer{
    width: 620px;
    height: 180px;
    background-color: white;
    padding: 10px;
    display: flex;
}

.barPicture{
    width: 200px;
    height: 180px;
    margin: 0;
    background-color: yellow;
}

.barName{
    background-color: blue;
    margin: 0px;
    width: 200px;
    height: 180px;
}

.barAddress{
    background-color: green;
    margin: 0px;
    width: 200px;
    height: 180px;
}
<button type="button" id="myBtn" onclick="appendBars()">Append</button>     
    <div id="resultsContainer"></div>

Answer №3

It seems like you're asking about how to style certain elements in CSS. To achieve this, simply add the following code snippet to your stylesheet:

/* TARGETED CLASSES FOR STYLING */
.barPicture,
.barName,
.barAddress {
    display: inline-block;
}

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

The state update does not seem to be affecting the DOM

I've implemented this component in my React app. It updates the state every second, but oddly enough the value <p>{this.state.time}</p> is not changing as expected. When I print the state value, it does change every second. import React f ...

Display an icon from the glyphicon library in an Angular application based on a

My Controller: .controller('BlogController', function(blogFactory, $routeParams, $scope){ var that=this; stat=false; this.checkbookmark = function(bId){ console.log(bId) blogFactory.checkBookmark(bId, function(response){ ...

Determine the overall sum of rows present within this particular tbody section

I am struggling to calculate the total number of tr's within my nested tbody, but I am not getting the correct count. The jQuery code I used is returning a high number like 44 rows instead of the expected 7 rows. Can anyone point out where I might ha ...

What is the reason the 'Add' type does not meet the 'number' constraint?

I experimented with type gymnastics using Typescript, focusing on implementing mathematical operations with numeric literals. First, I created the BuildArray type: type BuildArray< Length extends number, Ele = unknown, Arr extends unknown ...

Vue.js routing and mixin dilemma

Calling all Vue developers! I am currently working on a vuebnb tutorial and running into an issue with routing and mixins. I have attempted to assign data before entering the router using the beforeRouteEnter guard, but it seems like my template is being r ...

The time parameter in jQuery's ScrollLeft animation method seems to be disregarded

Hey everyone, I'm having trouble understanding this: thumb.animate( {'scrollLeft': active.width()*3}, 'slow' ); The scrolling works fine, but the "slow" parameter seems to be ignored. Instead of moving slowly, it moves instan ...

What is the best way to prevent the content within a div from being obscured by a sticky element affixed to the bottom of the div?

I am struggling with ensuring that the content in my wrapper does not get cut off by the "view more" div attached to the bottom. The default wrapper cuts off the children, and even when expanded, the issue persists due to CSS problems. In React, the gener ...

I attempted to connect my JavaScript file to my HTML file, but unfortunately, no changes are taking place

I have created an HTML file where I am trying to pass input from it into a function located in a separate JavaScript file, and then have the data added to a SQL server. The JavaScript file works perfectly fine when run independently, but when I try to call ...

Effective ways to enable users to upload files in a React Native app

Being in the process of developing a react native app, I am faced with the challenge of allowing users to easily upload files from their mobile devices (pdf, doc, etc). Unfortunately, my search for a suitable native component has proven fruitless. Can anyo ...

Enhancing the structure and authentication of license plates

I'm having trouble figuring out how to apply Javascript to the text area. Can you advise on whether it's best to use onfocus, onblur, or onclick? Here is the code for the text area: <input type="text" name="c_Kenteken" id="invoerKenteken" cl ...

What are some ways to enhance the opacity of a Material UI backdrop?

I'm looking to enhance the darkness of the material UI backdrop as its default appearance is not very dark. My goal is to make it dimmer and closer to black in color. ...

Select Box in HTML now supports the addition of a Background Image, enhancing

Currently, I am trying to customize the select option box using HTML and CSS. My main goal is to incorporate a scroll feature into the option box. However, I have encountered an issue where the image of the checked box remains visible on the screen. Below ...

Issue encountered while using Typescript with mocha: Unable to utilize import statement outside a module

Exploring the world of unit testing with mocha and trying to create a basic test. Project Structure node_modules package.json package-lock.json testA.ts testA.spec.ts tsconfig.json tsconfig.json { "compilerOptions": { "target&qu ...

Angular: Utilizing Nested ng-repeat Alongside groupBy Feature on Initial Page Load or Refresh

With some help, I've made it this far on my project. However, I need to take one more step to achieve my goal. I want to group data based on an attribute that is currently passed through an ng-click action. Is there a way to automatically do this on p ...

Is the contact form confirmation message displaying too prominently?

I am currently troubleshooting two different forms on a website I'm developing, and I am unsure of the exact cause of the issue. Form 1: Form 2: When attempting to submit Form 1 without entering any information, an error message is displayed exactl ...

Encountering a 404 error when typing a link and refreshing the page in Laravel combined with VueJS

I encountered an issue while working on a Laravel VueJS application for our Proof of Concept (POC). I have integrated vue-router into the project and it is functional. However, whenever I attempt to navigate to a specific page defined in the routes of app. ...

Having trouble with PHPmailer resulting in a 504 gateway timeout error

I keep receiving a 504 gateway timeout error from my server when using phpmyadmin to individually send emails to a list of approximately 1200 users. It is crucial for me that the emails are sent out one by one, as I want to avoid exposing any email addres ...

Encountering issues with basic login functionality using Node.js and MongoDB - receiving a "Cannot

I'm experiencing some difficulties trying to set up a login for my website. I have a user registered in my database with the email: [email protected] and password 123. The issue arises when I attempt to use the POST method, as it returns the fo ...

"Although disabled, input elements can still be focused on in Firefox browser

Illustrative example let userInput = document.createElement("input"); userInput.id = "user-input"; userInput.type = "number"; userInput.className = "user-number-input"; userInput.disabled = true; document.body.appendChild(userInput); .number-inp ...

What could be the reason for Internet Explorer pulling styles from the media=print CSS?

The HTML below incorporates a DHTML behavior into a CSS class. Unfortunately, Internet Explorer (specifically version 8 in compatibility mode) does not read the top style exclusively; instead, it also recognizes the @media print. <!--[if IE]> < ...