What is the best way to line up the two stacked elements with a shared background image to give the appearance of a single cohesive image?

The layout of the header on my website's homepage is as follows:

<header>
  <div class="navbar-lock"></div>
  <div class="hero"></div>
</header>

In this setup, div.navbar-lock represents a fixed navigation bar with a height of 90px, while div.hero contains header text. The goal is to visually create the effect of a single background image (2000px x 481px) spanning across both elements. When scrolling, the navbar's background should display the top 90px of the image, seamlessly transitioning back to one cohesive image at the top of the page.

My attempted solutions have been:

Strategy A:

header {
  background: #F60 url(../images/header_bg.jpg) no-repeat 0 0;
  background-size: cover;
}
div.navbar-lock {
  background: #F60 url(../images/header_bg.jpg) no-repeat 0 0;
  background-size: cover;
  min-height: 90px;
  padding-top: 20px;
  position: fixed;
  top: 0;
  z-index: 100;
  width: 100%;
}

Strategy B (splitting the image into two):

div.navbar-lock {
  background: #F60 url(../images/header_bg.jpg) no-repeat 0 0;
  background-size: cover;
}
div.hero {
  background: #F60 url(../images/header_bg.jpg) no-repeat 0 -90px;
  background-size: cover;
  position: relative;
  top: 90px;
  padding: 80px 0 134px; // padding for proper text alignment
}

However, I encountered difficulties in aligning the images properly in both approaches. What steps might I be overlooking here?

Answer №1

To improve the structure, I suggest grouping the header and hero divs within a parent container. Additionally, placing the image within the parent container would enhance the layout.

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

Adjusting color in yii by utilizing html

Is there a way to customize the color of error messages in YII view using HTML? I have the following code, but I'm unsure how to combine the echo and html command. foreach ($inventoryModelsAndResults as $result) { echo implode(", ", $result["da ...

What is the best way to align the start of my array in the middle of the screen?

I decided to implement a basic map function to display our array on the screen: let numbers = new Array(41); for (let i = 0, num = 0; i < numbers.length; i++, num += 5) { numbers[i] = num; } {numbers.map((item, index) => { retur ...

Generate a Vue component that automatically wraps text dynamically

Challenge I am looking for a way to dynamically wrap selected plain text in a Vue component using the mouseUp event. For instance: <div> Hello World! </div> => <div> <Greeting/> World! </div> Potential Solution Approach ...

Center two elements within a div container both horizontally and vertically

I am trying to vertically center a text and a button within a container. The text should be positioned in the middle of the div, while the button should be aligned to the right. What is the best way to achieve this layout where the text element is centere ...

Checking the Value of the Second Child Element Using jQuery

I am struggling with a DIV structure that looks like this: <div class="metafield_table"> <div class="metafield"></div> <div class="metafield"></div> <div class="metafield"> ...

Experience the captivating effect of jQuery's fade in animation as one image gracefully

I have an 800x500 image where the bottom two quadrants feature edits to the graphics that I want to fade in gradually. If we divide the whole image into AB CD Sections C & D are what will be covered up after 10 seconds with two images fitting in the bott ...

SASS fails to recognize the variable

I am trying to incorporate borders based on specific @media breakpoints. Here is the code I have written: @media(max-width:767px) { $height: auto; $grid: 1; } @media(min-width: 768px) and (max-width:991px) { $height: 370px; $grid: 2; } @media(min-width: 9 ...

Rearrange the middle column to be the top column on smaller screens

I am trying to create a layout with 3 columns in a row. The challenge is to prioritize the middle column on top when viewed on small screens, with the left and right columns below it. To clarify, on large screens the order should be 1 2 3, while on small s ...

The link generated through ERB using link_to cannot be clicked on

When using the ERB link_to helper method to generate a hypertext link to an individual article, I am encountering an issue where the link appears as clickable in the view but is not actually clickable (the cursor does not change to a pointer). I have atte ...

IIFE and the Twitter share button are a powerful duo

As I experiment with this quick creation of mine, two questions have arisen. The first one is, how can I encapsulate all the JS code within an IIFE without breaking it? The second question is about finishing the twitter button to enable sharing the act ...

Add a Variety of Data Entries to a Table

I am currently attempting to add multiple pieces of data (from a date form) into a table using Jquery and MomentJS. However, the output does not appear as desired. Below is my code: $(".btnNext").on('click', function(e) { var Day = 1; ...

Constructing an HTML table with PHP echoes

I am currently developing a PHP script for tracking user attendance. The structure I am aiming for is as follows: Alternatively, you can view my jsFiddle <form action='this_page.php' method='post'> <table> <th>Me ...

Using a jQuery variable in PHP on the same page without needing to submit the form can easily be achieved

Is there a way to pass a Jquery variable to a php variable on the same page when the selected list item changes? <select name="RegistrationTypeID" class="formselectbox" id="RD" onchange="getText(this.val)"> <option value="">Select Registra ...

Customize the Grid Offset in CSS like BootstrapCSS Grid Offset

I am attempting to replicate the offset feature in Bootstrap's desktop grid system, where a class like .col-md-offset-3 creates an offset using margin-left. However, in my implementation found at this link, the CSS selector does not work as intended: ...

Achieving effective targeting of the second segment within a string enclosed in a span tag through increased specificity

When it comes to styling elements, the easiest way is to use classes and assign the properties needed. However, 1. I'm not a huge fan of creating classes all over the place. That's what specificity and CSS mapping are for. 2. Going thro ...

What steps do I need to take to design a menu?

I need assistance in organizing my menu with submenus. The code I currently have successfully retrieves all the submenus, but I would like to categorize them accordingly: For instance: Main Menu - Submenu 1, Submenu 2, Submenu 3 How can I go about categ ...

Utilizing the Django Template to Organize Data through Model Fields

Hello there, I am looking to categorize my players in HTML based on their positions from my model. For example, if a player's position is listed as "striker" in the model, I would like to place them in a div with the class "Strikers". How can I achiev ...

What is the best way to retrieve the current date and time in PHP?

How do I get the date and time similar to 05-17-05 at 02:33:15 Can you please assist me in writing this in php? Can I use at with the date function in php? ...

What is the best way to instantly update multiple form input fields with the slider value as it changes?

When a value is selected from a dropdown in a form, three input fields with the same value are displayed. For example, selecting "2" from the dropdown will result in two rows with six input fields - three in the first row and another three in the second ro ...

iOS hover menu behavior: Close dropdown when the same element is tapped again on iPads

The navigation bar features categories such as: politics, economy, education Upon clicking on these categories, a dropdown menu appears with locations like: asia, europe, North America What I am looking for is this: If the user clicks (tabs) or hovers (o ...