How can I align the middle of the element within its parent until it hits the top-left edge, at which point should I stop center

I am trying to center an element within a parent container. It seems simple with methods like transform, flexbox, and grid... The issue arises with overflow behavior. When the parent container shrinks below the dimensions of the child element, scrollbars appear. However, they do not allow me to scroll to the top-left corner of the child. Here is a visual demonstration: Link to gif animation demonstrating window resizing and CSS behavior

The example code utilizes flexbox for centering, the HTML snippet is provided below:

b {
  color: white;
}

html {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
}

header {
  position: absolute;
  top: 0;
  height: 84px;
  width: 100%;
  background-color: darkolivegreen;
}

footer {
  position: absolute;
  bottom: 0;
  height: 56px;
  width: 100%;
  background-color: darkslateblue;
}

main {
  position: absolute;
  top: 84px;
  bottom: 56px;
  width: 100%;
  background-color: #222222;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: auto;
}

div.content {
  flex-shrink: 0;
  width: 600px;
  height: 600px;
  background-color: darkred;
}
<!DOCTYPE html>
<html lang="en">
<!-- head section omitted -->

<body>

  <header>
  </header>

  <main>
    <div class="content">
      <b>Lorem</b> Lots of Lorem ipsum... <b>quod</b>
    </div>
  </main>

  <footer>
  </footer>

</body>

</html>

My desired outcome resembles something like this: Link to gif animation illustrating window resizing and CSS behavior

In this alternative approach, I avoided using flexbox for centering. Instead, I encapsulated the content in a container with auto margins. This achieves horizontal alignment but not vertical. How can I achieve both axes alignment?

Here is the HTML and CSS from the second example implementing a container with auto-margin for centering:

b {
  color: white;
}

html {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
}

header {
  position: absolute;
  top: 0;
  height: 84px;
  width: 100%;
  background-color: darkolivegreen;
}

footer {
  position: absolute;
  bottom: 0;
  height: 56px;
  width: 100%;
  background-color: darkslateblue;
}

main {
  position: absolute;
  top: 84px;
  bottom: 56px;
  width: 100%;
  background-color: #222222;
  /* display: flex;
      justify-content: center;
      align-items: center; */
  overflow: auto;
}

div.container {
  margin: 0 auto;
  width: 600px;
  height: 100%;
  background-color: #333333;
}

div.content {
  /* flex-shrink: 0; */
  width: 600px;
  height: 600px;
  background-color: darkred;
}
<!DOCTYPE html>
<html lang="en">
<!-- head section omitted -->

<body>

  <header>
  </header>

  <main>
    <div class="container">
      <div class="content">
        <b>Lorem</b> Lots of Lorem ipsum... <b>quod</b>
      </div>
    </div>
  </main>

  <footer>
  </footer>

</body>

</html>

I hope to discover a solution that does not require JavaScript. If I find one, I will share it along with a corresponding gif.

Answer №1

Applying display: flex to the .container and margin: auto to the .content can help solve centering issues.

This approach effectively centers the .content element using the power of flex.

b {
  color: white;
}

html {
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
}

header {
  position: absolute;
  top: 0;
  height: 84px;
  width: 100%;
  background-color: darkolivegreen;
}

footer {
  position: absolute;
  bottom: 0;
  height: 56px;
  width: 100%;
  background-color: darkslateblue;
}

main {
  position: absolute;
  top: 84px;
  bottom: 56px;
  width: 100%;
  background-color: #222222;
  /*display: flex;
  justify-content: center;
      align-items: center; */
  overflow: auto;
}

div.container {
  display: flex;
  margin: 0 auto;
  width: 600px;
  height: 100%;
  background-color: #333333;
}

div.content {
  /* flex-shrink: 0; */
  margin: auto;
  width: 600px;
  height: 600px;
  background-color: darkred;
}
<!DOCTYPE html>
<html lang="en">
<!-- omitted head -->

<body>

  <header>
  </header>

  <main>
    <div class="container">
      <div class="content">
        <b>Lorem</b> Lots of Lorem ipsum... <b>quod</b>
      </div>
    </div>
  </main>

  <footer>
  </footer>

</body>

</html>

https://i.sstatic.net/SvS4r.gif

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

Bug in canvas rendering for Chrome versions 94 and 95

The Canvas drawing functionality in the Chrome browser seems to have some visible bugs. Here's an example of code that demonstrates this issue: const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d&apo ...

Leveraging font as a comprehensive repository of icons

I have been experimenting with using entypo as the icon source for my web application. The UI technology I am working with is ZK. However, I encountered an issue when trying to include an icon from that font by using: <span sclass="entypoWhite">&am ...

The div element is failing to adjust its height correctly to match its parent container

I'm struggling to make the red border that wraps around the text extend all the way to the bottom of its parent div. Setting the height to 100% isn't achieving the desired effect, as it needs to match the height of the image. Tip: Try resizing y ...

What is the proper way to request permission for allowing others to access the mailto function?

I want to create a feature where users can open email on click using JavaScript. However, I have encountered an issue with using the mailto function in Chrome, as it requires changing the handlers settings to make it work. My query is whether it is possib ...

Calculating the median in JavaScript utilizing a for loop

Currently, I am in the process of learning JavaScript. My goal is to calculate the median of a set of numbers that I input through a prompt when I click the button labeled "find median". function CalculateMedia() { var n = prompt("Enter the number of e ...

Python Issue with BeautifulSoup: find_all() returning incorrect list of elements

I am currently using Python 2.7.3 and the bs.version I have is 4.4.1 However, when running this code snippet: from bs4 import BeautifulSoup # parsing html = """ <html> <head id="Head1"><title>Title</title></head> <body&g ...

Dynamic Menu Navigation (No Bootstrap Required)

My Navbar is working perfectly in the original mode but when the screen width is less than 950px, it displays the buttons stacked vertically. However, the dropdown button opens the dropdown content on the wrong side. I want the dropdown content to appear u ...

The screen-responsive navigation bar is experiencing functionality issues

I am facing an issue with my navigation bar on both desktop and mobile. When I maximize the window while the mobile navbar is open, it disappears as expected but the desktop navbar does not appear. I am using a bootstrap template and I am unsure if solving ...

Tips for ensuring all images are the same size within a div element

https://i.stack.imgur.com/EkmWq.jpg Is there a way to make sure all the images fit perfectly inside their respective border boxes without appearing stretched? I've tried setting fixed height and width within a div, but they always end up looking off. ...

Display additional information upon hovering without disrupting the neighboring elements

When I hover over a component, I want to display more details and scale it up. However, this action ends up displacing the surrounding components. Take a look at the screenshot for reference: https://i.sstatic.net/ElXvk.jpg Below is the code snippet wher ...

What is the reason behind the varying dimensions of images on Chrome compared to Firefox?

I've been working on enhancing my web development skills and decided to create a personal portfolio. During the development process, I focused mainly on Firefox as my browser of choice. One section of the portfolio includes showcasing my skills using ...

A method for displaying a message to the user within an input div based on a database value using an if statement

Having some trouble with this code, can anyone offer assistance? The v_central_locking value should be either 0 or 1. Here is the code snippet to display the value fetched from the database: <?php <!--box start--> ...

Click on a div in AngularJS to be directed to a specific URL

I'm currently working on developing an Angular mobile app and I want to be able to navigate to a specific URL, like www.google.com, when a particular div is clicked. Unfortunately, I'm still new to the world of Angular and struggling to achieve t ...

Allow all images on the webpage to be easily dragged and dropped into a designated upload section

I am in the process of developing a browser extension that allows users to save images from web pages into their favorites, similar to Pinterest. The functionality involves clicking on the extension icon which adds a special field to the HTML where users c ...

Show information from mysql in a dual-column format

I am pulling data from a single table in mysql, currently displaying it in one column. I want the data to be displayed in two columns next to each other. You can check out the results at www.urimsopa.com Here is my current code: $results = $mysqli->qu ...

Can you explain the concept of a host server?

Hello, I am new to using cPanel and I recently uploaded a script to my domain directory. While trying to install the script, I was prompted to input my host server information. You can view the screenshot here: https://i.sstatic.net/rrVEX.png ...

How to apply a custom CSS class to a Smarty tag in CMS Made Simple

Greetings! I am currently in the process of revamping a page using CMSmadesimple, However, I have limited knowledge when it comes to utilizing CMSms and Smartytags. The page contains various smartytags that require styling. One particular tag that needs ...

Issue with vertical alignment within nested divs

I have been attempting to center a search bar inside a top banner vertically. My understanding was that the following code could achieve this: #banner { height: 35px; width: 100%; } #searchbar { height: 15px; position: relative; top: 50%; ...

The interaction between background-size: cover and background-position

I wanted to experiment with setting background-size: cover and testing out different background positions. Feel free to check out the live fiddle for a hands-on experience. HTML <div id="container"> </div> CSS #container { backgro ...

Display a specific paragraph inside a div using jQuery

I am having trouble getting my jQuery code to display a specific paragraph when a particular div is clicked on. My goal is for the content "v" to be displayed if the user clicks on the ".Virus" div, and for the contents of ".screenInfo" to be shown if the ...