What's the best way to make the background image fill the entire page and have the buttons perfectly centered on top of the image?

I am currently working with Angular version 13 and have included the following CSS code.

    .image{
        min-height: 100vh;
        background-position: center center;
        background-repeat: no-repeat;
        background-size: cover;
        background-attachment: fixed;
    }

    .about-us {
      position: relative;
      margin-top: 20%;
      margin-bottom: 2%;
    }

    .login {
      margin-top: 2%;
      margin-bottom: 2%;
    }

    .social-media {
      margin-top: 2%;
      margin-bottom: 20%;
    }

    .button-box {
      height: 40px;
      width: 80px;
      font-size: 10px;
      border: 1px solid;
      box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.25);
      box-sizing: border-box;
      background-color: rgb(68, 127, 178);
    }

    #text-style {
      text-align: center;
      position: relative;
    }
    <body
      [ngStyle]="{
        backgroundImage: 'url(./../../assets/family-history-image.jpg)'
      }"
      class="image"
    >
      <div id="text-style">
        <div class="about-us" routerLink="/about-us" routerLinkActive="active">
          <button class="button-box" type="button">about-us</button>
        </div>
        <div class="login" routerLink="/login" routerLinkActive="active">
          <button class="button-box" type="button">login</button>
        </div>
        <div
          class="social-media"
          routerLink="/social-media"
          routerLinkActive="active"
        >
          <button class="button-box" type="button">social-media</button>
        </div>
      </div>
    </body>

The HTML code I have written does not function as expected. The result displays a top margin for both the buttons and the image, whereas I intended the margin to only apply to the buttons, allowing the image to fill the entire page. Here is the final output of this code.

Answer №1

If you're looking for a solution to your problem, consider trying a different approach. Simply replace the picsum.photos stock image with the URL of your preferred image.

body {
        min-height: 100vh;
        background-position: center center;
        background-repeat: no-repeat;
        background-size: cover;
        background-attachment: fixed;
        background-image: url('https://picsum.photos/1920/1080');
    }

    .about-us {
      position: relative;
      margin-top: 20%;
      margin-bottom: 2%;
    }

    .login {
      margin-top: 2%;
      margin-bottom: 2%;
    }

    .social-media {
      margin-top: 2%;
      margin-bottom: 20%;
    }

    .button-box {
      height: 40px;
      width: 80px;
      font-size: 10px;
      border: 1px solid;
      box-shadow: 0 0 8px 0 rgba(0, 0, 0, 0.25);
      box-sizing: border-box;
      background-color: rgb(68, 127, 178);
    }

    #text-style {
      text-align: center;
      position: relative;
    }
<body
      class="image"
    >
      <div id="text-style">
        <div class="about-us" routerLink="/about-us" routerLinkActive="active">
          <button class="button-box" type="button">about-us</button>
        </div>
        <div class="login" routerLink="/login" routerLinkActive="active">
          <button class="button-box" type="button">login</button>
        </div>
        <div
          class="social-media"
          routerLink="/social-media"
          routerLinkActive="active"
        >
          <button class="button-box" type="button">social-media</button>
        </div>
      </div>
    </body>

Give this method a try and see if it works for you!

Answer №2

.image {
  min-height: 100vh;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  height: auto;
}

.about-us {
  position: absolute;
  margin-top: 21%;
  margin-left: 42%;
}

.login {
  position: absolute;
  margin-top: 27%;
  margin-left: 42%;
}

.social-media {
  position: absolute;
  margin-top: 33%;
  margin-left: 42%;
}
<body
      [ngStyle]="{
        backgroundImage: 'url(./../../assets/family-history-image.jpg)'
      }"
      class="image"
    >
      <div id="text-style">
        <div class="about-us" routerLink="/about-us" routerLinkActive="active">
          <button class="button-box" type="button">about-us</button>
        </div>
        <div class="login" routerLink="/login" routerLinkActive="active">
          <button class="button-box" type="button">login</button>
        </div>
        <div
          class="social-media"
          routerLink="/social-media"
          routerLinkActive="active"
        >
          <button class="button-box" type="button">social-media</button>
        </div>
      </div>
    </body>

This code snippet worked perfectly for me.

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

Steps to invoking a custom-named function

I am currently exploring how to call a specific function name in WebApi. When calling the WebApi with the structure api/{controllerName}, it works (as long as the function name starts with "get"). My goal is to achieve the following: Using JavaScript ...

Which is more effective for a webpage that solely calculates a formula: user input fields in DIVs or forms?

My idea is to create a webpage with initially 6 input fields, which can expand to 7, 8, 9, or 10 fields when a button is pressed. These fields will have editable preset values that will be used to calculate a final value using a formula. Should I use f ...

Sending a file using Angular's $http service

I am facing an issue while trying to upload a form with an image file using the angular $http function and multer in the background for receiving. I have successfully uploaded the image via a direct form submission (without angular) as shown below: <fo ...

Strange bug affecting jQuery carousel in Internet Explorer

Encountering a strange jQuery problem here - everything seems to be working fine in Safari and Chrome, with just 4 columns being displayed. However, when I try it in IE(10), I only see 1 column and occasionally all 4 columns appear. Check it out here: ...

Encountering issues with HTML loading interpolation before constructor in TypeScript

I am currently working on a project using Angular 6 and encountering some challenges. Here is the issue at hand: I am facing an error in the HTML Console, which occurs after reloading the page. The error message indicates that the variable "atual" is unde ...

Calculating the average of numbers using a loop in PHP

The table currently includes a loop that generates 8 calculated values in one row spanning across 8 columns. Check out the screenshot below: https://i.sstatic.net/rnK3R.png Everything looks good, except for the last column cell labeled corrected accuracy ...

How do I get rid of the form border and shadow on my WordPress login page?

Currently revamping the appearance of the Wordpress login page and most aspects are coming together smoothly. I opted to utilize a separate css file for this project, which has been effective overall. However, one issue persists. I've encountered diff ...

Is there a way to save a base64 image to an excel file?

I need assistance with exporting Excel Charts from NVd3 using Angularjs. Here is the code I have been trying: (jsfiddle) <button id="myButtonControlID">Export Table data into Excel</button> <div id="divTableDataHolder"> <table> ...

Click events are not functioning properly after an AJAX request is made

$(document).ready(function () { var user = 1; $("a.like").on("click", function () { var article = $(this).attr('data-article'); $.ajax({ type: "POST", url: "WebService.asmx/Like", ...

Encountered an error with AngularJS $http.post - Unexpected token F

I've encountered an issue while running my $http.post script and have been searching for a solution without success. Below is the error that appears when I try to run the webpage: XHR finished loading: POST "http://mypage/services/json/DownTimeStartB ...

Why does one image render while the other with the same src does not?

Has anyone encountered a situation where there are 2 img tags with the same src, "images/export.png", but one displays correctly while the other doesn't? Any insights on how this discrepancy can occur? https://i.sstatic.net/z6rnW.png Here's som ...

Retrieving data from an external PHP script

I'm facing an issue with retrieving results from a PHP file after submitting a form: index.php (located at ) <form id='loginForm' action='http://domain1.com/mail.php' method='POST'> <input id=&apo ...

Changing the cursor to a waiting state following a mouse click

Looking for some help here - when I click a button, the cursor remains standard and doesn't show loading. Any suggestions on how to fix this? Below is the snippet of my code: <div class="signup"> <input type="submit" value="Sign Up" tit ...

Issue with Angular not correctly implementing Bootstrap Tooltip

I've been trying to use Bootstrap Tooltip in my Angular code, but I'm having trouble getting it to work. I thought the Angular expressions like: {{o.client_name}}, {{o.client_name}} would work, but they are not. Can someone help me understand why ...

Tips for connecting a pop-up window to your webpage

SCRIPT <SCRIPT TYPE="text/javascript"> <!-- function popup(mylink, windowname) { if (! window.focus)return true; var href; if (typeof(mylink) == 'string') href=mylink; else href ...

Retrieving information from an API and incorporating it into JSX results in displaying text within the HTML element

I have retrieved data from an API with the following response: { "name": "family: woman, woman, girl, girl", "unicode": "1F469 200D 1F469 200D 1F467 200D 1F467", "html": "&#128 ...

What is the process for sending a cross-site request to Azure's mobile service API?

When working with Azure's MobileServiceClient, jquery, or AngularJS, I have encountered a dilemma. The service necessitates a custom header, and now I am uncertain if it is possible to incorporate both simultaneously. ...

Transferring information from Console to a web address

Every time I click on a button, the correct data appears in the console. However, now I want the data 'reid' to be passed to the URL when the button is clicked. When I attempt it using: this.router.navigateByUrl('/details/' + this.resu ...

creating a pre-filled date input field in the user interface

Hey there, I'm just getting started with vmware clarity UI. Currently, I am facing an issue with using datetime-local. I am trying to prepopulate the current date and time in the datetime-local field using ngModel with the format 2017-06-13T13:00. Whi ...

What is the best way to include an attribute to an object in a knockout observable array and ensure a notification is triggered?

Implementing Knockout.js in my project, I have an observable array included in the view model. function MyViewModel() { var self = this; this.getMoreInfo = function(thing){ var updatedThing = jQuery.extend(true, {}, thing); ...