如何将绝对定位的元素与中心对齐? [英] How to align absolutely positioned element to center?

查看:112
本文介绍了如何将绝对定位的元素与中心对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个画布堆叠在一起,使其成为双层画布.

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>

但是我想将两个画布都设置在屏幕中央.如果我将left的值设置为常数,则在更改屏幕方向(如在iPad上进行aps操作)时,画布将不会像其作用一样保留在屏幕中间

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

任何人都可以帮忙吗?

推荐答案

如果将左右两个都设置为零,并且将左右边距设置为自动,则可以将绝对定位的元素居中.

If you set both left and right to zero, and left and right margins to auto you can center an absolutely positioned element.

position:absolute;
left:0;
right:0;
margin-left:auto;
margin-right:auto;

这篇关于如何将绝对定位的元素与中心对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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