The alignment of the text in the footer is off

Currently experiencing an issue: The alignment of the footer text seems off. When using text-align center, it appears slightly left of center. Text-align left places it further to the left and text-align right works correctly.

I attempted using display: table-cell for proper text alignment, but it caused the footer itself to be positioned awkwardly and couldn't adjust with margins.

Below is my relevant code:

.footer {
    display: table-cell;
    background-color: aqua;
    width: 1000px;
    max-width: 1000px;
    margin: auto;
    border: 5px solid #ccc;
    margin-top: 30px;
    line-height: 12px;
    text-align: center; 
}
<footer>
  <div class="footer">
    <h2>Footer stuff</h2>
  </div>
</footer>

If this information suffices, I'd prefer to keep it concise. Let me know if more details are needed. Appreciate any assistance in advance.

EDIT: Revised version of my complete code snippet

Body {
    background-color: darkred;     
}
.header {
    text-align: center;
    width: 100%;
}
#navbar {
    width: 100%;
    float: left;
    margin: 0 0 3em 0;
    padding: 0;
    list-style: none;
    background-color: darkblue;
    border-bottom: 1px solid #ccc;
    border-top: 1px solid #ccc;
}
#navbar li {
    float: left;
}
#navbar li a {
    display: block;
    padding: 8px 160px;
    font-weight: bold;
    color: white;
    border-right: 1px solid #ccc;
}
#navbar li a:hover {
    color: #c00;
    background-color: #fff;
}
.container {
    background-color: aqua;
    width: 100%;
    max-width: 1000px;
    margin-top: 50px;
    margin-bottom: auto;
    margin-right: auto;
    margin-left: auto;
    border: 5px solid #ccc;
    text-align: center;
    height: 100%; /* BE ADVISED! (????) */
}
#ipsumpic1 {
    width: 150px;
    float: right;
}
#ipsumpic2 {
    width: 100px;
    float: left;
}
#sidebarleft {
    margin: 0px auto;
    position: relative;
    left: 1000px;
    padding-top: 0px;
    width: 240px;
    float: left;
    border: 5px solid #ccc;
    background-color: aqua;
    margin-top: -350px;
}
#sidebarright {
    padding-top: 0px;
    width: 240px;
    float: right;
    border: 5px solid #ccc;
    background-color: aqua;
    margin-top: -537px;
    margin-right: 1100px;
}
.footer {
    display: table;
    background-color: aqua;
    width: 1000px;
    max-width: 1000px;
    margin: auto;
    border: 5px solid #ccc;
    margin-top: 30px;
    line-height: 12px;
    text-align: center; 
}
<!DOCTYPE html>
<html>
<head>
<title>Amazing Website</title>
<meta charset="utf-8">
        <link rel="Stylesheet"type="text/css" href="css/style.css">
        <script type=text/javascript src="script.js"></script>
</head>
<body>
        <header>
            <div class="header">
                <a href="index.html"><img src="img/logo.svg" width="200"></a>
            </div>
        </header>
            <ul id="navbar"> 
                <li><a href="index.html">Home</a></li>
                <li><a href="products.html">Products</a></li>
                <li><a href="services.html">Services</a></li>
                <li><a href="about.html">About Us</a></li>
                <li><a href="contact.html">Contact</a></li>
            </ul>
        <div class="container">
            <main>
                <h1>Lorem Ipsum</h1>
                <img id="ipsumpic1" src="img/ipsum1.svg">
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a pulvinar dolor, a rutrum tortor. Vestibulum auctor finibus odio id efficitur. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur ullamcorper risus neque, sed vehicula augue finibus sed. Donec pretium ornare libero a venenatis. Donec sed ex id tellus posuere interdum. Nullam tincidunt egestas odio non gravida. Sed ut felis aliquet eros commodo elementum. Aliquam ullamcorper, diam quis aliquet tempor, magna nisi laoreet nisl, at vestibulum arcu enim quis elit.</p>
                <img id="ipsumpic2" src="img/ipsum2.svg">
                <p>Ut faucibus dui lectus, vitae pharetra magna maximus quis. Donec sollicitudin sem non ornare vulputate. Suspendisse pretium lacus nec gravida tristique. Sed sed nibh et eros facilisis accumsan. Nam mi elit, fermentum eu laoreet vel, venenatis sit amet leo. Curabitur erat arcu, dictum eu massa quis, hendrerit mattis turpis. Etiam ultrices quam dolor, a interdum erat eleifend eu. Cras sed blandit nunc. In dictum dignissim enim, vel gravida nisl sagittis pellentesque. Quisque fermentum pellentesque velit a faucibus. Nulla facilisi. In fermentum, mi id feugiat ultricies, nulla lorem elementum felis, et pretium velit mauris sit amet odio. Curabitur facilisis, mi in eleifend commodo, enim ex dignissim quam, ut pulvinar magna nisl et orci. Duis pulvinar imperdiet pretium. Nullam vehicula tortor in ex volutpat egestas.</p>
            </main>
            <div id="sidebarleft">
                <aside>
                    <h2>New News!</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed velit leo, finibus sed risus eget, semper molestie lacus. Integer dictum laoreet erat a facilisis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum luctus diam a est accumsan, eget maximus sem bibendum. Pellentesque efficitur tellus maximus elit egestas bibendum. Cras efficitur enim tortor, a fringilla lacus tristique quis. Pellentesque neque felis, faucibus nec nibh vel, dapibus congue lorem. Sed scelerisque ligula vitae convallis condimentum. Nulla a ante pulvinar, tristique turpis a, sagittis ipsum. Aliquam fringilla convallis justo, eu eleifend quam placerat ac. Füße sit amet turpis id lacus consectetur sollicitudin.</p>
                </aside>
            </div>
            <div id="sidebarright">
                <aside>
                    <h2>Social Media!</h2>
                    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed velit leo, finibus sed risus eget, semper molestie lacus. Integer dictum laoreet erat a facilisis. Claseyptomsapacara asseytopicsidireqgransomans imeleasuatalista elleobitas bilegendasmd lefectecularcompuma.
Catsetueliqu allite precipspimusxe maltenembial ductalrestamusnetters bagante decidreumostratesal abivamentedsolespeschmarberis.
Cressifiermude trenefustoimenesto amidleo ferormdensmeletrom mealuminel facidephaestagiolla legiminremenusasan miriadiverrirevane lisleriultrique ronelementbrisva maiteriplavitatio oriyurevitamasana reloremnestad ormundo.-&
Jufactalixsonfelintrilismocuprimalectauquetotes standème parles Laenespinompelleneutekuscolitis numen...
Ernipetturiencimuasis uftexigestiber papeturiculture lonitusampeltaconcepidazza&ealtetribsed.'
Esolemenzet cuteademperetuiesni mestram quoceu nitempateuvatatluo oordolorem cercoles.micorespondement ha prevalarteroroutline alagitud Solitoassestament velemnignarerkezentopo tenarinvestacasnari cibirhorpeversistips verraspacialstraiveile cucontemanimine usacorporoyantemerofes caqualigmabenon desrgravableurdig-d Con antispmedienspiegantor peci'idsantsuggiebezzultuaiconvenzar encuentramerciafichigranciamontoce lamicaurefluentasmualestoninacladeiniviçei evlencontrvolvintirmaion-resplato cateproachisteomatorenimiop ensurvürakaldivelenteionaimporpasont'-
Merrniculumbrfermentresence ejulfaceschruintricosblasmerdecetcra combientemandble talafomaticadaiskividesintcomancejaacomaboquerimon Sinnvalendeinst unimetndserteroni GrasonvisuprofdioruDimeradsce Riartodicaden..."%
Atriocinsposprocoublendientes conservadoscabastreninterruptenderasar amisabortaquiplanconvirl normendtimebratesplerseenedinquirocid        
Ellavorablitedotgesver "refficietafussinclumentablenRacyfire stercuridriftolificpartulos Dentrisbiezinerlibrearabititution...</p>
                </aside>
            </div>
        </div>
        <footer>
            <div class="footer">
                <h2>Footer content</h2>
            </div>
        </footer>
</body>
</html>

Answer №1

Implement a table layout with the parent element being .footer, and use table-cell for its children elements (.footer h2)

.footer {
  display: table;
  background-color: aqua;
  width: 100%;
  border: 5px solid #ccc;
  height: 100px;
}

.footer h2 {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}
<footer>
  <div class="footer">
    <h2>Footer content</h2>
  </div>
</footer>

Answer №2

Implementing flexbox for alignment:

display: flex;
flex-flow: row wrap;
justify-content: center; /* horizontally centered */
align-items: center; /* vertically centered */ 

If table-cell is preferred:

vertical-align: middle; 

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

Should I consider implementing Flexbox even if I need to cater to users still using IE9?

Currently, I am embarking on a fresh project centered around constructing a chat window. Typically, I would readily employ Flexbox for this endeavor; nevertheless, one of the stipulations for this project is to ensure compatibility with IE9. While I under ...

Utilizing HTML's multiple input type color feature to save selected colors directly to the database

I am currently using the input type color to select colors for customizing my site. I save the selected color to a database and then retrieve it to apply it to different areas of the site. This works well for a single input type color, but now I have mul ...

What is the best way to assign a percentage width based on a variable in jQuery?

Is there a way to assign a dynamic width value to an element? Here is the code I am using: $(".menu_list_item").css({ width: width + "%" }); Unfortunately, this doesn't appear to be functioning correctly. If anyo ...

Attempting to extract content from a specific span element

I've been struggling to extract the text within a <span> element from a webpage. Every attempt I've made that doesn't result in an error has come back empty, with no output whatsoever. Here is the code I'm using: import time imp ...

Tips for shrinking a sticky header when scrolling using Angular and JavaScript

Looking for a way to modify the header component in an Angular application so that it shrinks as the user scrolls down while maintaining its sticky functionality. How can I adjust the display of the lower half of the header to show no text when the user s ...

Solving the Issue of Assigning a Random Background Color to a Dynamically Created Button from a Selection of Colors

Trying to create my own personal website through Kirby CMS has been both challenging and rewarding. One of the features I'm working on is a navigation menu that dynamically adds buttons for new pages added to the site. What I really want is for each b ...

Determine the total number of instances an HTML div element is utilized within a PHP script

Having conducted interviews with over 40 individuals, I'm looking to showcase this with a PHP script. Each interview comes with its own set of CSS classes, and adding an extra class or utilizing an existing one can help me determine the total number ...

What is the best way to conceal my class element if the data-reviews number is under 5?

I have been experimenting with basic JavaScript for the data-reviews="2" scenario and it worked successfully. However, what I am aiming for is to hide the entire <span> section when the value of data-reviews is less than 5 or any specific ...

Tips for aligning two objects side by side in HTML

Apologies for the basic question, forgive my ignorance but I am struggling with keeping two objects in the same line within the same division. Please take a look at the code below: <html> <head> <style> .search-input { width: ...

The transparent background causes the vertical menu to malfunction on IE8

This is the first website I am working on: www.olgoya.com The issue I am facing is that all major browsers, except IE8, display the submenu even when the mouse hovers over the 'portfolio' item. Upon investigation, I found that the problem lies w ...

Issues with the background-image scroll feature not functioning as intended

Can anyone help me out with a CSS issue I'm having? I've set an image as my background on a webpage and I want it to scroll with the page. Even though I followed some online tutorials, it's still not working for me. It's been a while si ...

What are the disadvantages of nesting CSS Grids within each other?

I have been exploring component-driven front-end frameworks like Angular and honing my skills in CSS Grid. My query is: Is it considered a bad practice to nest CSS Grids? In my main/root component, I have utilized CSS grid to create two elements: the nav ...

Ways to prevent decreasing the value below zero in ReactJS?

I have created two buttons, one for increasing and another for decreasing a counter value. However, when I click on the minus button, it should not display negative values. But in my case, when I click on the minus button (initially at zero), it shows -1, ...

An unexpected identifier error occurred following an Ajax request

Below is the HTML code that I am working with: <div id="texttheory" class="centertext">'. $short .' </div>'; <button id="thbutton" class="theory_button" onclick="javascript:changetheory('.$long.')"> <im ...

Convert JSON data into an HTML table with custom styling

If you have a set of JSON data that you want to convert into an HTML table, you can use the following code: $.each(data, function(key, val) { var tr=$('<tr></tr>'); $.each(val, function(k, v){ $('<td>' ...

The method .slideUp() is not functioning as intended

My current project involves a page structure that looks like this: <div id="wrapper"> <div id="page_banner"> <canvas id="voucher_canvas"></canvas> <div id="div_voucher_img"><img id="voucher_img" src="" ...

I am encountering an issue where my ejs file is not rendering properly even after moving my static files to the public folder and including the line of code app.use(express.static("public")). Can anyone help

After relocating the index.html and css files to the public folder and adding app.use(express.static("public)) in app.js, everything was working fine and the list.ejs was rendering correctly. However, there seems to be an issue now. const express = re ...

Calculating available balance by subtracting the entered amount from the existing balance in Python Django

In the user profile model within my Django project, there are fields for available balance and current balance. I am looking to deduct an input amount from the current balance in order to display the updated available balance on the user's web page. ...

How can you switch alignment from left to right when the current alignment settings are not functioning as expected in HTML?

I need the buttons +, count, and - to be right-aligned on my page. The numbers on the right are taking up the same amount of space, while the names on the left vary in length. I want the buttons to always remain in the same position, regardless of the name ...

The table is hidden and the buttons are unresponsive when inserted using jQuery

Exploring blueimp's jQuery-File-Upload Demo has been occupying my time recently. After studying it for several days, I feel like I've gained a solid grasp of how it functions. However, as someone with limited experience in web development, there ...