简体   繁体   English

div宽度大于100%,并将溢出的部分隐藏在视口之外

[英]div width greater than 100% and hide overflowing part out of the viewport

I am working on a css piece where I need to render a circle at the top of the div. 我正在处理一个css件,需要在div的顶部绘制一个圆。 I got to do that with border radius. 我必须使用边界半径来做到这一点。

But I wanted to get a smoother curve, so I got to increase the width and align it using left value. 但是我想获得更平滑的曲线,所以我必须增加宽度并使用左值将其对齐。 But now I get a horizontal scroll bar in the page. 但是现在我在页面中有了一个水平滚动条。 Is there a way to avoid the horizontal bar. 有没有办法避免单杠。 I tried to overflow values and that didn't seem to help. 我试图溢出值,但这似乎没有帮助。

PS: This is for < 700px media query. PS:这是针对<700像素的媒体查询。

#semi-circle {
    height: 70%;
    width: 180%;
    border-radius: 235px 235px 0 0;
    -moz-border-radius: 235px 235px 0 0;
    -webkit-border-radius: 500px 500px 0 0;
    background-color: red;
    top: 157px;
    margin-right: 0;
    left: -215px;
    overflow: auto;
}

https://jsfiddle.net/5oorwmxd/1/ https://jsfiddle.net/5oorwmxd/1/

You need to wrap your div with another, and then overflow-hidden the wrapping element. 您需要用另一个包裹div,然后将包裹元素overflow-hidden起来。 For instance: 例如:

https://jsfiddle.net/5oorwmxd/3/ https://jsfiddle.net/5oorwmxd/3/

CSS: CSS:

.wrapper{
  overflow:hidden;
  width:100%;
}

HTML: HTML:

<div class="wrapper">
  <div id="semi-circle">test</div>
</div>

Can you put it inside a container with 100% width and hidden overflow? 您可以将其放入宽度为100%且隐藏溢出的容器中吗?

<div class="container">
     <div id="semi-circle">test</div>
</div>

and CSS 和CSS

.container {width:100%; overflow:hidden;}

https://jsfiddle.net/6q8nndzL/ https://jsfiddle.net/6q8nndzL/

To achieve what you want you can just use the border-top-left-radius property instead of using the shorthand border-radius . 要实现您想要的功能,您可以仅使用border-top-left-radius属性,而不是使用简写的border-radius


jsFiddle jsFiddle


CODE SNIPPET: 代码片段:

 #semi-circle { background-color: tomato; padding-left: 15px; transition: border-top-left-radius 1s ease-in; } @media (max-width: 700px) { #semi-circle { border-top-left-radius: 235px; } } 
 <div id="semi-circle">test</div> 


NOTES: 笔记:

You have several things wrong in your code. 您的代码中有几处错误。

First, you cannot set top, left, right or bottom values if your element's position is static, which is the default position value for every element. 首先,如果元素的位置是静态的,则不能设置顶部,左侧,右侧或底部的值,这是每个元素的默认位置值。 So just remove those properties. 因此,只需删除这些属性。

Second, using a percentage in height requires the element's parent to have a defined height, which in this case it does not, so remove it. 其次,使用高度百分比要求元素的父级具有定义的高度,在这种情况下,它没有定义的高度,因此将其删除。

There's no need to increase the width , nor set margin-right to 0. 无需增加width ,也无需将margin-right设置为0。

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

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