Position text fields precisely above the canvas elements

My issue involves positioning two side-by-side canvases within a browser window that the user can resize. I want these canvases to remain centered and not wrap when the window is shrunk, but there's a twist - I also need to collect user information through text fields and drop-down menus that seamlessly integrate with the canvas environment, appearing as if they are part of the artwork itself. Achieving this look has proven tricky, even with relative and absolute positions applied to the text fields and menus. I've experimented with nested DIVs for better control over positioning, but the results have been unexpected. As an alternative approach, I'm contemplating aligning the canvases to the left for simplified positioning. Below is the CSS code snippet:

#centerAll {
    margin-left:auto;
    margin-right:auto;
    width:1300px;
}
div.theParent {
    position: relative;
} 
div.absolute1 {
    position: absolute;
    top: 70px;
    left: 0;
    width: 90px;
    height: 20px;
}
div.absolute2 {
    position: absolute;
    top: 120px;
    left: 0;
    width: 90px;
    height: 20px;
}
div.absolute3 {
    position: absolute;
    top: 120px;
    left: 200;
    width: 90px;
    height: 20px;
}
body{ 
    background-repeat: no-repeat; 
    background-position: left;
}
<script type="text/javascript" src="javascriptFile.js"></script>

<div class="parent">
    <div class="absolute1">
        <input type="text" id="letteringText" value="one line of text">
    </div>
    
    <div class="absolute2">
        <select id="select something">
            <option value="option 1">option1</option>
            <option value="option 2">option2</option>
            <option value="option 3">option3</option>
        </select>
    </div>

    <div class="absolute3">
        <select id="select some more">
            <option value="option 1">option1</option>
            <option value="option 2">option2</option>
            <option value="option 3">option3</option>
        </select>
    </div>
</div>

<div id="centerAll">
    <canvas id="TheLeftSideCanvas" width="300" height="300" style="border:1px solid #999999;"></canvas>
    <canvas id="TheRightSideCanvas" width="300" height="300" style="border:1px solid #999999;"></canvas>
    <canvas id="SomeHiddenCanvas" width="300" height="300" style="display:none;"></canvas>
    <canvas id="AnotherHiddencanvas" width="300" height="300" style="display:none;"></canvas>
</div>

Answer №1

the parent div does not align with the width of the canvas container (#centerAll). I adjusted the parent div to be absolute and matched its width to that of the canvas container. Then, I floated the absolute divs and utilized the text-align property to center the input element either to the left or right based on the canvas element.

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
#centerAll {
    margin-left:auto;
    margin-right:auto;
    width:1300px;
    text-align: center;
}
div.parent {
  position: absolute;
  width:1300px;
} 
div.parent:after {
content: '';
display:block;
clear:both;
}
div.absolute1, div.absolute2, div.absolute3 {
  width: 50%;
  float: left;
  box-sizing: border-box;
  text-align: right;
  padding-top: 20px;
  padding: 20px;
}
div.absolute2 {
  text-align: left;
}
div.absolute3 {
  text-align: right;
  padding-left: 50px;
}
body{ background-repeat: no-repeat; background-position: left;}
</style>
</head>
<body>
<div class="parent">

<div class="absolute1">
<input type="text" id="letteringText" value="one line of text">
</div>
<div class="absolute2">
<select id="select something">
<option value="option 1">option1</option>
<option value="option 2">option2</option>
<option value="option 3">option3</option>
</option>
</select>
</div>
<div class="absolute3">
<select id="select some more">
<option value="option 1">option1</option>
<option value="option 2">option2</option>
<option value="option 3">option3</option>
</select>
</div>
</div>

<div id="centerAll">
<canvas id="TheLeftSideCanvas" width="300" height="300" style="border:1px solid #999999;"></canvas>
<canvas id="TheRightSideCanvas" width="300" height="300" style="border:1px solid #999999;"></canvas>
<canvas id="SomeHiddenCanvas" width="300" height="300" style="display:none;"></canvas>
<canvas id="AnotherHiddencanvas" width="300" height="300" style="display:none;"></canvas>

<script type="text/javascript" src="javascriptFile.js"></script>
</div>

</body>
</html>

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

Canvas ctx.drawImage() function not functioning properly

I've encountered an issue while trying to display images in a canvas using my rendering function. Here is the code snippet: function populateSquareImages(){ for(var i = 0, ii = squares.length; i < ii; i++) { if(squares[i].hasImage) { ...

When removing a class from a group of elements in JavaScript, it seems to only erase approximately fifty percent of the

I've been working on a Word Puzzle algorithm that involves displaying a set of words on a grid for users to solve. They can either attempt to solve the puzzle themselves or click a "solve" button, triggering a function to visually solve the Word Puzzl ...

jQuery offset(coords) behaves inconsistently when called multiple times

I am attempting to position a div using the jQuery offset() function. The goal is to have it placed at a fixed offset from another element within the DOM. This is taking place in a complex environment with nested divs. What's puzzling is that when I ...

Choosing a specific page number - kendo grid

Currently, I am working on creating a grid using kendo-telrik. Let's say I have 100 data items, but in the JSON format, I only have 10 data items (assuming each page contains 10 data items for pagination). However, I want to display all the pages (10 ...

Ways to host static content in ExpressJS for specific directories and exclude others

I need to configure my ExpressJS server to serve static files from specific paths only, excluding others. For example, I want to serve static files from all paths except /files where I only need to manipulate a file on the server. Currently, my code looks ...

Using HTML and JavaScript allows for the video URL to seamlessly open in the default video player app on the device

Working on my mediaplayer website, I want to give users the option to choose which app to play their uploaded videos with. So far, I've attempted to implement a button that triggers this action: window.open("video.mkv", '_blank'); Howeve ...

Is there a way to modify the standard width of semantic-ui sidebars?

I'm trying to adjust the width of semantic-ui sidebars, which are originally 275px wide. Where should I include the css/js code to make them wider or narrower? Here is their default code: .ui.sidebar { width: 275px!important; margin-left: -275 ...

Generating an image id upon click in ReactJS

Below is the code designed to showcase a collection of images on a webpage with React JS: this.displayedImageList = this.imageResults.map(function(picture){ return <div className="galleryBox" key={picture.toString()}><img src={picture} alt="I ...

Discover the perfect method for combining two objects while updating any empty values with a new specified value. Furthermore, in the case where the new value is also

My task involves working with an array of objects where each time I select a value, it gets pushed into the array. My goal is to merge two objects that share the same key "code" and remove any empty values. (4) [{…}, {…}, {…}, {…}] 0: {code: "abc ...

Exploring the Method of Accessing data-* Attributes in Vue.js

I have a button that, when clicked, triggers a modal to open. The content displayed in the modal is determined by the data attributes passed to the button. Here is my button: <button class="btn btn-info" data-toggle="modal" data-t ...

Stop the user from entering anything other than numbers

I am working on a directive to only allow users to input numbers. Check out my implementation below. Snippet from my directive template: <input type="text" maxlength="5" ng-keypress="allowNumbers($event)"> Function in my directive: $scope.allowNu ...

Altering the background color of an individual mat-card component in an Angular application

Here is the representation of my mat-card list in my Angular application: <div [ngClass]="verticalResultsBarStyle"> <div class="innerDiv"> <mat-card [ngClass]="barStyle" *ngFor="let card of obs | async | paginate: { id: 'paginato ...

I'm having trouble grasping the significance of the "i" in the forEach function within my code

Trying to determine word frequencies, but struggling with the code. Here's what I have: function check(){ var word = document.querySelector('textarea').value.split(" "); frequency ={}; word.forEach(function(i){ console.log(i) ...

Experiencing difficulty in connecting with the service providers

What's the process for obtaining these providers? I recently followed the JavaScript Mastery tutorial step by step, however, I encountered an issue with the useEffect hook to fetch the providers (I even tried using alert to check, but it keeps showin ...

Using multiple ng-controller directives with the "controller as x" syntax on a single element

What is the reason that Angular does not allow two ng-controller directives on a single element and What are some potential solutions for this issue - such as using custom directives or nesting HTML elements with a single ng-controller directive, among oth ...

Protractor Error: Identifier Unexpectedly Not Found

I've encountered a slight issue with importing and exporting files while working on Protractor tests. HomePage.js export default class HomePage { constructor() { this.path = 'http://automationpractice.com/index.php'; this.searchQ ...

I'm having trouble with the onImageUpload and sendFile functions in the summernote image upload feature

I have recently implemented summernote version 0.8.12 into my project. Below is a snippet of my page containing summernote: <!DOCTYPE html> <html lang="en"><head> <meta charset="UTF-8> <title>bootstrap4</title> ...

Creating a dynamic line with three.js

I am aiming to create a customizable polygon with modifiable vertices represented by red circles. My goal is to dynamically construct the polygon. When I initialize the geometry as var geometry = new THREE.Geometry(); geometry.vertices.push(point); geom ...

ways to undo modifications made to a value within an input field using jquery or javascript

$(document).ready(function () { //Highlight row when selected. $(function () { $('#Cases tr').click(function () { $('#Cases tr').removeClass('selectedRow'); $(this).addClass(&apos ...

How can I position the navigation bar ul list on the opposite side of the logo?

I've been struggling to align the text within my id= "nav-bar" to the right of the header using float:right. Despite attempting options such as using float:left and absolute positioning for the logo on the left, I haven't been successful. Even af ...