Script tag causing alignment issues for centering div on page

Currently, I am in the process of creating a web application using Bootstrap 4.3.1 and integrating the Spotify API. My main goal right now is to center a div element on the screen both vertically and horizontally, which will display the authenticated user's username. However, I have encountered an issue when trying to achieve this if the div is located within a script tag. Is there any workaround for this problem? Below is a snippet from my index.html file and style.css file.

Index.html

<script id="user-info-template" type="text/x-handlebars-template">
  <div class="container h-100 d-flex justify-content-center">
    <div class="jumbotron my-auto text-center">
      <h1 class="display-4">Welcome, {{display_name}}.</h1>
      <h4 class="display-5">Select one of your playlists to get started.</h4>
    </div>
  </div>
</script>

Style.css

html,
body {
  height: 100%;
}

EDIT I appreciate all the suggestions provided so far. Many have pointed out that having div tags within script tags is not recommended. However, I am following an example set by Spotify, where they embed JS code within the index.html file and use Handlebars in the following manner:

From their Index.html

<script id="user-profile-template" type="text/x-handlebars-template">
      <h1>Logged in as {{display_name}}</h1>
      <div class="media">
        <div class="pull-left">
          <img class="media-object" width="150" src="{{images.0.url}}" />
        </div>
        <div class="media-body">
          <dl class="dl-horizontal">
            <dt>Display name</dt><dd class="clearfix">{{display_name}}</dd>
            <dt>Id</dt><dd>{{id}}</dd>
            <dt>Email</dt><dd>{{email}}</dd>
            <dt>Spotify URI</dt><dd><a href="{{external_urls.spotify}}">{{external_urls.spotify}}</a></dd>
            <dt>Link</dt><dd><a href="{{href}}">{{href}}</a></dd>
            <dt>Profile Image</dt><dd class="clearfix"><a href="{{images.0.url}}">{{images.0.url}}</a></dd>
            <dt>Country</dt><dd>{{country}}</dd>
          </dl>
        </div>
      </div>
    </script>

At the bottom of their Index.html

<script>
      (function() {

        /**
         * Obtains parameters from the hash of the URL
         * @return Object
         */
        function getHashParams() {
          ...
        }

        var userProfileSource = document.getElementById('user-profile-template').innerHTML,
            userProfileTemplate = Handlebars.compile(userProfileSource),
            userProfilePlaceholder = document.getElementById('user-profile');
    ...
            $.ajax({
                url: 'https://api.spotify.com/v1/me',
                headers: {
                  'Authorization': 'Bearer ' + access_token
                },
                success: function(response) {
                  userProfilePlaceholder.innerHTML = userProfileTemplate(response);

                  $('#login').hide();
                  $('#loggedin').show();
                }
            });
    ...
}

Answer №1

To achieve this on a single page without importing the app.js file, you can structure it using div elements as shown below:

You can add your JavaScript code between the script tags. If you want the {display_name} to be dynamic, you'll need to create a function within the script tag that references it.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./index.css" />
    <title>Spotify</title>
  </head>
  <body>
    <div class="container h-100 d-flex justify-content-center">
      <div class="jumbotron my-auto text-center">
        <h1 class="display-4">Welcome, {{display_name}}.</h1>
        <h4 class="display-5">Select one of your playlists to get started.</h4>
      </div>
    </div>
  <script>

  </script>
  </body>
</html>

You can then rearrange all the divs and classes in your CSS according to your preferences.

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

Achieve separation without resorting to the width hack

While I currently use the width hack 49% and border 1px to create a separator for a two-column layout, I am looking for a better way to achieve this. The issue with the current method is that it breaks the design when the viewport size changes. Below is a ...

Tips for integrating DataTables selection filtering functionality

Here's a snapshot of my screen I'm attempting to utilize data tables This is what I have on the main page $('#student-listing-table').dataTable(); $('#student-listing-table').find('label').html('Search By Nam ...

Using Jquery to toggle the visibility of a DIV element and adding a blinking effect when it becomes visible

I have a hidden div that is displayed when a link is clicked, and I want it to blink as well. Here is my current setup: The link to display the hidden div: <a class="buttons" href="#" onclick="show(this)">join us</a> The hidden div to be di ...

Transfer the HTML5 FullScreen (MAP) feature onto a separate display

I'm trying to integrate a leaflet map into my AngularJS Application, but I've hit a roadblock. My goal is to move the Fullscreen feature of the Map to a second screen (if one is available), allowing users to interact with the application on the ...

The leftward relocation of 3 boxes is required

Help needed! I created 3 boxes that appear upon clicking a button, but they are not aligned properly. They need to be shifted a bit to the left. My attempt to adjust this using a large div with left:5%; did not work. If you're unsure about the issue, ...

A variety of negative () DOM Selectors

I've been trying to select a specific node using two not clauses, but so far I haven't had any luck. What I'm attempting to achieve is selecting an element whose div contains the string 0008, but it's not 10008 and also does not contain ...

Make the background image draggable while maintaining its size with background-size:cover

I'm working on a fixed div with an image as the background, set up like this: <div style=' width:230px; height:230px; background-repeat: no-repeat; background-image:url(img/myimage.jpeg) background-size: cover;'&g ...

What is causing the fluctuation in the width?

I am struggling to understand this portion of CSS code. When I resize the page, one button sometimes overlaps with the edit box. While debugging in Firebug, I noticed that the width is displayed as 0 for a container. Adding some width brings the button bac ...

Field for user input along with a pair of interactive buttons

I created a form with one input field and two buttons - one for checking in and the other for checking out using a code. However, when I submit the form, it leads to a blank page in the php file. What could be causing this issue? UPDATE After changing fro ...

Tips and tricks for implementing vuetify tooltips within a list component

Looking for help with my code: <div id='app'> <v-app> <v-list-tile-content> <v-list-tile v-for="(item, index) in items" :key="item.id" > <v-list-tile-action> {{index+1}}. </v-list-tile-action> < ...

Bootstrap dropdown feature automatically rearranges all items in my navigation bar

I recently set up a navbar for my website, but I'm struggling to grasp the concept of positioning items with Bootstrap. One issue I encountered is that when I click on the dropdown button (profile image), it causes all the items in my navbar to shift, ...

Creating a unique Tag Name for Dynamic Components in Angular

I want to customize a component @Component({ selector: '[my-component]', template: `<i>1</i><p>content</p><b>2</b>` }) export class MyComponent { public tagName: string; } Then there's another comp ...

Showing content generated by a JavaScript function

Having issues displaying the output of a JavaScript function that outputs text in seconds on my HTML page. The function I am using is: hypeDocument.currentTimeInTimelineNamed(timelineName) I've tried several solutions without success. My latest atte ...

What causes Chrome to crash when dealing with lengthy tail files?

My objective is to display a log file in real-time using a websocket connection. However, I am facing performance issues with Chrome when the paragraph ('p') element in the HTML becomes large (about 450 lines). This is the current implementation ...

Seeking guidance on how to perfectly align my collapsible navigation bar for a university assignment

Currently, I am tackling a project for my college course, and the navigation bar is posing some challenges. Despite conducting various research attempts to resolve the issue, the site remains quite basic as it stands as just a template. Primarily, my objec ...

The background on my modal remains unchanged

I have integrated a modal using Iframe for user login/registration on my website. Here is the HTML code: <div class="modal" id="ays-popin-connexion" aria-hidden="true"> <div class="modal-body" aria-hidden="true"> <iframe src=" ...

Why has my dropdown menu decided to go horizontal, rather than dropping down as it should?

Hi there! I'm new to css and tried following a tutorial with some tweaks. However, when adding a drop down menu using lists, it ended up going sideways instead of downward. Can anyone help me out? I also would like to have a collapsible menu implemen ...

Discovering an <a> element with an href attribute containing two specified strings

Here's what I have: $("a[href*='string1' && 'string2']") Unfortunately, this code didn't work for me. I also attempted: $("a:contains('string1' && 'string2')") The second one only return ...

Using Django variable in li a:nth-child selector produces unexpected results

Currently, I am honing my web programming skills by working on a personal blog in Django. However, I have encountered a hurdle. My intention was to enable the webpage to recognize which menu element is active by passing an argument from views.py to the tem ...

Hide the button with jQuery Ajax if the variable is deemed acceptable upon submission

I need to figure out how to hide the submit button if the email entered is the same as the one in the database from action.php. Can anyone help me with integrating this functionality into my existing code? <form onsubmit="return submitdata();"> ...