简体   繁体   English

如何使CSS元素超出包含div的所有边界?

[英]How to make a CSS element extend beyond all borders of containing div?

I have a JS Fiddle going of something that I am trying to do here. 我有一个JS小提琴,我想在这里做些什么。 As you can see I have a circle sitting in the center of a div. 如你所见,我有一个圆圈坐在div的中心。 When you run the script it expends evenly in all directions until it hits the left and top of the screen. 当您运行脚本时,它会在所有方向上均匀消耗,直到它到达屏幕的左侧和顶部。 What I want to happen is the circle to expand evenly in all directions (through all of the containing divs borders) so that it fills the screen with red, but I am at a loss on how to get it to overflow left and top. 我想要发生的是圆圈在所有方向均匀扩展(通过所有包含div的边框),以便它用红色填充屏幕,但我不知道如何让它溢出左上方。

here is the fiddle: 这是小提琴:

http://jsfiddle.net/dkarasinski/UxtJV/711/ http://jsfiddle.net/dkarasinski/UxtJV/711/

HTML

<div class="outerwrapper">
    <div class="test">
        <div class="circle"></div>
    </div>
</div>

jquery

$(document).ready(function() {
setTimeout(function() {
$('.circle').addClass('open');
}, 2000);
});

Any help would be appreciated! 任何帮助,将不胜感激!

I think it's more to do with your top and left absolute on the .open css class style. 我认为这更多地与你的.open css类风格的上下绝对有关。

Here's a fork of your fiddle with -200px on both top and left: http://jsfiddle.net/ghopkins/3eybkrz1/ 这是你的小提琴的叉子,上下左右是-200px: http//jsfiddle.net/ghopkins/3eybkrz1/

Change in code: 代码更改:

div.open {
 top: -200px;
 left: -200px;
 width: 1000px;
 height: 1000px; 
}

Your version is pinning the top left 'corner' of the circle and expanding down and right. 您的版本固定在圆圈的左上角,并向下和向右扩展。

You're explicitly putting the <div> at (0, 0) on the page. 您明确地将<div>放在页面上的(0, 0)

Instead of trying to figure out the position, you can just take advantage of the transform property: 您可以利用transform属性,而不是试图找出位置:

div.open {
    transform: scale(50, 50);
}

That makes the circle 50 times bigger in both the X and Y axes. 这使得圆圈在X轴和Y轴上都大50倍。 The element won't change position; 元素不会改变位置; it'll just get bigger. 它会变得更大。

you can use this 你可以用它

transform: translate(-25%, -25%);

demo - http://jsfiddle.net/UxtJV/714/ 演示 - http://jsfiddle.net/UxtJV/714/

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

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