How can I shift a DIV to be 200 pixels left of the center?

I am trying to position a DIV element 200 pixels to the left of center.

My current code is as follows, but I have noticed that on higher resolution screens (such as 1920x1080), the DIV tends to shift out of its intended position:

.hsonuc {
    position: absolute;  
    top: 20px; 
    margin:auto;
    margin-left:200px;
    display:none;
}

My Desired Outcome:

Answer №1

To easily position it, you can place it at 50% from the right using right:50%;, and then adjust its placement by adding margin-right:200px;. You can view an example here.

HTML

<div class="hsonuc">DIV</div>

CSS

.hsonuc {
    position:absolute;
    top:20px;
    right:50%; /* Positioned at 50% from the right (centered) */
    margin-right:200px; /* Shifted 200px to the left from center */
}

Answer №2

To create a unique layout, try combining percentages and pixel values. For example, if your div has a width of 300px, half of that would be 150px. Add another 200px to make it 350px in total. Then, you can use the following CSS code:

margin-left:calc(50% - 350px);

Answer №3

To find the distance of 200 pixels from the center towards the left side of the browser without relying on position: absolute, you can utilize Javascript.

Here is an example using jQuery:

var centerMark = Math.round( $(window).width() / 2 ); //calculate the screen center
var elementWidth = $('.hsonuc').width();
var position = centerMark - 200 - elementWidth; //determine the horizontal position 200 pixels to the left of the center and subtract the element's width

//You now have the value stored in `position` which can be utilized in various ways.
//One common approach is to apply it as margin, however, the choice is yours.

$('.hsonuc').css('margin-left', position);

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

How can I align my logo on the website header separately from the menu tabs while keeping them on the same row?

Trying to vertically align a logo and headers' tabs in the same row can be quite challenging, especially when the logo is taller than the tabs. I've attempted different methods, including using padding to manually adjust the alignment, but it&apo ...

Prevent DIV from being filled by background color

Currently, I am working with a PNG image that has a transparent area resembling a torn checkout receipt with a zig-zag edge. This image is intended to function as the bottom part of a div that has a white background to mimic a till receipt. In my attempt t ...

Updating the CSS link href in ASP.NET using code behind

I'm struggling with changing the CSS href in the code-behind of my .ASPX page. I've tried various methods but haven't found a solution that works as intended. HTML Markup: <link id="linkCSS" runat="server" href='/css/1.css' re ...

Tips for employing CSS hover within HTML elements?

Is there a way to achieve the following: <li style="hover:background-color:#006db9;"> I have tried this approach but it doesn't seem to work. Is there an alternative method, or do I need to include the CSS in the head section or an external CS ...

"Achieving Alignment: Placing a Div Element Inside an H

I am in the process of building a portfolio for freecodecamp, but I am having trouble with centering the button and row div on the page. The row div is necessary because I will be adding more buttons later on, but for now I just need the FCC button. Below ...

retrieve the image name of the background using jQuery

How can I use jQuery to extract only the background image name from a div with a background image? I currently get 'http://localhost/xampp/uconnect/images/tour-networks-ss.png' but I only want 'tour-networks-ss.png' Any assistance wi ...

The rendering of Vue-bootstrap is not functioning correctly

I recently set up a new webpack project using Vue and Bootstrap. However, it appears that the styling for bootstrap elements such as b-nav, b-nav-item, and b-badge is not displaying correctly. This issue seems to affect most other element types as well. Y ...

Encountering spacing problems on IE9 and FF11 while using OS X

After conducting some testing on a design I coded, I found that it functions well in FF 13, Chrome 21, IE 7/8, and Opera 11.62 for Windows, as well as Safari 5.1 for OS X. Unfortunately, since I only have WinXP, I had to utilize Adobe Browserlab to test f ...

Remove the color gradient for the column headers in the Google Visualization table

Whenever I attempt to change the colors of the column headers using the method demonstrated in this insightful source, a rather generic gradient is applied. Interestingly, the example code provided also demonstrates the same default gradient on the secon ...

Can you explain the purpose of including text-rendering: optimizeLegibility in a CSS stylesheet?

Can you explain the significance of setting text-rendering to optimizeLegibility in a CSS file? For example, like this: html { text-rendering: optimizeLegibility; } ...

What holds more weight in CSS: an ID or a class declaration?

In this example, I am using both an ID (#mybox) and a class (.box), but they are both being styled the same. The background color is set through the ID selector. Although I understand the basic difference between an ID and a class, in this specific scenari ...

Implementing CSS classes onto HTML elements following their retrieval from a database

Is there a way to add CSS classes to HTML tags when retrieving them from a database for a blog? For instance, if the database contains the following in a column named body: <p> SO is great </p> Then when outputting it in the View using s ...

dismiss data and button for closing notification bar

I am currently using a notification bar at the top of my website, instead of a modal. I am wondering if it is possible to click the X (close-button) and have the notification bar disappear. I attempted to use data-dismiss but it did not work. Also, how can ...

The function $(this).addClass() seems to be malfunctioning

While trying to follow a tutorial, I noticed that the style sheet isn't being applied to the clicked element in the list. What could be causing this issue? In the example provided, when a string is added to the text box and the button is clicked, a n ...

Can CSS create a "half strikethrough" effect without relying on images?

Currently, I am in the process of developing a website that prioritizes responsiveness and speed by avoiding the use of any images. Instead, I aim to achieve desired effects solely through CSS. In the past, I would create a particular effect using the fol ...

What is the best way to ensure an element stops at the edge of its container?

I am trying to create a stylish slanted sidebar that aligns perfectly with the red section of my webpage, without overlapping into the blue area. Can anyone guide me on how to achieve this? Even though my current code doesn't seem to work here, it fu ...

Creating dynamic button animations with Bootstrap 4 without impacting neighboring rows

I'm facing an issue with adding a simple animation to a button in one row of my webpage. When I change the margin-top of the button, all the rows below it also move along with the button. I tried using position-absolute on the button and position-rela ...

Non-Modal functionalities, an alert displayed prominently in a vibrant red bar at the top of the screen

Is there a way to make the text show up at the top of the webpage in a red banner when the button is clicked? I've searched on Google and YouTube but haven't found a solution. Could someone please take a look at my code to help me figure this out ...

Tips on attaching a class to elements in a loop when a click event occurs

In my HTML, I am displaying information boxes from an array of objects that are selectable. To achieve this, I bind a class on the click event. However, since I am retrieving the elements through a v-for loop, when I select one box, the class gets bound to ...

Issues with modals appearing improperly after transitioning to NG-Bootstrap 15 and Bootstrap 5 upgrade

UPDATED following a thorough examination: In the process of upgrading our application from NG-Boostrap v11 with bootstrap 4 to NG-Boostrap v15 with Bootstrap 5 and Popper, we also made the switch from Angular 15 to Angular 16. Despite successfully resolvi ...