如何将绝对定位的元素对齐到中心?
我正在尝试将两个画布堆叠在一起并使其成为双层画布。
我在这里看到了一个例子:
<div style="position: relative;">
<canvas id="layer1" width="100" height="100"
style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="layer2" width="100" height="100"
style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
</div>
但我想将两个画布设置在屏幕中心对齐。如果我将 left
的值设置为常量,当我更改屏幕方向时(就像我在 iPad 上执行 aps 一样),画布将不会像这样保持在屏幕中间它的作用是
<div align="center">
有人能帮忙吗?
I am trying to stack two canvas together and make it a double layers canvas.
I've saw an example here:
<div style="position: relative;">
<canvas id="layer1" width="100" height="100"
style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="layer2" width="100" height="100"
style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
</div>
But i would like to set both of the canvas align at the center of the screen. If i set the value of left
as a constant, while I change the orientation of the screen (as I'm doing aps on iPad) the canvas won't remain at the middle of the screen like how it act in
<div align="center">
Can anyone help, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果将左右边距设置为零,并将左右边距设置为自动,则可以将绝对定位的元素居中。
If you set both left and right to zero, and left and right margins to auto you can center an absolutely positioned element.
如果您想在不知道元素宽度和高度的情况下居中对齐元素,请执行以下操作:
示例:
If you want to center align an element without knowing it's width and height do:
Example:
尝试这个方法,对我来说效果很好
try this method, working fine for me
要对齐已知宽度和高度的元素,请使用以下解决方案:
对于未知宽度和高度的元素,请使用以下解决方案:
To align elements with knowing width and height use the solution below:
For elements with unknown width and height use the following solution:
您尝试过使用吗?:
不确定它是否有效,但值得一试...
小修改:添加了左边距部分,正如 Chetan 评论中所指出的那样...
Have you tried using?:
Not sure if it'll work, but it's worth a try...
Minor edit: Added the margin-left part, as pointed out on the comments by Chetan...
您所要做的就是确保您的父
具有
position:relative
,并为您想要居中的元素设置高度和宽度。使用以下 CSS:https://output.jsbin.com/aXEZUgEJ/1/
All you have to do is make sure your parent
<div>
hasposition:relative
, and set a height and width for the element you want centered. Use the following CSS:https://output.jsbin.com/aXEZUgEJ/1/
将父级 div 移动到中间 使用
将第二层移动到另一层之上
Move the parent div to the middle with
Move the second layer over the other with
我想分享另一种方法,如果您从头开始构建某些东西,它可能会很有用。
我在同样的情况下苦苦挣扎,但是当我使用上面的方法时,它不起作用,因为我在每个标签上都有一个类,可以在滚动时更改背景。
我在 Javascript 上创建了一个方法来计算元素和视口的大小,以便在每次视口大小发生变化时正确定位。
当然,setOnCenter 方法必须在文档准备好时执行。如果有什么需要改进的地方,我很乐意阅读。
There's another approach I'd like to share, may be useful if your are building something from scratch.
I was strugueling with this same situation but when I used the methods above it didn't work because I had a class on every label that changes the background when scrooling.
I created a method on Javascript to calculate the size of the element and viewport to locate properly every time the viewport size changes.
Of course the setOnCenter method must be execute when document is ready. If there something to improved I'd be glad to read it.
此答案适用于那些不确定宽度并希望确保文本保持在一行上的人:
This answer is for those who are unsure about the width and want to ensure that the text stays on a single line: