如何将绝对定位的元素对齐到中心?

发布于 2024-12-08 21:43:10 字数 569 浏览 0 评论 0原文

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

我在这里看到了一个例子:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

幽梦紫曦~ 2024-12-15 21:43:10

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

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

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;
阿楠 2024-12-15 21:43:10

如果您想在不知道元素宽度和高度的情况下居中对齐元素,请执行以下操作:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

示例:

*{
  margin:0;
  padding:0;
}
section{
  background:red;
  height: 100vh;
  width: 100vw;
}
div{  
  width: 80vw;
  height: 80vh;
  background: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<section>
  <div>
    <h1>Popup</h1>
  </div>
</section>

If you want to center align an element without knowing it's width and height do:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

Example:

*{
  margin:0;
  padding:0;
}
section{
  background:red;
  height: 100vh;
  width: 100vw;
}
div{  
  width: 80vw;
  height: 80vh;
  background: white;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
<section>
  <div>
    <h1>Popup</h1>
  </div>
</section>

余罪 2024-12-15 21:43:10

尝试这个方法,对我来说效果很好

position: absolute;
left: 50%;
transform: translateX(-50%); 

try this method, working fine for me

position: absolute;
left: 50%;
transform: translateX(-50%); 
你的心境我的脸 2024-12-15 21:43:10

要对齐已知宽度和高度的元素,请使用以下解决方案:

position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;

对于未知宽度和高度的元素,请使用以下解决方案:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

To align elements with knowing width and height use the solution below:

position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;

For elements with unknown width and height use the following solution:

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
口干舌燥 2024-12-15 21:43:10

您尝试过使用吗?:

left:50%;
top:50%;
margin-left:-[half the width] /* As pointed out on the comments by Chetan Sastry */

不确定它是否有效,但值得一试...

小修改:添加了左边距部分,正如 Chetan 评论中所指出的那样...

Have you tried using?:

left:50%;
top:50%;
margin-left:-[half the width] /* As pointed out on the comments by Chetan Sastry */

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

伊面 2024-12-15 21:43:10

您所要做的就是确保您的父

具有 position:relative,并为您想要居中的元素设置高度和宽度。使用以下 CSS:

.layer {
    width: 600px; height: 500px;
    display: block;
    position:absolute;
    top:0;
    left: 0;
    right:0;
    bottom: 0;
    margin:auto;
}

https://output.jsbin.com/aXEZUgEJ/1/

All you have to do is make sure your parent <div> has position:relative, and set a height and width for the element you want centered. Use the following CSS:

.layer {
    width: 600px; height: 500px;
    display: block;
    position:absolute;
    top:0;
    left: 0;
    right:0;
    bottom: 0;
    margin:auto;
}

https://output.jsbin.com/aXEZUgEJ/1/

逆光下的微笑 2024-12-15 21:43:10

将父级 ​​div 移动到中间 使用

left: 50%;
top: 50%;
margin-left: -50px;

将第二层移动到另一层之上

position: relative;
left: -100px;

Move the parent div to the middle with

left: 50%;
top: 50%;
margin-left: -50px;

Move the second layer over the other with

position: relative;
left: -100px;
懷念過去 2024-12-15 21:43:10

我想分享另一种方法,如果您从头开始构建某些东西,它可能会很有用。

我在同样的情况下苦苦挣扎,但是当我使用上面的方法时,它不起作用,因为我在每个标签上都有一个类,可以在滚动时更改背景。

我在 Javascript 上创建了一个方法来计算元素和视口的大小,以便在每次视口大小发生变化时正确定位。

function setOnCenter(element) {
  let elm = $("#" + element);
  let currentlyViewportWidth = $(window).width();
  let unitWidthViewport = currentlyViewportWidth / 100;
  let pxSizeText = elm.outerWidth();
  let percentSizeText = pxSizeText / unitWidthViewport;
  let halfPercentSizeText = percentSizeText / 2;
  let result = 50 - halfPercentSizeText;

  elm.css({ width: pxSizeText + 'px', left: result + '%'});
}

window.addEventListener("resize", function (event) {
  setOnCenter('stuff_text');
})

当然,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.

function setOnCenter(element) {
  let elm = $("#" + element);
  let currentlyViewportWidth = $(window).width();
  let unitWidthViewport = currentlyViewportWidth / 100;
  let pxSizeText = elm.outerWidth();
  let percentSizeText = pxSizeText / unitWidthViewport;
  let halfPercentSizeText = percentSizeText / 2;
  let result = 50 - halfPercentSizeText;

  elm.css({ width: pxSizeText + 'px', left: result + '%'});
}

window.addEventListener("resize", function (event) {
  setOnCenter('stuff_text');
})

Of course the setOnCenter method must be execute when document is ready. If there something to improved I'd be glad to read it.

仙气飘飘 2024-12-15 21:43:10

此答案适用于那些不确定宽度并希望确保文本保持在一行上的人:

position: absolute;
text-align: center;
left: 50%;
transform: translateX(-50%);
white-space: nowrap; /* Prevents text from breaking into multiple lines */

This answer is for those who are unsure about the width and want to ensure that the text stays on a single line:

position: absolute;
text-align: center;
left: 50%;
transform: translateX(-50%);
white-space: nowrap; /* Prevents text from breaking into multiple lines */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文