How can you make a Unity Web GL html container adjust its width to match the size of the browser window?

Looking to create a Unity WebGL build that allows for specifying width and height in the HTML template.

How can I dynamically set the width of div id="gameContainer" based on the browser window width to ensure it always fills the space?

.webgl-content * {
  border: 0; 
  margin: 0; 
  padding: 0
}

.webgl-content {
  position: absolute; 
  top: 50%; 
  left: 50%; 
  -webkit-transform: translate(-50%, -50%); 
  transform: translate(-50%, -50%);
}

... (the CSS code was not altered) ...

</body>

Answer №1

To make an element as wide and high as the viewport, use the vw (view width) and vh (view height) attributes.

For more information on possible values for viewport-percentage lengths, refer to MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/length#Viewport-percentage_lengths

To achieve this, set the element's width and height to 100vw and 100vh respectively:

<div id="gameContainer" style="width: 100vw; height: 100vh; margin: auto"></div>

In the following snippet, I have moved the inline CSS to a separate CSS file for better organization.

.body {
   margin: 0px;
}
#gameContainer {
  width: 100vw;
  height: 100vh;
  display: block;
  background-color: black;
}

.webgl-content * {
  border: 0; 
  margin: 0; 
  padding: 0
}

<!-- More CSS code here -->

</body>

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

Tips for storing an array of checkboxes from a form submission into a variable in PHP

Seeking assistance in implementing an array for multiple checkboxes and storing data in a MySQL database using PHP code. The current implementation is not functioning as expected. PHP Code <?php require_once "../lakota/config.php"; if ($_SER ...

I am a newcomer to HTML and I am eager to display the selected options from a <select> element

Can someone help me display the selected licorice flavor, chocolate color, and topping? For example: "Green apple, white, christmas sprinkles." I'm not sure if assigning a numeric value to each option is necessary. I did it on a whim. Please suggest ...

Dynamic layout problem with navigation pills

I currently have 2 navigation pill buttons designed to alter the layout upon being clicked. Both layouts always include the sidebar which remains constant throughout. Clicking on Nav pill 1 will display the layout as follows: sidebar | column 1 | column ...

Utilizing Pseudo Classes in Custom Styled Components with MUI

Exploring the world of React and MUI styled components for the first time. Currently, I'm delving into grid layouts and trying to style all children of a specific component. My goal is to target ${Item1}:nth-child(3n-1), ${Item2}:nth-child(3n-1), ${It ...

Challenges of Python Web Scraping: Insufficient Extraction of HTML Code

As someone new to Python, I am currently experiencing difficulties with my web scraping code. The script is able to access the website and bypass cookies successfully. However, it is failing to capture the entire HTML code. Here is a snippet of the HTML c ...

Deactivate a button on a specific tab

My setup includes two tabs: <div class="modal-body"> <form name="myForm" novalidate="novalidate"> <ul class="nav nav-tabs"> <li class="active"><a data-toggle="tab" href="#basicInfo">Info</a></li> ...

troubleshoot clientWidth not updating when CSS changes are made

Summary: When I adjust the size of a DOM element using CSS, its directive fails to acknowledge this change even with a watch on the size. Aim I have multiple custom directives (simple-graph) each containing an svg. My goal is to enlarge the one under the ...

Troubleshooting problem with AngularJS ng-repeat

Seeking assistance with an angularjs issue as a newcomer to the platform. Building a photo gallery where the initial page displays thumbnail photos from twitter and instagram using ng-repeat loop. Hovering over an image fades it out, revealing a button f ...

Emulating form functionality: How to programmatically open a link using PHP

Do you want to provide package tracking on your website like DHL does? With DHL, users can track their packages by visiting a specific URL, such as . When a tracking number is entered, it opens a new window with the following format: SOMENUMBER1234 If you ...

Using a table row as a counter in HTML

I am looking for a way to automatically assign IDs to table rows using XSLT in a systematic manner. The idea is to have the ID consist of a string followed by a counter, like this: <table> <tr id="Row1"> # it can be only a number => id=" ...

A clever way to transfer the "HTML SELECT VALUE" to a different webpage using ajax without the need to refresh the page

Having trouble sending a selected value to another webpage without refreshing? I tried using ajax but couldn't get it to work. After the alert, it seems to stop functioning. Here's the script in one file, where I'm trying to send values 0, 2 ...

The process of altering a grid in HTML and adding color to a single square

I am facing a challenge that I can't seem to overcome. I need to create a game using HTML, CSS, and JS. The concept involves a grid where upon entering a number into a text box, a picture of a cartoon character is displayed in a square which turns gre ...

How can you retrieve the component's state from a higher level without relying on useContext?

I have a question regarding creating expanding flex cards. I found an example on Codepen that showcases what I'd like to achieve: https://codepen.io/z-/pen/OBPJKK In my code, I have a component called HomeButtons that generates these flex cards. With ...

Building a hyperlink from a textbox input: A step-by-step guide

I am attempting to modify the href of my link based on the content of the textbox with id="serie". However, Firefox is indicating that el is null. Can you help me identify where the issue lies? (The top section is the Page, the middle part shows the debug ...

What does "cascading" refer to in the context of Cascading Style Sheets?

Similar Query: Explaining the concept of "cascading" in CSS Can you clarify the term "cascading" within Cascading Style Sheets? ...

Tips for incorporating a variable into an action link for HTML and Flask

I am completely new to using Flask and HTML. Currently, I am working on developing a user management tool that includes buttons to perform various actions within Flask. One of the functionalities I am trying to implement is a button that allows users to ...

Retrieve and update data on-the-fly using XMLHttpRequest from an external API

I am currently dealing with a Web App that refreshes every 5 seconds using a meta tag in HTML. It connects to a web service each time the page is refreshed to display the JSON results. However, I now require this process to occur only when there is a new v ...

What is the best approach for creating a single jQuery click function to manage multiple elements within a slider?

Here's my query: I have a slider on my website where each slider item consists of an image and a Bandcamp iframe link. Below is an example code of one of the slider items: <div class="tapecol col-md-4"> <img class="media-ob ...

What could be the reason for 'selected' not appearing?

Can anyone explain why the 'selected' attribute is not appearing in my <option value=""> element? <p> <select name="images" class="dropdown"> <option value="empty" <?php if(isset($_GET[ ...

How to customize the active color of the navigation pills in Bootstrap 4

I want to customize the background color of each pill, but I also want a faded version of that custom color with an active color matching the full color of the corresponding pill. pill one > faded red pill two > faded blue pill three > faded bla ...