如何居中放置宽度未知的绝对位置的内容? [英] How do I centre absolutely positioned content of unknown width?

查看:194
本文介绍了如何居中放置宽度未知的绝对位置的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有人问我为什么要这么做之前,让我直接告诉你.这样你们中一个聪明的偷窥者可以告诉我一种更好的方法...

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...

我正在为绘画的查看器做准备,绘画应该拉伸以填满整个页面,准确地说,这是屏幕高度的90%.我想将一幅画逐渐淡化,然后将每幅画置于屏幕中央.

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.

要使绘画彼此渐隐,我需要将它们绝对"放置以阻止它们堆叠.麻烦来了.自从将它们设置为绝对值以来,我用来使包含div居中的每种方法都无效.

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.

部分问题是我没有为绘画设置任何宽度,因为我希望它们能够动态调整自身大小以填充用户屏幕的90%.

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.

我已经找到了数百种用于使绝对内容居中的方法,并且认为我可能需要收缩包装所包含的div.但是,到目前为止,我还没有任何成功.

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>

样式表

#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?

推荐答案

使用JavaScript计算宽度并移动宽度,

Either use JavaScript to calculate the width and move it,

使用CSS技巧将元素左右移动50%,

use a CSS hack to move the element right and left by 50%,

或者不要绝对定位它.

这个答案很短,但是很关键.如果您需要对某些东西进行集中处理(这意味着您希望浏览器确定中心点在哪里,然后将其定位在其中),则不能使用绝对定位-因为这会从浏览器中夺走控制权.


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.

要使绘画彼此渐隐,我需要将它们定位 绝对"阻止它们堆叠.

To fade the paintings in over each other I need to position them 'absolute' to stop them from stacking.

这就是您的问题所在.您已经假设出于错误原因需要绝对定位.

This is where your problem lies. You have assumed that you need absolute positioning for the wrong reason.

如果在将元素放置在顶部时遇到问题,请将图像包装在绝对定位的容器中,该容器的宽度为100%,宽度为text-align: center

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:

div {
    position: absolute;
    /* move the element half way across the screen */
    left: 50%;
    /* allow the width to be calculated dynamically */
    width: auto;
    /* then move the element back again using a transform */
    transform: translateX(-50%);
}

很明显,上述黑客攻击行为代码异味,但它在某些浏览器上仍然有效.请注意:对于其他开发人员或其他浏览器(尤其是IE或移动设备),此黑客不一定很明显.

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).

这篇关于如何居中放置宽度未知的绝对位置的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆