简体   繁体   English

使用CSS flexbox重叠两个居中的元素

[英]Overlap two centered elements using CSS flexbox

I'm trying to absolutely center two elements in the middle of the page, with one behind the other. 我试图将两个元素放在页面中间,一个在另一个后面。 The intent is to have a page that is fully responsive with a circle in the middle which has a pulse effect behind it. 目的是让一个页面完全响应中间的圆圈,后面有一个脉冲效果。

Here is a fiddle of the below: 这是下面的小提琴

 html, body { height: 100%; width: 100%; } body { background: lightblue; display: flex; align-items: center; justify-content: center; } .container { display: flex; align-items: center; justify-content: center; } .sphere { display: flex; background: black; border-radius: 300px; height: 100px; width: 100px; } .container:after { display: flex; background: #FFF; border-radius: 300px; height: 130px; width: 130px; animation: pulsate 2.5s ease-out; animation-iteration-count: infinite; opacity: 0.0; content: ""; z-index: -1; margin: auto; } @keyframes pulsate { 0% { transform: scale(0.1, 0.1); opacity: 0.0; } 50% { opacity: 1.0; } 100% { transform: scale(1.2, 1.2); opacity: 0.0; } } 
 <div class="container"> <div class="sphere"></div> </div> 

Method: 方法:

I'm currently using flexbox to center a container div with a circle inside, and an :after tag on the container to create the pulse. 我目前使用Flexbox,就到中心里面的圆形容器DIV和:after在容器上的标签来创建脉冲。 I've tried using z-indexing (doesn't seem to do anything), and absolute positioning (which requires hard-coded px values which I'd like to stay away from). 我尝试过使用z-indexing(似乎没有做任何事情)和绝对定位(需要硬编码的px值,我想远离它)。

Is there a graceful way to achieve this using flexbox? 有没有一种优雅的方法来实现这个使用flexbox?

Desired Effect: 期望的效果:

This is close , but I'd like to move away from using px values for the :after element if possible. 这很接近 ,但是如果可能的话,我想放弃使用:after元素的px值。

Not sure if that'll do it, but you could simply add to your :after 不确定是否会这样做,但你可以简单地添加到你的:之后

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

fiddle: http://jsfiddle.net/ccyd6xh1/ 小提琴: http//jsfiddle.net/ccyd6xh1/

Update that was a quick draft, and was not absolutely responsive. 更新是一个快速草案,并没有绝对响应。 If you want the effect the really follow your sphere and its size, you should add the :after on the sphere element and not its container, and use % width and height (here the glow grows to 130% of the sphere size): 如果你想要效果真的跟随你的球体及其大小,你应该在球体元素而不是它的容器上添加:after ,并使用%width和height(这里辉光增长到球体大小的130% ):

http://jsfiddle.net/tjoo739y/ http://jsfiddle.net/tjoo739y/

This is easily created with position: absolute , which removes the elements / pseudo elements from the normal flow of the document so they ignore each other. 这很容易用position: absolute创建,它从文档的正常流中删除元素/伪元素,使它们相互忽略。 You can use flexible units and don't need to use px . 可以使用灵活单位,不需要使用px There is no need for the flex properties to achieve this. flex属性不需要实现这一点。

  • The .sphere element is given its height / width and is centered. .sphere元素的高度/宽度为中心。 The circles conform to this width / height and position. 圆圈符合此宽度/高度和位置。

  • Using a pseudo-element for the black circle is the easiest way to have it overlap the white pulsating circle. 对黑色圆圈使用伪元素是使其与白色脉动圆重叠的最简单方法。 Both the circles are now siblings and the second sibling will overlap naturally 这两个圆圈现在都是兄弟姐妹,第二个兄弟姐妹会自然重叠

  • The pseudo-element children of .sphere are stretched to fit the parents height and width with the top / left / bottom / right properties set at 0 . .sphere的伪元素子.sphere被拉伸以适合父项的高度和宽度, top / left / bottom / right属性设置为0

  • One way to keep this responsive is to use a viewport width unit for the width and height. 保持此响应的一种方法是使用视口宽度单位作为宽度和高度。 This will keep the height / width 1:1. 这将保持高度/宽度1:1。

Example

 body { background: lightblue; } .sphere { top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%); position: absolute; height: 10vw; width: 10vw; } .sphere:before, .sphere:after { position: absolute; content: ''; border-radius: 50%; left: 0; right: 0; bottom: 0; top: 0; } .sphere:before { background: #FFF; animation: pulsate 2.5s ease-out infinite; } .sphere:after { background: black; } @keyframes pulsate { 0%, 100% { opacity: 0; } 0% { transform: scale(0.1, 0.1); } 50% { opacity: 1.0; } 100% { transform: scale(1.5, 1.5); } } 
 <div class="sphere"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM