简体   繁体   English

Bootstrap静态列宽

[英]Bootstrap Static Column Width

This is more of a best practice question than anything. 这比任何事情都更像是一个最佳实践问题。 So, say I have two cols in Bootstrap and I want the right col to be set at 300px until it hits the 768px breakpoint then have it stack. 所以,假设我在Bootstrap中有两个cols,我希望将右侧col设置为300px,直到它达到768px断点,然后将其堆叠。

 <div class="container"> <div class="row"> <!-- This column needs to fill the rest of the container fluidly --> <div class="col-sm-8"></div> <!-- This column needs to stay 300px --> <div class="col-sm-4"></div> </div> </div> 

My initial solution was to just add classes to each container and statically set the width of both containers for each media breakpoint then set width to auto on the stack breakpoint. 我的初始解决方案是只为每个容器添加类,并为每个介质断点静态设置两个容器的宽度,然后在堆栈断点上将宽度设置为auto。 However, I don't like this solution because it's extremely verbose and it seems too fragile to set the columns both up as static. 但是,我不喜欢这个解决方案,因为它非常冗长,并且将列设置为静态似乎太脆弱了。 I'd much prefer the left column use a dynamic mix-in OR be set with a percentage. 我更喜欢左栏使用动态混合或用百分比设置。

Any thoughts? 有什么想法吗? Thanks! 谢谢!

For this particular scenario, my best suggestion would actually be to not use Bootstrap for the functionality you're after. 对于这种特殊情况,我最好的建议实际上是不使用Bootstrap来实现您所需的功能。 You can achieve this rather trivially with another kind of solution. 你可以通过另一种解决方案轻松实现这一目标。 May I suggest an alternative? 我可以建议另类吗?

Introducing display: flex 介绍display:flex

The Flexbox Layout (Flexible Box) module (currently a W3C Last Call Working Draft) aims at providing a more efficient way to lay out, align and distribute space among items in a container, even when their size is unknown and/or dynamic (thus the word "flex"). Flexbox布局(灵活盒子)模块(目前是W3C最后呼叫工作草案)旨在提供一种更有效的方式来布置,对齐和分配容器中的项目之间的空间,即使它们的大小未知和/或动态(因此“flex”这个词。

I've written a pen that demonstrates my idea of how to tackle this, which you can see here . 我写了一支笔,展示了我如何解决这个问题的想法, 你可以在这里看到

Let's take a look at the code, first up, new HTML: 我们来看看代码,首先是新的HTML:

<div class="flex-container">
  <div class="left-content content">

  </div>
  <div class="right-content content">

  </div>
</div>

We've got a similar structure to what you're already dealing with here, just we've changed the format a little (I've used class names which should help illustrate what's happening.) 我们已经有了类似于你在这里处理的结构,只是我们已经改变了一点格式(我使用的类名应该有助于说明发生了什么。)

And here's the accompanying CSS: 这是附带的CSS:

.flex-container {
  display: flex;
  flex-flow: row;
}

.content {
  min-height: 500px;
}

.left-content {
  background-color: rgba(0,0,0,0.2);
  flex: 1 1 auto;
}

.right-content {
  width: 300px;
  background-color: rgba(0,0,0,0.4);
}

@media (max-width: 768px) {
  .flex-container {
    flex-flow:column;
  }
  .right-content {
    width: 100%;
  }
}

So initially, we want to use the flex-flow: row property to get our elements to display side-by-side (this means the container is arranging its children in a row essentially). 所以最初,我们想要使用flex-flow: row属性来使我们的元素并排显示(这意味着容器基本上将其子元素排列在一行)。 We set a fixed width of 300px on the right-hand column, and then use the property flex: 1 1 auto; 我们在右侧列上设置固定宽度为300px ,然后使用属性flex: 1 1 auto; on the left-hand, which in a little more detail is... 在左边,更详细的是......

This is the shorthand for flex-grow, flex-shrink and flex-basis combined. 这是flex-grow,flex-shrink和flex-basis组合的简写。 The second and third parameters (flex-shrink and flex-basis) are optional. 第二和第三个参数(flex-shrink和flex-basis)是可选的。 Default is 0 1 auto. 默认值为0 1 auto。

The property above tells the item to fill the remaining space of the container. 上面的属性告诉项目填充容器的剩余空间。

Stacking depending on viewport size 根据视口大小进行堆叠

You can see I've used a basic media query with max-width: 768px , when the viewport is smaller than this, we simply stack the content areas by setting flex-flow: column on the parent container and width: 100% on its children, which tells the browser to treat the container as a column and thus stack its elements on top of one another. 您可以看到我使用了max-width: 768px的基本媒体查询,当视口小于此时,我们只需通过设置父容器上的flex-flow: columnwidth: 100%来叠加内容区域children,告诉浏览器将容器视为一个列,从而将其元素堆叠在一起。

If anything's unclear please let me know and I'll improve my answer. 如果有什么不清楚请告诉我,我会改进我的答案。

You should avoid using bootstrap's built in columns for this task. 您应该避免使用bootstrap的内置列来执行此任务。

You can (and should!) define your own media size rules for your own classes using @media when they don't conform to bootstrap's default behavior. 您可以(并且应该!)在不符合引导程序默认行为的情况下,使用@media为您自己的类定义自己的媒体大小规则。 For example, @media (min-width: 768px) { .my-class: { width: 300px; } } 例如,@ @media (min-width: 768px) { .my-class: { width: 300px; } } @media (min-width: 768px) { .my-class: { width: 300px; } } . @media (min-width: 768px) { .my-class: { width: 300px; } } You can read up on @media at Mozilla Dev . 您可以在Mozilla Dev上阅读@media

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

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