Linking two block backgrounds with CSS or JavaScript: A comprehensive guide

I'm working on a website that has a challenging background image. I want to add text at a specific point in the background, but I'm having trouble figuring out how to do it.

The page is split into two sections, with each section supposed to display a different part of the background image. I attempted to divide the background and use background-size: cover, but the transition between the images isn't seamless.

Ensuring that the background remains fully responsive is crucial. How can I achieve this? Any assistance would be greatly appreciated!

I have included an image illustrating how I envision everything should appear.

Visualization of desired outcome

Answer №1

To achieve precise positioning on a webpage, utilize absolute positioning and specify the desired location in the CSS code. There are various options available for positioning elements, including the sticky value which keeps the element fixed within the viewport rather than relative to other content.

<style>
div.absolute { 
  position: absolute;
  top: 100px; 
  right: 100px; 
  width: 200px; 
  height: 100px; 
}
</style>
</head>
<body>
<div>This div element contains an image.
  <div class="absolute">This nested div will be precisely positioned based on the CSS rules specified above.</div>
</div>
</body>

Answer №2

Using a custom JavaScript function, I retrieved the height of the window and utilized it as the flex-basis for two main sections. Additionally, I placed special text in an absolute position near the center to suit your preference. Feel free to implement this structure for responsive design, as demonstrated with fullpage view. The intentional use of background colors enhances the visibility of the layout.

const height = window.outerHeight;
document.querySelector('.part1').style.flexBasis = height/2 + 'px';
document.querySelector('.part2').style.flexBasis = height/2 + 'px';
console.log(height);
body{
  margin:0;
  background-image:url('https://images.pexels.com/photos/4992859/pexels-photo-4992859.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500');
   background-repeat: no-repeat;
  background-size: cover;
  
}
.container{
  display:flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;
  color:white;
}
.part{
  opacity:.3;
  display:flex;
  align-items:center;
  font-size:3rem;
}
.part1{
  background-color:red;
}

.part2{
  background-color:blue;
}

.special{
  font-size:1rem;
  position:relative;
  margin:50px auto 50px 50px;

}
<div class="container">
  <div class="part part1">
    part1: here is the part one
  </div>
    <p class="special">Some special text</p>
  <div class="part part2">
    part2: here is the part two
  </div>
</div>

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

JS/TS Strategies for Sharing Code Across Multiple Projects

Perhaps there is a different approach that has not crossed my mind. I have spent some time exploring various methods like js/ts with npm link, git submodules, tsconfig paths, symlink, npm/pnpm/yarn workspace, npm pack & npm link, package.json exports, ...

How can we modify the elements within an Object tag in HTML? If it is achievable, what is the process?

I am working on a project involving an HTML page that includes content from an external website using an HTML object tag as shown below: <object data="someUrl-on-different-domain-than-host" type="text/html"></object> My goal is to use jQuery ...

Manage Raspberry Pi through a local website

Seeking guidance on creating a system where pressing a button on a webpage triggers a signal to a raspberry pi. For example, clicking on button1 on my laravel-built website initiates operation1 on the raspberry pi side, which will be using a .Net App. An ...

What is the best way to relocate the box within this HTML/CSS document?

Looking at this specific HTML webpage <html> <head> <title>CSS Structure</title> <link href="layout.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="header">Header 900x100 px</div> ...

What is the best way to uncheck a checkbox once both of my input fields [type=text] have been cleared or are empty

Is there a way to uncheck the checkbox input if both text inputs are empty, and check it if one of the text inputs is filled? $('input[name="t2"],input[name="t3"]').keyup(function() { $('input[name="t1"]').prop("checked", $.trim($( ...

What is the best way to execute a jQuery method once WordPress posts have finished loading?

I am currently utilizing the Smooth Div Scroll jQuery Plugin to create a dynamic filmstrip feature on a website. The film strip consists of images pulled from a custom post type, each with its own title and single image. The plugin allows for horizontal sc ...

Updating the UpdatePanel causes duplication of CssClass

In my ASP.NET Webforms project, I have created a user control that inherits from the LinkButton. This user control has a property to adjust the size by adding predefined CSS classes to the control. Protected Overrides Sub CreateChildControls() Dim Siz ...

The content contained within the .each loop within the click event is only executed a single time

While working on coding a menu opening animation, I encountered an issue today. Upon clicking the menu button, the menu opens and the elements inside receive an added class (resulting in a fade-in effect). Clicking the menu button again should close the ...

Combine multiple arrays of JSON objects into a single array in PHP

I have organized a json array of results in php and here is the output: [{ "05-10-2018": "Seeing dads differently" }, { "05-10-2018": "Extraordinary ordinary Britain" }, { "05-10-2018": " Roll up for the Social Science Market!" }, { "05-10 ...

Is there a special Angular technique to ensure a div remains scrolled to the bottom of its items?

In this interactive demonstration, you can see how jQuery can be utilized to create a terminal-like feature. This functionality ensures that as new items are added to a scrollable div, the scroll automatically locks at the bottom. Pay close attention to l ...

Establish the focus when the TinyMCE editor loads

After clicking the edit button, the tinymce text editor is displayed but there's an issue where the focus is not set on the first load of the editor. However, it works fine from the second load onwards. Here's what I have attempted: HTML <h ...

Modifying column layout within an HTML webpage

I'm seeking advice on modifying a code. I'd like to keep it the same as it is now, but with one change - I want to decrease the width of the main video box and add another one beside it with an iframe code for a chatbox. Here's the current c ...

Sending users to either Page A or Page B depending on the response received from the API

Currently facing a dilemma. Imagine having a main hub page where you can navigate to either page A or page B. On this main page, there is a list of items. The goal is to trigger a GET API call upon clicking any item in the list. Based on a boolean field i ...

Troubleshooting: Issue with Ajax communication to PHP backend script

I followed the solution on this Stack Overflow post but unfortunately it did not work as expected. When I open test.php, I see a box to input text and a submit button. However, when I try to submit the form, Firefox's developer console shows the follo ...

When attempting to swap out ":customimage:" with an image in a React.js HTML view, the result displayed is [object Object]

I have created a function below: WordColonsToImage(comment) { var newcomment = comment.replace(/:wave:\s*/g, <img src={wavinghand} />) return newcomment } Here is an example: WordColonsToImage("Hi! :wave:") whi ...

After completing the installation of "node-pty" in an electron-forge project on Linux, I noticed that the pty.node.js file is not present. What is the proper way to install node-pty

Following the installation of node-pty, an external module utilized to generate pseudo terminals with Node.js in a boilerplate electron-forge project, I encountered an issue. The error indicated that a core module within node-pty was attempting to import a ...

Is it possible to conduct an auction in JavaScript without using the settimeout or setinterval functions?

I'm currently working on creating an auction site and I need help improving its speed and performance. I've been using setInterval and setTimeout functions for the countdown, but it's quite slow as a request is sent to the server every secon ...

Change the icon switch from fas fa-lock-open to fas fa-lock by adding an event listener for a click action

let lockIcon = document.createElement("i"); lockIcon.setAttribute("class", "fas fa-lock-open"); lockIcon.setAttribute("id", color + "lock"); Is there a way to toggle between the icons fas fa-lock-open and fas fa-lock when clicking using a ...

Issues encountered with jQuery's $.ajax function when communicating with PHP

I am having trouble creating a simple app that displays data from a MySQL database using PHP and jQuery. The issue I am facing is with retrieving the data using jQuery. While my PHP script successfully returns the data without any problems, I am not receiv ...

What is the process for using JQuery to switch the class of an element to 'nested-class'?

Styling with CSS: #logo-container { position: fixed; right:5px; .home-page { /* homepage styling */ } .content-page { /* Modify the $element's class to this class */ margin-top: 78px; } } Using JQuery to change the class of #lo ...