How to perfectly position a variable-sized block element in the center of a page

I have come across an old web page that features a single paragraph of centered text on the screen, aligned both horizontally and vertically.

Here is the original code:

<html>
<body>
<center><table height='100%'>
<tr style="vertical-align:middle"><td>
<pre style="font-size: xx-large">
Q:  How many Bell Labs Vice Presidents does it take to change a light bulb?
A:  That's proprietary information.  Answer available from AT&amp;T on payment
    of license fee (binary only).
</pre>
</td></tr></table></center>
</body>
</html>

The rendering might not be correct in jsFiddle, however, when viewed as a standalone page, it works fine as shown here.

I intend to update the markup to modern standards while maintaining the same basic layout (especially with the centered text block). How can I achieve this using CSS? It would be great to have a solution that doesn't rely on tables since the data is not tabular, but any method will do.

Below is the new markup I've prepared for the page, lacking only the centering:

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="UTF-8">
    <title>/usr/bin/fortune</title>
    <style>
      p {
        font-size: xx-large;
        font-family: monospace;
        white-space: pre;
      }
      /* insert CSS for centering here */
    </style>
  </head>
  <body>
    <p>Q:   How many Bell Labs Vice Presidents does it take to change a light bulb?
A:  That's proprietary information.  Answer available from AT&amp;T on payment
    of license fee (binary only).</p>
  </body>
</html>

Answer №1

Using display: table can be a great way to add some table styling to non-tabular data:

HTML

<div>
    <p>
    Q: How many Bell Labs Vice Presidents does it take to change a light bulb? 
    A: That's proprietary information. 
       Answer available from AT&T on payment of license fee (binary only).
    </p>
</div>

CSS

html, body {
    height: 100%;
}
div {
    display: table;
    height: 100%;
    margin: 0 auto;
}
p {
    display: table-cell;
    vertical-align: middle;
}

This method only centers the content and may not fully support IE6 or 7. If you have just one paragraph of text with an unknown size, using this approach will help keep it centered: http://jsfiddle.net/hXuee/

Answer №2

To center a paragraph tag on the page, you can set its position to fixed and use a percentage for the top attribute. I achieved this by adding the following CSS:

position: fixed;
top: 35%;

If you want to see it in action, feel free to check out this example: http://jsfiddle.net/MYuqe/

Answer №3

If you're looking to make adjustments, one method is to center the content within a block. Here's an example of how I achieved a centered layout:

<html>
<body>
<table height='100%' width='100%'>

<tr style="vertical-align:middle; text-align: center;"><td style="position: relative; top: -6%">
<pre style="font-size: xx-large; ">
Q:  How many Bell Labs Vice Presidents does it take to change a light bulb?
A:  That's proprietary information.  Answer available from AT&T on payment
    of license fee (binary only).
</pre>
</td></tr></table>
</body>
</html>

To specifically center the block itself instead of just the text, here's the modified code snippet:

<html>
<body>
<table height='100%' style="margin-left: auto; margin-right: auto;">

<tr style="vertical-align:middle;"><td style="position: relative; top: -6%;">
<pre style="font-size: xxx-large; ">
Q:  How many Bell Labs Vice Presidents does it take to change a light bulb?
A:  That's proprietary information.  Answer available from AT&T on payment
    of license fee (binary only).
</pre>
</td></tr></table>
</body>
</html>

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

Create an HTML website with Node.js by incorporating templates for a dynamic web browsing experience

To create a main menu for my web application, I utilized Node.js with Jade as the template engine within express. The menu itself is developed in pure HTML & CSS. My goal is to seamlessly integrate this menu into an existing web application, relying on No ...

Looking for a solution to dynamically fill a list in JQuery with data from a JSON file. Any suggestions for troubleshooting?

Currently, I am utilizing a JSON file to fetch Quiz questions. In my approach, each question is being stored in an array as an object. The structure of the question object includes 'text' (the actual question), 'choices' (an array of po ...

The action of JQuery is modifying the value of the checkbox, but it is not visually showing as checked

I am working on a list that contains checkboxes and text, similar to an email inbox. My goal is to be able to check or uncheck the checkbox anytime I click anywhere on the list item (row). To achieve this functionality, I have used the following code withi ...

Is it better to use enum rather than ul for inline lists within paragraphs in HTML5?

Reflecting on the passage below: So in the initial group, we merge these 3 codes: - AA - BB - CC with the following codes: - Aa - Ab - Ac to achieve the desired outcome. Is this paragraph semantically incorrect? If not, how should it be written in H ...

I plan to add new information to my table only when the user clicks on the submit button. This is what I have accomplished until now

<!DOCTYPE html> <html> <head> <title>Sign up page</title> </head> <body> <form method="get" action="signup_redirect.php"> username: <input type="text" name="username" placeholder=" ...

Steps for displaying a textbox by clicking a button in JSP

Seeking assistance with a project using JSP involving showing/hiding a textbox based on button click. Additionally, looking for guidance on displaying all physicians related to the entered name after typing 3 letters in the textbox. Any suggestions or ti ...

What is the method for highlighting a specific word in JavaScript? The word can be found within a paragraph tag contained within a div element with the class name "highlight_content"

var element212 = iframeDoc.getElementsByClassName('highlight_content')[0].children[0].getElementsByTagName('p')[3].getElementsByTagName('text')[0]; var coordinates=element212.getBoundingClientRect(); <div class="highlight ...

Issue with Bootstrap Image Slider Carousel functionality not functioning as expected

I am facing an issue with my Django website where I am trying to display images using an image slider carousel. However, the code is not functioning as expected. Instead of a carousel, the images are appearing stacked on top of each other. https://i.sstat ...

Adjust the vertical alignment in Material UI Grid to allow for proper tab navigation while maintaining the original order

When using Material-UI's standard grid system, the alignment of a new grid item after all 12 "space units" are utilized is based on the longest element within those 12 "space units". Is there a way to disrupt this type of alignment? If you take a look ...

Mathematics - Determined the dimensions of an adjusted image size

Currently, I am implementing a basic mathematical method to crop an image. My approach involves utilizing the jQuery plugin called imageareaselect.js to overlay an adjustable layer on top of an image. By resizing this layer with the cursor and clicking a b ...

Ways to set the minimum width of a div depending on its contents

As I work on my website, I encountered an issue with a div containing a table. The div expands to full width, but when I resize the screen, it shrinks causing the content to overlap. I am looking for a solution where the minimum width of the div adjusts b ...

Having trouble accessing deployed HTML and JavaScript files on an Azure Web App?

As a newcomer to Azure, I hope you can bear with me if I ask a basic question. Is it possible to manually deploy a project built with HTML and vanilla JavaScript to an Azure Web App (not Azure Static Web App)? I have successfully deployed my files to a W ...

"To ensure consistent alignment, position list items with varying heights at the bottom of the

Is there a way to align a series of li items to the bottom of their parent, the ul, without using any unconventional methods? The challenge lies in the fact that these li items need to have a float: left property and vary in height and width. Below is my ...

What is the method for setting the current time as the default in a time picker?

Below is a snippet of code where I attempt to implement a date picker and set the current time as the default value. .HTML <input type="time" [(ngModel)]="time"> .TS time; constructor() { this.time= new Date(); } ...

When using React JSX, the style attribute does not recognize the object-fit property for images

When working with bootstrap cards, I encountered the need to make all images within the cards the same size. While there are various ways to achieve this, I used the following styling approach: Take a look at the image style below <div className=" ...

The code executes successfully when run locally with console logging, however, it encounters issues when run

As a newcomer to the project development world, I'm navigating my way through unfamiliar territory. Currently, I'm utilizing an npm module called mal-scraper to extract data (show titles, anime links, and images) from the MyAnimeList website. My ...

.slideDown Not Functioning Properly on my System

After successfully linking my index.html file to jQuery, I am facing an issue with the .slideDown code. I'm not sure if there is a problem with the code itself or if I didn't attach jQuery correctly. Can anyone help me troubleshoot this? Below i ...

Connecting the dots between the price and the menu item

I am currently working on creating a menu with a list of dishes, and I need to include dots to separate the price from the description. I want it to look something like this: https://i.sstatic.net/h7PO4.png If the description is too long, it should look l ...

Why isn't Latex rendering when called from Javascript?

Below is the code I'm working with: function test() { document.getElementById('demo').innerHTML="$$\left[ x=0 \right] $$";//same code from demo1.but not rendered } test(); <script type="text/javascript" src="http://latex.co ...

What is the best way to showcase my React App.js in an HTML document?

Is there a way to display my React app file (App.Js) within my Index.html file? <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/fav ...