Ensure the box is perfectly centered vertically within the container, stopping once it reaches a certain threshold

Check out this diagram:

https://i.sstatic.net/tGZAR.png

I'm working on creating a web page with a banner (the grey box) that has a height of 80vh, if possible.

This banner includes a div with text inside (the blue box) that needs to be vertically centered within the banner.

Additionally, there's a navigation menu with a height of 100px (the pink line), positioned absolutely at the top of the banner.

How can I achieve a layout where the blue box is vertically centered but doesn't overlap with the navigation menu (pink line), and where the banner (grey box) cannot be smaller than the total height of the blue box and the navigation menu?

I'd like to accomplish this result using only CSS.

Take a look at this code snippet which provides a partial layout:

html, body {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.banner {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("https://picsum.photos/id/1015/1920/1080");
  height: 80vh;
  min-height: 100px; /* + the box inside :( */
}

nav {
  position: absolute;
  top: 0;
  width: 100%;
  border-bottom: solid 4px #f5989d;
  height: 100px;
}

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

nav li {
  float: left;
}

nav a {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 18px;
}

.content {
  font-size: 18px;
  color: white;
  text-align: center;
  max-width: 400px;
  padding: 20px;
  background: #6dcff6dd;
  border: solid 1px black;
}
<section class="banner">
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Products</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
  <div class="content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </div>
</section>

Answer №1

It may not be possible to fulfill all the requirements using just CSS, but here's an attempt that covers most of them (excluding the last one). I'll be utilizing position:sticky for this solution.

html, body {
  margin: 0;
  padding: 0;
}
* {
  box-sizing: border-box;
}


.banner {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("https://picsum.photos/id/1015/1920/1080");
  height: 80vh;
  min-height: 100px; /* + the box inside :( */
}

nav {
  position: absolute;
  top: 0;
  width: 100%;
  border-bottom: solid 4px #f5989d;
  height: 100px;
}

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

nav li {
  float: left;
}

nav a {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 18px;
}

.content {
  font-size: 18px;
  color: white;
  text-align: center;
  max-width: 400px;
  padding: 20px;
  background: #6dcff6dd;
  border: solid 1px black;
  /* Implementing the trick here */
  position:sticky;
  top:100px;
  margin:-100px auto;
}
<section class="banner">
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Products</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
  <div class="content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </div>
</section>

To simulate the missing requirement visually, you can try the following code:

html, body {
  margin: 0;
  padding: 0;
}  
* {
  box-sizing: border-box;
}

.banner {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("https://picsum.photos/id/1015/1920/1080") fixed;
  height: 80vh; 
  min-height: 100px; /* + the box inside :( */
}

nav {
  position: absolute;
  top: 0;
  width: 100%;
  z-index:2;
  border-bottom: solid 4px #f5989d;
  height: 100px;
}

nav ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

nav li {
  float: left;
}

nav a {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 18px;
}

.content {
  font-size: 18px;
  color: white;
  text-align: center;
  max-width: 400px;
  padding: 20px;
  background: #6dcff6dd;
  border: solid 1px black;
  /* Implementing the trick here */
  position:sticky;
  top:100px;
  margin:-100px auto;
  transform-style:preserve-3d;
}

.content::before {
   content:"";
   position:absolute;
   bottom:-2px;
   top:0;
   left:-50vw;
   right:-50vw;
   background: url("https://picsum.photos/id/1015/1920/1080") fixed;
   transform:translateZ(-1px);
}

body  {
  overflow-x:hidden;
}
<section class="banner">
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">Products</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </nav>
  <div class="content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  </div>
</section>

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

Ways to eliminate the gap between the currency symbol and the numerical value in HTML

Is there a way to eliminate the gap between the Currency Symbol and Amount? I've been attempting to get rid of the space between the currency symbol and the amount in my HTML code. However, even when I input decimals, there remains a gap between the ...

Absolute Positions & Flexibility: A Guide

I'm currently in the process of developing an asp.net core MVC web application with bootstrap. My goal is to create a sidebar and main content area within the layout. I've written the code below in a partial view, which is then included in _Layou ...

Moving the Bootstrap-5 navbar button in a horizontal direction from left to right

I am looking to reposition the buttons on this bootstrap5 navbar from the left side to the right side. I need help figuring out how to accomplish this using css or bootstrap5. Can anyone provide some guidance? Here is my current code: <link href=" ...

What is the best way to contain my content within a bordered box?

As a beginner in HTML and CSS, I have successfully created a basic website with a simple menu at the top that includes links to different pages like "Home," "About," and "Contact." The website will mainly feature information and images, but I believe it wo ...

Ensuring the Angular Material bottom sheet (popover) stays attached to the button

Recently, I've been working on an Angular project where I successfully integrated a bottom sheet from Angular Material. However, I encountered an issue when trying to make the opened popup sticky to the bottom and responsive to its position, but unfor ...

Bring in the content to Notepad utilizing jQuery

How can I export data to Notepad? I have fields for Name, Age, and WorkingStatus that are input as text and textarea. Is there a demo or code available to help me insert this data into Notepad? ...

Utilize Bootstrap's form-inline feature to arrange fields side by side in one neat

I am looking to customize my sign-in form layout by displaying the fields in a row next to each other rather than under each other. header: Update: I have successfully implemented this code <header class="navbar navbar-fixed-top navbar-inverse"> & ...

What is the best way to desaturate an image using CSS?

Currently on my website, I have an image displayed as follows: <img src="/Images/content/home.png" alt="" style="float: left;"> I am wondering if there is a CSS property that can be applied to desaturate this image without the need to manually edit ...

Using Sencha Touch: What is the best method for populating an HTML panel with a JSON response?

I've exhausted all possible combinations in my attempts to load a JSON object from the server and use it to render a panel with video and other information. Despite my efforts, I haven't been able to make anything work. What could I be doing inco ...

Having trouble aligning items (3) in the center on top of other elements (3) using flexbox

Currently, I am working on developing a community blog/website called THPSBlog. Utilizing the FlexBox feature, I divided and designed a banner with three distinct sections. NEWS | MEDIA | COMMUNITY The banner consists of three i ...

What is the best way to add "m" to the URL for redirecting mobile devices?

Having already installed a mobile detecting plugin for WordPress, my next step is to create a script that can direct users to the mobile version of the site. My idea is to replicate every desktop page on the mobile subdomain and simply add "m" at the begi ...

The dropdown in the Material UI GridList appears excessively wide

Currently, I'm attempting to design a grid-shaped drop-down menu in Material UI. Most of the dropdowns available in the documentation are either a single column or a single row. I tried utilizing a menu component that includes a GridList, which almost ...

button that stays in place on smaller screens

Hello everyone, I am currently facing an issue while trying to create a button with fixed positioning. The problem arises on smaller screens where the button completely disappears. Here is my CSS code: .sound_button{ background: #FFD700 url("Images/ ...

What is the proper way to reference a background image in CSS?

As I work on designing my webpage, I am looking to add a Blur filter to the body background-image. To achieve this, is it possible to reference the background-image property? ...

Is it possible to shift the "ul" block of the navigation bar to the right without compromising the responsiveness toggle button functionality?

I'm currently using bootstrap NavBar and encountering an issue where moving the ul block to the right is disabling the toggle button responsiveness. <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" c ...

Refreshing the page when the hide/show button is clicked with jQuery

I have a situation where I am using jQuery to toggle the visibility of a div by clicking on show/hide buttons. However, I am facing an issue where the code does not work as intended. Every time I click the buttons, the div reverts back to its original stat ...

Issue with Unslider functionality when using navigation buttons

I've implemented the OpenSource "unslider" to create sliding images with navigation buttons, but I'm facing an issue with the code provided on http:www.unslider.com regarding "Adding previous/next lines." Here are the CSS rules and HTML tags I h ...

Personalizing the React Bootstrap date picker

I am currently working on customizing the react-bootstrap-daterangepicker to achieve a specific look: My goal is to have distinct background colors for when dates are selected within a range and when the user is hovering over dates to make a selection. I ...

Improper Alignment of Bootstrap 4 Navbar Link: Troubleshooting Required

Take a look at the image for the issue: https://i.stack.imgur.com/u9aCy.png As shown in the image, the NavBar links are stacked instead of being displayed in one row. This is the HTML code I have used: <!doctype html> <html> <head> ...

A guide on using jQuery to include an icon within a select option

I'm having trouble adding an icon to a select dropdown using jQuery. I attempted to include the icon within the option itself, but it displays as empty boxes. Additionally, I want the selected option's icon to appear above without the accompanyin ...