Align text to the center, positioned on top of an image

I'm trying to center text in an image on my website, but I am having trouble with using the margin property in CSS. The text needs to be centered within the image as it will be part of an animation later on. If using the image as a background doesn't work out, I can find an alternative solution. How can I achieve this? My current approach doesn't seem to be working.

EDIT: Apologies for missing this earlier, but the text I want to center is the .IOStext in the CSS file. It should be positioned in the middle of the Moon.

Here is my code:

@font-face {
  font-family: UDFont;
  src: url(gomarice_gogono_cocoa_mochi.ttf);
}
body {
  background-image: url(background.png);
  background-repeat: no-repeat;
  background-size: cover;
  background-color: #101423;
  min-width: 100%;
  min-height: 100%;
  font-family: 'UDFont', Impact, Helvetica, Arial, Sans-serif;
}
#header {
  width: 50%;
  margin: 100px auto;
}
#headerContent {
  width: 300px;
  margin: auto;
}
#mainTitle {
  width: 100%;
  margin: auto;
  text-align: center;
}
.universeTitle, .devTitle {
  font-size: 3em;
  margin: auto;
  text-align: center;
}
.universeTitle {
  color: #74aeee;
}
.devTitle {
  color: #f1ab3c;
}
.slogan {
  color: #822E81;
  text-align: center;
  margin: 10px auto;
  width: 100%;
}
#content {

}
#selectors {

}
#IOSselector {
  margin: auto;
  width: 300px;
  height: 300px;
  background-image: url(IOSMoon.png);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
}
.IOStext {
  text-align: center;
  margin: auto;
  margin-top: auto;
  width: 100%;
  height: 50px;
}
<!DOCTYPE html>
<html>
  <head>
    <title>Welcome! | Universe Dev</title>
    <link rel="shortcut icon" type="image/png" href="icon.png"/>
    <link rel="stylesheet" href="style.css"/>
  </head>
  <body>
    <div id="header">
      <div id="headerContent">
        <div class="mainTitle">
          <span class="universeTitle">Universe </span><span class="devTitle">Dev</span>
        </div>
          <h3 class="slogan">Universal Development</h3>
      </div>
    </div>
    <div id="content">
      <div id="selectors">
          <div id="IOSselector">
            <h1 class="IOStext">IOS</h1>
          </div>
    <script src="script.js"></script>
  </body>
</html>
<!-- END -->

Answer №1

To create a neat layout for your "moon" element, try using flexbox and set your text to display as inline-block:

#IOSselector {
  margin: auto;
  width: 300px;
  height: 300px;
  background-image: url(http://via.placeholder.com/300x300);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  display:flex; 
  justify-content: center;
  align-items: center;
}

.IOStext {
  margin: auto;
  margin-top: auto;
  display:inline-block;
}
<div id="IOSselector">
  <h1 class="IOStext">IOS</h1>
</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 is the best way to implement a day timer feature using JavaScript?

I am looking for a timer that can automatically change the rows in an HTML table every day. For example, if it is Day 11, 12, or 25 and the month is February at 8 AM, the rows should display "Hello!". function time() { var xdate = new Date(); var ...

The text enclosed by a span located inside a div and identified by xpath

I am struggling to extract text from a span nested within another text in a div (highlighted in the image). https://i.sstatic.net/8uIWz.png This snippet of code is relevant ... <div id="groupBlock3"> <div class="groupBlockTitle& ...

Customize the appearance of an ASP link button within a navigation menu

Just getting started with ASP.net and CSS, I want to customize my link button using CSS similar to other <a href> buttons, but I seem to be running into some issues. This is the code for my menu: <div id="templatemo_menu"> <ul ...

Having difficulty incorporating a component into a different component within Angular

I'm facing an issue where I can't use the selector tag of a header component in another component, even though it works fine when used alone in app.component.html In my header.component.ts file: import { Component } from '@angular/core&apos ...

Ensure that the elements are properly arranged inside divs that have been stretched

My goal is to create divs that are all the same size, while aligning some content within each div differently due to varying lengths. The objective is to keep the icon and text in a consistent position within all divs, but allow the divs to expand at the ...

What is the best way to achieve the specific grid arrangement in Bootstrap?

After spending a significant amount of time on this project, I've come to realize that the centering issue is always persistent. The thumbnail charts on the right side refuse to align properly in a square formation as depicted in the image attached. M ...

Modify the background color of the <li> element without using JavaScript after 3 seconds

Is it possible to change the background color of a <li> element without using javascript or JQuery, only with CSS? I've seen incredible things done with CSS alone and believe this is achievable. My <li> element functions as a horizontal m ...

HTML and CSS: Focus on border for <td> cells only

I'm looking to apply a border to only one cell (B2) without nesting tables. Here's the code: <table border="0"> <tr> <td>A1</td> <td>B1</td> <td>C1</td> </tr> ...

Is there a way to position this Flex-Box Item beside the image?

Please refer to the image How do I rearrange this Flex-Box Item to be positioned next to the image? https://i.sstatic.net/eSy3w.png <div class="brif fb"> <div class="sml-con"><img class="sml-thumb" src="images/chosen/edwa ...

Display full desktop version on mobile devices with minimized view and no need for horizontal scrolling

While it may seem unusual, my client has requested temporarily removing responsiveness from the site to view the desktop version on mobile. I initially tried removing the responsive meta tag, but encountered horizontal scrolls on the page. My goal is to di ...

JavaScript: Implementing a seamless spinning effect for an image [Compass needle]

I have been working on developing a compass app for mobile devices and have successfully created a function that returns the change-values for the compass-needle rotation (e.g. 20). However, I have noticed that setting the 'image-transform' to &a ...

Text randomly appears on the html page

I've been dedicating a significant amount of time to finding a solution, but haven't had any luck. I'm aiming to create a visual effect where 10 words with varying font sizes slide in from different directions on a canvas within my document ...

Dynamically change the fill color of an SVG using styled-components

I've been attempting to target the fill property of my SVG using CSS in styled-components without any luck. Here is my SVG: <svg width="65" height="19" viewBox="0 0 65 19" fill="none" xmlns="http://www. ...

Tips for aligning radio button selection with input check in the center

How can I horizontally center the blue circle that appears when a radio button is clicked? Option 1 Option 2 Option 3 Option 4 Checkbox 1 ...

How can the size of the font in a <div> be adjusted based on the number of characters present?

I am attempting to create a basic HTML and CSS layout with two div blocks, each 1000px wide, within a parent container that is 3000px wide. <div class="blocks"> <div class="block-a">text 1</div> <div class="block-b">text 2& ...

Add a new row to a table using auto increment feature in Javascript

I am having issues with inserting rows using a button in Javascript. When I click the button, rows are being inserted unexpectedly as shown in the images below. Additionally, I also need a delete row button to work in a similar manner. Thank you for any he ...

Be patient for the complete loading of the image during an AJAX operation

My webpage includes an ajax action that loads a div containing an image on the left and text on the right. The issue I am facing is that the text loads first, aligned to the left, and then the image loads, causing the text to shift to the right, resulting ...

Changing the color of a placeholder using Javascript: A step-by-step guide

I've been searching online without any luck so far. I'm attempting to change the placeholder color of a text box using JavaScript, but I'm not sure how to go about it. I already have a color picker that changes colors. If my CSS looks somet ...

``In JavaScript, the ternary conditional operator is a useful

I am looking to implement the following logic using a JavaScript ternary operation. Do you think it's feasible? condition1 ? console.log("condition1 pass") : condition2 ? console.log("condition2 pass") : console.log("It is different"); ...

Fixed width in the left column with a fluid and clear:both in the right column in a 2-column layout

Struggling to figure this out after spending a lot of time on it. Here's what I'm trying to accomplish: I have a 2 column layout, with the left column having a fixed width of 220px and the right column having a fluid width. The code looks like ...