Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/40.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
Css 为什么图像会随着浏览器移动而不居中?_Css - Fatal编程技术网

Css 为什么图像会随着浏览器移动而不居中?

Css 为什么图像会随着浏览器移动而不居中?,css,Css,我如何让它保持居中。我试过很多不同的方法,但都不管用。显示器无需保留,因为我需要它来显示和隐藏图像。我还需要添加什么才能让它工作?我只希望它保持水平居中 这是js小提琴 您可以尝试将当前的#边框搜索内部img元素居中,并在CSS中进行以下更改,相对使用自动(相等)左右边距定位图像,并使其居中 #border-search { position: relative; top: 50% !important; left: 25% !important; width:

我如何让它保持居中。我试过很多不同的方法,但都不管用。显示器无需保留,因为我需要它来显示和隐藏图像。我还需要添加什么才能让它工作?我只希望它保持水平居中

这是js小提琴


您可以尝试将当前的
#边框搜索
内部
img
元素居中,并在CSS中进行以下更改,相对使用自动(相等)左右边距定位图像,并使其居中

#border-search {
    position: relative;
    top: 50% !important;
    left: 25% !important;
    width: 100% !important; 
    margin-left: auto !important;
     margin-right: auto !important;
    display: none;
    }   
    #border-search.center img {
    position: absolute; top: 50%; left: 50%;
    transform: translate(-50%, -50%); 
    width: 30%; 
    height: auto;
    }
显示:块语句是将图像视为块元素所必需的,因此它可能会受到边距属性的影响


检查以验证我刚才所说的。:)

Fiddle请帮助你?
位置:固定
使DIV在你滚动时保持在原位这里是js Fiddle Volvox:我只希望它保持水平居中。我添加了你的代码,它似乎没有任何作用。
#border-search {
    margin: 0 auto;
    position: relative;
}

#border-search img {
    display: block;
    margin: 0 auto;
    position: relative;
}