简体   繁体   English

将 HR 宽度扩展到所有屏幕而忽略容器的宽度

[英]Extend HR width to all screen ignoring container's width

I have an HR element inside a 50%'s width div container and I would like the HR to extend to all the width of the screen ignoring it's parent width.我在50% 宽度的div容器中有一个HR元素,我希望HR扩展到屏幕的所有宽度,而忽略它的父宽度。

I know I can solve this by applying the HR outside the container and use relative position to make it move where it has to be... but is there another solution?我知道我可以通过在容器外应用HR并使用相对位置使其移动到它必须的位置来解决这个问题……但是还有另一种解决方案吗?

I tried setting the width more than 100% , lets say 300% and let it overflow, but it will only enlarge to the right (and show an ugly scrollbar).我尝试将宽度设置为超过100% ,比如说300%并让它溢出,但它只会向右放大(并显示一个丑陋的滚动条)。

Browser treat HR as a block but I was unable to center it with margin: 0 auto (after resizing it as above).浏览器将HR视为一个块,但我无法使用margin: 0 auto将其居中(按上述方式调整大小后)。

Is there a way to accomplish this without relative positioning?有没有办法在没有相对定位的情况下实现这一点?

You can use a negative margin for the hr like this:您可以像这样为hr使用负边距:

hr {
  width: 200%;
  margin-left: -50%;
}​

Fiddle小提琴

Note, these %s will only extend to the end of the viewport if its parent div is at the top level and/or no other parent has width , padding , or margin attached.请注意,这些 %s 仅在其父div位于顶层和/或没有其他父级具有widthpaddingmargin附加时才会延伸到视口的末尾。 To handle that, maybe try a width: 9999px; margin-left: -3333px;为了解决这个问题,也许可以尝试一个width: 9999px; margin-left: -3333px; width: 9999px; margin-left: -3333px; and an overflow: hidden on the bodyoverflow: hiddenbody

I stumbled upon this question after searching for a similar thing.在搜索类似的东西后,我偶然发现了这个问题。 Since (sadly) I didn't find a CSS-only solution for this problem, I ended up using jQuery to do the job.由于(遗憾的是)我没有找到针对此问题的纯 CSS 解决方案,因此我最终使用 jQuery 来完成这项工作。

function hrExpand() {
   var windowWidth = $(window).width();
   $('hr').width(windowWidth).css('margin-left', function(){
     return '-' + ($(this).offset().left) + 'px';
   });
}

Just make sure to run the function both onload and onresize.只需确保同时运行 onload 和 onresize 函数。

Example: http://codepen.io/anon/pen/qEOoGb示例: http : //codepen.io/anon/pen/qEOoGb

(You could replace $('hr') with eg $('.content > hr') to increase performance a bit and prevent it from replacing ALL the hr's on your page) (您可以将 $('hr') 替换为例如 $('.content > hr') 以稍微提高性能并防止它替换您页面上的所有 hr)

If you use the right values, using relative positioning works without a scrollbar:如果使用正确的值,则使用相对定位无需滚动条即可:

div {
    width: 50%;
    min-height: 20em;
    margin: 0 auto;
}

hr {
    width: 200%;
    position: relative;
    left: -50%;
}

If a div is 50%, and you want the hr to be 100% of the root (twice as big as 50%), use 200%.如果 div 为 50%,并且您希望 hr 为根的 100%(是 50% 的两倍),请使用 200%。 For a 25% div, use a 400% hr, etc对于 25% div,使用 400% hr 等

Demo演示

If working with tailwind:如果使用顺风:

<hr class="my-2 -ml-10 -mr-10"/> 

I have not tested bootstrap我还没有测试过引导程序

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

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