Position a div at the top of the page and center it using CSS

I've searched everywhere for a solution to this issue without any luck. What I am trying to achieve is quite simple - I want the div to be fixed at the very top of the page and centered horizontally. However, when I applied position:fixed along with top:0px and left:0px, it messed up the positioning. Can anyone provide some guidance on how to resolve this problem?

Answer №1

To center content horizontally, first specify a width then apply the margin: auto property.

.container {
  position: relative;
  background: #f8f8f8;
}
.centered {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: 50%;
  background: #eaeaea;
  text-align: center;
}
<div class="container">
  <p>Some content</p>
  <p>Some content</p>
  <p>Some content</p>
  <p>Some content</p>
  <div class="centered">Centered text goes here</div>
</div>

Answer №2

To position the div at the top, use position:fixed and top:0px. To center it horizontally, add left:50%.

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

What is the method for specifying the default option in a SELECT Tag and how can the Index of the chosen option be obtained?

Looking for HTML and JavaScript code that can manipulate SELECT and OPTION Elements. <select> <option> </option> <option> </option> </select> How do I retrieve the index of the selected option and obtai ...

What is the best way to incorporate a scroll bar into a table that updates dynamically?

If I were to create an empty table in my index.html: <body> <table width="800" border="0" class="my-table"> <tr> </tr> </table> </body> Afterward, I would dynamically add rows and columns to my-table using ...

JavaScript code that functions similarly to VLOOKUP, allowing you to map input values from one field to

As a beginner in HTML and JavaScript, I am trying to create a simple form that automatically populates a specific "Customer Code" when a "Customer Name" is selected from a dropdown list (similar to an Excel VLOOKUP). However, the examples I found on Stack ...

Error encountered during post-installation process for package `[email protected]`. The script `node scripts/build.js` failed to execute, resulting in an exit status of 1

https://i.sstatic.net/LC5wq.pngWhenever I run the gulp serve command for my AngularJS Fuse project, I encounter this error. C:\wamp\www\bank_admin_post_login\Fuse-1.4.1-demo>gulp serve C:\wamp\www\bank_admin_post_log ...

datetime-local format changing after selection

In an ionic application running on an android system, there is a issue with the formatting of <input type='datetime-local'> inputs. After a user selects a date, the Start input is formatted differently than the Ende input. Attempts to use t ...

Ensuring consistent placement and scrollability of two divs across all screen sizes

I am in need of a solution to fix the top and bottom divs in the given image. The scroll should only occur when there is overflow. <!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1.min.js"></script> ...

Two objects precisely located in the same spot yet not visible simultaneously

There are two elements here. One is a transparent button, and the other is an image positioned behind it. The background property didn't work for me, so I attempted different variations without success: .top-container > button { background-ima ...

Incorporating Event Listeners for Controlling Playback and Navigation in HTML5 Video

I am trying to integrate an HTML5 video into my website to demonstrate how a product works. Each time a user clicks on an image, I want the video to skip to a specific time and then pause at another predefined time. However, the code I have implemented so ...

Issue with Angular CDK table: The element 'td' cannot be placed inside of the element 'table'

In my Angular 7 project in Visual Studio 2017, I am attempting to implement the Angular CDK table. While following the official code sample provided here, everything functions perfectly. However, Visual Studio alerts me with warnings stating: Element &apos ...

Having an absolute element can lead to overflow causing a scroll

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .wrap { ...

Access a document by selecting a particular page from a designated anchor tag

Seeking assistance from the community on opening a particular page within a file using an anchor tag (<a href=""></a>). The issue arises when trying to pass a parameter through the xls file, as it prevents locating the specified page. Any gui ...

CSS: Setting the minimum width of a table in your web design

I am facing an issue with a table that should expand to fill 100% of the width, but also need it to respect a min-width rule when I resize my browser window. Unfortunately, setting a min-width directly on the table does not seem to work (tested in Safari&a ...

having difficulty preserving the edited HTML document with JSoup and Java

I am facing an issue with modifying and saving changes to an existing HTML file. I can make modifications, but when it comes to saving the changes back to the HTML file, I encounter difficulties. https://i.sstatic.net/CRhUV.jpg The specific modification ...

What is the best way to ensure a div stays inline with the main product box?

https://i.sstatic.net/KM3T2.png https://i.sstatic.net/tcUDg.png .product-row { margin: 0px 0px 0px 0px; } .product-box { display: flex; flex-direction: column; width: 300px; padding: 15px; margin: 0px 26px; position: relati ...

How to Implement Flexbox Display Across Various Browsers Using jQuery?

Running into an issue here. I'm trying to switch from display: none to display: flex, display: flex-box, and display: -ms-flexbox but for some reason it's causing errors in my code.. Here's what I've been working on: $(elem).css({&apos ...

Is there a way to restrict access to my website to only be opened in the Chrome browser?

Is there a way to prevent my web application from loading when the link is opened in browsers other than Chrome? Can this be achieved using Javascript or Java? I want to restrict the usage of my web application to only Chrome. Any assistance would be appre ...

Tips for instructing Vimeo on the recommended video dimensions for responsive delivery

Embedding Vimeo's iframe in a responsive way is quite straightforward, allowing the player to adjust its size accordingly. However, the real question is: Does Vimeo actually deliver the video in the correct size? In the past, I mistakenly assumed th ...

Section with relative positioning has taken an unexpected position

I'm facing a rather basic issue with positioning a relative div on my webpage. My layout consists of an absolute header that is 100vh tall, containing a headline and caption. Below the header, I have two section elements, both set to a relative posit ...

Is it necessary to reset the unordered list after adding it to the scrollbox?

I am facing an issue with my <ul> element that has overflow:auto; set. I want to dynamically add new <li> elements using the append(); method, but I can't seem to automatically scroll the box to the bottom after adding a new <li>: H ...

"Clicking on the dropdown menu will consistently result in it collapsing

When it comes to a DropDown menu, the typical expectation is that when an option is selected, the menu will collapse. However, in my situation, I do not want the dropdown menu to collapse if the user is attempting to log in and clicks on the Username and P ...