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

What is the best way to ensure my responsive form is centered and aligned properly with all other elements on the page

Link to website This final step is all that stands between me and the completion of my website deployment. However, I am faced with a persistent issue that has me stumped. It seems that there is an unexplained margin on the left side of the contact form, ...

Steps for Converting Unicode Characters to HTML Encoding in C++

Can you assist me with converting unicode characters like the ones below in C++? Thére Àre sôme spëcial charâcters ïn thìs têxt عربى I need to convert them to HTML encoding as shown below: Th&eacute;re &Agrave;re s&ocirc;me sp&am ...

I am having trouble getting the (:active) pseudo-class to function properly with the menu on my WordPress theme

Purchased a theme called HelpGuru and encountering issues with the CSS of the menu. Reached out to support but they were unable to assist, directing me to a company that specializes in customizing WordPress themes instead. The link to the demo can be found ...

Transform JSON data into an HTML layout

I'm looking to design a structure that showcases JSON information using HTML div elements. For example, utilizing the h4 tag for headers, p tag for text descriptions, and img tag for images. Can anyone provide guidance on the most efficient approach ...

Can a HTML div be created with an animation and a hover-scale effect added to it?

I experimented with using divs as clickable buttons that direct to another page on my website. I added animations to make them rotate into view when the site is loaded. Now, I'm trying to implement a hover effect that will scale the buttons when hover ...

Accessing a key from an AJAX JSON response and applying it within a .each() loop in jQuery

I'm struggling with a seemingly simple task that I just can't seem to get right. My goal is to convert multiple text inputs into select fields using AJAX and jQuery. Everything works smoothly, except when trying to make the $.each function dynam ...

What is the solution for resolving scrolling issues when flexbox content is aligned vertically in the center?

One of my goals is to ensure that when the viewport is taller than the content, the content is vertically centered. However, if the viewport is not tall enough and the content overflows, I want the parent element to provide a vertical scrollbar. How can I ...

Remembering position in JSP using Java Beans

In my "Web Engineering" assignment, I am tasked with developing a Web Application game using JSP, Servlets, and Java Beans. The game mechanics are already in place with the Servlet controlling the algorithms, JSP handling the Model/View, and Beans managing ...

Sending a collection of text inputs from a web form and saving them in MongoDB

I have been attempting to store an array of strings from my HTML form into my database (MongoDB). Here's the HTML form for creating a new class: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"& ...

Why have the bars been positioned on the left instead of the right, and why does the mobile version require sliding instead of simply accessing the menu through a function?

Hello everyone, I'm currently working on designing a mobile-friendly header menu with the bars positioned on the right side of the screen. The goal is to utilize JavaScript to allow users to click either an 'X' icon or the bars to open the m ...

Exploring Django's view field for efficient data rendering

Is it possible to display multiple views for a single page/template? For instance, imagine a website where users can submit movie reviews and read others' reviews as well. Now, I need to add an edit button to the review pages but only want the button ...

Resolved the z-index problem with the header and sidebar placement

Hello there! I have come across an issue that I need some help with. I have a fixed sidebar and header on my website, both of which have drop shadows applied to them. However, I don't want the header to cast a shadow on the sidebar as I want them to ...

Tips for aligning two divs vertically and horizontally next to each other

I'm struggling to center two divs both horizontally and vertically at the top of my page. I've tried various methods like "vertically align" & "margin: auto 0", but nothing seems to work. Can anyone help me figure out what I'm doing wron ...

Vue - unable to display component CSS classes in application when using class-style bindings

Just diving into Vue and starting with class-style binding syntax. I'm facing an issue where the CSS classes for header and footer that I've defined are not displaying, even though I've referenced them in the component tags. Can't seem ...

Issues with Vue.js v-for functionality causing inconsistencies

Just delving into the world of Vue.js and encountering a hitch. I've been following a tutorial on Laracasts, but my v-for directive seems to be causing some trouble. Here's the HTML: <div id="root"> <ul> <li v-for="name in ...

The Input element's onChange event is not functioning as expected

I am experiencing some issues with my code. I am trying to dynamically change the background color based on user input, but I am struggling to make it work. It seems like a simple task, so I must be missing something. Below is my HTML markup and jQuery: ...

Align the headers of columns to the right in the AgGrid widget

Is there a way to align the column headers to the right in AgGrid without having to implement a custom header component? It seems like a lot of work for something that should be simple. You can see an example here: https://stackblitz.com/edit/angular-ag-g ...

Tips on appending a parameter to an image URL using JavaScript

On my website, I am able to insert images with specified width and height using this code: <img src="image.php?w=100&h=100"> I would like the image size to change based on the device screen width. For screens smaller than 600px, I want to displa ...

Creating a dynamic website with user-friendly editing capabilities

Having a good understanding of html5 and css3, I am currently exploring ways to create a website that enables visitors to edit content in real time for all other users to see. While aware of the contenteditable attribute, I have found that it does not reta ...