Css 如何将DIV位置设置为中心左侧200像素

Css 如何将DIV位置设置为中心左侧200像素,css,positioning,center,Css,Positioning,Center,我想将一个DIV200像素定位到中心的左侧 我目前正在使用以下代码,但在更高分辨率的显示器(例如1920×1080)上,DIV滑出位置: .hsonuc { position: absolute; top: 20px; margin:auto; margin-left:200px; display:none; } 我想要达到的目标: 只需将其从右侧放置50%(右侧:50%;),然后使用右边距:200px() HTML 您还可以使用Javascrip

我想将一个
DIV
200像素定位到中心的左侧

我目前正在使用以下代码,但在更高分辨率的显示器(例如1920×1080)上,
DIV
滑出位置:

.hsonuc {
    position: absolute;  
    top: 20px; 
    margin:auto;
    margin-left:200px;
    display:none;
}
我想要达到的目标:

只需将其从右侧放置
50%
右侧:50%;
),然后使用
右边距:200px()

HTML
您还可以使用Javascript确定浏览器左侧距离中心200px的实际像素数。这样您就不必使用
位置:绝对

示例(jQuery):


您可以使用%和px的组合; 如果div宽度为300px,则div的一半以上为150px。150+200=350px; 那就用这个

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

事实上,这是不正确的。这将使元素的左边缘位于中心左侧200px,而不是元素的右边缘。此外,您还必须计算元素的大小,并从
位置
@bfrohs中减去它。您是对的。我误解了这个问题。我正在编辑我的答案。
var centerMark = Math.round( $(window).width() / 2 ); //define the center of the screen
var elementWidth = $('.hsonuc').width();
var position = centerMark - 200 - elementWidth; //define where 200 left of the center is and subtract the width of the element.

//now you have `position` you can use it almost any way you like.
//I prefer to use margins, but that's all up to you.

$('.hsonuc').css('margin-left', position);
margin-left:calc(50% - 350px);