Mastering CSS positioning and perfect alignment

I'm currently working on a website where I want the text to be centered on the page without scrolling. Although I have the container div centered, I'm struggling to align the text properly. It's crucial that h1 has a position: fixed because I plan to include animations. You can view what I've done previously here. Please watch the video for a better understanding of my goals.
However, since it wasn't responsive, I decided to make some changes. You can check out the updated code on jsFiddle.

If there are alternative methods to achieve the animations without using h1{position:fixed}, I would appreciate any suggestions.

P.S. I attempted to use <span> and JavaScript/jQuery for the effect but was dissatisfied with the outcome.

Thank you!

If you look at the code, you'll notice two h1 tags. Upon loading the page, I want the content of the first h1 tag to fade in and then fade out after a 2-second delay (using animate.css). Not setting position:fixed on the h1 tags results in them stacking on top of each other, which I want to avoid.

You can refer to the jsFiddle link above, where I've implemented the animations.

body {
  overflow: hidden
}

.content {
  height: 50vh;
  width: 80%;
  color: #ff7100;
  width: 80%;
  margin: 0 auto;
  text-align: center;

}

h1 {
position:fixed;
  /* Uncomment this line to see how it affects the animation (not desired effect) */
  opacity: 0
}

.main .content h1 {

  animation: fadeInDown, fadeOutDown;
  animation-duration: 1s, 1.5s;
  animation-timing-function: linear;
  animation-fill-mode: forwards, forwards;
  animation-timing-function: ease-in, linear;
}

.main .content h1:nth-child(1) {
  -webkit-animation-delay: 2s, 4s;
  animation-delay: 2s, 4s;
}

.main .content h1:nth-child(2) {
  -webkit-animation-delay: 4.5s, 6s;
  animation-delay: 4.5s, 6;
}

.main .content h1:nth-child(3) {
  -webkit-animation-delay: 15.5s, 18s;
  animation-delay: 15.5s, 18s;
}

.main .content h1:nth-child(4) {
  animation: fadeInDown;
  animation-duration: 1s;
  animation-fill-mode: forwards;
  animation-timing-function: ease-in;
  -webkit-animation-delay: 18.5s;
  animation-delay: 18.5s;
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    -webkit-transform: translate3d(0, -100%, 0);
    transform: translate3d(0, -100%, 0)
  }
  to {
    opacity: 1;
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0)
  }
}

@keyframes fadeOutDown {
  from {
    opacity: 1
  }
  to {
    opacity: 0;
    -webkit-transform: translate3d(0, 100%, 0);
    transform: translate3d(0, 100%, 0)
  }
}
<div class="container-fluid main" style="border:10px solid red;">
<p> I need the 'Websites' and 'Development' in the center of the column. At the moment .text-center doesn't work 
  <div style="border:10px solid purple;width:80%;margin:15% auto ">
    <div class="content row">
      <div class="col text-center" style="border:10px solid black;">
        <h1>Websites</h1>
        <h1>Development</h1>
      </div>
    </div>

  </div>

</div>

The primary issue I'm facing is that I can't seem to center the h1 within the column effectively.

Answer №1

When your page is the same size as the body, you can center an h1 element by using the following CSS properties:

transform: translate(-50%, -50%);
top: 50%;
left: 50%;

This should be sufficient for centering the h1 element. Additionally, consider replacing 'fixed' with 'absolute' in your case, as centering the container has no effect when using fixed elements.

Answer №2

To center your fixed h1 element on the page, you can utilize the following CSS properties. It's also beneficial to set a position: relative on a parent element to provide context for the position: absolute child element.

h1 {
  position: fixed;
  top: 50%;
  left: 0;
  right: 0;
}

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

Creating responsive CSS layouts for various device sizes

My webpage features a form with a prompt for the input box, followed by the input box itself. I want to implement two different layouts based on the device accessing the page. For cell phones, I want everything stacked vertically. However, for computers, ...

Show feedback on the webpage

I've encountered a challenge when trying to post comments on the page based on the posting ID. Controller: public function viewUserQuestion(Post $post) { $comment = Comment::where('post_id', $post->id)->get(); return view(& ...

What is the best way to create a fixed footer in Next.js using React?

I'm looking to create a fixed footer that will stay at the bottom of the page if there isn't enough content to fill it. I've been researching ways to achieve this using CSS, but many of the methods don't easily translate to React/Next.j ...

CSS menu displayed in inline-block is not functioning as expected

Struggling with a CSS menu that I'm developing. Despite applying the display:inline-block to all elements, the menu items continue to display vertically rather than inline. Any tips on how to resolve this issue? View the code here: http://jsfiddle.net ...

Issue with CSS background images not resizing properly on iPad and iPhone devices

While my site's background image resizes nicely in Chrome and Safari using background-size: cover, I encountered an issue when testing it on an iPad or iPhone. The CSS background image appears to be extremely zoomed in, resulting in a horrible appeara ...

Best practices for sending variables to an HTML page using nodemailer

I am interested in sending an email with an HTML template that includes dynamic data. For example, I want to include a unique id within a link on the page. Is it possible to insert dynamic data into HTML content when sending emails? If yes, I would apprec ...

Exploring the world of HTML5 speed testing: getting started

Can anyone provide guidance on how to begin with an HTML5 bandwidth test? I am trying to replicate a flash-based version and any recommendations would be greatly appreciated. ...

Is it possible to keep my JavaScript scripts running continuously within my HTML code?

I recently set up a JavaScript file that continuously queries an API for updates. It's currently linked to my index.html, but I'm looking for a way to keep it live and running 24/7 without requiring the browser to be open. Any suggestions on how ...

How to Retrieve Upload Progress via HTTP Request in PHP

Is there a way to monitor the progress of file uploads by accessing the HTTP request in PHP? If so, how can this be achieved when uploading files into a MySQL database? require('../connect_db.php'); //Collect all necessary data $name = $dbc-> ...

What steps can I take to prevent an HTML box from appearing too close to one side of the screen?

My current goal is to achieve this layout (without the red arrows, of course): https://i.sstatic.net/Mm1rB.png However, I have encountered a CSS issue that I can't seem to figure out on my own. When I view my page, it appears like this: https://i.s ...

Integrate CSS and Javascript Plugins into your Ruby on Rails application

I am utilizing an HTML, CSS, and JS template to design the interface for my Rails application. Within this template, there are several plug-ins that are required in both CSS and JS formats. I have stored these plug-ins within the "/assets/plugins/" directo ...

Is there a way to modify the styling for ListItemButton specifically when it is in a selected state?

My first experience with MUI involves trying to customize the styling of a button. After installing my project with default emotion as the styling engine, I attempted to override the existing styles using the css() method mentioned in the documentation: ...

Embed one module within another module and utilize the controller from the embedded module

I am attempting to create a module and inject it into the main module. Then, I want to inject the controller into the injected module but am facing issues. In my index.html file: <html ng-app="MenuApp"> <head> </head> <body> <d ...

Ways to identify local storage when a user visits the page for the first time using jQuery

As a beginner in the world of local storage in Jquery, I am looking to implement a feature on my Bootstrap 4 accordion. The desired functionality is as follows: If a user is visiting the page for the first time, all accordions should be open by default ...

The BottomNavigation component in MUI has a minimum size limit of 400px when it displays 5 items

My issue involves a bottom navigation bar with 5 elements. When the window is resized to less than 400px, the bottom navigation does not shrink, instead maintaining a minimum width of 400px and causing a scrollbar to appear on the x-axis. You can view a m ...

Applying CSS styles to a page depending on certain conditions

Currently, I have a page in SharePoint that can function as a pop-up. I am using JavaScript to identify whether it is a pop-up by checking if window.location.search.match("[?&]IsDlg=1"). However, I am struggling to figure out how to insert CSS based on ...

What is the best way to extract the text inside a div element based on the input value in a

I attempted to extract the innerText of a div along with the input values within it. For instance: <div> My name is Shubham, I work for <input type="text"/> for the last 5 years.</div> My objective is to retrieve all the text ...

Using htaccess to restrict access to a specific URL path based on IP Address

(I don't believe the previous link addresses this question; please refer to the comment below) I am currently utilizing a CMS platform that utilizes a database instead of the usual file-directory structure for managing webpages. Due to the absence of ...

Using Javascript to select a radio button in a form depending on the value entered in a text box

I have a form that interacts with a Google Sheet to insert and retrieve data. For instance, the form contains two radio buttons: <input id="Rdio_1" name="RdioSelect" type="radio" class="FirstCheck" value="1" onchange="RadioValInsert ()"/> < ...

Mobile Chrome allows users to utilize the IFrame player API for enhanced video

Currently, I am working on creating a mobile website where I plan to include some YouTube videos using the IFrame player API (https://developers.google.com/youtube/iframe_api_reference). My main goal is to have the video start playing only after the user ...