Aligning items in several columns

Setting up a grid layout where the content is centered can be tricky, but I've found a solution that works well. Take a look at the code snippet below:

.outer {
    width: 100%;
    height: 100px;
    margin-top: 10px;
    margin-bottom: 10px;
    position: relative;
    background: pink;
    text-align: center;
}

.inner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
}
<div class="outer">
<div class="inner">
<h1>Content Here</h1>
</div>
</div>

While using text-align: center; does help center horizontally, achieving vertical centering can be challenging. Especially when you have multiple columns next to each other with centered content like in this example:

.outer {
    width: 50%;
    float: left;
    position: relative;
    background: pink;
}

@media only screen and (max-width: 500px) {
.outer {
    width: 100%;
    float: left;
    position: relative;
    background: pink;
}
}

.inner {
    position: relative;
}

.inner-position {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
}
<div class="outer">
    <div class="inner">
        <div class="inner-position">
            <p>Centered Content</p>
        </div>
    </div>
</div>

To achieve the desired layout as shown in the image linked [here](https://i.stack.imgur.com/B2Yoc.png), proper alignment of columns and centered content is essential. Check out the revised CSS code below for better alignment:

.container {
    width: 100%;
    height: 500px;
    background: pink;
    margin-top: 10px;
    margin-bottom: 10px;
}

.col {
    width: 50%;
    float: left;
    position: relative;
}

@media only screen and (max-width: 500px) {
.col {
    width: 100%;
    float: left;
    position: relative;
}
}

.inner {
    position: relative;
}

.inner-details {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
}
<div class="container">
    <div class="col">
        <div class="inner">
            <div class="inner-details">
                <h1>Middle 1</h1>
            </div>
        </div>
    </div>

    <div class="col">
        <div class="inner">
            <div class="inner-details">
                <h1>Middle 2<h1>
            </div>
        </div>
    </div>
</div>

Answer №1

To center items using the display: flex property on the container div, you can also utilize:

align-items: center; // for vertical alignment
justify-content: center; // for horizontal alignment

To achieve the image provided, there is no need for multiple containers. You can simplify it with the following example:

.container {
  width: 100%;
  height: 300px;
  margin-top: 10px;
  margin-bottom: 10px;
  display: flex;
  flex-wrap: wrap;
}

@media only screen and (max-width: 500px) {
  .inner-details {
    width: 50%;
    float: left;
    position: relative;
  }
}

.inner-details {
  background: pink;
  display: flex;
  flex-grow: 1;
  justify-content: center;
  align-items: center;
  margin: 10px;
}
<div class="container">
  <div class="inner-details">
    <h1>Middle 1</h1>
  </div>
  <div class="inner-details">
    <h1>Middle 2</h1>
  </div>
</div>

Answer №2

Here is the desired output you were looking for. Take a look at the following code snippets.

* {
  box-sizing: border-box;
}
.outer {
  width: 50%;
  height: 100px;
  float: left;
  position: relative;
  background: pink;
  margin: 10px 0; 
}

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

.inner {
  position: relative;
  width: 100%;
  height: 100%;
}

.inner-position {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<div class="outer">
  <div class="inner">
    <div class="inner-position">
      <p>I should be centered</p>
    </div>
  </div>
</div>

<div class="outer">
  <div class="inner">
    <div class="inner-position">
      <p>I should be centered</p>
    </div>
  </div>
</div>

Answer №3

After experimenting with the example provided in the initial snippet and double nesting it, I was able to achieve the desired result. However, there is still an issue with having to use text-align for horizontal alignment, as this is the closest solution I could come up with without utilizing flex or box-sizing: border-box;. If there is a more suitable approach to accomplish this, I would appreciate an example.

.wrap {
width: 100%;
    display: table;
}

.col {
width: 50%;
    float: left;
}

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

.outer {a
    width: 100%;
    height: 100px;
    margin-top: 10px;
    margin-bottom: 10px;
    position: relative;
    background: pink;
    text-align: center;
}

.inner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
}
<div class="wrap">
   <div class="col">
      <div class="outer">
         <div class="inner">
            <h1>I'm Centered</h1>
         </div>
      </div>
   </div>
   <div class="col">
      <div class="outer">
         <div class="inner">
            <h1>I'm Centered Too</h1>
         </div>
      </div>
   </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

generate a different identifier for ajax requests avoiding the use of JSON

The AJAX request functions in the following way: success: function(data) { $('#totalvotes1').text(data); } However, I need it to work with unique IDs as follows: success: function(data) { $('#total_' + $(this).attr("id")). t ...

What is the best way to adjust the size of the circles in my d3 leaflet implementation based on the frequency of longitude/latitude pairs?

I'm currently diving into the world of d3 and attempting to create a map using leaflet. Within my dataset, I have thousands of latitude and longitude coordinates. While I've successfully plotted circles on a leaflet map in d3, I'm now faced ...

Obtain pictures from MongoDB for a website using Angular6

I'm currently in the process of developing my website and I want to showcase an image from my database on the header. Each customer is assigned a unique logo based on their ID, but I'm unsure how to use Angular to display it. Below is my code s ...

Is there a way to adjust the transparency of the reflection while utilizing the -webkit-box-reflect property?

Exploring the capabilities of the -webkit-box-reflect property in Chrome has proven to be quite interesting. By utilizing the code snippet provided below (taken directly from the Webkit blog), I have been able to achieve a fading reflection effect: -webki ...

I am looking to reposition the navbar span tag so that it appears beneath the icon, utilizing both HTML and CSS

I'm currently working on a navbar design with 5 items, each containing an icon and a span tag. However, I'm facing difficulties in aligning the span tag below the icon for all 5 items in the navbar. Despite my best efforts, I have been unable to ...

Position an element in CSS relative to two other elements

Can an element be positioned horizontally in relation to one element and vertically to another using only CSS? I am attempting to include a dropdown menu in my navbar. However, the submenu is not aligning correctly. I want to position element A so that its ...

Make sure to trigger a callback function once the radio button or checkbox is selected using jQuery, JavaScript, or Angular

I'm looking to receive a callback once the click event has finished in the original function. <input type="radio" onchange="changefun()" /> function changefun() { // some code will be done here } on another page $(document).on('input: ...

CSS Media Query Assistance - optimizing text display for mobile responsiveness

I am currently facing an issue with my CSS code where the content is displaying as one long sentence on mobile devices, requiring users to scroll to read it. I am trying to modify it so that it displays as two lines on mobile. <!-- promo banner --> ...

If the height of the window changes, then update the CSS height accordingly

Here is the code that I am using: $(document).ready(function() { $('body').css('overflow', 'scroll'); var heightWithScrollBars = $(window).height(); $('body').css('overflow', 'auto') ...

Aligning images and input fields vertically with relative measurements

I'm looking for a way to vertically position two images, one on the left and one on the right, so that they are centered between labels and input fields within a containing div. Is it achievable using only relative lengths? Check out this jsfiddle fo ...

Javascript error specific to Internet Explorer. Can't retrieve the value of the property 'childNodes'

After removing the header information from the XML file, the issue was resolved. Internet Explorer did not handle it well but Firefox and Chrome worked fine. An error occurred when trying to sort nodes in IE: SCRIPT5007: Unable to get value of the proper ...

Use ng-class in a P tag to assess a variety of expressions

Is there a way to apply ng-class to automatically evaluate negative values within a < p > tag? <p <strong>LW$:</strong> {{d.lw_metric}} <strong>LW:</strong> {{d.lw_metric_percentage}} <strong>L4W:</strong> {{ ...

How to apply background-color to a div element with ng-repeat directive?

Hey there, I'm dealing with the following code: angular.module("myApp", []).controller("myController", function($scope) { $scope.boxList = [{ color: 'red' }, { color: '#F8F8F8' }, { color: 'rgb(50, 77, 32) ...

What could be the reason for the email not being displayed in the form?

I am attempting to automatically populate the user's email in a form when a button is clicked, preferably when the page is loaded. However, I am encountering issues with this process. This project is being developed using Google Apps Script. Code.gs ...

Transform SVG with a Click

Can someone assist me with rotating the pink area on click and moving it to a new angle from its current position? I am looking for a way to save the current position of the pink area and then, when clicked, move it smoothly to a new position. Your help ...

What is the best way to adjust the size of a button using CSS within a ReactJS

I am facing an issue where I need to create a button with a specific width, but the template I'm using already has predefined styles for buttons. When I try to customize the button's style, nothing seems to change. Below is the code snippet: Her ...

Transform an array of strings into an array of floating point numbers using AngularJS

Hey everyone! I'm having trouble converting an array of strings into floats. When using map(parseFloat), I'm only getting integer numbers. Could the issue be with the commas in 40,78 causing parseFloat to interpret it as 40 instead of 40.78? Her ...

Unable to generate webpage source code

Having some trouble with my Next.js project as I'm using getStaticProps and getStaticPath to build a page, but unfortunately, the HTML is not being generated in the page source. This issue is causing problems with SEO, and I haven't implemented r ...

I can't seem to figure out why this file upload error is happening. What could be the

Having trouble with a simple file upload function and keep getting an "Error" message when trying to use it. Here's my HTML file: <form enctype='multipart/form-data' action='upload.php'> <input type='file' name ...

Guide to positioning an icon at the center of a material card

I am new to CSS and Angular Material, and I need help centering the icon on the card following the design from Figma. I tried copying the CSS from Figma, but it didn't center the icon as expected. Can someone please share techniques to achieve this? S ...