In HTML, the <div> element will automatically receive a width setting without requiring any

I designed a maze that adjusts its background size based on the height and width values entered by the user. The background color of the maze object is set, but I want it to dynamically resize along with the width of the maze itself. How can I achieve this? Below is my HTML code:

 <!DOCTYPE html>
 <html>
 <head>
      <meta charset="utf-8" />
      <style type="text/css">
          .maze
          {
             color: Blue;
             text-align: center;
             background-color: Yellow;
             width: 100%;
          }
         .prompt
         {
             background-color: Yellow;
             color: Red;
         }
         </style>
         <script src="mazegenerator.js"></script>
         <title>Maze</title>
 </head>
 <body>
    <div id="maze">
        <form style="text-align: center" name="forma1">
        <br/>
            <label>
               HEIGHT:&nbsp;</label>
            <input type="text" id="height" name="height" autofocus="autofocus" 
              maxlength="2" size="6" value="" />
            <label>
                &nbsp;&nbsp;&nbsp; WIDTH:&nbsp;</label>
            <input type="text" id="width" name="width" maxlength="2" size="6" value="" />
            <br/>
            <br/>
            <span id="prompt"></span>
            <br/>
            <input type="button" alt="submit" onclick="CreateMaze();" 
                   value="Generate" style="margin-top: 10px;">
        </form>
    </div>

    <div id="Mzobj" align="center">

        <pre id="out" class="maze"></pre>
    </div>
</body>
</html>

This JavaScript function retrieves the user input for width and height:

function CreateMaze(){
//gets the value of Height & Width from user
var a = parseInt(document.getElementById("height").value);
var b = parseInt(document.getElementById("width").value);
//Values of Height and Width can't be zero
if (a == 0) {
    document.getElementById('prompt').innerHTML = "<b>height</b> is invalid";
    document.getElementById('prompt').className = "prompt";
    return;
}
if (b == 0) {
    document.getElementById('prompt').innerHTML = "<b>width</b> is invalid";
    document.getElementById('prompt').className = "prompt";
    return;
}
//Width can't be smaller than height
if (a > b) {
    document.getElementById('prompt').innerHTML = "not possible";
    document.getElementById('prompt').className = "prompt";
}
else {
    document.getElementById('prompt').innerHTML = "";
    document.getElementById('prompt').className = "";
}

document.getElementById('out').innerHTML = display(maze(a,b));
}


//Display the maze
function display(m) {
  var text= [];
  for (var j= 0; j<m.x*2+1; j++) {
    var line= [];
    if (0 == j%2)
        for (var k=0; k<m.y*4+1; k++)
            if (0 == k%4) 
                line[k]= '+';
            else
                if (j>0 && m.verti[j/2-1][Math.floor(k/4)])
                    line[k]= ' ';
                else
                    line[k]= '-';
    else
        for (var k=0; k<m.y*4+1; k++)
            if (0 == k%4)
                if (k>0 && m.horiz[(j-1)/2][k/4-1])
                    line[k]= ' ';
                else
                    line[k]= '|';
            else
                line[k]= ' ';
    if (0 == j) line[1]=line[3]=' ',line[2]= 'S';
    if (m.x*2-1 == j) line[4*m.y]= 'E';
    text.push(line.join('')+'\r\n');
}
return text.join('');

Here is an image showing the result of the generated Maze:

Answer №1

If the width specified by the user is in pixels, you can integrate the code snippet below into your 'CreateMaze()' function...

document.getElementById("Mzobj").setAttribute("style", "width:" + b + "px");

If not, you'll need to find a way to convert the entered width into a unit that is compatible with an HTML page. If it represents the number of characters horizontally, determine the measurement for each character's width and calculate accordingly.

var width = <unit per character> * <character count>
. For reference on units, check out the link provided below.

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

What is the best character encoding for my website and how can I determine the right one to use?

What is the significance of specifying character encoding in an HTML page and how can I determine which type of character encoding is best for my website? In the past, I always defaulted to using UTF-8 for all HTML pages. However, is there a particular ra ...

Issues persisting with the fixed header functionality in Bootstrap table

I implemented a table using bootstrap-table in my project. Everything seems to be working fine except for the scroll bar within the tbody. For some reason, the scroll bar is not appearing. Can anyone point out what I might be missing? I believe that if th ...

Using jQuery to hide an element when the script is invoked

I'm encountering an issue where I create a file containing HTML and JavaScript that is loaded later via AJAX. One of the files should hide an element after it is loaded, but for some reason, it is not working. I have tried various methods to hide the ...

What is the purpose of using square brackets in the angular.module() function in AngularJS?

const myapp=angular.module('myApp',[]); As someone venturing into the realm of angularjs, I have a question. What is the significance of using [] in angular.module()? If anyone could shed some light on this, it would be greatly appreciated. ...

Is there a way to simultaneously query two values in mongoose?

I am currently creating a buildQuery function to extract posts for a social media feed. When users create a post, their affiliated university is stored in the post as university. They can also opt to limit visibility to only students from the same univers ...

Troubleshooting a Basic jQuery Validation Problem

I've been attempting to incorporate the "Refactoring rules" segment from http://jqueryvalidation.org/reference/ However, I'm struggling to figure out the appropriate placement for the $.validator code. Currently, I'm inserting it like this: ...

Guide on dynamically changing the color of polymer 1.0 paper-button from an array of colors

I have an array called buttonColors that contains a set of colors in hex format. I am looking to create paper-buttons, each with the color from the buttonColors array. How can I achieve this in Polymer 1.0? <template is="dom-repeat" items="{{buttonCo ...

Preserve the status of expanded nodes within the jqGrid tree grid

How can I make sure the Expanded State of my JQTree Grid is persistent? I want it to retain its previous expanded state even after reloading the grid. Can anyone guide me on how to achieve this? Thank you in advance! ...

Retrieve all corresponding CSS values from the provided string

I have a CSS file in Shopify with the following styles: @font-face{font-family:"Work Sans";font-weight:600;font-style:normal;src:url("https://fonts.shopifycdn.com/work_sans/worksans_n6.136d99375282ffb6ea8c3dc4a8fe189c7be691b2.woff2?&hmac=xxxxxxxxxx") ...

Send both queries using JSON

I am trying to pass company information using Json, and I also want to pass the areas using the same method. However, I am encountering some issues. Can anyone provide assistance? if($_GET['method']=="get_all_companies"){ $query = "SELECT co ...

Generating a three-dimensional sphere from flat coordinates with the help of sine and cosine functions

I am trying to achieve mapping a set of 2D coordinates to a 3D surface of a sphere. To accomplish this, I am starting with an array of xy coordinates. Using the following loops, I am generating 20*20 xy coordinates ranging from 0 to 1 on each axis: var p ...

Adjusting the background element of a fullpage.js layout during resizing and customization

Is there a way to make the background image responsive on a fullpage.js page, specifically for mobile and tablet devices? How can I ensure that a specific part of the image stays at the center of the page when resizing? For instance, in the provided imag ...

Creating a map in Typescript initialized with a JSON object

In my Typescript class, there is a map that I'm trying to initialize: public map:Map<string,string>; constructor() { let jsonString = { "peureo" : "dsdlsdksd" }; this.map = jsonString; } The issue I'm encounte ...

Issue with rendering HTML tags when replacing strings within Ionic 2 and Angular 2

I am currently working with an array of content in my JSON that includes URLs as plain text. My goal is to detect these text URLs and convert them into actual clickable links. However, I'm facing an issue where even though the URL is properly replaced ...

Concealing and revealing the sidebar container

I created a website here and needed to implement a button in the header to hide and show the sidebar. Unfortunately, my current code is not working. Here is what I have attempted: <div id="B"> <button>toggle</button> </div> $ ...

Uh oh! React encountered a cross-origin error and is unable to access the real error object during development

I've encountered an issue while attempting to extract and display data from the student_learn field in firestore. Despite my efforts to map it to jsx components, I keep receiving a cors-origin error. Below is a screenshot of the collection I'm tr ...

Issue: The module '[object Object]' could not be located

const express = require('express'); const app = express(); const jade = require('jade'); const path = require('path'); const server = require('http').createServer(app); const io = require('socket.io').liste ...

Modify text by adjusting text color based on the contrast with the background color using knockout databinding

I am working with a table that contains data from my database: Here is the ViewModel structure: function alertViewModel(i) { var self = this; self.Id = ko.observable(i.Id); self.AlertType = ko.observable(i.AlertType); self.Category = ko.observab ...

Let's talk about the CSS properties for centering content and automatically

I've been struggling with getting my navbar to center and adjust its width based on the content inside. Can anyone help me with this? <nav> <ul id="menu" class="black"> <li><a href="index.html">Home</a></l ...

Verifying the activation status of a button within a Chrome extension

I have been working on a chrome plugin that continuously checks the status of a button to see if it is enabled. If it is, the plugin clicks on the button. I initially used an infinite for loop for this task, but realized that it was causing the browser to ...