Having trouble aligning my fixed div to the center of the screen

I have scoured numerous resources in search of a solution to my problem, but nothing seems to work for me. I have created a simple portfolio site where I want the word 'Developer' to be the first thing visible upon loading, followed by the portfolio as you scroll down.

I have added top padding to my portfolio header to fill the screen, but I can't seem to center the word 'Developer' within it. Being new to this, I must have made a mistake somewhere!

HTML:

<header id="hero">
    <div id="hero_text">Developer.</div>
    <img class="arrow" src="Images/lake_tahoe_img/arrow.svg" alt="Down arrow"></a>
  <a href="index.html" id="logo">
    <h1>Rob Wood</h1>
    <h2>Front-end Developer</h2>
  </a>
  <nav>
    <ul>
      <li><a href="index.html" class="selected">Portfolio</a></li>
      <li><a href="about.html">About</a></li>
      <li><a href="contact.html">Contact</a></li>
    </ul>
  </nav>
</header>

CSS:

#hero {
    padding-top: 800px;
    z-index: -2;
}

#hero_text {
    position: fixed;
    margin-left: auto;
    margin-right: auto;
    top: 35%;
    color: white;
    font-size: 9em;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
}

.arrow {
    width: 50px;
    top: 70%;
    position: center;
    padding-left: 50%;
    position: fixed;
}

Answer №1

For your #hero_text, consider adding text-align:center and removing position:fixed.

The issue with using position:fixed is that the element behaves like an absolutely positioned element, causing it to collapse onto itself without any width for centering.

One solution could be keeping the fixed positioning but specifying a width for the element. With auto margins, you can center both the element and its text content within.

It's worth noting that there is no valid property called position:center as mentioned in the comments.

Answer №2

Are you looking for something similar to this?

In order to center the developer on the page, I utilized

display:flex; justify-content:center
and align-items:center;. With these properties, the word "developer" always remains centered.

body{
  margin: 0px;
}
.box{
  background-color: pink;
  background-size: cover;
  height: 100vh;
  position: relative;
  display:flex;
  justify-content: center;
  align-items: center;
  }
h1{
  font-size:25px;
  }
 .down{
height: 100px;
}
<div class="box">
<h1>
Developer
</h1>
</div>
<div class="down">
 <p>
the rest of the page
</p>
</div>

Answer №3

It appears that you are in need of a code snippet like the one below:

.outer-container {
    position: fixed;
    display: table;
    width: 100%;
    height: 100%;
    background: #ccc;
}

.inner-container {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

#hero {
    display: inline-block;
    background: #fff;
}

#hero_text {
    font-size: 9em;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
}

#hero ul {
    list-style-type: none;
}
<div class="outer-container">
    <div class="inner-container">
        <header id="hero">
            <div id="hero_text">Developer.</div>
            <img class="arrow" src="Images/lake_tahoe_img/arrow.svg" alt="Down arrow">
            <a href="index.html" id="logo">
                <h1>Rob Wood</h1>
                <h2>Front-end Developer</h2>
            </a>
            <nav>
                <ul>
                    <li><a href="index.html" class="selected">Portfolio</a></li>
                    <li><a href="about.html">About</a></li>
                    <li><a href="contact.html">Contact</a></li>
                </ul>
            </nav>
        </header>
    </div>
</div>

(check out this Fiddle)

Answer №4

One downside of the solution provided is that it requires setting a fixed height and line-height for the outer div (the gray one).

.abs-center {
  position: absolute;
  top: 0; bottom: 0;
  left: 0; right: 0;
  margin: auto;
}
.v-center {
  background: yellow;
  display: inline-block;
  vertical-align: middle;
  line-height: normal;
}
#graybox {
  height: 120px;
  line-height: 120px;
  background: #bbb;
  text-align: center;
}
<div id="graybox" class="abs-center">
  <div class="v-center">
    <p>vertically<br />centered</p>
  </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

Tips on sending multiple values to AngularJS Directive

Currently, I am in the process of creating a simple AngularJS directive. As I am still very new to AngularJS, I would appreciate it if the answers provided could be simplified! The directive I am working on is essentially a combobox. For those unfamiliar ...

placed three blocks inside the table cell

Is there a way to align three blocks in the table-cell layout so that p1 is at the top, p2 is at the bottom, and p3 is in the middle? Here is the corresponding HTML: <div id="table"> <div id="row"> <div id= ...

The X path and CSS selector for a particular table will vary on every page

Hello, I am new to HTML and currently working on a project that involves scraping data from html tables using RSelenium. I have managed to use the following code successfully: for(i in 1:50){ remDR$navigate(URLs[i]) CPSHList[[i]] <- remDR$getPageSou ...

Arrange the table by column following a service request

Is there a method to organize the table below once it has been loaded? I am utilizing Google API to retrieve distance matrix data, but I want the table to be sorted by distance. However, this sorting should take place after the Google service call is comp ...

After exiting the PWA, the IOS camera displays a black screen

I have implemented an HTML file input to open the camera and capture photos for my Progressive Web App (PWA). <input type="file" accept="image/*" capture="camera" name="photo" id="photo-input-js" data-project-id="<?php echo $projectId ?>"> // ...

What is the best way to extract the value from an anchor element?

To retrieve the value from an input tag, we can use <id>.value like this: <input type="text" #solutionInput (keyup)="0"><br> <solutionDetail [solutionName]="solutionInput.value" > </solutionDetail> Here, 'solutionInput& ...

Comparing inline-block and block display options for displaying images

In this HTML snippet, there is a main div with the id of "main_div" that contains two sub-divs. The first sub-div has an id of "logo" and includes a link to a picture. The second sub-div has an id of "intro" and contains text. For reference, here is the o ...

The CSS rule for @media screen is failing to function properly on mobile devices

Is there a way to hide a specific section of my home page only on smartphones? I've tried using the code below but it's still showing up on my phone. Any advice? @media screen and (max-width: 640px) { #statistics .row { visi ...

Aligning images both horizontally and vertically inside a div with changing dimensions

Looking for advice on centering forward and back arrows on either side of an image within a resizable div. The dimensions of the div change based on the image size. Check out my progress here: Play around with the layout on this jsfiddle: http://jsfiddle ...

Reduce the size of a container element without using jquery

In my Angular application, I have structured the header as follows: -- Header -- -- Sub header -- -- Search Box -- -- Create and Search Button -- -- Scroll Div -- HTML: <h1> Header </h1> <h3> Sub header </h3> <div class="s ...

Turning off CSS on a Wordpress page

I need some assistance with WordPress and CSS. By default, WordPress applies CSS to all posts and pages. However, I want to disable specific CSS styles for certain objects on the page. For example: When I insert a table, it automatically inherits the CSS ...

How to open a PDF file in a new tab using Angular

Within my Angular 12 application, I am seeking to enable users to "view" a PDF file stored locally in the application within a new tab on Google Chrome. Currently, when implementing the code below in HTML, the file downloads rather than opening in a new ta ...

Mastering the Art of Revealing and Concealing

I stumbled upon a great demonstration on reversing the Hide and Show functionality at https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_toggle_hide_show <!DOCTYPE html> <html> <head> <meta name="viewport" content="width ...

Creating a CSS layout with tabs that include a visually appealing right space

My webpage is set up for tabbed browsing, with the tabs displayed as <li>s in block form. The parent div containing the tabs is floated to the right. However, I am encountering an issue where the last tab is not fully aligning with the right side of ...

A Step-by-Step Guide on Importing Components in Vue.js

I have defined the following component as shown below: import Vue from 'vue'; import Datepicker from 'vuejs-datepicker'; Vue.component('DatePicker', { template: ` <datepicker name="date"></datepicker> ` ...

Creating various styles for cards using ngFor within Angular

I have been working on applying different styles to cards created using ngFor in Angular. Here's how it currently looks: https://i.sstatic.net/UhbSp.png My aim is to set unique styles for each card. Due to the ngFor loop used in creating them, I ini ...

Did I accidentally overlook a tag for this stylish stripe mesh Gradient design?

I've been attempting to replicate the striped animated Gradient mesh using whatamesh.vercel.app. I've set up the JS file, inserted all the gist code into it, and placed everything in the correct locations, but unfortunately, it's not functio ...

Creating a unique tab spacing effect using HTML and CSS

I'm currently working on my personal website and I'm looking to create a tab-like effect (similar to word processors) for showcasing projects along with their descriptions. Here's an example layout: ReallyLong NameProject Project1 Descr ...

Looking for assistance with aligning an image in a <td> cell that doubles as a link?

How can I center a play icon in the middle of a td cell and make the entire content clickable as a link, including the background image? I've tried various methods but can't seem to get it right without breaking the alignment. Any suggestions wou ...

Center the content of the HTML body

Despite my best efforts, I seem to be making a mistake somewhere. While creating a bootstrap website at , the site appears well and aligned in the center. However, when I try to adjust the screen size to less than 850px or even smaller, all the content shi ...