Align an image in the center both vertically and horizontally, with a line of text placed directly underneath

I'm currently designing a landing page for one of my clients with their logo placed prominently in the center. Right below the logo, they want their catchy slogan to appear on page load with a fading effect. To achieve this, I have included the slogan as a paragraph in the HTML code.

Now, I am facing a challenge in making sure that the text stays responsive and aligned directly below the centered image. I want to ensure that it looks perfect on all devices regardless of the screen size.

Here is the structure of my HTML code:

<div class="viewport1">
<div class="center-wrapper">
<div class="image-wrapper">
<a href="home.html"><img src="images/landing_logo2.png" /></a>
<div id="test"><p>enter the sunshine state</p></div>
</div>
</div>
</div>

Additionally, here is the CSS styling I have implemented:

<style>

div.viewport1 {
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
width: 100%;
overflow: hidden;
z-index: -9999;
}

div.center-wrapper {
max-width: 100%;
height: 100%;
float: left;
position:relative;
left: 50%;
}

div.image-wrapper {
left: -50%;
position: relative;
max-width: 100%;
height: 100%;
margin-left: auto;
margin-right: auto;
}

img {
top: 50%;
left: 50%;
position: relative;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
max-height: 100%;
max-width: 100%;
vertical-align: middle;
display: inline-block;
}

*, *:before, *:after {
box-sizing: border-box; 
}

.image-wrapper p {
width: 100%;
padding-top: 200px;
margin-left: 10px;
font-family: segoe;
font-size: 21px;
text-align: center;
color: #fff;
-webkit-animation: fadein 5s; /* Safari, Chrome and Opera > 12.1 */
   -moz-animation: fadein 5s; /* Firefox < 16 */
    -ms-animation: fadein 5s; /* Internet Explorer */
     -o-animation: fadein 5s; /* Opera < 12.1 */
        animation: fadein 5s;
  }

@keyframes fadein {
from { opacity: 0; }
to   { opacity: 1; }
}

</style>

Answer №1

I have a clear understanding that the logo should be perfectly centered while the text must appear below it.

It seems like there are unnecessary layers of wrappers in your code, so I have streamlined it for simplicity.

To achieve the desired layout, position the image-wrapper absolutely at the center using standard techniques. However, make sure to position the #test div absolutely as well, but this time at the bottom of the parent div.

div.image-wrapper {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid grey;
}
.image-wrapper img {
  display: block;
}
#test {
  position: absolute;
  top: 100%;
  width: 100%;
  border: 1px solid grey;
  border-top: none;
}
#test p {
  width: 100%;
  font-family: segoe;
  font-size: 21px;
  text-align: center;
  color: #000;
}
<div class="image-wrapper">
  <a href="home.html">
    <img src="http://lorempixel.com/output/people-q-c-250-250-3.jpg" />
  </a>

  <div id="test">
    <p>enter the sunshine state</p>
  </div>
</div>

JSFiddle Demo

Answer №2

Here is an illustration of my suggested approach:

By following this method, we ensure better compatibility with older systems and minimize potential issues. For example, consider what might occur if the container is smaller than the centered item

html, body {
  margin: 0;
  height: 100%;
}
.out {
  font-size: 0;
  text-align: center;
  height: 100%;
}
.out:before {
  content: " ";
  height: 100%;
}
.out:before, .in {
  display: inline-block;
  vertical-align: middle;
}
.in {
  font-size: 12pt;
}
<div class="out">
  <div class="in">
    <img src="//placehold.it/200x100">
    <div>This is a caption</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

When the content in the div exceeds its boundaries, it causes it to overlap with

In my design, I have a top menu with a z-index of 999 to ensure nothing covers it. However, I am facing an issue where a scrolling div is appearing on top of the menu bar even though it shouldn't. Any idea why this is happening? Here is the CSS for t ...

Adjust the initial position of the slider handle on the WooCommerce widget price filter

Is there a way to enhance the visibility of the woocommerce widget price filter by adjusting the starting position of the slider handle? For example, shifting it 10% towards the right from the beginning of the range bar. Check out the comparison of positi ...

Swapping React components within a list: How to easily change classes

For my latest project, I am building a straightforward ecommerce website. One of the key features on the product page is the ability for users to select different attributes such as sizes and colors. These options are represented by clickable divs that pul ...

Fixed navbar stays at the top with body occupying 100% of the screen

In the process of developing a react app, I encountered an issue with a sticky top navbar from Bootstrap. After setting the body to height: 100%, the navbar no longer sticks as intended. I cannot use vh for one of my pages due to mobile content display i ...

Speeding up your responsive design with a single CSS file or multiple files is a crucial

There are numerous discussions on whether it's better to have many CSS files or just one, with the consensus being that using a single file reduces the number of HTTP requests. However, my question is a bit different. Personally, I tend to use two CS ...

Steps on integrating a map into a vuetify image

Is it possible to connect my v-img to a standard html map? Below is my code snippet. <template> <v-container> <v-row class="mx-1"> <v-col cols="12"> <v-row justify="center"> ...

Place the social media division at the center of the webpage

I've been struggling to center the social media icons on my website, but haven't had any luck. Here's the code I've been working with: https://jsfiddle.net/2ahgL130/1/ CSS: .table1{ margin:0; padding:0; min-width:100% } ...

Print page headers are being overlapped by THEAD elements

I have created a table and am considering using the thead as page headers if the table spans multiple pages. However, I am encountering an issue where the content of the table overlaps with the table header on the second page. Here is a snippet of my HTML ...

Ways to incorporate color gradients into the corners of CSS cards

Below is an example of a basic bootstrap card: Example 1: https://i.stack.imgur.com/DK2Sz.png I attempted to draw blue and green colors side by side, but was only successful in drawing blue as shown below: Example 2: https://i.stack.imgur.com/CcSzP.png ...

Creative ways to design the HTML5 `<meter>` tag

I am curious about how to style the <meter> tag in a new way. <meter value=80 min=0 max=100> 80/100 </meter> All I want to do is customize the background color and the value color, but I haven't been able to locate the appropriat ...

JavaFX Animation for Charting

My FXML file contains a JavaFX AreaChart <AreaChart id="myAreaChart" onMouseClicked="#handleAreaChartMouseClicked" GridPane.columnIndex="0" GridPane.rowIndex="0" fx:id="myAreaChart" > <xAxis> <Nu ...

Confirming the information in two sections of a form

Could someone assist me with validating my form? Specifically, I need help ensuring that the house number field only accepts two numbers and the postcode field only allows 5 characters. I have implemented a pattern attribute for validation and I am curiou ...

Troubleshooting Angularjs: Why isn't the HTML displaying the variable value?

Trying to display the value of an AngularJS variable in HTML using this code: <div ng-controller="MyTextbox"> <p>Last update date: {{nodeID1}} </p> Here's my angularjs controller code: angular.module("umbraco").controller("MyTex ...

The jQuery animate() method fails to execute animations

I'm currently working on a JavaScript animation project and I've run into some roadblocks. One particular issue I'm facing is with animating the <label>'s margin-top, as it's not behaving as expected. For example: $(' ...

Cover the whole page in darkness and emphasize the division element

I attempted to implement a solution I found on stackoverflow without success. The issue is that the blackout feature does not seem to activate - nothing happens. Within my JavaScript code (all enclosed in the $(document).ready(function () tag), it appears ...

Having difficulty reaching a div element using either its id or class in a CSS stylesheet

I'm having trouble adding padding to the home section div and I can't seem to access it using either the id or class. Any suggestions on how to solve this issue would be greatly appreciated. Below is the CSS code: body { margin: 0 ; font ...

jQuery AJAX utilizes div elements

I am having trouble using functions in a select element loaded within a div using AJAX in jQuery. Can anyone offer assistance? Here is the snippet of jQuery code: $("#cbo_sede").change(function () { var sede = document.getElementById("cbo_sede").val ...

Unable to place within div

Utilizing HTML5 to implement the "DnD" function. There is a button that adds divs. I want to be able to drag items into these newly added divs. Currently, I can only drag into existing divs and not the ones added later. How can I resolve this issue ...

Adjust the orientation of the mat-slide-toggle

Is there a way to change the direction of a mat-slide-toggle from right-to-left to left-to-right without involving the text or label position? I want the switch itself to be on the right when off and on the left when on. I have considered inverting the va ...

Exploring the differences between translate3d and matrix in CSS 3 animations

Lately, my projects have demanded seamless transitions and animations. We've made the shift from using Javascript to CSS animations almost entirely. I've discovered that utilizing translate3d produces incredibly smooth animations on various devi ...