简体   繁体   English

无法将边框延伸超出容器的宽度

[英]not able to extend the border beyond the container's width

 .container { display: flex; justify-content: start; border-right:1px solid black; } .side-nav { border-right: 1px solid #111; height: 200px; padding-right: 20px; } .main-content { margin-right: 20px; margin-left: 20px; } .divider { border-bottom: 1px solid gray; } .divider:before{ position: relative; content: ""; width: 100%; top: 1px; right: 22px; padding-right: 20px; padding-left: 20px; border-bottom: 1px solid gray; } 
 <div class="container"> <div class="side-nav"> one </div> <div class="main-content"> two <div class="divider"></div> <p>some content gdvsgsgfghdgfhgdsfgdsgfdsfgdsfgdsgfgdsfgdsgfdsgfdsgfgdsgfffdfghjfghj</p> <div class="divider"></div> </div> </div> 

I'm creating a page layout. 我正在创建页面布局。 Inside the container, there are two containers- side-nav and main-content. 在容器内部,有两个容器-导航栏和主要内容。 In the main-content, there is ap tag with some demo text. 在主要内容中,带有一些演示文本的ap标签。 the p tag is surrounded by two border containers. p标签被两个边框容器包围。 I m not able to extend the border lines upto the main container width. 我无法将边界线扩展到主容器的宽度。 I have given a snippet of it, Can someone please help me to resolve this issue. 我提供了一个摘要,有人可以帮我解决此问题。

If I understand you correctly, add flex: 1 to .main-content so it will take the whole width that left. 如果我对您的理解正确,请在.main-content添加flex: 1 ,这样它将占用剩下的整个宽度。

 .container { display: flex; justify-content: start; border-right: 1px solid black; } .side-nav { border-right: 1px solid #111; height: 200px; padding-right: 20px; } .main-content { margin-right: 20px; margin-left: 20px; flex: 1; } .divider { border-bottom: 1px solid gray; } .divider:before { position: relative; content: ""; width: 100%; top: 1px; right: 22px; padding-right: 20px; padding-left: 20px; border-bottom: 1px solid gray; } 
 <div class="container"> <div class="side-nav"> one </div> <div class="main-content"> two <div class="divider"></div> <p>some content gdvsgsgfghdgfhgdsfgdsgfdsfgdsfgdsgfgdsfgdsgfdsgfdsgfgdsgfffdfghjfghj</p> <div class="divider"></div> </div> </div> 

But seems you have more problems here. 但似乎您在这里还有更多问题。 For example, if there is no enough space, the right border is displayed on top of the text. 例如,如果没有足够的空间,则在文本顶部显示右边框。 Also, small extra top and bottom borders in the left of the .main-content . 此外, .main-content左侧还有一些小的顶部和底部边框。 What're you trying to do? 你想做什么 How it should look? 它应该看起来如何?

You have given right:22px in your :before . 您已在:before赋予right:22px That's causing the issue here. 这就是这里的问题。

 .container { display: flex; justify-content: start; border-right: 1px solid black; } .side-nav { border-right: 1px solid #111; height: 200px; padding-right: 20px; } .divider { border-bottom: 1px solid gray; } .divider:before { position: relative; content: ""; width: 100%; top: 1px; padding-right: 20px; padding-left: 20px; border-bottom: 1px solid gray; } p { word-break: break-word; padding:10px } 
 <div class="container"> <div class="side-nav"> one </div> <div class="main-content"> <p style="padding:0 10px;margin:0">two</p> <div class="divider"></div> <p>some content gdvsgsgfghdgfhgdsfgdsgfdsfgdsfgdsgfgdsfgdsgfdsgfdsgfgdsgfffdfghjfghj</p> <div class="divider"></div> </div> </div> 

I honestly have no idea what you're trying to do here, but the modified example below looks a bit better, no? 老实说,我不知道您要在这里做什么,但是下面的修改示例看起来更好一些,不是吗?

 .container { display: flex; justify-content: start; border-right: 1px solid black; } .side-nav { border-right: 1px solid #111; height: 200px; padding-right: 20px; } .main-content { padding: 0 20px; word-break: break-word; } .divider { border-top: 1px solid tomato; border-bottom: 1px solid gray; display: block; margin: 0 -20px; position: relative; } 
 <div class="container"> <div class="side-nav"> one </div> <div class="main-content"> two <hr class="divider" /> <p>some content gdvsgsgfghdgfhgdsfgdsgfdsfgdsfgdsgfgdsfgdsgfdsgfdsgfgdsgfffdfghjfghj</p> <hr class="divider" /> </div> </div> 

I don't see the reason for having the :before element within the divider if it then has the same color as the border anyways, you could just make the border 2px...or more generally, even if you need different colors, you can just work with top/bottom borders and you don't need the :before at all, you could also choose to got with an <hr /> element for that purpose, would be more semantic anyways =) 我不知道在分隔符中使用:before元素的原因,如果它的颜色仍然与边框相同,则可以将边框设为2px ...或更笼统地说,即使您需要不同的颜色,您也可以可以只使用顶部/底部边框,并且根本不需要:before ,您也可以选择为此添加<hr />元素,无论如何,它会更加语义化=)

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

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