Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在顶部固定图像,但在侧面具有自动边距_Html_Css - Fatal编程技术网

Html 在顶部固定图像,但在侧面具有自动边距

Html 在顶部固定图像,但在侧面具有自动边距,html,css,Html,Css,我有以下html: <div class="fix-to-top"> <div class="background-image"></div> </div> 然而,侧边距并不是“自动”的。我如何正确地做到这一点 img { position: fixed; right: 50%; width: 100px; height: 100px; margin-top: -50px; margin-r

我有以下html:

<div class="fix-to-top">
    <div class="background-image"></div>
</div>
然而,侧边距并不是“自动”的。我如何正确地做到这一点

img {
    position: fixed;
    right: 50%;
    width: 100px;
    height: 100px;
    margin-top: -50px;
    margin-right: -50px;
    background: orange;
    top: 50%;
}​

检查此项

不需要使用两个div


您可以使用属性来实现它

这实际上并不能将其固定到顶部。如果更改浏览器窗口的大小,它将垂直“浮动”。我需要始终保持在顶部。对我来说,无论滚动,它都会粘在浏览器右侧的中间,这不是你想要的吗?嘿,我。也许这就是你想要的。
img {
    position: fixed;
    right: 50%;
    width: 100px;
    height: 100px;
    margin-top: -50px;
    margin-right: -50px;
    background: orange;
    top: 50%;
}​