What is the best way to adjust the position of an image background within a container without affecting the container's own placement?

While working on a registration form, I encountered an issue with moving the background image within a container. Despite adding position: relative and using top: with a negative value to shift the image, the entire container moved instead of just the background image. My frontend framework is Bootstrap.

.mt {
  margin-top: 25px;
  margin-bottom: 25px;
}

.reg-bg {
  background-image: url("../img/reg-bg.png");
  background-repeat: no-repeat;
  border-radius: 15px;
}

.reg-btn {
  background-color: #003049;
  color: #FCBF49;
  width: 150px;
  height: 50px;
  font-size: 20px;
}

.reg-btn:hover {
  background-color: #FCBF49;
  color: #003049;
}
<!-- Bootstrap-5 -->
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9af8f5f5eee9eee8fbeadaafb4aab4a8">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">


<!-- Body -->
<div class=" mask d-flex align-items-center mt">
  <div class=" container ">
    <div class="row d-flex justify-content-center align-items-center">
      <div class=" col-12 col-md-9 col-lg-7 col-xl-6">
        <div class="reg-bg card">
          <div class=" card-body p-5">
            <h2 class="text-uppercase text-center mb-5">Create an account</h2>
            <form>
              <div class="form-outline mb-4">
                <input type="text" class="form-bg form-control form-control-lg" />
                <label class="form-label">Your Name</label>
              </div>

              <div class=" form-outline mb-4">
                <input type="email" class="form-bg form-control form-control-lg" />
                <label class="form-label">Your Email</label>
              </div>

              <div class="form-outline mb-4">
                <input type="password" class="form-bg form-control form-control-lg" />
                <label class="form-label">Password</label>
              </div>

              <div class="form-outline mb-4">
                <input type="password" class="form-bg form-control form-control-lg" />
                <label class="form-label">Repeat your password</label>
              </div>

              <div class="d-flex justify-content-center">
                <button type="button" class="btn reg-btn">Register</button>
              </div>
              <p class="reg-ft text-center mt-5 mb-0">Already have an account with us? <a href="login.php" class="reg-ft-bld">Login here</a></p>
              <p class="reg-ft text-center">Validation email timed out? <a href="revalidation.php" class="reg-ft-bld">Click here</a></p>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

jsfiddle: https://jsfiddle.net/p4udb7c3/5/

Answer №1

One key point to keep in mind is to avoid using negative position values in your CSS code whenever possible. This can lead to messy code and potential styling issues down the line!

Instead, consider utilizing the background-position property to precisely set the position of your image background.

For instance, you can do something like this:

.reg-bg {
  background-position: top center;
}

If you need further guidance, check out more information here.

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

Bootstrap slider aligned to the right side

I'm currently utilizing bootstrap 3 for my website. However, I encountered an issue with the slider when viewing the page on a mobile device as the image was displaying outside the viewport, shifted to the right. Additionally, in Firefox, the slider ...

Slicknav is failing to appear on the screen, although it is being

After spending hours trying to implement a slicknav menu into my school project, I'm stuck and seeking guidance. Despite having some coding experience, I can't seem to find the solution to my problem. Any help, insight, or tips on fixing or impro ...

Title menu that can be both dragged and edited

I am currently working on developing an HTML user interface that enables my users to rearrange or organize the menu items or utilities according to their preference, much like the HOME screens on Android or iOS devices. I am seeking advice on how I can s ...

What is the best way to halt a JavaScript function originating from embedded iframe content?

When I embed another website using iframe, there is a javascript function on their page that I do not want to run on my own page. Here it is: if (top.location != location) { top.location.href = document.location.href; } I attempted a solution but it ende ...

Tips for extracting the content from a <span> tag within an iframe

In my setup, I have two iframes labeled Test1 and Test2. Each iframe contains the following code snippet with the goal of retrieving the value '24' from it. <div class="Test-font"> <h6 class="Test_something d-inline">Available MQ ...

Using a repeating background in CSS with overflow displayed at the top rather than the bottom

Does anyone know how to make the repeating background 'start' fixed at the bottom of the div and overflow on the top? I want it to be the opposite of the default behavior. Let me illustrate what I'm trying to achieve with a small example. I ...

Unable to conceal a div element on mobile devices

Creating a unique bootstrap theme for Wordpress and encountering an issue with hiding the "sptxt" text on mobile screens. <div class="row"> <div class="col-lg-5 col-sm-12"><div id="gr"></div></div> <div class="col- ...

Maintain continuous visibility of horizontal scroll in Bootstrap responsive tables

When utilizing responsive bootstrap tables in a .net web application to showcase data, the issue arises when a substantial amount of information is returned. In such cases, a horizontal scroll bar appears at the bottom of the page. This forces users to s ...

Change the right border style for the second and third ToggleButtons in the ToggleButtonGroup

I've been working on this for a few hours now and I can't seem to get it right. Currently, I'm using Mui v5 and trying to style the ToggleButtons to look like regular MUI buttons. So far, I was able to achieve this transformation: https:/ ...

Can the lazy load script dependent on jQuery be utilized before the jquery.js script tag in the footer?

After receiving HTML from an AJAX callback, I noticed that there is a script tag for loading code that uses jQuery. However, I consistently encounter the error of jQuery being undefined. All scripts are connected before the closing </body> tag. Is ...

How can I implement JQuery colorbox in a JSP file without creating a new file for content within a specific div?

Currently, I am utilizing JQuery colorbox in the following way: A link (represented as a button) with the class cboxElement displays the html content of colorbox from another JSP file when clicked. Everything works smoothly up to this point. However, I am ...

How can I ensure that a bigger sprite image fits inside a smaller div?

I have a unique situation where I am working with a large image sprite that is 1030px by 510px and contains two smaller images, each being 515px by 515px. My goal is to extract the two small images from the sprite and set them as backgrounds for two <im ...

Comparing currency to double in handlebars: a comprehensive guide

I've been working with Handlebars.js and encountered an issue when trying to compare product prices above $35. The problem arises from the fact that prices are stored as "$54.99" which gets treated as 0 when compared to a number. How can I effectively ...

The Javafx WebEngine's executescript() function is unable to send a multiline string

Issue: I'm struggling to make the JavaFX WebEngine trigger a JavaScript function when using a Java String object with multiple lines. If I input the following example in HTML: "asd" ENTER "qwe" into the textArea, and then click the send button, the f ...

The right side alignment is malfunctioning

I need assistance with creating a section on a webpage that resembles a piece of paper, where there is content displayed on white background against a grey background. Here's what my CSS currently looks like: body { background:grey; } .page { ...

Ways to resolve the issue of iOS Safari showcasing 'a' tag styling differently compared to other browsers

Here's a simple question for you. Take a look at the images below for reference. Check out the iOS Safari browser on my phone https://i.sstatic.net/o9jOe.jpg and compare it to my desktop browser, both screenshots were taken live from the server. ...

What is the best way to show a left and right arrow on a mobile website page?

I'm experiencing a strange issue with my webpage, specifically a post in Wordpress. When I try to insert the left right arrow character (U+2194) either by copying from a character map or using the html code, I end up with an icon instead of the actual ...

Issue with Bootstrap 4 columns overlapping upon screen resizing

Whenever I use a row with 4 columns within a container that has 3 columns, the text file overlaps with the column containing an image when the screen size is reduced or viewed on an iPhone. Take a look at the code snippet and screen capture provided below ...

Errors in AMP Validator

I have recently started working with next/amp and I am encountering some issues with the amp validator in dev mode. The errors that I am struggling to understand are as follows: An error related to CSS syntax in the 'style amp-custom' tag - show ...

Using jQuery, you can activate an onclick function based on specific keywords entered by the user

Within this element, there is a text input field that allows users to search for specific items. Next to the input field is an icon image that can be clicked on to trigger a jQuery onclick event. This event sends an AJAX request to a PHP file, retrieves da ...