Screen resolution in HTML refers to the number of pixels

I am facing an issue with pixel positioning on my web page. I have a cover image set as the background of the body and a button placed on top of it. However, when the page is resized, the button moves along with it. I want the button to remain in its original position even when the screen size changes. This problem arises because I am working on making the website mobile responsive. As I am new to web programming, I am struggling to find a solution.

<html>
    <head>
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
        <title>test page.</title>
        <style>
            body {
                background:url("test.jpg");
                background-size: cover;
                -webkit-background-size: cover;
                -moz-background-size: cover;
                -o-background-size: cover;
            }
            #mybutton {
                position: absolute;
                right:800px;
                top:300px;
            }
        </style>
    </head>
    <body>
        <button id="mybutton" onClick="javascript:alert('clicked!');">Click Me!</button>
    </body>
</html>

Answer №1

It seems like you may be overcomplicating things here. The position: absolute; property essentially means:

Position this element relative to the top right corner of its nearest positioned ancestor

So, instead of worrying about browser window resizing affecting your positioning, a simpler approach would be to set constraints on the size of your "canvas". This way, regardless of screen size changes, your elements will remain consistent. You can achieve this by setting a fixed size for your background image container, as shown in this example:

http://jsfiddle.net/dTc4R/1/

In this fiddle, you'll notice that the button stays centered within the "canvas" even when you resize the screen.

To position the button using margins, you can simply use CSS like this:

input {
  margin-left: 200px;
  margin-right: 300px;
}

Answer №2

transform the position of mybutton to fixed using css.

         #mybutton {
            position: fixed;
            right:600px;
            top:200px;
        }

Answer №3

Consider adjusting this to suit your requirements:

#custombutton {
    position: fixed;
    right: 200px;
    top: 50%;
    display: inline-block;
}

In simpler terms, utilize the property position:fixed

Answer №4

Give this a try my friend. Setting the position to fixed is the most effective way to keep your button in place. If you use absolute positioning, the button will move when you scroll the page.. and

right:0; top:0;

will ensure that your button stays at the top right corner.

 #mybutton {
            position: fixed;
            right:0;
            top:0;
            width:100px;//customize as needed
            height:30px;//customize as needed
        }

If you want to add some margin from the top and right, you can use the following:

right:100px;//customize as needed

top:100px;//customize as needed

Answer №5

In my opinion, it would be better to utilize the left property instead of right.

An absolute element will be placed relative to the first parent element with a position of relative.

For example, you can create a button with an absolute positioning, and then place a div above it with a relative positioning. This way, you can set the position of the button in relation to that div.

This setup will remain consistent regardless of screen size.

Check out this demo on Fiddle

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

Is there a way to create a new perspective for Ion-Popover?

Looking for a solution: <ion-grid *ngIf="headerService.showSearch()"> <ion-row id="searchbar" class="main-searchbar ion-align-items-center"> <ion-col size="11"> ...

The updated AjaxUpload feature now exclusively accepts images when triggered by a button element

I'm currently working with a button that has an onload script attached to it. HTML <button class="btn default-btn logo_btn" id="photo_uploader">Upload New Photo</button> SCRIPT $(function(){ var btnUpload=$('#photo_uploader') ...

The buttons are not resizing to fit the entire table cell

I'm currently working with Bootstrap 4 and trying to place buttons in a table cell for each row. However, the buttons are not expanding to fill the available space and appear squished. This issue persists even when I switch to mobile view. From the p ...

Is caching a feature in AngularJS, and are there methods available for disabling it?

var modalInstance = $modal.open({ templateUrl: '/template/edit-modal.html', controller: ModalInstanceCtrl2, resolve: { locations: function () { return locationToEdit; } }, scope: $scope.$new() }); ...

Moving the panel to follow the mouse cursor in Firefox Add-on SDK

Is there a way to show a panel on the screen at the exact position of the mouse? I'm finding it difficult to understand how to change the position of the panel in Firefox SDK, as the documentation lacks detailed information. ...

Dependency on the selection of items in the Bootstrap dropdown menu

I am currently struggling with a few tasks regarding Bootstrap Dropdown Selection and despite looking for information, I couldn't find anything helpful. Check out my dropdown menu Here are the functions I would like to implement: 1) I want the subm ...

Using jQuery to show/hide linked CSS3 animations upon Mouseenter/Mouseleave events

Exploring the capabilities of animate.css and jQuery within a bootstrap environment has been quite interesting for me. However, I've encountered a major issue! I am attempting to trigger animations on mouseenter / mouseleave, which led me to create a ...

Vite build error: TypeError - Unable to access properties of null while trying to read 'useContext'

I used the following component imported from material-ui : import Paper from '@mui/material/Paper'; After running npm run build followed by npm run preview, I encountered an error in the console: Uncaught TypeError: Cannot read properties of n ...

ng-repeat exceeding the boundaries of the div

I am using ng-repeat to iterate through elements <div class="description-block" style="text-align: left; margin: 0px"> <span class="description-text">Targets: </span> <span ng-repeat="target in obj.targets" class="lab ...

Once the form has been submitted, proceed to open a new page following processing

Trying to remove an entry or offer from the database and then return to the page with all entries/offers. Attempted using Javascript, but it successfully deletes the item without redirecting to the overview page. Is this the correct approach with Javascri ...

Refresh Angular controller after ng-click event is triggered

In my current setup, I have a controller that consists of one main object containing various functions. Initially, default values for variables are set and the init() function is used to fetch data from the database. Everything on the page seems to be fu ...

Avoiding default action on keyboard tab event resets cursor position while typing consecutively

Issue with cursor position resetting to start when tab is the last key pressed I am working on a chrome extension for Gmail using React and need to customize the behavior of the tab key. I have found that using preventDefault and stopImmediatePropagation ...

Using jQuery to parse JSON transforms an array into a hash object

I recently encountered an issue with a string that I needed to convert into a JSON object. The string looked like this: string = '{ "key": [ { "foo": "bar" } ] }'; To convert the string, I used: json = $.parseJSON(string); However, after co ...

Navigation bar expansion issue in Boostrap needs fixing

Hello, currently I am in the process of creating my portfolio website. This is my first time working with bootstrap so I am still figuring things out. Right now, I am trying to create a navbar that expands using navbar-expand, but for some reason it's ...

Creating an opaque effect on a Material UI Drop down menu: Step-by-step guide

For the last half hour, I've been experimenting with my Select dropdown menu in an attempt to make it semi-transparent, but without success. I've tried adjusting the properties of the Paper, then the Menu, and then the MenuList, yet it appears th ...

Stop button from being clicked inside a div when mouse hovers over it

I am facing an issue with a div containing a mouseenter event and a button inside it with a click event. The concept is that when the user hovers over the div triggering the mouseenter event, the div becomes "active", allowing the button to be visible and ...

Unable to import library in Angular framework

I'm currently in the process of setting up js-beautify, which can be found at https://www.npmjs.com/package/js-beautify I installed it using the command npm install js-beautify --save Next, I added the import to my app.component.ts file The documen ...

Automated method for triggering button clicks on a webpage using JavaScript with a tailored combination of unique tag and class name

Currently, I am exploring methods to automatically trigger a button click on a webpage with a specific tag and class. My approach involves using Tampermonkey (Javascript userscripts) to accomplish this task. Referencing the image of the CSS/HTML elements r ...

jQuery AJAX Concurrent Event

I have encountered a problem similar to ones discussed on SO, but none exactly match my specific issue. Here's the situation: var x = 5; if( some condition ){ $.ajax({ url:"...", success: function(response){ x = respo ...

I am unable to locate the attribute 'match' as it is undefined in this context

I encountered an issue while using prettyPhoto. When I click on an image, a loading gif appears and the image fails to load. Upon checking the browser console, I found the following error: Uncaught TypeError: Cannot read property 'match' of unde ...