Empty gaps separating two divs nested within another div within a div (unable to function)

I'm diving into the world of HTML, CSS, and JS as I embark on creating a weather application. My vision is to include the temperature, followed by the city and its corresponding weather (accompanied by an icon), and lastly additional details like humidity and wind speed, each in their own separate columns for clarity. However, when attempting to add two more columns within the extra info column, things started to go awry. How can I achieve this layout, where each piece of information is displayed neatly with justified spacing? Here's an excerpt from my code:

Humidity___________________ 30%

In my JSFiddle demo, you'll notice that the last two columns are merged together. How can I address this issue while ensuring that the layout remains responsive based on screen size, adjusting the space-between dynamically without rigid margin values?

* {
                background-color: rgb(41, 35, 35);
                font-family: 'Rubik', sans-serif;
            }

            body {
                background-color: rgb(41, 35, 35);
                position: relative;
                display: flex;
                height: 96vh;
                width: 98vw;
            }

            p {
                color: whitesmoke;
                font-size: 32px;
                font-weight: 300;
                margin: 0;
            }

            .divSearch {
                position: absolute;
                top: 0;
                right: 0;
            }

            #searchBar {
                width: 260px;
                height: 42px;
                margin-right: 10px;
            }

            #searchIcon {
                width: 42px;
                height: 48px;
                background-color: rgb(71, 71, 63)
            }

            .recentCity {
                font-size: 26px;
                font-weight: 300;
                margin: 12px;
            }

            .cityStats {
                display: flex;
                position: absolute;
                bottom: 0;
                left: 0;
                margin-left: 12px;
                width: 540px;
                text-align: left;
                align-items: center;
                justify-content: space-around;
            }

            #temperature {
                font-size: 82px;
                margin-right: 26px;
            }

            #temperature span {
                font-size: 60px;
                font-weight: 300;
            }

            .divExtraInfo {
                max-width: 150px;
                min-width: 130px;
            }

            #spaceBetween {
                display: flex;
                justify-content: space-between;
            }
<div class="cityStats">
                <div id="divTemperature">
                    <p id="temperature"> 19°<span>C</span></p>
                </div>
                <div class="divExtraInfo">
                    <p id="city">Rosario</p>
                    <p id="weatherText"> <span id="weatherIcon">☀</span> Sunny</p>
                </div>
                <div class="divExtraInfo" id="spaceBetween">
                    <div>
                        <p id="windSpeed"> Wind</p>
                        <p id="humidity">Humidity</p>
                    </div>
                    <div>
                        <p id="windSpeedKM"> 32km/h</p>
                        <p id="humidity%">62%</p>
                    </div>
                </div>
            </div>

Answer №1

Here is a JSFiddle I created that might help address your question, although I can't guarantee it's exactly what you're seeking. The design should adjust accordingly since I incorporated the use of vw.

Answer №2

Utilizing display grid can help eliminate the need for fixed widths; By using flex and auto margins, you can align the temperature statistics to the left and right. Any text misalignment that is visible is a result of the icon, which can be adjusted with additional CSS alignment.

* {
  margin:0;
  padding:0;
  box-sizing: border-box;
  background-color: rgb(41, 35, 35);
  font-family: 'Rubik', sans-serif;
}

.container{
  color: whitesmoke;
  font-size: 2rem;
  font-weight: 300;
  
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: auto auto 1fr;
}

.temperature {
  font-size: 5rem;
}

.quick-info {
  display: flex;
  flex-direction: column
}


.conditions {
  display: flex;
  flex-direction: column;
}

.condition {
  display: flex;
}

.condition-stat{  
  margin-left: auto;
}
<head>
  <link href="https://fonts.googleapis.com/css2?family=Rubik:wght@300;400&display=swap" rel="stylesheet">
  </head>
<div class="container">
  <p class="temperature"> 19°<span>C</span></p>
  <div class="quick-info">
    <p>Rosario</p>
    <p>☀ Sunny</p>
  </div>
  <div class="conditions">
    <div class="condition">
      <p>Wind</p>
      <p class="condition-stat">32km/h</p>
    </div>
    <div class="condition">
      <p>Humidity</p>
      <p class="condition-stat">62%</p>
    </div>
  </div>

</div>

Answer №3

Apply the use of padding-right to resolve this issue:

* {

  background-color: rgb(41, 35, 35);
  font-family: 'Rubik', sans-serif;
}

body {

  background-color: rgb(41, 35, 35);
  position: relative;
  display: flex;
  height: 96vh;
  width: 98vw;
}

p {

  color: whitesmoke;
  font-size: 32px;
  font-weight: 300;
  margin: 0;
}

.divSearch {

  position: absolute;
  top: 0;
  right: 0;

}

#searchBar {

  width: 260px;
  height: 42px;
  margin-right: 10px;
}

#searchIcon {

  width: 42px;
  height: 48px;
  background-color: rgb(71, 71, 63)
}

.recentCity {

  font-size: 26px;
  font-weight: 300;
  margin: 12px;

}

.cityStats {

  display: flex;
  position: absolute;
  bottom: 0;
  left: 0;

  margin-left: 12px;
  width: 540px;
  text-align: left;
  align-items: center;
  justify-content: space-around;
}

#temperature {

  font-size: 82px;
  margin-right: 26px; /* Apply padding-right here */
}

#temperature span {

  font-size: 60px;
  font-weight: 300;
}

.divExtraInfo {
  max-width: 150px;
  min-width: 130px;

}

#spaceBetween {

  display: flex;
  justify-content: space-between;

}
<link href="https://fonts.googleapis.com/css2?family=Rubik:wght@300;400&display=swap" rel="stylesheet">

<div class="cityStats">

      <div id="divTemperature">

        <p id="temperature"> 19°<span>C</span></p>

      </div>

      <div class="divExtraInfo">

        <p id="city">Rosario</p>
        <p id="weatherText"> <span id="weatherIcon">☀</span> Sunny</p>

      </div>

      <div class="divExtraInfo" id="spaceBetween">

        <div>

          <p id="windSpeed"> Wind</p>
          <p id="humidity">Humidity</p>

        </div>

        <div style="padding-left:30px">

          <p id="windSpeedKM">32km/h</p>
          <p id="humidity%">62%</p>

        </div>

      </div>


    </div>

Answer №4

To improve the layout, I recommend organizing it into separate "containers". For instance, you can use a "group-container" for elements that should be stacked together like the city name and weather information. Within this, create another container labeled city-container with three sub-groups. The last group can be aligned to the far right by adding margin-left: auto;. Adjusting the width of these groups can help align them better vertically. To align values next to labels, utilize

.group-container .content-text:nth-child(n+1){ margin-left:auto; }
, where n+1 targets the second child (values) under group-container while n+0 would refer to the label. Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child

I had some fun experimenting with multiple cities which also showcased longer city names and color-coding through data attributes and CSS styles.

Please note that the city names wrap awkwardly due to the current setting in

.extra-info { max-width: 150px; min-width: 130px; }
- widening this slightly may provide a more appealing result.

// JavaScript code snippet here
// City weather readings array
var readings = [{
    name: "El Desertos",
    weather: {
        name: "Sunny",
        type: "sunny"
    },
    temperature: 35,
    windspeed: 17,
    humidity: 9
}, 
{
    // Additional city data...
}];
// Accessing DOM elements
const cityWeather = document.getElementById('cities')
const cityStats = cityWeather.querySelector('.city-stats');
// Loop through city readings
readings.forEach(city => {
    cityClone = cityStats.cloneNode(true);;
    cityClone.querySelector('.city').textContent = city.name;
    cityClone.querySelector('.temperature').textContent = city.temperature;
    cityClone.querySelector('.wind-speed').textContent = city.windspeed;
    cityClone.querySelector('.humidity-percent').textContent = city.humidity;
    cityClone.querySelector('.weather-text').textContent = city.weather.name;
    cityClone.querySelector('.weather-icon').dataset.icon = city.weather.type;
    cityWeather.appendChild(cityClone);
});
// CSS styles for the layout
* {
    font-family: 'Rubik', sans-serif;
}
body {
    background-color: rgb(41, 35, 35);
    height: 96vh;
    width: 98vw;
}
// Additional CSS styling...
<div id="cities">
    <div class="city-container city-stats">
        <div class="group-container temperature-container">
            <div class="content-text"><span class="temperature">19</span><span class="temperature-degree">°</span><span class="temperature-scale">C</span></div>
        </div>
        // More HTML content...
    </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

Stopping a velocity.js animation once it has completed: is it possible?

For the pulsating effect I'm creating using velocity.js as a fallback for IE9, refer to box2 for CSS animation. If the mouse leaves the box before the animation is complete (wait until pulse expands and then move out), the pulsating element remains vi ...

Ways to display URL parameters on an HTML page without using PHP

I'm currently working on a website using HTML (without PHP) and I'm facing an issue with displaying URL parameters on an appointment confirmation page. The appointment details are being successfully passed through the URL parameters, but I'm ...

Bizarre actions with jQuery append in Internet Explorer 8

Issue with adding elements to a div in IE8: The element is not added until the button is clicked twice, resulting in two new elements being added at once. Here's the code snippet in question: $(options.addButton).click(function(event) { var proto ...

CSS page rule option for optimal display in Safari

What is the workaround for using alternative rules in Safari? I am currently implementing the angularPrint directive to enable print functionality on certain pages. This directive includes some helper classes in my document and utilizes the @page direct ...

Arranging elements within the flex container in perfect alignment

I'm stuck trying to figure out how to adjust the layout of a flex container with three child elements. Here is the code I'm working with: html,body,div {margin:0;padding:0;} .cont { display: flex; justify-content: space-between; height: ...

Upon clicking, the input texts revert to their original default values

I have a form below that is working as intended, except for one issue. When I click the 'Add' link, the texts in all the textboxes revert to their default values, as shown in the images. How can I resolve this problem? Thank you. before click: ...

How can I turn off autocomplete in MUI textfields?

Currently, I am working with the latest version of mui. Within my user contact info form, there is a zip code field that I do not want to be auto completed if the value is null. However, despite my efforts, it continues to autocomplete with the email saved ...

Deactivate the image button when no image has been chosen from the input field

Is it possible to have the image button disabled when no image is selected in the input field? The button should be inactive if an image is not chosen. import { useState } from "react"; const CommentForm = ({ handleSubmit, submitLabel }) => ...

Exporting an HTML table to Excel with JavaScript runs into issues when it encounters the '#' character

Looking for a JavaScript solution to export HTML tables to Excel. I attempted a script that exports tables, but it stops when encountering special characters like '#'. Can someone assist me with this issue? Thank you in advance. <script src= ...

PHP code containing an if/else statement that performs form validation and validates the entered date of birth

Currently, I am practicing my PHP skills with if and else statements as well as form validation. The main concept I am working on involves the following: Upon entering a name, Date of Birth (DOB), and email, and then clicking the submit button, the action ...

Tips for aligning text in MUI Breadcrumbs

I am currently utilizing MUI Breadcrumb within my code and I am seeking a solution to center the content within the Breadcrumb. Below is the code snippet that I have attempted: https://i.stack.imgur.com/7zb1H.png <BreadcrumbStyle style={{marginTop:30}} ...

Column content merging in BS layout

Currently, I am exploring how to implement a two-column layout with the left column serving as a scrollable navbar. At this early stage of development, please excuse any imperfections in the code! One issue that arose involved using the .sidebar class wit ...

The Bing map control fails to adhere to the div element

After visiting , I successfully generated a map using the following HTML code: <div class="fluid-row" style="height:300px;"> <div class="span12"> <div id="prdmap" class="map"> </div> </div> Here is the accompanying J ...

Stop child elements from spilling out of the parent container without using fixed height measurements

I am in the process of creating an angular component with a header, buttons, a table, more buttons, and a footer. However, I am facing some challenges with the layout. <my-component> <div>Header Section</div> <some-stuff class=&quo ...

Differentiate the items within a list containing identical divs using JavaScript

Currently, I am expanding my knowledge in HTML, CSS, and JS by incorporating Angular and JQuery. In one of my projects, there is a div labeled "eventBoxes" where multiple divs known as "eventBox" can be added. I have created a template for the eventBox i ...

Differences between Animation Easing in iOS and CSS3 Cubic Bezier Functions

I am currently working on an iOS app and I am trying to improve the animations within it. Having a lot of experience with CSS3 animation and transitions, I want to be able to define my animations in iOS with the same precision that is possible with CSS3 ( ...

The variable continuously updates itself inexplicably, without any specific code dictating it to

In the sandbox-based game I am playing, two variables are present. blocks (this is an array) and blockssave (also an array) However, there are also functions included: var game = { blocksave: function() { blockssave = blocks; blo ...

Develop a personalized event using JavaScript and activate it

I have encountered a problem with a Google ad that expands on click and closes when the close button is hit. However, due to changing conditions, I now need it to expand first and then automatically close after a certain time. Additionally, every time it e ...

Modify the hover color of heading 1 only for hyperlink texts

I am attempting to adjust the hover color specifically for text that contains a link. Below is the CSS code I have tried, but it changes the color regardless of whether there are links present or not. h1, h2, h3, h4 { color:#3F3F3F; } h1:hover, h2:hove ...

"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 ...