Tips for arranging multiple divs in a parent div in a horizontal alignment with uniform space in between

Although there are numerous inquiries concerning this topic, I am having trouble sorting it out. As a newcomer to CSS, I am attempting to replicate the straightforward box within a box technique.

You can view my Codepen here.

Additionally, here is the HTML snippet for further clarification.

<html>

<head>
    <link rel="stylesheet" href="index.css">
    </script>
</head>

<body class="body">
<!-- Navbar -->
        <nav class="navbar">
            <h1>Hello World</h1>
        </nav>
<!-- Header Boxes -->
        <div class="header-box">
            <div class="child-box-1">
                <div class="child-box-2">
                    <div class="child-box-3">
                    </div>
                </div>
            </div>
        </div>
</body>
</html>

In essence, the class = 'header-box' should contain child-box-1,child-box-2,child-box-3, resulting in a red, yellow and black box nested within the primary blue box.

Answer №1

    @import url('https://fonts.googleapis.com/css2?family=Lato:wght@300;400&family=Montserrat&display=swap');
    
    * {
        box-sizing: border-box;
        padding: 0;
        margin: 0;
    }
    
    .body {
        padding: 0;
        margin: 0;
        background-color:rgb(57, 67, 77);
    }
    
    .navbar {
        padding: 20;
        margin: 0;
        max-width: 100%;
        background-color:rgb(57, 67, 77);
        display: flex;
        justify-content: center;
    }
    
    h1 {
        padding-top: 10;
        margin: 0;
        font-family:'Montserrat';
        color: whitesmoke;
        font-size: xx-large;
    }
    
    .header-box {
        padding:0;
        margin: 10;
        max-width: 100%;
        height:200px;
        background-color: blue;
        display: flex;
        text-align: center;
    }
    
    .child-box-1 {
        padding: 0;
        margin: 10;
        background: red;
        width: 32%;
        height: 40px;
        margin: 1%;
    }
    
    .child-box-2 {
        padding: 0;
        margin: 10;
        background: yellow;
        width: 32%;
        height: 40px;
        margin: 1%;
    }
    
    .child-box-3 {
        padding: 0;
        margin: 10;
        background: black;
        width: 32%;
        height: 40px;
        margin: 1%;
      
    }
    <html>
    
    <head>
        <link rel="stylesheet" href="index.css">
        </script>
    </head>
    
    <body class="body">
    <!-- Navbar -->
            <nav class="navbar">
                <h1>Hello World</h1>
            </nav>
    <!-- Header Boxes -->
            <div class="header-box">
                <div class="child-box-1"></div>
              <div class="child-box-2"></div>
              <div class="child-box-3"></div>
            </div>
    </body>
    </html>

Answer №2

.header-container {
    margin: 0 auto;

    width: 300px;
    height: 100px;
    background: lightblue;

    padding: 10px 20px;

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

.header-container > div {
    width: 50px;
    aspect-ratio: 1;
    background: red;
}
<div class="header-container">
  <div class="box-1"></div>
  <div class="box-2"></div>
  <div class="box-3"></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

Give a sidebar within a grid layout a sticky position

As I work on coding a website layout, I have utilized a grid template. My goal is to make the header and left sidebar sticky. I have successfully achieved this for the header, but unfortunately, the sidebar remains non-sticky. Despite researching solutions ...

How does margin:auto differ from using justify-content and align-items center together?

If you want to center both horizontally and vertically at the same time, there are two easy methods available: Option One .outer { display:flex; } .inner { margin:auto; } Option Two .outer { display: flex; justify-content: center; align-items ...

Bootstrap - encompassing or layering columns

I am trying to create a layout where two columns overlap or cover each other. The layout should consist of one column on the left side (6 columns) and one on the right side (6 columns). The desired result is to have: left side (8 columns) and right side ...

Tips for preserving dynamically generated HTML through Javascript during page reload

I have a straightforward question, but I haven't been able to find a suitable answer. Here's my situation: On my HTML page, I have a form. Using jQuery and AJAX, I submit the form and then use some JavaScript code to change the content of a spec ...

Navigation dropdown menu with the latest Bootstrap 4 features

Attempting to recreate the design from Codepen using Bootstrap 4. The current progress can be viewed in this Codepen (drop down with 2 columns), which is assisting with online tutorials. Here's the HTML for the Navbar: <nav class="navbar navbar- ...

Moving the sidebar from the app component to a separate component in Angular results in a situation where the sidebar does not maintain full height when the page is scrolled down

Within my Angular application, I have implemented a sidebar as a separate component. Previously, the content of the sidebar was housed within the main app component's HTML page, and it functioned properly by taking up the full height of the page when ...

pick out particular values from an array

I have a question that may seem basic to some, but I'm curious about how to select specific elements from an array. In this case, I have an array with 100 elements: var cubes = [element1, element2, element3 ...] and I want to select elements 25-35. ...

Strategies for aligning tooltips with the locations of dragged elements

One of my projects involves a simple drag element example inspired by Angular documentation. The example features a button that can be dragged around within a container and comes with a tooltip. <div class="example-boundary"> <div ...

Mandatory element in HTML5

While writing in html5, I came across the required attribute. Even after implementing it in my code, I noticed that the field does not display an alert when left empty with the message "This field is required". Can anyone provide insights on why the pop-up ...

Printing results in textfield and Textarea where there are additional white spaces

<input type=text name=goTitle value="<?php require'db_connection.inc.php'; $QUERY='SELECT title FROM news_topics WHERE id = '.$_POST['id']; $data=mysql_fetch_assoc(mysql_query($QUERY)) ;echo$data['title']; ?> ...

Prevent body from scrolling when mouse is over a div, while keeping the position unchanged

Although I've come across a few discussions about this topic before (including one of my own), I'm still searching for an improvement on the only solution that has actually worked for me. My goal is to enable mouse wheel scrolling for a height-l ...

Creating interactive SVG paths using only Javascript/jQuery to allow for dynamic rendering

Is there a way to simplify the code below? To view the result, click here I am wondering if it's possible to dynamically generate the "var pathx" line. If so, how can this be achieved? I want to write only one line for "var pathx" with dynamically c ...

Tips for styling an array of objects using mapping techniques

I have an array of messages and I am currently using the map() function. Each message in the array has two keys - one for the author and another for the message content. What I want to achieve is to change the styles of the div tag when displaying the last ...

Adjust the text size in a collapsible tree based on the number of expanded components inside the container

Hi there, I'm currently developing a code for an expandable/collapsible tree using checkboxes. I have successfully implemented the basic functionality, but I have a specific requirement. I want the text size to decrease as the tree expands to prevent ...

Guide to managing dynamic checkboxes in a form with HTML, PHP, or JavaScript

Currently, I am working on a form that allows users to add books to a database. The authors of these books should be pulled from the database as well. My plan is to create a PHP loop that generates a checkbox in the form for each author listed in the datab ...

When clicking on a div with the CSS property user-select set to none, the selected text in a contenteditable element is

I'm currently working on a unique JavaScript rich text editor that utilizes div elements with the CSS property user-select: none instead of traditional buttons. While this approach works perfectly in Firefox, I've encountered a major issue with G ...

Unusual behavior observed in Google Font when using display=swap

After attempting to integrate a Google Font API into my website by adding the following code to my HTML: <link href="https://fonts.googleapis.com/css?family=Playfair+Display|Source+Serif+Pro|Suwannaphum&display=swap" rel="stylesheet"> I encount ...

Place a list with scrolling and overflow features side by side

Having trouble getting my headers to scroll with overflow auto and white-space nowrap. Can't figure out why it's not working. I want to create hyperlinks within headers to link to specific parts of the website. I have the code for hyperlinking s ...

Regain the original properties after implementing a universal CSS reset

Working with older code that I must build upon always reminds me of the drawbacks of using global CSS resets. I have this outdated foo.css file that begins with * {margin:0; padding:0;} In the past, I would duplicate it to a new file called bar.css, ma ...

Incorporate a variable from an HTML form into PHP for FPDF functionality

How can I retrieve a variable from an "input" element in another HTML file? <input type="text" id="client" name="clientTest" required minlength="1" maxlength="30" size="30"> I need to pass this variable to my PHP file for FPDF. It seems like I hav ...