Alignment in a vertical plane with Bootstrap 4

I'm attempting to center text over an image. I've utilized mx-auto for Horizontal centering my overlay text and the use of align-middle for Vertical alignment. However, the vertical alignment doesn't seem to be working as expected. Any ideas on what might be causing this issue?

  

  <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>

      <div class="card ">
<img class="img-fluid card-img" src="https://snap-photos.s3.amazonaws.com/img-thumbs/960w/1U2EGZ07GU.jpg" alt="Deer in nature">
<div class="card-img-overlay d-flex">
  <div class="mx-auto">
<h4 class="card-title align-middle">Animal Farm</h4>
<h6 class="text align-middle">George Orwell</h6>
<p class="card-text align-middle">Tired of their servitude to man, a group of farm animals revolt and establish their own society...</p>
</div>
</div>
</div>

Answer №1

Simply utilize my-auto to achieve vertical alignment...

  <div class="card">
        <img class="img-fluid card-img" src="https://snap-photos.s3.amazonaws.com/img-thumbs/960w/1U2EGZ07GU.jpg" alt="Deer in nature">
        <div class="card-img-overlay d-flex">
            <div class="my-auto mx-auto text-center">
                <h4 class="card-title">Animal Farm</h4>
                <h6 class="text">George Orwell</h6>
                <p class="card-text">Tired of their servitude to man, a group of farm animals revolt and establish their own society...</p>
            </div>
        </div>
  </div>

align-middle is effective on elements with display: table or display: inline.

For more insights, check out this related query

Answer №2

Instead of using .mx-auto, you can achieve the same result by simply applying .justify-content-center, .align-items-center, and .flex-column to the parent element. Check out more details at this link.

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />

<div class="card ">
  <img class="img-fluid card-img" src="https://snap-photos.s3.amazonaws.com/img-thumbs/960w/1U2EGZ07GU.jpg" alt="Deer in nature">
  <div class="card-img-overlay d-flex justify-content-center align-items-center flex-column">
      <h4 class="card-title align-middle">Animal Farm</h4>
      <h6 class="text align-middle">George Orwell</h6>
      <p class="card-text align-middle">Tired of their servitude to man, a group of farm animals revolt and establish their own society...</p>
  </div>
</div>

Answer №3

Feel free to give this code a try:

<div class="card ">
<img class="img-fluid card-img" src="https://snap-photos.s3.amazonaws.com/img-thumbs/960w/1U2EGZ07GU.jpg" alt="Deer in nature">
<div class="card-img-overlay d-flex">

<div class="mx-auto" style="margin-top: auto;margin-bottom: auto;">
<h4 class="card-title">Animal Farm</h4>
<h6 class="text">George Orwell</h6>
<p class="card-text">Tired of their servitude to man, a group of farm animals revolt and establish their own society...</p>
</div>

</div>
</div>

To enhance the layout, I included

style="margin-top: auto;margin-bottom: auto;"
to the div with mx-auto class.

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

Struggling with pinpointing the specific element

I'm struggling to select an element within a nested <ul>. In this code snippet, my goal is to pinpoint the <a> tag that follows the parent <li> <ul class="wc-block-product-categories-list wc-block-product-categories-list--dept ...

Eliminate the standard blue border that appears when control-clicking on table elements

I've encountered this question before, but unfortunately none of the solutions provided have worked for me. Some things I've attempted are: Using event.preventDefault() - did not produce the desired result. Removing user-select from CS ...

What is the best way to have two div elements scroll simultaneously?

Is there a way to synchronize scrolling between two divs? I have a situation where one div is larger than the other, and I want them both to scroll together. Any suggestions? .content { width: 100%; } .left { background-color: yellow; width: 50%; ...

Having difficulty implementing Bootstrap on a demo Angular project

I am currently working on an Angular Application and facing issues with loading styles properly. Below is the code I have: menubar.html: <header> <nav class="navbar navbar-expand-lg navbar-dark bg-dark" style="display: inline&q ...

Is there a way to continuously cycle through multiple images fading in and out at various locations on a webpage using absolute positioning?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head> <title>Harley's Art Gallery</title> <script t ...

Utilize ng-bootstrap in an Angular CLI project by integrating it with npm commands

I've been attempting to set up a project using Angular CLI with ng-bootstrap, but I'm having trouble getting the style to work properly. Here are the exact steps I followed (as outlined in the get-started page): Create a new project using `ng n ...

Are the methods of implementing a modal in JavaScript, HTML, and CSS similar to those in Electron JS?

Struggling to implement a button that triggers a modal display using JavaScript? The HTML and CSS components are set up correctly, but the JavaScript functionality seems to be causing issues. Is it possible that Electron JS differs from traditional JavaScr ...

How can line-height be used to crop the bottom of characters in CSS?

My goal is to make a text character reach the edge below it. Despite having no margin or padding, a space still exists due to the inherent spacing below the font characters. Lowering the line-height removes the top of the characters, but I want to chop o ...

Hide the scroll bar in html/css while keeping the functionality of the arrows intact

Is there a way to remove the scroll bar but still be able to access overflown data using arrows? Any assistance would be greatly appreciated. Thank you in advance. ...

The hyperlink to a different webpage does not trigger any JavaScript functionalities or render any CSS styles

I am facing an issue with linking HTML pages that run Javascript and JQuery Mobile from another HTML page. My link setup is as follows: <a href="hours.html">Hours</a> The linking page and the linked pages are in the same directory. However, ...

Can you explain how to set a title to align with the object on the far left within a Grid component using justify="space-between" in Material UI?

I am currently working on a Grid layout project that involves a Grid container with four Grid items. The first Grid item takes up the full width and contains a title, while the other three Grid items contain avatars. My goal is to align the avatars to the ...

The navigation bar toggle feature seems to be malfunctioning in Bootstrap 5

I've encountered a problem with the navbar while using Bootstrap 5.0.2 dist. The toggler on the navbar expands and collapses, but it doesn't roll back after clicking the bar icon. I have implemented the fixed-top navbar from the Bootstrap 5 examp ...

Using an Icon Button within a Material Card in React

I am attempting to place the edit icon button on the right side of my card, similar to the image shown below. Please refer to this sandbox link for the current output: VIEW IMAGE HERE SEE SANDBOX LINK HERE <CardHeader classes={{ ...

Tips for dynamically resizing a div element as a user scrolls, allowing it to expand and contract based on

I was working on a project and everything seemed easy until I hit a roadblock. What I am trying to achieve is expanding a div to 100% when scrolling down to the bottom of the div, then shrink it back to 90% at the bottom and do the reverse when scrolling ...

What is the best way to reposition the origin to a suitable location using JavaScript?

I am currently struggling with a challenge. My goal is to make the dot on the progress bar reach specific positions when the value is 10 or 20. However, I am facing difficulties in achieving this due to gaps between the segments of the progress bar. I woul ...

Center the font in the middle of the text field

I'm having a text field with a jQuery hint. How do I align the font to be in the middle? I've attempted using vertical-align: middle but it doesn't seem to work. <style type="text/css"> #name { border: 1px solid #c810ca; heig ...

Troubleshooting Problems with CSS Media Queries on Various 1280 screens

I'm facing an issue with creating a responsive layout using CSS media queries to adapt to different screen resolutions. Even though I've set up Media Queries for specific resolutions like: /*1280 x 1024*/ /*1280 x 960*/ /*1280 x 800*/ /*1280 x 7 ...

Ensure that a child div is displayed above the parent div that is absolutely positioned

Is it possible to have a child div within an absolutely positioned parent div appear above its parent, aligned in such a way that the top of the child div aligns with the bottom of the parent div, while also being displayed above neighboring parent divs on ...

PrimeFaces: Achieving Conditional Coloring Based on Status (Passed/Failed)

Attempting to dynamically change the color of a column based on its status value (Passed/Failed/InProgress) using jQuery. I tried copying and pasting the table into jsfiddle, where it worked. However, when implemented in the actual XHTML file, the jQuery ...

Getting started with CSS and HTML: Tips and Tricks for Beginners

Seeking advice on how to enhance my web designing skills, particularly in starting point and techniques. Currently, I am familiar with HTML and CSS, but have been primarily using pre-made templates for building websites. I aspire to be able to transform ...