Center align the text in a vertical group placed over an image

Here is my html page along with its corresponding css code:

<div class="container">
    <div class="row mt-4">

      <div class="col-sm-8">
        <h2>blah blah</h2>
        <p>blah blah</p>
      </div>

      <div class="col-sm-4">

        <h2>Contact Us</h2>

        <address>
          <strong>blah blah</strong>
        </address>
        
        <div class="row">

          <div class="col-6 col-sm-12 col-md-8">
            <a target="_blank" rel="noopener noreferrer" href="https://www.blahblah/home/">
              <img src="media/logos/blah.png" class="img-fluid mb-2">
            </a>
          </div>
         
          <div class="col-6 col-sm-12 col-md-8">
            <text>Sponsored by:</text>
            <a target="_blank" rel="noopener noreferrer" href="https://www.blahblah/">            
              <img src="media/logos/blah.png" class="img-fluid mb-2">
            </a>
          </div>

        </div>
      </div>

    </div>
</div>

text {
  position: absolute;
  font-size: small;
  background: blanchedalmond;
}

I made some changes to the css code in an attempt to center the text within the text tags, but it doesn't seem to be working as expected...

text {
  position: absolute;
  font-size: small;
  background: blanchedalmond;

  text-align: center;
  vertical-align: middle;
  justify-content: center;
  align-items: center;
}

Despite trying different combinations of these additional styling lines, the text is still not centered. What am I missing to achieve the desired center alignment?

Answer №1

To ensure that the items are always centered within the parent container, you can apply properties such as

display: inline-flex, align-items: center
to the parent element.

Here is an example of how to achieve this:

text {
  position: absolute;
  font-size: small;
  background: blanchedalmond;
}

.parent-div {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
}
<div class="container">
  <div class="row mt-4">

    <div class="col-sm-8">
      <h2>blah blah</h2>
      <p>blah blah</p>
    </div>

    <div class="col-sm-4">

      <h2>Contact Us</h2>

      <address>
          <strong>blah blah</strong>
        </address>

      <div class="row">

        <div class="col-6 col-sm-12 col-md-8">
          <a target="_blank" rel="noopener noreferrer" href="https://www.blahblah/home/">
            <img src="https://i.picsum.photos/id/387/536/354.jpg?hmac=Zs1KZbc0jXe2zZP7yKL_OrUKxdUVFH0LqLoegVZnIuA" class="img-fluid mb-2">
          </a>
        </div>

        <div class="col-6 col-sm-12 col-md-8 parent-div">
          <text>Sponsored by:</text>
          <a target="_blank" rel="noopener noreferrer" href="https://www.blahblah/">
            <img src="https://i.picsum.photos/id/387/536/354.jpg?hmac=Zs1KZbc0jXe2zZP7yKL_OrUKxdUVFH0LqLoegVZnIuA" class="img-fluid mb-2">
          </a>
        </div>

      </div>
    </div>

  </div>
</div>

See the code in action on this Fiddle

Answer №2

The initial 2 attributes control the positioning of text on a line.

text-align: center;
vertical-align: middle;

text-align determines the horizontal placement on the line, so if the container is 100% wide, it will be centered.

vertical-align affects the vertical placement on the text line. To center it within your image, the line-height must match the image size.

justify-content: center;
align-items: center;

These are utilized alongside flexbox display:flex. Additionally, you must use position: relative and position: absolute to stack the elements on top of each other.

.image-conatiner {
position:relative;
}
.text {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;

display: flex;
justify-content: center;
align-items: center;
}

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

Comparing hardware-accelerated CSS3 animations, transitions, and jQuery animate for mobile devices

While developing my app using PhoneGap and jQuery, I encountered a dilemma regarding animations. Initially, I relied on what I knew best - jQuery animate - to create smooth movements. However, discussions about hardware acceleration caught my attention. ...

Unable to discover a method to accomplish this task

Greetings to the Stack Overflow community! I have a scenario with two div blocks containing images of dimensions 1280px width and 700px height each. In the first block, there is a div with a width of 400px and a height of 100% floating left. The second ...

I'm having trouble getting my website to align in the center of the

Struggling to center my website no matter what I try. I experimented with different CSS properties but haven't had much luck. margin: auto 0; I also attempted the following: margin-left: 50%; margin-right: 50%; The closest I got was aligning every ...

notification that appears when checkbox is ticked

If a certain delivery option is selected during the checkout process on our website, we would like to trigger a popup box displaying a custom message. Here is the specific checkbox for that delivery option: <input id="delivery_option_22_6" class="deliv ...

Vue.js transition-group does not apply the *-move class

As I dive into learning Vue, I find myself wondering if I may have overlooked something fundamental or stumbled upon a bug. Despite multiple readings of the documentation at https://v2.vuejs.org/v2/guide/transitions.html#List-Move-Transitions, I still can& ...

Issues with CSS3 height transitions in a specific scenario

Check out this simplified version of my code on CodePen: http://codepen.io/anon/pen/pjZXob I am attempting to create a smooth transition in the height of the .destinationsTopicContent div, from 0 to auto. However, it is nested within a div that has visibi ...

The element in the data cell is a form input field

This is the content of my webpage: <input type="text" class="form-control" >id="ButonSociete"name="ButonSociete"placeholder="Societe"> <table cellpadding="0" cellspacing="0" border="0" class="display" >id="demande" width="100%"> ...

Conceal and reveal a list on page load with jQuery

Is there a way to automatically display this listing collapsed without making changes to the HTML structure? I would like all folders in the tree to be collapsed. Instead of the initial display: https://i.stack.imgur.com/cDw4s.png I prefer it to look li ...

CSS Selector for when a radio button is selected

I'm currently using images in place of radio buttons: <label class="radio-inline thumbs"><input type="radio" id="type_nee" name="type" value="nee" onkeyup="validate()" onclick=" ...

To determine whether a variable is an integer in PHP

Is there a more elegant solution to this problem? if( $_POST['id'] != (integer)$_POST['id'] ) echo 'not an integer'; I attempted if( !is_int($_POST['id']) ) However, I encountered issues with is_int(). This ...

The 3D hover effect is malfunctioning in the Firefox browser

I've been working on a 3D hover effect and my code is functioning properly in Opera and Chrome, but for some reason it's not working in Firefox. I've even attempted using vendor prefixes, but to no avail. The strange thing is that when I rem ...

What causes the Object expected error in jQuery?

Looking for a way to demo horizontal collapse pane with a simple web page that includes html, css, and jquery. <html> <head> <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> <title>Sa ...

transfer the output from the second selection to the adjacent div

I am utilizing a library called select2 specifically for single choice scenarios. My goal is to transfer the selected option to a different div once it has been chosen. Here is the code I have so far: https://jsfiddle.net/hxe7wr65/ Is there a way to ach ...

A jquery class for styling with CSS

I am looking to add a CSS class to a gridview. I attempted to use this style from a reference link, so I implemented the following code snippet: $(function () { $('[ID*=search_data]').on('click', function () { var fromda ...

Hide the menu when a user taps on an item on mobile or tablet devices

I'm facing a unique challenge and could use some guidance. The issue is with the mobile version of my website, accessible at . On tablets or mobile devices, the menu is condensed into a button. To navigate through the menu, I have to click the button ...

Make the text in the SCSS file placeholder bold

Within my Angular front end application, there is a form containing a textarea: <mat-form-field class="full-width"> <textarea class="left-aligned" formcontrolname="x1" matInput placeholder="some text"/> </mat-form-field> In the co ...

Tips for highlighting a Material UI Button when pressing the Enter key

Once the user has completed the final input, they need to press the save button The 'Tab' key typically moves the focus to the next element (in this case a button), but I want to use the 'Enter' key for this action. Can anyone advise o ...

Display flex behaving unexpectedly in Chrome and Safari browsers

I utilized flexbox properties to achieve this layout for my section: https://i.sstatic.net/UxQeW.jpg While it functions smoothly in Chrome, I noticed some disparities when viewing in Firefox and Safari. This is how it appears in Chrome: https://i.sstati ...

Is there an animated Google Contact Form in the works?

Is it possible to replicate the onclick animation featured on the Google login form? The animation involves the placeholder text shrinking and moving to the top left corner as you click the button box, while a border forms around the text. Click here to ...

Improper Placement of Bootstrap 5 Modal

I seem to be facing an unexpected issue where the Bootstrap 5 Modal and other components are displaying inside the sidebar menu instead of the main content area. Has anyone encountered this problem before? How can it be resolved? Here is a test example: ...