Responsively center text vertically

I am facing a challenge with two divs that are floating next to each other: one contains a large headline text, and the other has a photo with a caption. My objective is to ensure that the headline text is vertically centered regardless of the height of the div.

Despite trying various methods to achieve this, I have found it quite difficult. You can refer to my code in the following codepen:

http://codepen.io/ckatz/pen/KaBNxm

HTML:

<div class="container_16">
  <div class="grid_8 headline">
    <span class="gold"><strong>We have a special way of helping</strong>/span><br> each individual find their success.</p>
  </div>
  <div class="grid_8">
    <div>
      <div class="wp-caption alignnone" style="width: 100%">
        <img src="http://placehold.it/500x500" alt="" width="100%" height="auto">
        <p class="wp-caption-text">photo caption</p>
      </div>
    </div>
  </div>

CSS:

.container_16 {
  width: 90%;
}

.container_16 .grid_8 {
  width: 47%;
  display: inline;
  float: left;
  position: relative;
  margin-left: 10px;
  margin-right: 10px;
}

p.wp-caption-text {
  text-align: center;
  font-weight: bold;
  font-style: normal;
  margin: 1em;
  padding: 0 0 1em 0;
}

Answer №1

This approach involves setting the container's height based on the image and then vertically aligning the text.

.wrapper {
  position: relative;
}
.content {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

Answer №2

Utilize flex alignment and the align-self property within the child element. Implement these CSS rules:

.box_27 {
    display: flex;
    width: 90%;
}
.subtitle {
    align-self: center;
}

Answer №3

If you want to ensure your text is vertically aligned, here's a CSS solution:

.silver {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

By using this code, your text will remain centered no matter the height of the containing div.

Answer №4

Instead of using the float property, consider using display with options like (inline-block,table,table-cell, flex) along with vertical-align or margin:

  • inline-block + vertical-align

@import url('https://fonts.googleapis.com/css?family=Montserrat');

body {
  font-family: 'Montserrat', Arial, Helvetica, sans-serif;
  font-weight: normal;
}

p {
  font-size: 1em;
  line-height: 1.4em;
}

.container_16 {
  width: 90%;
}

.container_16 .grid_8 {
  width: 47%;
  display: inline-block;
  vertical-align:middle;
  position: relative;
}

.headline {
  font-size: 3em;
}

.gold {
  color: #ffb500;
}

p.wp-caption-text {
  text-align: center;
  font-weight: bold;
  font-style: normal;
  margin: 1em;
  padding: 0 0 1em 0;
}
<div class="container_16">
  <div class="grid_8 headline>
    <p>
      <span class="gold"><strong>We have a special way of helping</strong></span>
      <br> each individual find their success.
    </p>
  </div>
  <div class="grid_8">
    <div>
      <div class="wp-caption alignnone" style="width: 100%px">
        <img src="http://placehold.it/500x500" alt="" width="100%" height="auto">
        <p class="wp-caption-text">This is a caption for the photo here.</p>
      </div>
    </div>
  </div>

  • display:table-cell and vertical-align

@import url('https://fonts.googleapis.com/css?family=Montserrat');
 body {
  font-family: 'Montserrat', Arial, Helvetica, sans-serif;
  font-weight: normal;
}
p {
  font-size: 1em;
  line-height: 1.4em;
}
.container_16 {
  width: 90%;
  display:table;
  table-layout:fixed;
}
.container_16 .grid_8 {
  width: 47%;
  display: table-cell;
  vertical-align: middle;
  position: relative;
  padding-left: 10px;
  padding-right: 10px;
}
.headline {
  font-size: 3em;
}
.gold {
  color: #ffb500;
}
p.wp-caption-text {
  text-align: center;
  font-weight: bold;
  font-style: normal;
  margin: 1em;
  padding: 0 0 1em 0;
}
<div class="container_16">
  <div class="grid_8 headline">
    <p>
      <span class="gold"><strong>We have a special way of helping</strong></span>
      <br>each individual find their success.
    </p>
  </div>
  <div class="grid_8">
    <div>
      <div class="wp-caption alignnone" style="width: 100%px">
        <img src="http://placehold.it/500x500" alt="" width="100%" height="auto"><p class="wp-caption-text">This is a caption for the photo here.</p>
      </div>
    </div>
  </div>

  • display:flex and margin:auto 0;

@import url('https://fonts.googleapis.com/css?family=Montserrat');

body {
  font-family: 'Montserrat', Arial, Helvetica, sans-serif;
  font-weight: normal;
}

p {
  font-size: 1em;
  line-height: 1.4em;
}

.container_16 {
  width: 90%;
  display:flex;
}

.container_16 .grid_8 {
  width: 47%;
  margin:auto 0;
  position: relative;
  margin-left: 10px;
  margin-right: 10px;
}

.headline {
  font-size: 3em;
}

.gold {
  color: #ffb500;
}

p.wp-caption-text {
  text-align: center;
  font-weight: bold;
  font-style: normal;
  margin: 1em;
  padding: 0 0 1em 0;
}
<div class="container_16">
  <div class="grid_8 headline">
    <p>
      <span class="gold"><strong>We have a special way of helping</strong></span>
      <br> each individual find their success.
    </p>
  </div>
  <div class="grid_8">
    <div>
      <div class="wp-caption alignnone" style="width: 100%px">
        <img src="http://placehold.it/500x500" alt="" width="100%" height="auto">
        <p class="wp-caption-text">This is a caption for the photo here.</p>
      </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 could be causing my carousel to malfunction?

Can someone assist me, please? I am having an issue where the carousel displays on the page, but the images are not showing and the buttons do not work. Please refer to the CSS + HTML snippet below: #my-carousel .carousel-inner .item { width: 100%; ...

Convert traditional class-based styles to inline styles

Is there a tool available that can efficiently convert class-based styles to inline styles? I am designing an email and find it much easier and quicker to work with classes, but the final output requires inline styles. It seems like there should be softwar ...

The alarm feature is malfunctioning

I have been struggling with a simple alarm application that I created using jQuery and Javascript. The issue lies in the alarm functionality not working as expected. Below is the relevant code snippet: var today = new Date(); var h = today.getHours(); var ...

The HTML button triggers a function to execute on a different webpage when clicked

I'm facing a straightforward issue that I can't seem to figure out due to my limited experience with Angular and web development. The problem revolves around two components, namely home and dashboard. In the home.component.html file, there's ...

Unusual display of footer controlled by CSS in certain template pages

I recently made changes to the footer on a template-based website. I noticed that, for some unknown reason, the footer is not appearing at the bottom of the page as it should be. I wonder if the footer functionality is faulty altogether and if I only got ...

Optimizing CSS table styles for larger cell width

In my CSS file, I have defined the following style: .ES{ max-width:20px; word-wrap:break-word; } However, when I apply this to my table... <table border="1" cellpadding="2" class="ES" > <tr> <td><input name="rbeso" type="radio" ...

Previewing a PDF file with multiple headers displayed above the text content

Creating multiple headers for each page of a PDF document using dompdf can be tricky. The headers you generated are fixed at the top with a width of 100%. However, you are facing an issue where on the second and subsequent pages, the header overlaps with t ...

Changing the order of element names depending on their location within the parent element using jQuery

<div class="content"> <div> <input type="text" name="newname[name]0"/> <div> <input type="text" name="newname[utility]0"/> <div> <textarea name="newname[text]0 ...

In React, CSS @media queries are specifically designed to function only within the Device Mode of Developer Tools

As indicated by the title, I have designed a responsive web app using the React framework along with various @media queries. When I resize the page in Developer Tools' Device Mode, the queries work perfectly fine and everything functions as expected. ...

"Which is better for maximizing the efficiency of an image grid: CSS or Jquery? What are the key

As a UX Designer looking to enhance my coding skills, I must admit my code may not be perfect. Please bear with me as I navigate through this process. I am in the process of revamping my portfolio website. The original seamless grid was created using a Ma ...

Instructions for removing a class using the onclick event in JavaScript

Is there a way to make it so that pressing a button will display a specific class, and if another button is pressed, the previous class is removed and a new one is shown? Thank you for your assistance. function myFunction() { document.getElementById ...

What is the best way to format each <li> element onto a separate line?

Creating a review section for an item on my website required me to organize the reviews and comments. I utilized the following code structure: <ul> <li> Comment code </li> <li> Comment code </li> </ul> Within the ...

Looking for a specific search term within the middle of strings in an array

Is there a way to improve the autocomplete feature of my input field so that it shows suggestions based on any part of the word typed in? I currently have it set up to only show suggestions if the input matches the start of a word in the array. How can I m ...

Is there a way to emphasize a particular link in a navigation bar without causing changes to the other links?

I am trying to create a hover effect for my links where they raise up when hovered, but I don't want the other links in the navigation bar to be affected. However, whenever I hover over one link, the others move downward as well. Can anyone help me so ...

Eliminating extra space below the footer using CSS in Wordpress site

After recently updating my website, I am struggling with some of the finer points. It's worth noting that I lack experience in web development, despite working in the software field; I am trying to broaden my knowledge. In particular, I have noticed ...

What is the best way to obtain the final HTML output once all scripts have been executed

Is there a way to obtain the final HTML after all scripts have been executed? The scripts on the page are adding and changing controls and CSS, and I want to see the HTML of the display as a static page. Is there a method to achieve this? Edit: For exa ...

Can a div be made to resize gradually?

Within a centered div, I have floating elements, each with uniform height and width. Whenever the div is resized, a gap appears on the right side where there isn't enough space to accommodate another element. My goal is for the div to expand only when ...

Adjusting the text color based on the dynamically set background color

If I have a calendar where each entry has a unique background color assigned to it (one color per user), including both light and dark colors that users can choose from, how can I set the text color to match the background style? Where should I begin wit ...

Adjusting the height of an element as you scroll will activate the scroll event

One issue I'm facing is with adding a class to my header. The goal is to reduce the height of the top part of the header when the user scrolls down and then remove the class when they scroll back up. While this functionality is working, there is an un ...

Center or left-align the divs

Can anyone offer assistance with this section of code? HTML: <div id="holder"> <div class="box"> </div> <div class="box"> </div> <div class="box"> </div> <div class="box"> </div> </div> ...