如何将未知宽度的绝对定位内容居中?
在有人问我为什么要这样做之前,让我直接告诉你。这样一来,你们中的一个聪明人就可以告诉我一种更好的方法……
我正在制作一个绘画查看器,它应该拉伸到填满页面,准确地说是屏幕高度的 90%。我想让一幅幅画作淡入另一幅,并希望将每幅画作置于屏幕中央的中心。
为了使这些画相互淡入,我需要将它们放置在“绝对”位置以防止它们堆叠。这就是麻烦所在。自从我将它们设置为绝对后,我用来居中包含 div 的每种方法都不起作用。
部分问题是我没有为绘画设置任何宽度,因为我希望它们动态调整自身大小以填充用户屏幕的 90%。
我已经找到了数百种将绝对内容居中的方法,并且相信我可能需要收缩包装包含的 div。但是到目前为止我还没有取得任何成功。
HTML-
<div id="viewer_div">
<img src="" id="first" />
<img id="second" class="hidden"/>
</div>
样式表
#viewer_div {
width:1264px;
}
img {
height:90%;
display:block;
margin-left: auto;
margin-right:auto;
}
上面给了我想要的效果,但不允许我将图像绝对定位。谁能建议一种使图像居中的方法,同时也允许我将一个图像淡化到另一个图像上?
Before someone asks me why the hell I would want to do this let me come straight out and tell you. That way one of you clever peeps out there can tell me a far better way...
I'm making a viewer for paintings which should stretch to fill the page, well 90% of the height of the screen to be precise. I want to fade the paintings in one over the other and want to center each of them in the middle of the screen.
To fade the paintings in over each other I need to position them 'absolute' to stop them from stacking. Here's where the trouble comes. Ever since I've set them to absolute, every method I use to center the containing div hasn't worked.
Part of the problem is that I'm not setting any width for the paintings as I want them to dynamically size themselves to fill 90% of the user's screen.
I've found a hundreds of methods for centering absolute content and believe I might need to shrink wrap the containing div. However I've not had any success as of yet.
HTML-
<div id="viewer_div">
<img src="" id="first" />
<img id="second" class="hidden"/>
</div>
Style Sheet
#viewer_div {
width:1264px;
}
img {
height:90%;
display:block;
margin-left: auto;
margin-right:auto;
}
The above gives me the desired effect, but doesn't allow me to position the images absolute. Can anyone suggest a way of centering the images but also allows me to fade one over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将元素
向左
推送其宽度的50%
,然后将其水平平移50%
对我来说很有效。我在以下链接中找到了这个概念,然后进行了翻译以满足我的水平对齐需求:https://gist.github.com/colintoh/62c78414443e758c9991#file-douchebag-vertical-align- CSS
Pushing the element
left
by50%
of its width and then translating it horizontally by50%
has worked for me.I found the concept in the following link, and then I translated to fit my horizontal align needs: https://gist.github.com/colintoh/62c78414443e758c9991#file-douchebag-vertical-align-css
要么使用 JavaScript 计算宽度并移动它,
使用 CSS hack 将元素左右移动 50%,
或者不绝对定位它。
这个答案非常简短,但却切中要点。如果您需要将某些内容集中(意味着您希望浏览器确定中心点在哪里,并将其定位在那里),那么您不能使用绝对定位 - 因为这会剥夺浏览器的控制权。
这就是你的问题所在。您认为您需要出于错误的原因而需要绝对定位。
如果您在放置元素时遇到问题,请将图像包装在绝对定位的容器中,该容器宽度为 100%,并且具有
text-align: center
如果您这样做觉得绝对定位是必要的,可以使用以下 hack 来达到您想要的结果:
显然上面的 hack 有一个可怕的 代码味道,但它有效在某些浏览器上。请注意:这种黑客攻击对于其他开发人员或其他浏览器(尤其是 IE 或移动设备)来说不一定是显而易见的。
Either use JavaScript to calculate the width and move it,
use a CSS hack to move the element right and left by 50%,
or don't absolutely position it.
This answer is incredibly short, but it is to the point. If you require something to be centralised (meaning you would like the browser to determine where the centre point is, and position it there), then you can't use absolute positioning - because that takes away control from the browser.
This is where your problem lies. You have assumed that you need absolute positioning for the wrong reason.
If you are experiencing problems placing elements on top of each other, wrap the images in an absolutely positioned container which is 100% width and has
text-align: center
If you do feel that absolute positioning is necessary, the following hack can be used to achieve your desired results:
Obviously the above hack has a terrible code smell, but it works on some browsers. Be aware: this hack is not necessarily obvious to other developers, or other browsers (especially IE or mobile).
为了摆脱 Samec 的答案,您还可以使用它来将未知尺寸的绝对位置元素垂直和水平居中:
To go off of Samec's answer, you can also use this to center an absolute position element vertically and horizontally of unknown dimensions:
使用 position:absolute 和 width:unknown 居中 div:
HTML:
CSS:
即使内容比屏幕宽度更宽,也可以使用此解决方案,并且不需要到
transform:translate
(可以模糊元素)工作原理:
Grid 将在第一个
auto
之前插入一个span
,并插入一个>div
就在第二个auto
之前,并且你会得到:span(0px) - auto - div(...px) - auto
auto 将彼此相等。
与垂直居中相同,但编写
grid-template-rows
而不是grid-template-columns
Centering div with position: absolute and width: unknown:
HTML:
CSS:
You can use this solution even if the content is wider than the screen width, and don't need to
transform: translate
(which can blur elements)How it works:
Grid will insert a
span
before the firstauto
, and insert adiv
right before the secondauto
, and you get:span(0px) - auto - div(...px) - auto
auto will be equal to each other.
Same for vertical centering, but write
grid-template-rows
instead of agrid-template-columns
2021
采用网格和插图实现绝对居中内容的现代方法
2021
A modern approach to absolutely centered content with grid and inset