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

Enhancing Dropdown Functionality Using Jquery

Hey there, I am looking to create a dropdown list/menu similar to the one shown in this image: (a screenshot). Could you please guide me on how to achieve this? Can it be done using JQUERY? Thank you in advance, Andrea ...

Switching images between mobile and desktop view in responsive mode is a useful feature for optimizing

Within a specific folder structure, I have a collection of images designated for both desktop and mobile displays: img: img-1.png img-2.png img-3.png img-4.png img-5.png img-6.png img/mobile: img-1.png img-2.png ...

Using the request.getParameter method for casting in Java

I'm really struggling to understand why I'm being instructed to cast the object returned by getParameter to a string in the code below. When I try to assign it to the String variable "user," I encounter the error 'Type mismatch: cannot conve ...

What is the best way to display an alert box through AJAX technology?

My code snippet is as follows: <script> function updateQty(quantity) { $.ajax({ alert(quantity); }) } </script> Here is the corresponding HTML markup: <form name="quantityForm"> <select name="quantity" id="quantity" onChan ...

Image-switching button

I'm new to JavaScript and struggling with my first code. I've been staring at it for two days now, but can't figure out what's wrong. The goal is to create an HTML page where the user can change an image by clicking on a button, and th ...

A guide on incorporating a set of components to be utilized as custom HTML elements in Angular

I am in the process of revamping my application to be more modular on the UI side, with a focus on separating different elements including: App header Left navigation panel Main content on the right side of the nav panel I have successfully figured out ...

Creating a Jquery method to handle multi-line ellipsis for long text

Check out this fiddle I made: http://jsfiddle.net/mupersan82/JYuSY/ This snippet handles multiline ellipsis: $(function() { var ellipsisText = $('.multilineEllipseText'); var textContainer = $('.incidentCellBottomRight').height(); whi ...

What is the best way to perform calculations within a PHP loop for <input> elements and then display the results using a JavaScript loop?

Hello everyone, I'm currently struggling with displaying the calculations from a loop of input tags. What I'm trying to achieve is having 5 rows with input fields. At the end of each row, there should be a span area that displays the calculation ...

What could be causing my website's screen resolution to not fit properly on mobile devices?

Initially, the resolution perfectly matched the width of mobile devices. However, after changing the background image, for some reason, the width no longer fits the device length precisely. I've tried resetting to a point where the resolution was fine ...

Create a dynamic HTML page using JavaScript

When I click on the following links: How can I create a new page using JavaScript? Create and dynamically append elements I'm looking to dynamically add HTML elements with JavaScript within a div, but I don't want my code to become overly comp ...

Adding a dynamic object to the secondary column header of an output table: A step-by-step guide

Working on a transition table module, my main challenge is presenting the output in a user-friendly manner. In the past, I used Excel for this task, where making the table readable was simple but extracting data from millions of rows took hours. Now with R ...

What is the best way to integrate a PHP page with its own CSS and JavaScript into a Wordpress page?

I'm facing a challenge with integrating a dynamic PHP table into my WordPress page. Despite working perfectly when opened locally, the CSS and JavaScript used to create the table are not functioning properly when included in a WordPress page via short ...

Two Mistakes Found in the Dropdown Menu

Can anyone assist me with two errors I am encountering in my "Drop Down Menu"? Error 1: Whenever I hover towards the right direction in the sub-menu (e.g., Portfolio 2), a black box appears. This issue does not occur when hovering above the text (e.g., P ...

Display the progress bar's completion percentage during animation

Creating a progress bar using HTML with the following structure: <div class="progressbar"><div class="text">%0 Completed</div> <div class="progressbar_inner"></div> </div> Implemented jQuery code fo ...

Upon the page loading, only select a handful of checkboxes (JSF)

I am currently developing a web application using JSF 2.0 and I need to have checkboxes pre-selected when the page loads. <h:selectManyCheckbox value="#{UserRegistration.rightSelected}" id="myRight"> <f:selectItem itemValue="add" itemLabel="A ...

What's the best way to ensure that my image remains perfectly centered at the point where the background colors of my

html <body> <header> <div class="indexHeaderWall" id="headerLeftWall"> </div> <img id="profilePic" src="icons/myPicture.jpg" alt="Picture of Daniel Campo ...

Utilize Javascript to Populate Form Fields Based on "Selected Name"

I am currently facing a challenge in using javascript to automatically populate a form, specifically when it comes to setting the value for the country field. <form action="/payment" method="post" novalidate=""> <div class="input_group"> ...

Utilizing a fallback option for HTML5 video with a flash player and the ability to manipulate the

My dilemma involves an HTML5 video where I am utilizing jquery to interact with the player using the code snippet: video.currentTime += 1;. However, when Internet Explorer switches to Flash plugins, my JQ commands cease to function. How can I manage and co ...

Steps for compiling SCSS to CSS using Angular-CLI and moving it to the assets directory

I am currently working on an Angular 5 project with an assets folder where I store my common CSS file and reference it in index.html. I now plan to create a new folder called "sasstyles" and place some .scss files there. When I compile or run the project ...

Place an element in a higher position than a slideshow

I recently encountered an issue where I was trying to place a png image above my slideshow. Despite trying various solutions, the image always appeared behind or on top without transparency. Based on the erratic display, it seems like the problem might be ...