Aligning the content within the <main> element at the center of the <body> section

Trying to center the content of MAIN both horizontally and vertically on the page has been a challenge. None of the solutions I've attempted seem to work, and it's frustrating because I had it working before making changes that caused it to reset.

If anyone could take a look and provide some guidance on where I might be going wrong, I would greatly appreciate it.

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}

/********** LANDINGPAGE.CSS **********/
/**********  **********/
*, header, footer {
font-family: 'Roboto', sans-serif;
font-size: 1vw;
color: #4c4c4c;
letter-spacing: 0.05em;
text-decoration: none;
}

h1 {
font-family: 'Lato', sans-serif;
font-weight: normal;
font-size: 4vw;
}


/********** SITE **********/
.wrap_site {
max-width: 100%;
margin: 0 5%;
}

.wrap_content {
display: flex;
align-items: center;
justify-content: center;
}

.button {
border: 1px solid #4c4c4c;
display: inline-block;
cursor: pointer;
font-size: 1vw;
padding: 8px 18px;
background-color: white;
}

.button:hover {
transition: background-color 0.5s ease;
color: white;
background-color: #4c4c4c;
}


/********** HEADER **********/
.wrap_header {
left: 0;
right: 0;
top: 0;
border-bottom: 1px solid #4c4c4c;
padding-top: 1rem;
padding-bottom: 1rem;
}

header {
display: flex;
justify-content: space-between;
}


/********** FOOTER **********/
.wrap_footer {
position: absolute;
left: 0;
right: 0;
bottom: 0;
margin: 0 5%;
border-top: 1px solid #4c4c4c;
padding-top: 1rem;
padding-bottom: 1rem;
}

footer {
display: flex;
justify-content: space-between;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="wrap_site">
<div class="wrap_header">
<header>
<p></p> <!-- filler -->
<p>This website is currently under construction.</p></header></div>

<main class="wrap_content">
<h1>Hi, my name is x. Welcome to my website.</h1>
<h2 id="translate">你好, 我叫 x。欢迎来到我的网站。</h2>
</br>
<h2><a href="#" class="button">ABOUT ME</a></h2></main>

<div class="wrap_footer">
<footer>
<a href="mailto:x"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="75161a1b01141601350d5b161a18">[email protected]</a> 📧 </a>
<a href="imprint">⚖️ Imprint</a></footer></div></div>

<!-- AUTOTRANSLATE -->
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/x-javascript">
jQuery(function ($) {
    var languages = ["¡Hola, me llamo x! Bienvenido a mi página web.", "Привет, меня зовут x. Добро пожаловать на мой сайт.","Hallo, ich heiße x. Willkommen auf meiner Webseite.", "你好, 我叫 x。欢迎来到我的网站。"];
    var counter = 0;
    var $exc = $('#translate')
    setInterval(function () {
        $exc.text(languages[counter++]);
        if (counter >= languages.length) {
            counter = 0;
        }
    }, 2500)
})
</script>
</body>
</html>

Answer №1

In order to center the content, you'll need to add the following CSS to body, .wrap_content, and possibly also .wrap_site:

display: flex;
align-items: center;
justify-content: center;
flex-direction: column;

Additionally, apply the following CSS specifically to only body:

height: 100vh;

This will result in a centralized layout similar to the code snippet below:

jQuery(function($) {
  var languages = ["¡Hola, me llamo x! Bienvenido a mi página web.", "Привет, меня зовут x. Добро пожаловать на мой сайт.", "Hallo, ich heiße x. Willkommen auf meiner Webseite.", "你好, 我叫 x。欢迎来到我的网站。"];
  var counter = 0;
  var $exc = $('#translate')
  setInterval(function() {
    $exc.text(languages[counter++]);
    if (counter >= languages.length) {
      counter = 0;
    }
  }, 2500)
})
html,
body,
...(remaining CSS properties)...;

/* CSS continues with various styling rules for different elements */

.wrap_site {...}
.wrap_content {...}
.button {...}
...(more CSS styles)...;
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
  ...(HTML structure)...
</body>

</html>

It's important to note that jQuery should be included only once inside the <head> section of your HTML document to avoid duplication.

I hope this explanation helps you achieve your desired centered layout! :)

Answer №2

Take a look at my solution:

https://codepen.io/Czeran/pen/OjpKoj?editors=1100

In order to center the content, I utilized the display: flex property on the body element and applied position: absolute; specifically to the header.

body {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 5%;
}

Answer №3

To create a vertical layout, add a height property and implement the flex-direction in CSS:

.container {
  display: flex;
  height: 100vh;
  flex-direction: column;
}

See the demonstration on JSFiddle here: https://jsfiddle.net/789yerdg/4/

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

Obtain the rotational value in 3D CSS using JavaScript by extracting it from the matrix3d()

My element has been 3D transformed in the following way: .foo { transform: rotateX(-30deg) rotateY(35deg); } Now, I am looking to retrieve these values using JavaScript. Extracting the 3D matrix is simple: var matrix = $('.foo').css('tr ...

execute the function whenever the variable undergoes a change

<script> function updateVariable(value){ document.getElementById("demo").innerHTML=value; } </script> Script to update variable on click <?php $numbers=array(0,1,2,3,4,5); $count=sizeof($numbers); echo'<div class="navbox"> ...

Element positioning in Material UI is fluid and responsive across all devices, with the ability to place elements at

As a novice in layout design, I am currently developing a web application for both mobile and desktop using React and Material UI. My challenge lies in placing the app navigation at the bottom of the screen. The issue arises because the location of the app ...

CSS3 Arrow missing from display

Is there a way to create a box with an arrow on its right center using CSS? Here is the code I have so far, but the arrow is not displaying. Any suggestions? CSS: .pageHelp{ float:right; margin:10px 20px 0 0; width:85px; height:25px; border-radius:3px; b ...

The modal content is not displaying within the dropdown menu

When I call the Bootstrap 5 modal from a dropdown, it only shows a grey background instead of the content. What could I be missing? You can find the code in this JSFiddle link: <div class="container"> <div class="row"> ...

What is the method for calculating the difference in days between two Jalali dates when selecting the second date picker input?

I have integrated the Persian Datepicker script from GitHub to display jalali dates on my webpage. Users can select date1 and date2 from the datepicker to input their desired dates. Below is the code snippet: $(document).ready(function() { $('. ...

Is it possible to have text links styled, but without any actual hyperlinking?

Today has been rough, and I'm struggling to find a solution to my problem. Here's the issue: I have some nice CSS for links, but I only want it to apply to text links, not images or divs. Just plain text. My current code looks like this: a { ...

Error with Unexpected Token HTML found in IBM Domino Web application

Currently working on an outdated IBM Lotus Domino web application that does not utilize XPages. The page is consistently encountering HTML errors related to the following line of code: <tr valign="top"><td width="20"><img width="1" height= ...

Creating a Bootstrap layout with columns of varying heights

My current setup looks like this: <div class="row"> <div class="col-md-4">Content</div> <div class="col-md-4">Content</div> <div class="col-md-4">Content</div> <div class="col-md-4">Content&l ...

Browser crashes due to JavaScript for loop

I am facing an issue where a particular loop is crashing the browser when executed. This loop is part of a set of three functions designed to factorize quadratic equations. After conducting tests, I am confident that the problem lies within the loop itse ...

Tips for modifying the class of a span inline element using a javascript function

Looking for assistance in creating a password text box with an eye icon that toggles visibility. When the user clicks on the eye icon, it should change to a crossed-out eye icon and switch the input type to text. <div class="input-group show-hide-passw ...

Is it feasible to submit a request without needing to redirect the page or relying on JavaScript?

I am attempting to send a post request to an API. This is what I have so far: <form method="POST" action="/api/updoot/ID:{{ post.ID }}"> <input type="submit" value="updoot"/> </form> Upon clicking submit, it redirects. While I can p ...

The PhoneGap Android whitelist feature seems to be malfunctioning and preventing navigation to the next page

Currently, I am utilizing the jQuery function load() with the code snippet $('#result').load('http://.... #div'); to retrieve the content of an external website. Moreover, I have made modifications to the domain whitelist specifically f ...

Can we incorporate <a> tags in XML as we do in HTML?

Currently, I am honing my XML skills. I am planning to incorporate the <a> tag within the XML file in order to create a hyperlink to another webpage. ...

Guide on implementing Observables in Angular 2+ to update a single entry in a table in real-time

I'm facing a challenge with my Angular4 and Node.js application. I have a table that displays data with 7 columns using *ngFor. The issue arises when I need to update the last column dynamically and in real-time. Specifically, I want to show a green c ...

"Enhance Your Website with Boostrap Inputs and a Stylish

After changing my navbar to a full-width navbar, the width of my input div and ul with icons also changed. I have tried many solutions but am unable to fix the problem. I suspect that using a container to center the content in the navbar may be causing the ...

Customize your popover content with Bootstrap settings

I've been on a quest to dynamically update the content of a Bootstrap popover using JavaScript, but unfortunately, the methods I've tried so far haven't worked out as expected : <!--object with the popover--> <input id="popoverlist ...

I am curious to learn the best way to ensure that all the elements on my navigation are evenly spaced

I'm trying to figure out how to evenly space all the elements on my navigation bar. It's for a school project where I'm designing a website, but I just can't seem to get it right. None of the solutions I've tried so far have worked ...

Detecting a click outside of a div or its opening button and closing the element using Vanilla JS

When dealing with two buttons, one opens a form, and the other a dropdown. The goal is to have both elements close when clicking outside. The dropdown functionality already works as expected because it closes whenever the user clicks outside the opening b ...

Fix the positioning of a div in BootStrap and CSS code

Hey there, I'm relatively new to CSS/Bootstrap and currently in the process of learning. I might need to incorporate some CSS into the "chat_funcs" div, but unsure about what code to input to achieve my desired outcome. The issue at hand: What I am ...