Looking to position content at the vertical midpoint for a polished look?

Struggling to center the 'MAIN' content vertically in the middle of the page despite trying various methods.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e4868b8b909790968594a4d0cad1cad7">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<style>
  body {
    height: 100vh;
    background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url(img link) background-size: cover;
    background-position: center;
  }
  
  .brandFont {
    font-family: 'Staatliches', cursive;
    font-size: 2rem;
  }
</style>

<body class='text-center text-white'>
  <div class='w-100'>
    <nav class='navbar navbar-dark bg-dark'>
      <span class="brandFont navbar-brand mb-0 h1">TEXT HERE</span>
    </nav>
    <main class='container'>
      <h1>
        TEXT HERE
      </h1>
      <h2>
        LONGER TEXT HERE
      </h2>
    </main>
  </div>

</body>

Even tried using flexbox but the text just won't budge.

Answer №1

I've also experimented with flexbox

Achieving vertical centering in CSS Flexbox can be done by using the following properties:

  • flex-direction: column
  • justify-content: center

Here's a functional example:

main {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
<main class='container'>
  <h1>INSERT TEXT HERE</h1>
  <h2>LONGER TEXT GOES HERE</h2>
</main>

Answer №2

Made some tweaks to the HTML structure: https://codepen.io/emmeiWhite/pen/PoGeJze

UPDATED CODE SNIPPET:

*{
  margin:0;
  padding:0;
  box-sizing:border-box;
}
.hero-wrapper{
      min-height: 100vh;
    background: linear-gradient(to right, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.6)), url(https://picsum.photos/id/1/367/267) center top;
    background-size: cover;
    position: relative;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
}

.container{
  color:#fff;
  text-align:center;
}

.navbar{
  position:absolute;
  left:0;
  top:0;
  right:0;
  background:yellow;
  padding:0.5rem;
  opacity:0.5;
  color:red;
}
<body class='text-center text-white'>
<div class='hero-wrapper'>
    <nav class='navbar navbar-dark bg-dark'>
        <span class="brandFont navbar-brand mb-0 h1">TEXT HERE</span>
    </nav>
    <main class='container'>
      <div>
        <h1>
            TEXT HERE
        </h1>
        <h2>
            LONGER TEXT HERE
        </h2>
        <p>People of this world are travellers</p>
      </div>
    </main>
</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

Ways to turn off the dragging animation for cdkDrag?

I am facing an issue with cdkDrag where a small preview of the item being dragged shows up. I want to disable this feature and remove any CSS classes related to the dragging state. Can anyone guide me on how to achieve this? https://i.sstatic.net/nYU07.pn ...

determine the proportion of the item

My function is supposed to map an object to create new keys. The new key "percent" is meant to calculate the percentage of each value of the key "data". It should be calculated as the value divided by the sum of all values. However, for some reason, it&apo ...

Adding text to several elements by class in Vue.js

Although I am aware that directly modifying the dom in vue.js is not recommended, I find that the alternatives would result in much messier and harder to maintain code. The Challenge Currently, with the use of vue-i18n, I need to dynamically move the curr ...

Changing jQuery to plain JavaScript in order to show a div element more effectively

I am attempting to toggle a checkbox to display a header or div that is currently hidden. @media screen and (max-width: 639.98px){ #menuPanel{ display: none !important; } } I am encountering an unresolved method error on ready in JQuery, ...

What's preventing the buttons from shifting positions?

Having some trouble with my Minesweeper Web Game setup. My buttons are stuck in place and won't budge. Can someone help me figure out what's wrong? Here's the code snippet I've been working on (FYI, using NetBeans 8.1 IDE) HTML: <! ...

I have a JSON file that I want to parse and display the data on my website from my local server

I am currently facing an issue with fetching my JSON file using an ajax request. The specific data contained in the JSON file needs to be displayed on the web page by parsing it through a variable. However, upon running the code, only the HTML elements I h ...

Tips for displaying complex JSON structures in VueJS

I want to extract this information from a JSON dataset <ol class="dd-list simple_with_drop vertical contract_main"> <li class="alert mar" data-id="1" data-name="Active" style=""> <div class="dd-handle state-main">Active<span cl ...

pattern validation using a regular expression

I am just starting to learn about regular expressions. In my current project, I am allowing users to input amounts in both shorthand and full digit formats using a material UI TextField component. Here are some examples of the input formats: 400k - short ...

Graph not displaying dates on the x-axis in flot chart

I've been attempting to plot the x-axis in a Flot chart with dates. I've tried configuring the x-axis and using JavaScript EPOCH, but have had no success so far. Here's a snippet of my code: <?php foreach($data[0] as $i => $d){ ...

Storing data in a text or HTML file using server-side JavaScript

Currently, I am working on a JavaScript form that involves saving user-entered variables to either a .txt file or a new webpage with the variables pre-filled in the inputs. I know that JavaScript cannot directly manipulate the user's machine, but I am ...

A step-by-step guide on revealing a complete div upon selection

Can anyone assist me in showing a div with a form inside it whenever a specific option is selected from a tag? Here is the code I currently have: <script type="text/javascript"> $(function() { $('#contactOptionSelect&apo ...

Bootstrap version 4.1 featuring two columns that overlap each other

Can anyone help me implement this design in bootstrap 4? Currently, I am using the following code snippet: .outer1:after { box-sizing: border-box; content: ''; position: absolute; top: 50%; margin-top: 0; top: 0; right: -10px; background-color: ...

Unable to modify primary color within Understrap theme

I'm having trouble getting my changes in _theme_variables.scss to show up on my website. I attempted to alter the primary color in assets/bootstrap4/_variables.scss, but for some reason, the changes didn't take effect. If anyone has experience wi ...

Is it possible to transfer a child from one parent to another using JavaScript?

I need help with moving an element from one parent to another using JavaScript, preferably using the jQuery library. Original code: <div id = "div1"> <p id = "paragraph"> Lorem ipsum dolor sit amet, adipiscing pellentesque egestas. &l ...

Cube.js Highlights of the Week and Month

How can I calculate metrics (count, sum) for specific time periods like Today/This Week/This Month/Last Month in Cube.js? I attempted to use rollingWindow but it did not provide me with the correct data. The documentation is a bit unclear to me. For a cle ...

AngularJS ng-repeat is not updating when the state changes

Seeking assistance with an Angular application challenge I'm facing. I have implemented a ng-repeat in my app to display the latest messages stored in an array within a controller named "comunicacion": ng-repeat in comunicacion.html <div class=" ...

Utilizing React Typescript Discriminating Unions to choose between two different types based solely on props

In my project, I have a component that consists of different types: type Base = { color: string } type Button = { to: string } & Base type Link = { link: string linkNewTab: boolean } & Base type ComponentProps = Button | Link e ...

What is the best way to extract numbers from a string using JavaScript?

I am working with a string in javascript that looks like this: var xyz= "M429,100L504.5,100L504.5,106L580,106L570,98M580,106L570,114"; My goal is to extract the numbers and store them in an array. I attempted the following code: var x=xyz.match(/\ ...

Error in Next.js 13 due to Prisma table mapping causing hydration issues

I recently created a basic project in Next.js 13 and encountered a Hydration Error even though my project is not doing much. The code snippet below seems to be the cause of the issue: import { PrismaClient } from "@prisma/client"; export default ...

Alter the color of text when hovering over the caption with a mouse

I'm currently working on a layout for a page that features multiple titles in a row, each accompanied by a descriptive paragraph below. My goal is to have the paragraphs dynamically change based on which title the user hovers over. Additionally, I wan ...