css - 如何真正将两个元素的中心与 css 对齐?

标签 css

我有一个 Canvas ,里面有一个圆形图像,它被放置在这样的包装器中:

<div id="kaleidowrapper"><canvas class="kaleidoscope" height="1448" width="1448"></canvas></div>

.kaleidoscope 是圆形图像。圆形图片半径根据浏览器大小设置,使图片能覆盖整个背景。高和宽都是2*radius这样的

enter image description here

包装器具有以下 css:

#kaleidowrapper{
  position: fixed;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

默认情况下,我的图像定位如下: enter image description here

如何定位图像以使其在屏幕上居中?

最佳答案

你需要为此使用 jquery。试试这个 fiddle

JS:

var imgWidth = Math.sqrt(($("div").width()*$("div").width())*2);
$("img").css({
    "width": imgWidth,
    "position":"relative",
    "left":-((imgWidth-$("div").width())/2),
    "top":-((imgWidth-$("div").height())/2),
    });

关于css - 如何真正将两个元素的中心与 css 对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23827233/

相关文章:

javascript - 如何使元素位置固定但又离开窗口?

python - Font-awesome 在 Flask 中不起作用?

html - Bourbon-Neat 列到容器顶部

css - Qt 样式表 : Setting the icon mode for a QTabBar

css - 如何选择表中的最后一个行跨度?

css - 是否可以将 Twitter Bootstrap 3 模态 "slide across"转换为新模态

html - div 中的 CSS 对象定位

css - (CSS) 创建一个背景模糊的盒子

jquery - 如何使用jquery使页面显示滚动效果

javascript - Js倒计时文本不在边框中心内