简体   繁体   English

如何使Bootstrap 3布局响应div大小而不是屏幕大小

[英]How to make Bootstrap 3 layout responsive to div size, not screen size

I've got a page with a main content area and a fixed-width sidebar ( https://jsfiddle.net/vesg7wm0/ ). 我有一个页面,其中包含主要内容区域和固定宽度的侧边栏( https://jsfiddle.net/vesg7wm0/ )。 I'd like to populate a section of the sidebar and a section of the main content area with the same responsive HTML (maybe not forever, but at least until I decide where I want to put it). 我想用相同的响应HTML填充侧边栏的一部分和主内容区域的一部分(可能不会永远存在,但至少要等到我决定将其放置在哪里时为止)。 The problem, though, is that Bootstrap seems to decide which sizing (col-md-* vs. col-sm-*) to use based on screen size, not on div size. 但是,问题在于,Bootstrap似乎是根据屏幕大小而不是div大小来决定使用哪种大小设置(col-md- *与col-sm- *)。 This means that the main content and the sidebar will always use the same layout. 这意味着主要内容和侧边栏将始终使用相同的布局。

Is there a way to force the html in the sidebar, which is fixed at 300px, to use the col-sm-* / col-xs-* layout? 有没有办法强制固定在300px的侧边栏中的html使用col-sm- * / col-xs- *布局?

I think iframes are a possible solution ( https://jsfiddle.net/kwzcL8y6/1/ ), but that seems to have a host of its own formatting issues, plus I would have to move that content out to its own page (which, y'know, maybe I should do anyway). 我认为iframe是一种可能的解决方案( https://jsfiddle.net/kwzcL8y6/1/ ),但这似乎有很多自身的格式问题,此外,我还必须将该内容移至其自己的页面( ,您知道,也许我还是应该这样做)。

So, other than iframes, can you make HTML be responsive to its parent div, and not to the total screen size? 因此,除了iframe之外,您还可以让HTML响应其父div,而不响应总屏幕尺寸吗? Or is it iframes or nothing? 还是iframe还是什么?

Thanks! 谢谢!

 /* Latest compiled and minified CSS included as External Resource*/ /* Optional theme */ @import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css'); .sidebar-transition { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; -o-box-sizing: border-box; box-sizing: border-box; -webkit-transition: .50s ease-in-out; -moz-transition: .50s ease-in-out; -o-transition: .50s ease-in-out; transition: .50s ease-in-out; -webkit-text-size-adjust: none; } .MainWindow { width: calc(100% - 15px); margin-left: 15px; height: 100%; overflow-x: hidden; overflow-y: auto; -webkit-overflow-scrolling: touch; } .SideBar { position: fixed; top: 0; left: -300px; height: 100%; width: 295px; overflow-x: hidden; overflow-y: auto; padding: 20px; } .sidebar-toggle { position: absolute; opacity: 0; } .sidebar-toggle-label { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; height: auto; width: auto; display: block; position: fixed; left: 5px; top: 50%; cursor: pointer; background: rgba(0, 0, 0, .0); z-index: 1; } .sidebar-icon:before { content: "\\f138"; } .sidebar-toggle:checked ~ .sidebar-icon:before { content: "\\f137"; } .sidebar-toggle:checked ~ .sidebar-toggle-label { left: 292px; } .sidebar-toggle:checked ~ .SideBar { left: 0px; } .sidebar-toggle:checked ~ .MainWindow { margin-left: 300px; width: calc(100% - 300px); } 
 <div class="container-fluid"> <div class="row"> <input id="sidebar-toggle" type="checkbox" name="sidebar-toggle" class="sidebar-toggle sidebar-transition" checked /> <label for="sidebar-toggle" id="sidebar-toggle-label" class="sidebar-toggle-label fa sidebar-icon bigger-170 sidebar-transition"></label> <div id="SideBar" class="SideBar sidebar-transition"> <div class="row" id="AjaxRow"> <div class="col-md-12"> <div class="row"> <div class="panel panel-primary"> <a data-toggle="collapse" data-parent="#Ajax-heading" href="#Ajax-body" aria-expanded="false" aria-controls="Ajax-body" class="section-header"> <div class="panel-custom panel-heading" role="tab" id="Ajax-heading"> <div class="panel-title font14px"> Sidebar <div class="pull-right"><i id="iAjaxImgSwap" class="fa accordion-icon"></i> </div> </div> </div> </a> <div class="row"> <div class="col-md-12"> <div id="Ajax-body" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="Ajax-heading"> <div class="panel-body"> <div class="form-group"> <div class="row" style="margin-top: 10px"> <div id="ajaxPaneHtml" class="col-md-12"> <div class="row"> <div class="col-md-6 col-sm-3 col-xs-3" style="height: 50px; background-color: green"></div> <div class="col-md-6 col-sm-3 col-xs-3" style="height: 50px; background-color: red"></div> <div class="col-md-6 col-sm-3 col-xs-3" style="height: 50px; background-color: blue"></div> <div class="col-md-6 col-sm-3 col-xs-3" style="height: 50px; background-color: yellow"></div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> <div id="MainWindow" class="MainWindow col-md-12 sidebar-transition"> <div class="row" style="margin-top: 10px"> <div id="mainAjaxPaneHtml" class="col-md-12"> <div class="row"> <div class="col-md-6 col-sm-3 col-xs-3" style="height: 50px; background-color: green"></div> <div class="col-md-6 col-sm-3 col-xs-3" style="height: 50px; background-color: red"></div> <div class="col-md-6 col-sm-3 col-xs-3" style="height: 50px; background-color: blue"></div> <div class="col-md-6 col-sm-3 col-xs-3" style="height: 50px; background-color: yellow"></div> </div> </div> </div> </div> </div> </div> 

You'd have to use JavaScript in order to make this work for an element. 您必须使用JavaScript才能使此功能适用于某个元素。

The reason Bootstrap resizes its columns based on the screen size is because Bootstrap uses media queries. Bootstrap根据屏幕大小调整其列大小的原因是Bootstrap使用媒体查询。 This post explains that media queries are designed to work based on devices or media types . 这篇文章解释说,媒体查询旨在根据设备或媒体类型工作

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

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