Align the h2 header vertically within a div container

So, here's the HTML structure that I'm working with:

<div class="category">
    <div class="container bottom">
        <h2>Name1</h2>
    </div>
    <div class="container top">
        <h2>Name2</h2>
    </div>
    <div class="container ">
        <h2>Name3</h2>
    </div>
    <div class="container">
        <h2>Name4</h2>
    </div>
    <div class="container">
        <h2>Name5</h2>
    </div>
    <div class="container">
        <h2>Name6</h2>
    </div>
    <div class="container">
        <h2>Name7</h2>
    </div>
</div>

This is the CSS being used:

* {
    margin: 0;
    padding: 0;
}

.category {
    text-align: center;
    width: 95%;
    margin: 0 auto;
}

.container {
    display: inline-block;
    width: 33%;
    height: 4em;
}

h2 {
    display: inline-block;
    width: 100%;
}

.left > * {
    text-align: left;
}

.right > *{
    text-align: right;
}

.top > * {
    vertical-align: top;
}

.bottom > * {
    vertical-align: bottom;
}

The objective is to achieve a layout similar to this: Example

In the scenario presented in the image, all the .container (gray boxes) are of equal size as defined in the CSS.

The issue I'm facing is that the vertical-align property doesn't seem to be functioning correctly. I've explored solutions like the one provided in this CodePen example, which works without using display: table-cell or other table-related techniques found on Stack Overflow. Why isn't it working with my specific HTML and CSS setup? Currently, all the <h2> elements align in the middle instead of following the defined alignment :\

Answer №1

In this particular Codepen demonstration, the alignment of the h2 element within the box is not solely responsible for vertical alignment. Rather, it is about how elements are aligned next to each other, rather than within the container itself. This can be a bit confusing at first glance. If there are two inline elements present, you will observe them aligning vertically with one another. However, if a table cell is utilized, this behavior changes.

To further illustrate this point, try removing the images from the Codepen example and setting a specific height on the div. You'll notice that the vertical alignment ceases to exist.

I suggest utilizing display:table and display:table-cell for alignment purposes. While the use of position:absolute may seem like a viable option, issues may arise if the text expands beyond the bounds of the box, as the text no longer adheres to the document flow. By employing table properties, you retain some level of flexibility in your layout design.

Answer №2

<h2> is aligned the way it is because of its default margin. To adjust the alignment of h2, you can try using translateY.

.category {
text-align: center;
width: 95%;
margin: 0 auto;
}

.container {
border: 1px solid red;
display: inline-block;
width: 32%;
height: 4em;
}

.left {
text-align: left;
}

.right {
text-align: right;
}

.top h2{
  transform: translateY(0);
}

.bottom h2 {
  transform: translateY(100%);
}
h2{
margin: 0;
display: inline-block;
transform: translateY(50%);
}
<div class="category">
<div class="container bottom">
    <h2>Name1</h2>
</div>
<div class="container top">
    <h2>Name2</h2>
</div>
<div class="container left">
    <h2>Name3</h2>
</div>
<div class="container bottom">
    <h2>Name4</h2>
</div>
<div class="container right">
    <h2>Name5</h2>
</div>
<div class="container bottom right">
    <h2>Name6</h2>
</div>
<div class="container">
    <h2>Name7</h2>
</div>
</div>

Answer №3

To achieve this layout, consider utilizing the absolute positioning technique.

.container {
  position: relative;
}

.container h2.middle {
  position: absolute;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}

You can customize the styling of different classes by adjusting the .middle class after h2 and setting values for left right top bottom accordingly. Here is an example:

.container h2.top {
  position: absolute;
  top: 0;
}

.container h2.bottom {
  position: absolute;
  bottom: 0;
}

.container h2.right {
  position: absolute;
  right: 0;
}

.container h2.left {
  position: absolute;
  left: 0;
}

Finally, implement these changes in your HTML code:

<div class="container">
    <h2 class="top">Title1</h2>
</div>
<div class="container">
    <h2 class="bottom">Title2</h2>
</div>
<div class="container ">
    <h2 class="left">Title3</h2>
</div>
<div class="container">
    <h2 class="right">Title4</h2>
</div>

Answer №4

Initially, the height of the div appears to be quite small and the h2 has a default margin. The most effective method I suggest for achieving a vertical align bottom is as follows:

.container{ 
   position: relative;
}
.bottom { 
    position: absolute; 
    bottom: 0px;
 } 
<div class="container">
   <h2 class="bottom">Name1</h2>
</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

Use jQuery to retrieve the response from a JSON request and showcase it on the existing HTML page

Currently, I am working on a project that involves integrating a JSON-based web service from a remote server. The method of this service can be accessed by using specially formatted URLs such as: http://root-url/ws/service-name?request={json-string} The ...

What is the best way to add randomness to the background colors of mapped elements?

I am looking for a way to randomly change the background color of each element However, when I try to implement it in the code below, the background color ends up being transparent: { modules.map((module, index) => ( <div className='carou ...

Show random images of different sizes by employing jquery

I am seeking a solution for my webpage which currently loads a random image from an array and centers it using negative margins in CSS. While this method works fine with images of the same size, I am looking to modify the code so that it can handle images ...

Struggling to synchronize animation timing between elements using jquery

When you navigate to an album (for example, Nature because I'm still working on the others) and select one of the images, they all gradually fade out while the selected image appears on the screen. What's actually happening is that the selected i ...

The vertical lines separating the buttons are not evenly centered

I need help to center a vertical line between my buttons in the middle of the header. Currently, the line goes up and my disconnect button is not aligned properly. Can you assist me in creating a responsive design? Thank you. HTML <div class='tab ...

Creating a customized icon scheduling feature with CSS

Creating an icon using CSS that looks like the one below seems to be quite challenging. I attempted to achieve it with this approach, but the result was not satisfactory. https://i.stack.imgur.com/5W7Hj.png .schedule { height: 10px; width ...

Arranging HTML components in Vue.js using v-for loop

Recently, I started using vue.js and I have a requirement where I need to display an HTML structure based on API data. The structure should look like this: <div class="row"> <div><p>text1</p><img src="{{imgurl1}}" /></di ...

Is there a way to connect a CSS external file that is saved on Dropbox?

Is it possible to link an external CSS file stored in Dropbox with HTML? I have followed all the instructions I could find online, including clicking and pressing "Copy Dropbox Link" to generate an href link. However, it doesn't seem to be working. ...

Changing font styles using the jMenu jQuery plugin: A step-by-step guide

I'm having trouble changing the font-family using CSS with jQuery's "jMenu" plugin. Here is the CSS I currently have: .jMenu{ position : absolute; top : 80px; left : 0px; width : 100%; display:table; margin:0; padding ...

Tips for Resizing and Reviving Divs with jQuery

I have recently ventured into the world of web development to assist a family member with their website. My knowledge and experience are limited, but I am facing an interesting challenge. I am trying to manipulate certain divs as users scroll down the page ...

Is it possible to implement the SCSS CSS preprocessor in Angular 1.6.6, even though it is already utilizing LESS as the current CSS preprocessor?

Is it necessary to compile SCSS and Stylus files into CSS before importing them into my Angular version 1 app? Are there any other alternatives available? Context: I have two applications, one using Angular 4 with SCSS and the other using Angular 1 with L ...

Why am I unable to attach this to JQuery?

$("input").keydown(function(event){ if(event.keyCode === 13){ return false; } }); I need to prevent the default action when the "enter" key is pressed in any of my text input fie ...

Include a <div> element to display the date in an HTML format, ensuring that the days and months

I am working on a project where I have a list of dates and I need to format them by adding div tags to separate the days, months, and years into different divisions. <div class="campaign">30/11/2016 - <a href="#" target="_blank">Dummy Text& ...

The chosen options are not appearing. Could this be a problem related to AngularJS?

I am working on setting up a dropdown menu in HTML that includes only the AngularJS tag. This dropdown menu will be used to populate another AngularJS dropdown menu. Below is the code for the first dropdown menu: <select class="form-control" ng-model=" ...

Disabling hover effects in Chart.js: A step-by-step guide

Is there a way to disable hover effects, hover options, and hover links on a chart using chart.js? Here is the code I have for setting up a pie chart: HTML.. <div id="canvas-holder" style="width:90%;"> <canvas id="chart-area" /> </div ...

Customizing the width of the JQuery $ui.progressbar by overriding its properties

After spending some time, I successfully overrode the function that controls the width of the progress bar element in the jQuery UI widget version 1.12.1 from September 14, 2016. Initially, I needed the progress bar to completely fill a column in a table. ...

How do I view a wildcard in my ID selector using jQuery?

When I want to initially hide various content scattered around the page, I usually use the following jQuery code: $('#objective_details, #time_estimate_details, #team_members_details, #resources_details').hide(); Is there a method to utilize a ...

What is causing the alt or title tags to not function properly in emails?

I am currently facing an issue with e-mail templates that include images. The problem arises from the fact that these images need to be downloaded before they can be displayed in the email. To work around this, I always use ALT and TITLE tags to provide de ...

What is the best way to create a footer in this scenario and is there a method to perfectly center an

What is the best way to position the footer at the bottom of the page without overlapping the top content? Is there a method to center both the height and width of the header element on the page? Can you review the layout of this webpage and provide feed ...

Variations in CSS Stylesheets for Various Parts of the Body

Currently, I am in the process of creating a single page website. One issue that I have encountered is needing a Bootstrap "modal" class popup to appear when a button is clicked. However, the template I am utilizing was designed using Bootstrap Version 1, ...