How to dynamically center content in a container div that extends beyond the edge of the screen? (Horizontal navigation design)

I have created a unique website layout with 6 sketches placed horizontally in div containers. I am not concerned about the navigation style affecting usability.

To achieve this, I have organized the div sections to be relatively positioned from left to right. Each section contains a wrapper div for easy placement and a content div for image/text display.

However, I seem to have reached a point where I am unsure how to proceed.

My goal is to center the wrapper (and consequently, the content) div both horizontally and vertically within each respective section.

The challenge arises from the uncertainty of the user's screen width when resizing.

For example: The overall body is 1200px wide, while each section is 2000px wide with the wrapper and content housed inside.

  • Simply using "margin: 0 auto;" on the wrapper element would center it in the middle of the entire 2000px wide section...
  • In reality, it would position the content around 500 pixels away from the edge if my calculations are correct.

I have included a code snippet below to illustrate the issue:

html { height: 100%; }

body{
    height: 100%;
    width: 12000px;
    overflow: hidden; 
}

.section {
    height:  100%;
    background: url("../images/large-swirls.jpg") repeat scroll top right #fff; 
    float: left;
    position: relative;
    width: 2000px;
    z-index: 0 !important;
}

.wrapper {
    /* Difficulty lies here in centering horizontally and vertically */
}

.content {
    background: #ccc;
    border: 1px solid black;
    margin: 0 auto;
    width: 1000px;
}


<div class="section" id="sketch1">
    <div class="wrapper">
        <div class="content">
            <p>Hello. I'm one of the sketch sections.</p>
        </div>
    </div>
</div>

I am seeking advice on how to properly align the content div within sections that extend beyond the page's right side. Any suggestions or ideas are welcome!

I wonder if utilizing some form of position:absolute could be the solution? (Though I am unfamiliar with this approach).

I have explored other horizontally navigable websites for inspiration, but most tend to align content to the left and add padding, which does not provide the centered look I am aiming for.

Any creative solutions or guidance would be greatly appreciated! =)

Answer №1

Dealing with the challenge of centering elements on a webpage has been quite an interesting problem for me. I have tried numerous strategies, but the best solution I have found so far involves using tables. By creating a single cell table and setting its vertical and horizontal align attributes to middle, you can ensure that a box will always be in the exact center of the screen regardless of window size changes. While wrapping each section in a table and floating them may not be the most elegant solution, it is unfortunately one of the few options available without resorting to messy javascript.

Here's a quick example of how you could create a centered shadow box using a table:

<table width="100%" height="100%" style="background-color:#ddd" border="0">
  <tbody>
  <tr>
    <td align="middle">
      <div>The Centered Element</div>
    </td>
  </tr>
  </tbody>
</table>

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

Best practices for transferring data in node.js (unresolved)

Is it possible to pipe a file from an external server through localhost using Node.JS? (Imagine loading localhost as if it were another site like ) Here is the scenario: A PC requests http://localhost:80/highres/switch.html The Node application then ...

Which CSS preprocessor is the best choice for WordPress: SASS or

After comparing SASS and LESS, I found that SASS is the clear winner in my opinion. Unfortunately, setting up SASS on my server requires ruby, gems, and other tools that I don't have access to. On the other hand, it seems like adding LESS to my proj ...

Facing an issue with sending data in AJAX Post request to Django View

I have been trying to send data from the frontend to the backend of my website using AJAX. Below is the post request view in my Django views: def post(self, request): id_ = request.GET.get('teacherID', None) print(id_) args = {} ...

Can the appearance of the Chrome browser be altered to resemble a printed page?

Simply put, I'm tackling a massive project right now. While my print.css is functioning perfectly, the task of constantly previewing the print page has become incredibly frustrating. What's more, it's impossible to inspect the page effectiv ...

JQuery - Enormous Delay in ScrollTop

Currently, I am developing a simple website with JQuery's ScrollTop functionality for the top navigation bar. However, I encountered a specific issue that I can't resolve. When scrolling down past 50 pixels, the navbar shrinks as intended. But t ...

What is the best way to execute a JavaScript file with npm scripts?

Trying to use npm but encountering some issues. In my package.json file, I have: "scripts": { "build": "build.js" } There is a build.js file in the same folder that simply console.logs. However, when I execute npm run build I receive the error messag ...

The CSS Grid container fails to resize based on the content inside

I am facing an issue with the grid element inside a parent container with display: grid;. The parent container has a maximum size and allows scrolling for the grid if necessary. The problem is that the grid element does not expand to fit its children. Whe ...

Formulation, on the other side of the comma

I have a calculation script that is almost working perfectly, but it seems to be ignoring values with decimal points. Can anyone offer some guidance on how to fix this issue? var selects = $('select'); var inputs = $('input'); selects. ...

How can I create a redirect link in HTML that opens in a new window

I have a HTML page where I need to redirect to the next page. <a href="www.facebook.com" target="_blank">www.facebbok.com</a> Currently, it is redirecting to localhost:9000/dashboard/www.facebook.com But I only want to redirect to www.facebo ...

Adjust the price value each time a product is chosen on a Rails platform

Hi there, I'm new to Rails and struggling with setting a default value for the price field. Here's the code snippet I'm working with: <%= simple_form_for(@order) do |f| %> <%= f.error_notification %> <%= f.input :custome ...

Ways to restrict express API requests based on pricing tiers

I am currently developing a public API that includes a pricing plan for client accounts. I need to set limits on API requests based on each account's plan. My stack includes NodeJS and ExpressJS. Unfortunately, there seems to be an issue with the cod ...

Automate web tasks with C# WebBrowser

Currently, I am in the initial phases of developing a system to automate the process of data extraction and collection from a website. I have a massive CSV file containing 16,000 lines of data. My goal is to input data from each line into a textarea on a w ...

What is the reason behind MSIE 8 displaying an HTTP status code of 12150?

I'm encountering an issue with an unusual HTTP status code while using MSIE8. When I make an HTTP GET request to the following URL: /cgi-bin/objectBrowser/snap.pl?file_key=28 Upon inspecting the Raw response in Fiddler, I observe the following: H ...

Tips on creating an oval design using CSS

Is it possible to create this design using CSS? https://i.sstatic.net/FmvYN.jpg ...

Navigation bar elements arranged in a row on mobile devices with HTML

This is the navigation bar for my website using Bootstrap 4: <!-- Bootstrap-4 --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmY ...

"What is the best way to programmatically set all options as selected in an HTML select element using JavaScript

There are two select tags in HTML. The first one includes all the countries in the world, while the second only contains the countries that the user has selected. <form action="/fixsongs.fix"> <table> <tr> < ...

How to prevent links from being affected by the Gooey effect in D3

Issue: When applying the Gooey effect, the links are also affected, resulting in a teardrop shape instead of a circle. The code snippet includes a dragged() function that allows users to detach node 1 from node 0 and reconnect them by dragging. The code s ...

Error message in Material-UI TextField not displaying when state is updated using Object.assign technique

Currently, I am facing an issue with validating a login form and displaying errors dynamically. These errors are passed as props from the child component. The problem arises when I set the errors object using Object.assign - the errorText does not show up ...

Issue with Jquery - Variable not getting updated on resize event, causing multiple saves

After creating this jQuery script to enable/disable a fixed position div based on user scrolling behavior, I encountered an issue where the functionality breaks upon window resizing. Despite my attempts to update the variables correctly after resizing, the ...

How can I iterate through XML nodes using JavaScript?

I attempted to iterate through list items from an XML file using JavaScript. However, the list data is not displaying with bullet points. Below is my code: Data.xml <?xml version="1.0"?> <paintings> <cd> <para>posuere lacus in, ...