How can we use Bootstrap to ensure that items in a div are aligned horizontally on larger devices and vertically on smaller devices?

I am currently working on creating a div that contains some information, similar to the layout used on the Coinbase website. However, I am encountering issues with styling as I want this div to be responsive - horizontally aligned on larger devices and vertically aligned on smaller devices. My knowledge of CSS is limited, but I believe Flexbox might be the solution. Can you guide me on how to achieve this?

The goal is for the layout to look like the one on the Coinbase website. On larger screens, the content should appear in rows, while on smaller screens, it should switch to columns. Is there a Bootstrap class that can simplify this process? I am using Bootstrap for my project.

<!DOCTYPE html>
<html lang="en>

<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0>
    <title>Document>

    <style>
        .container {
            background: dodgerblue;
        }

        .section {
            display: flex;
            flex-shrink: 0;
            width: 100%;
            max-width: 1180px;
            margin: 0px auto;
            padding: 24px;
        }

        .box {
            display: flex;
            flex: 1 1 auto;
            flex-direction: row;
            justify-content: center;
            color: rgb(255, 255, 255);
            margin: 40px 0px;
        }

        h2 {
            margin: 0px 0px 12px;
            line-height: 48px;
            font-size: 56px;
            font-weight: 500;
        }

        .divChild {
            line-height: 24px;
            font-size: 16px;
            opacity: 0.7;
        }

        .divChildBox {
            flex: 1 1 0%;
            text-align: center;
            flex: 1 1 0%;
            text-align: center;
            flex: 1 1 0%;
            text-align: center;
        }
    </style>
</head>

<body>

    <div class="container">
        <section class="section">
            <div class="box">
                <div class="divChildBox">
                    <h2>$320B+</h2>
                    <div class="divChild">Total Volume Traded</div>
                    <div class="divChildBox">
                        <h2>100+</h2>
                        <div class="divChild">Countries supported</div>
                        <div class="divChildBox">
                            <h2>35M+</h2>
                            <div class="divChild">Verified users</div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>

</body>

</html>

Answer №1

If you want to showcase your content in a row instead of a column, consider using the flex property. Simply place all your display items within div child elements. Flex naturally aligns horizontally, eliminating the need to specify a direction unless it needs to be altered. Therefore, encapsulate your h2 and smaller text-holding divs within a parent div with the flex display, where additional flex properties can be applied.

In the parent class, I included

display: flex; justify-content: space-around; align-content: center;
. To enhance clarity in your HTML, use p tags instead of divs whenever possible.

By assigning the class to the parent element, you can easily target both the parent and its children using CSS selectors. This simplifies styling multiple elements simultaneously, resulting in cleaner HTML structure. For instance, you could modify multiple elements with one block of code like .parent h2 or .parent p.

Flexibility with responsiveness is achieved through utilizing media queries alongside flex properties. Unlike traditional block elements manipulated with media queries, flexbox provides a flexible container ideal for mobile screens and dynamic layouts. Incorporating media queries into your CSS ensures adaptability across various devices. For more insights on media queries, check out this informative article from a reputable source called CSS Tricks.

If this doesn't align with your objectives, feel free to clarify further.

* {
  margin: 0;
  padding: 0;
}

#main section div {
   color: rgb(255, 255, 255); 
   margin: 40px 0px; 
}

.parent {
  display: flex;
  justify-content: center;
  align-content: center;
}

.parent h2 {
  margin: 0px 0px 12px;
  line-height: 48px;
  font-size: 56px;
  font-weight: 500;
}

.parent p {
  line-height: 24px;
  font-size: 16px;
  opacity: 0.7;
}

@media (max-width:499px) {

  #main section div {    
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }

  #main {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .parent {
    flex-direction: column;
    justify-content: center;
    align-content: center;
  }
}
<div id="main" style="background: rgb(22, 82, 240);">
  <section style="width: 100vw; padding: 24px">
    <div>
      <div class="parent">
        <div>
          <h2>$320B+
          </h2>
          <p>
            Total Volume Traded
          </p>
        </div>

        <div>
          <h2>
            100+
          </h2>
          <p>Countries supported
          </p>
        </div>

        <div>
          <h2>
            35M+
          </h2>
          <p>
            Verified users
          </p>
        </div>
      </div>
    </div>
  </section>
</div>

I recommend separating your HTML and CSS for better organization. Avoid inline styles by creating classes that are defined once in your CSS file or style tag. By adding these classes to your HTML attributes, any necessary changes can be made centrally within your CSS.

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

What is the best way to measure the timing of consecutive events within a web browser, utilizing JavaScript within an HTML script tag?

Currently delving into the realm of JavaScript, transitioning from a Java/Clojure background, I am attempting to implement a basic thread-sleep feature that will display lines of text on the screen at one second intervals. Initially, I considered using t ...

The process of deleting lines from an image using Javascript

If I have an image of a data-table and I want to eliminate all the grid lines (defined as continuous vertical or horizontal pixels), is there a way to achieve this using Javascript's image data manipulation? I envision looping through a 2D array conta ...

Exploring Angular Material Design's method for vertically populating a column

Is there a way to fill a column vertically in my current app? https://i.sstatic.net/uu4yO.png I've been struggling for hours trying to make the vertical items span the entire height of the page. I have pored over documentation and other posts, but I ...

Finding the webpage URL where a form element is located

Suppose I have two pages called a.php and b.php, each with a form-tag. Both of these forms have the same action attribute set to c.php. Is there a way to determine in c.php which form (from either page a or b) triggered its submission? Using a hidden inpu ...

The bottom border of the check boxes is not displaying in Internet Explorer

https://i.stack.imgur.com/EAkMC.pnghttps://i.stack.imgur.com/ysU1R.png While the checkboxes function correctly in Chrome, they are experiencing issues in Internet Explorer. Below is the HTML code being used: <div class="patient-div-w" style="width: 9 ...

Tips for aligning two objects side by side in HTML

Apologies for the basic question, forgive my ignorance but I am struggling with keeping two objects in the same line within the same division. Please take a look at the code below: <html> <head> <style> .search-input { width: ...

I'm having trouble retrieving a list of elements from a Python class in order to showcase them in an HTML file

I've written a snippet of code in my views.py file where I defined a class called Pizza. from django.shortcuts import render from .models import Pizza def index(request): pizzas = Pizza.objects.all() return render(request, 'menu/index. ...

CSS styling for the dropdown list in the content/option section

Can someone help me with a simple question I've been researching for hours without success? I'm trying to figure out how to style the content/option/popup of a dropdownlist on the right side of the 'open button' like in this screenshot ...

Can you determine the height of an image if only the width and border width are provided?

Currently delving into the world of HTML and CSS, I've successfully configured the img element's width and border width using a style element. However, one aspect that is still puzzling me is how the height is calculated in this scenario. I&apos ...

Using jQuery to dynamically populate a list with the information from a JSON dataset

How can I utilize JSON data to ensure that jquery populates the correct ul in this code snippet, creating 5 ul and populating them with li elements? Expected output: The slides should be assigned to specific modules as indicated in the JSON data, instead ...

Switching the background hue of the chat box after each message

Does anyone know how to change the colors of each message in a chat window using CSS and HTML? I'm trying to customize my stream but can't figure it out. Here is an example for reference. ...

How can I position two divs side by side within an Appbar?

I would like the entire Container to be in a single row, with the Typography centered as it already is, and the toggle-container to float to the right <AppBar className={styles.AppBar}> <Toolbar> <Container> ...

Lines that are next to each other within an svg path and have a stroke

I'm working with an SVG that contains multiple lines within a path element. <path stroke-width="13" stroke="black" fill="orange" d="M 100 100 L 300 100 Z L200 300 z"/> Due to the stroke-width, the lines a ...

What is the best way to iteratively fade in data from a MySQL database one by one?

How can I load data from a MySQL database and display it one by one with a fade-in effect? Let's say I have a total of 40 images stored in the database. First, I want to display each image like this: <li class="img" style=" list-style: none; flo ...

"Internet Explorer: Unleashing the Magic of Card Fl

I'm encountering a problem that I can't seem to solve. Everything works perfectly in Chrome, FF, Safari, etc., but in Internet Explorer, I see the image on the front side instead of the text on the backside. Can anyone please assist me with this ...

Discover additional and subtract options

I am trying to implement a "See more" and "See less" button functionality for my list items inside an unordered list using the code below, but it is not working as intended. $(document).ready(function() { var list = $(".partners__ul li"); var numTo ...

Ensuring HTML5 validation in Ionic

Currently, I am delving into the world of Ionic but encountering an issue with HTML5 validation not functioning as expected within the framework. Below is a snippet of my login form: <h3> Login </h3> <form> <input name="email ...

How can you use ES6 pure Javascript to set the src attribute of an html collection of images from an array of paths as values for each image?

Currently, I have an array filled with different src values like [path1, path2, path3], and a corresponding HTML collection which has the same number of elements as the array. I am looking for a way to assign each value from the array to set the src attrib ...

What is the best way to deactivate div elements once an overlay has been applied to them?

My goal is to place an overlay on my form to prevent users from accessing the content. Even though I have added an overlay, users can still interact with input fields. How can I prevent that? .overlay { background: rgba(0, 0, 0, .75); text-align: ce ...

The tabs in Bootstrap 5.0 seem to be malfunctioning as they are displaying different tab information when clicked on

Trying to integrate Bootstrap5.0 tabs in the code below, but encountering issues. The tabs are visible, but upon clicking them, incorrect tab information is displayed. For instance, clicking on the profile tab should only display "clicked profile", but ins ...