Looking for a way to align two child divs next to each other within a parent div? I've tried multiple methods, but none seem to be effective

Despite trying various methods like using inline-block and float, the divs in my code continue to stack on top of each other.

What could be causing this issue?

#resources {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
  display: block;
  width: 4%;
  background: rgba(0, 160, 255, 1);
  color: white;
  font-size: 1.5em;
  padding: 0.5em;
  border-radius: 10%;
}

#woodResource #stoneResource {
  display: inline-block;
  float: left;
}
<div id="resources">
  <div id="woodResource">
    <img src="/models/wood.png" id="wood"> 0
  </div>
  <div id="stoneResource">
    <img src="/models/stone.png" id="stone"> 0
  </div>

</div>

I have exhausted all possible solutions mentioned online such as using inline-block and float, but none seem to resolve the issue.

Answer №1

1.) To accommodate both children, consider increasing the width of the parent element. A 4% width may be too narrow depending on the size of the parent or window.

2.) Ensure that the selector includes a comma for both elements

#woodResource, #stoneResource { ... }
. Without the comma separating the selectors, the rule will only apply to #stoneResource as a child of #woodResource.

#resources {
position: fixed; top: 0; left: 0;
z-index: 1;
display: block;
width: 20%;
background: rgba(0, 160, 255, 1);
color: white;
font-size: 1.5em;
padding: 0.5em;
border-radius: 10%;
}

#woodResource, #stoneResource {
  display: inline-block;
}
 <div id="resources">
        <div id="woodResource">
            <img src="/models/wood.png" id="wood">
            0
        </div>
        <div id="stoneResource">
            <img src="/models/stone.png" id="stone">
            0
        </div>
    </div>

Pro tip: When using display: inline-block for the parent element, there is no need to specify its width. The width will automatically adjust to fit its contents in this scenario:

#resources {
display: inline-block;
background: rgba(0, 160, 255, 1);
color: white;
font-size: 1.5em;
padding: 0.5em;
border-radius: 10%;
}

#woodResource, #stoneResource {
  display: inline-block;
}
<div id="resources">
        <div id="woodResource">
            <img src="/models/wood.png" id="wood">
            0
        </div>
        <div id="stoneResource">
            <img src="/models/stone.png" id="stone">
            0
        </div>
    </div>

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

Execute a function in HTML that has been declared in the controller

Is there a way to automatically trigger a function when an HTML page is loaded? Currently, the function is manually invoked by clicking a button, as shown below: <p> {{address}} </p> <button class="btn btn-primary checkout" ng-click="g ...

How to apply new CSS styles to override the existing ones for an element

I have a website with custom CSS styling applied to an <A> tag, including styles for A:hover. However, I need to remove these hover effects for one specific instance of the tag. Is there a way in CSS to set a property so that it does not change? Let ...

Shifting Divs/Text while adjusting zoom levels in HTML/CSS with Bootstrap

As a newcomer to the coding world, I am encountering an issue with my Navbar. Everything appears fine at 100% zoom on the browser, but when I zoom in to 150%, everything becomes jumbled. How can I ensure that everything stays the same size regardless of zo ...

Website issues being displayed by Internet Explorer

Greetings! Let me begin by saying how wonderful this page is - I have already found many answers here before. Please forgive any errors in my English :) In my efforts to optimize my website for SEO and increase its speed, I made several changes. The site ...

The CSS div gradually descends as I increase the width of the browser

I'm in need of assistance with creating a website specifically for mobile devices and tablets. The issue I am facing is that when I expand my browser width to a tablet size, the "loginD" div shifts downwards - view this gif Below is my HTML code: &l ...

Mapping memory for FirefoxOS is like equivalent strides

Is there a way to create a memory mapped file in FirefoxOS, Tizen or other pure-JS mobile solutions? In the scenario of a mobile browser where you have large amounts of data that can't fit in RAM or you prefer not to load it all at once to conserve R ...

A pair of HTML/PHP forms coexisting on a single page

Initially, the first form will prompt for a customer ID, which will then be extracted from a database. Following that, the second form will request the new address to be updated in the database. The goal is for this entire process to take place on a single ...

The color of the button does not change when the jQuery onclick event is triggered

I am facing an issue with my buttons, specifically with Button 2. When Button 2 is clicked, I want the background color to change using an onclick event. However, despite my efforts, I have been unable to achieve this. Whenever I click Button 2, the colo ...

JS seems to kick in only after a couple of page refreshes

I'm facing an issue with my 3 columns that should have equal heights. I've implemented some JavaScript to achieve this, which you can see in action through the DEMO link below. <script> $(document).foundation(); </script> <scri ...

Enhance Your List with Icon Decorations

Is it feasible to utilize a sprite image in place of the default bullet for an ul list? Is this achievable? ...

What could be causing this jQuery selector to not select any elements?

Here's a simple question with some code that needs troubleshooting: $insides = $('<thead><tr><th><!--Blank space --></th></tr></thead><tbody><tr class="green-row"><td>Opportunities to D ...

Struggling to replicate Bootstrap's col-md-6 with Tailwind CSS in React?

How can I make my box/form appear in the center of the screen with its width filling half or all of the screen? I've attempted using the w-full class on different elements but haven't been successful in adjusting the width. I am working with rea ...

What is the best way to place a list within a <div> element?

There is a <div> with an image in the background. I now want to include a list within that div, but I'm having trouble positioning it. Check out this image for reference In the image above, you can see the background and the list, but I'm ...

Switch the color (make it gray) of the selected tab in the navigation bar

<!-- Customizing the Navbar --> <nav class="navbar navbar-expand-sm bg-primary navbar-dark"> <a class="navbar-brand" href="<?php echo base_url('Controller_dashboard'); ?>"><strong>StuFAP MS</strong></a> ...

col-md is taking precedence over col-sm at lower screen resolutions

Col-md takes precedence over col-sm in Bootstrap 4 https://i.sstatic.net/ydL5O.png ...

The margin-top property in CSS is not functioning as intended for a specific pixel value when applied to

I'm having trouble with positioning an icon using margin-top: -35px; under #menu. When I click on the icon, the side navigation bar doesn't work properly. However, if I adjust it to -15px, the side navigation bar displays correctly. Can someone h ...

Refreshing an HTML table using instance variables from a C# class (utilizing jQuery and AJAX)

Explore <script type="text/javascript"> $(document).ready(function () { $("#viewDetails").click(function () { $.ajax( { type: "POST", url: '@Url.Action("GetDetail", "ControllerName")' ...

Steps for aligning two input elements side by side in Bootstrap 4

I need help positioning the text input and button side by side, with a margin of about 10px between them. The code I have right now is producing the result shown in the attached photo. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/c ...

Expanding the size of your Bootstrap 3 input box

https://i.sstatic.net/h7HpX.png Incorporating Bootstrap 3 into a Flask application, I have created a basic layout so far: <div class="container"> <div class="row"> <div class="col-xs-12"> <div class="content"> ...

The CSS tag combination of 'position:relative;' and 'top:xx%;;' functions properly in Chrome, however, it does not work in any non-webkit browsers such as Firefox, Internet Explorer, Opera, etc

Encountering some strange behavior with a simple piece of code that I can't seem to figure out! I am trying to position a footer navigation at the bottom of my page using 'position:relative;' and it works perfectly in Chrome. However, in FF ...