Paragraphs that are not displaying on their own lines are typically viewed as a j

I'm in the process of designing a stats section for a website. The layout should have two columns on desktop and stack vertically on mobile devices. Ideally, I'd like the innermost DIV content to be stacked as well, but I'm struggling to achieve this.

The desktop version should resemble something like this: https://i.sstatic.net/Gbg2S.png

While the mobile version should look more like this: https://i.sstatic.net/CubGc.png

Here is my current code:

.site-stats-wrap {
    width: 100%;
    margin-left: auto;
    margin-right: auto;
    background: pink;
    margin-top: 10px;
    margin-bottom: 10px;
    
}
@media only screen and (max-width: 500px) {
.site-stats-wrap {
    width: 100%;
    margin-left: auto;
    margin-right: auto;
    background: pink;
    margin-top: 15px;
    margin-bottom: 15px;
}
}

/* More CSS styling properties... */

<div class="site-stats-wrap">
   <div class="site-stats-group">
      <!-- HTML structure -->
   </div>
</div>

The two main issues I'm currently encountering are: A) The content within each column is not centered properly especially on mobile where the height varies based on content. B) Text inside the stats section does not appear on its own line.

I would appreciate any solutions that can help center the contents and address the text alignment issue.

UPDATE Credits to Andrew Halpern for providing an answer.

.wrap {
  /* New CSS styles */
}

.col1 {
  width: 50%;
}

@media only screen and (max-width: 500px) {
.col1 {
  width: 100%;
}
}

.col2 {
  width: 50%;
}

@media only screen and (max-width: 500px) {
.col2 {
  width: 100%;
}
}
<div class="wrap">
  <div class="col1">
    <h1>I'm Centered</h1>
  </div>
  <div class="col2">
    <h1>I'm Centered Too</h1>
    <h1>I'm Centered Too</h1>
    <h1>I'm Centered Too</h1>
  </div>
</div>

Answer №1

If you're looking to achieve the desired behavior, here is the code revamped using Flexbox:

.wrap {
  width: 100%;
  display: flex;
  background: pink;
  margin-top: 10px;
  margin-bottom: 10px;
  align-items: center;
  justify-content: center;
  flex-flow: row wrap;
}

.col1,
.col2 {
  padding: 50px;
}
<div class="wrap">
  <div class="col1">
    <h1>I'm Centered</h1>
  </div>
  <div class="col2">
    <h1>I'm Centered Too</h1>
    <h1>I'm Centered Too</h1>
    <h1>I'm Centered Too</h1>
  </div>
</div>

Answer №2

Instead of applying a normal size text-center, I only applied it to mobile sizes. It seems like this is what you were looking for.

 .site-stats-group {
      background: pink;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      font-size: 2rem;
      flex-flow: row wrap;
    }

    @media screen and (max-width: 500px) {
        .site-stats-group {
          flex-direction: column;
        }
        .site-stat-info {
          text-align: center;
        }
    }
<div class="site-stats-wrap">
    <div class="site-stats-group">
      <div class="site-stat-title">
        <div class="site-stat-title-display">
          <p class="site-stats-title">Statistics</p>
        </div>
      </div>
      <div class="site-stat-info">
        <div class="site-stat-info-display">
          <p class="site-stat-title">Title</p>
          <p class="site-stat-subtitle">A Short Description</p>
        </div>
        <div class="site-stat-info-display">
          <p class="site-stat-title">Title</p>
          <p class="site-stat-subtitle">A Short Description</p>
        </div>
      </div>
    </div>
  </div>

Answer №3

Here is the CSS code snippet for creating a responsive grid layout:

.container {
                width: 50%;
                margin: 0 auto;
                display: grid;
                grid-template-columns: repeat(2, 1fr);
            }

            .left-side,
            .right-side {
                padding: 2rem;
                display: flex;
                justify-content: center;
                align-items: center;
            }

            .left-side {
                background-color: crimson;
            }

            .right-side {
                background-color: cornflowerblue;
            }

            @media screen and (max-width: 768px) {
                .container {
                    width: 100%;
                    grid-template-columns: 1fr;
                }
            }

And here is the corresponding HTML markup to go along with the CSS code:

<div class="container">
        <div class="left-side">
            <h1>Centered.</h1>
        </div>
        <div class="right-side">
            <div class="items">
                <h1>Centered.</h1>
                <h1>Centered.</h1>
                <h1>Centered.</h1>
            </div>
        </div>
    </div>

You can see the visual representation of this layout using the following images https://i.sstatic.net/RmFGK.png https://i.sstatic.net/H50Ug.png

Answer №4

To achieve a responsive layout, utilize flexbox and adjust the flex-direction property to column within the media query specified for smaller screens:

html,
body {
  height: 100%;
  margin: 0;
}

.wrap {
  width: 100%;
  height: 100%;
  display: flex;
  background: pink;
  align-items: center;
  align-content: center;
  justify-content: center;
  text-align: center;
}

.col1,
.col2 {
  box-sizing: border-box;
  width: 50%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

@media only screen and (max-width: 500px) {
  .wrap {
    flex-direction: column;
  }
  .col1,
  .col2 {
    width: 100%;
    height: 50%;
  }
}
<div class="wrap">
  <div class="col1">
    <h1>I'm Centered</h1>
  </div>
  <div class="col2">
    <h1>I'm Centered Too</h1>
    <h1>I'm Centered Too</h1>
    <h1>I'm Centered Too</h1>
  </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

Is it usual for the container to overflow with div content?

Is this common CSS behavior? How can we make the container wrap around the button? http://jsfiddle.net/afrontrow/yepw7oLw/ CSS: .button { background: grey; padding: 10px 20px; } .container { border: 1px solid black; } HTML: <div class= ...

The image inside an Ionic card's header will automatically adjust its size when text is added

Two cards are currently being displayed on the page: <ion-header> <ion-navbar> <ion-title>Home</ion-title> </ion-navbar> </ion-header> <ion-content padding> <ion-card class="offersCard"> ...

Issue with Jquery functionality on IE9 Release Candidate

Recently, I made the switch to IE9 RC (which, in my opinion, is not too shabby for a Microsoft product so far - though there's still time for them to mess it up! Please refrain from starting a browser war in the comments section by sharing your though ...

Using d-flex instead of col in Bootstrap 4

With the new Bootstrap 4 transitioning from using 'floating' elements to the improved 'flexbox' method, I can't help but wonder if it's acceptable to build the entire grid structure using .d-flex instead of the conventional .c ...

Bootstrap select menu validation functions correctly on Firefox, however encounters issues on Chrome

I'm currently tackling a project involving HTML forms and utilizing Bootstrap for form validation. However, I've encountered an issue. After adding a select menu to the form and testing the form validation, I noticed that the other input fields d ...

Is it common for the `col` class in Bootstrap 4 Grid System to have no padding?

This is my first time using Bootstrap version 4. Currently, I have a footer that utilizes the new flex col classes. It looks great on desktop, but on mobile, the columns are stacked so closely together that there is no vertical margin or padding. Is this ...

Angular 9 introduces a fresh approach to compiling alternate components

I am trying to append HTML to a div and have it compile the variables, loops, and all the Angular component elements within the string. The HTML content is being uploaded via a text file. While I am aware that this can be achieved using $compile in Angular ...

Adjust the Bootstrap design from an inline format to a grid layout specifically for mobile devices

I have a complex web form with multiple fields in a single line. The Bootstrap inline form works well on larger screens, but now I need to make the page responsive so that the inline form will switch to a 3-column grid layout on smaller screens like tablet ...

"Implementing a real-time countdown using jQuery on a dynamically generated

I require some assistance in implementing the jquery countdown plugin by Keith Wood. My task involves creating multiple countdowns by fetching data from a MySQL database using a PHP AJAX call, and then inserting it into a div: In my PHP file (getdetails. ...

Discovering instances of a specific string within a larger string

My goal is to customize the default behavior of the alert function. Below is the code I am using: window.alert=function(txt) { waitOk='wait'; setMsgBox(txt); btnMsgOk.focus(); } However, I need this functionality to vary ba ...

Is there a way to make document flow disregard the scale attribute?

I am looking to add a unique fly-in animation using CSS, where a specific block will scale from 10 to 1. However, when I implement the scaling, a horizontal scroll bar appears: https://jsfiddle.net/r2a5w7fo/15/ body { font: 10svw sans-serif; text-ali ...

Troubleshooting issues with jQuery background-position animation and CSS sprites functionality

Trying to add animation to my navigation links background using a Css sprite. Check out the picture here: $(document).ready(function(){ $('nav a') // Move the background on hover .mouseover(function(){ $(this).stop().animate({ba ...

Ways to merge duplicate entries in an html table?

As a junior developer working on my first project, I am currently struggling with creating a stats page for my application. My main issue is how to display the data in a user-friendly manner. After looking at similar applications, I noticed that they used ...

Is it possible to generate a table without any grid lines present?

Currently, I am in the process of designing a table that will function as a popup when a link is clicked within my imported SQL table. An example of this table is included with this message for reference. After conducting thorough research, I attempted to ...

Using jQuery cookies to create a CSS style switcher

I've been experimenting with creating a jQuery stylesheet switcher and have had some success. However, I'm running into an issue when trying to save the changes in a cookie using the jquery.cookie plugin. Here's the snippet of jQuery code t ...

What is the best way to extract numbers from a string using JavaScript?

I am working with a string in javascript that looks like this: var xyz= "M429,100L504.5,100L504.5,106L580,106L570,98M580,106L570,114"; My goal is to extract the numbers and store them in an array. I attempted the following code: var x=xyz.match(/\ ...

ng-class not updating using a combination of object and string

I am just starting to learn angular js and I recently came across the ng-class directive. Below is an example of how I have used it: ng-class="[{'highlighter-row-Class' : (file.id == 1 && file.processed), 'bold-row-Class' : ...

Using JQuery to ensure the validity of each input inside a form

I am looking to implement the same jQuery validate rule across all input fields with the name "time" within a table that is part of a form. Here is the HTML code: <form id="thresholds"> <table id="table" width="100%"> <thead> ...

Make sure your HTML content is optimized to fit on one page when printed

I'm currently using Swift along with a WKWebView to load HTML, establish the A4 page dimensions, and then save it as a PDF. (Though, I believe this is more of an HTML/CSS-focused question). The generation process looks like this: func generatePDF ...

Discover the complete compilation of CSS class regulations (derived from various stylesheets) along with their currently active properties for the chosen element

Is there a way to retrieve all the matched CSS Selectors for a selected element and access the properties of each active class applied to that element? I have researched methods like using getMatchedCSSRules and checking out However, I am hesitant to use ...