How to align absolutely positioned element to 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>
但我想将两个画布都设置在屏幕中心对齐.如果我将 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;
这篇关于如何将绝对定位的元素对齐到中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将绝对定位的元素对齐到中心?


- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01