使用Bootstrap的flex类将两个div垂直排列。

huangapple go评论62阅读模式
英文:

Putting 2 divs under each other with Bootstrap flex classes

问题

你可以在上传的图片上看到我的问题。在其中,第一张图片。

当我最小化浏览器窗口宽度时,这两个 div 不会叠在彼此下面,只有当窗口完全最小化,或者在移动设备上查看网站时才会叠在一起。

当窗口太小,以至于这两个 div 可以相互接触时,我希望它们叠在彼此下面。

我尝试了媒体查询,但我认为只用一些 Bootstrap 类就可以做到这一点,这会更好。

使用Bootstrap的flex类将两个div垂直排列。

以下是 HTML 代码片段:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

<section class="header_full_width py-2 px-4">
  <div class="container-fluid">
    <div class="row align-items-center justify-content-center justify-content-lg-between">
      <div>
        <a href="tel:+3630" class="text-white_disabled header_full_width_link text-white_redundant" title="Hívás"><i class="fa fa-phone mr-1 mr-md-2"></i>Kérdésed van? +36 30 123 4567</a>
      </div>

      <div>
        <span class="text-white_disabled header_full_width_link text-white_redundant"><i class="fa fa-envelope mr-1 mr-md-2"></i>info@emailcimem.hu</span>
        <span class="nav_separator_2 text-white mx-1">|</span>

        <a href="kapcsolat" class="text-white_disabled header_full_width_link text-white_redundant" title="Elérhetőségeink"><i class="fa fa-phone-square mr-1 mr-md-2"></i>Kapcsolat</a>
      </div>
    </div>
  </div>
</section>
英文:

You can see my problem on the uploaded image. Within that, the first image.

When I minimize the browser screen width these two divs won't get under each other, just when the window is fully minimized, or when i view the site on mobile.

When the window is too small, so the 2 divs can reach each other, I want them under each other.

I tried media queries, but I think I can do this with only a few Bootstrap classes, and that will be better.

使用Bootstrap的flex类将两个div垂直排列。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;section class=&quot;header_full_width py-2 px-4&quot;&gt;
  &lt;div class=&quot;container-fluid&quot;&gt;
    &lt;div class=&quot;row align-items-center justify-content-center justify-content-lg-between&quot;&gt;
      &lt;div&gt;
        &lt;a href=&quot;tel:+3630&quot; class=&quot;text-white_disabled header_full_width_link text-white_redundant&quot; title=&quot;H&#237;v&#225;s&quot;&gt;&lt;i class=&quot;fa fa-phone mr-1 mr-md-2&quot;&gt;&lt;/i&gt;K&#233;rd&#233;sed van? +36 30 123 4567&lt;/a&gt;
      &lt;/div&gt;
      
      &lt;div&gt;
        &lt;span class=&quot;text-white_disabled header_full_width_link text-white_redundant&quot;&gt;&lt;i class=&quot;fa fa-envelope mr-1 mr-md-2&quot;&gt;&lt;/i&gt;info@emailcimem.hu&lt;/span&gt;
        &lt;span class=&quot;nav_separator_2 text-white mx-1&quot;&gt;|&lt;/span&gt;
        
        &lt;a href=&quot;kapcsolat&quot; class=&quot;text-white_disabled header_full_width_link text-white_redundant&quot; title=&quot;El&#233;rhetős&#233;geink&quot;&gt;&lt;i class=&quot;fa fa-phone-square mr-1 mr-md-2&quot;&gt;&lt;/i&gt;Kapcsolat&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

答案1

得分: 0

你可以使用Bootstrap的rowcol类。
正如您在上面的代码示例中已经看到的那样,您已经使用了row,但没有为子元素的div使用col

Bootstrap认为row有12个单位。因此,对于这两个div,您可以分别给它们赋予col-12 col-lg-6的类。Bootstrap采用移动优先的方式,这意味着直到屏幕尺寸达到lg(992px)之前,这两个div将占满整行的宽度,然后它们将各自占用一半,即6个单位。

“当窗口太小,以至于两个div可以相互接触时,我希望它们排列在下面。”

要回答这个问题,您可以选择在什么时候希望您的div共享同一行。只需将lg替换为适当的断点,参考下面的图像。

使用Bootstrap的flex类将两个div垂直排列。

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

<section class="header_full_width py-2 px-4">
  <div class="container-fluid">
    <div class="row align-items-center justify-content-center justify-content-lg-between">
      <div class='col-12 col-lg-6'>
        <a href="tel:+3630" class="text-white_disabled header_full_width_link text-white_redundant" title="Hívás"><i class="fa fa-phone mr-1 mr-md-2"></i>Kérdésed van? +36 30 123 4567</a>
      </div>
      
      <div class='col-12 col-lg-6'>
        <span class="text-white_disabled header_full_width_link text-white_redundant"><i class="fa fa-envelope mr-1 mr-md-2"></i>info@emailcimem.hu</span>
        <span class="nav_separator_2 text-white mx-1">|</span>
        
        <a href="kapcsolat" class="text-white_disabled header_full_width_link text-white_redundant" title="Elérhetőségeink"><i class="fa fa-phone-square mr-1 mr-md-2"></i>Kapcsolat</a>
      </div>
    </div>
  </div>
</section>
英文:

You can use bootstrap's row and col classes.
As we can already see, in your code example above. You have used row but not col for the children div's

A bootstrap row is considered to have 12 units. So for both the div's you can give class of col-12 col-lg-6. Bootstrap takes a mobile first approach so this means, the div's will take full width of the row till the screen size reaches lg (992px), then the div's will occupy half each i.e. 6 units each.

"When the window is too small, so the 2 div's can reach each other, I want them under each other."

To answer this - you can choose when you want your div's to share the row. Just replace lg with the appropriate breakpoint by refrencing the image below.

使用Bootstrap的flex类将两个div垂直排列。

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N&quot; crossorigin=&quot;anonymous&quot;&gt;

    &lt;section class=&quot;header_full_width py-2 px-4&quot;&gt;
      &lt;div class=&quot;container-fluid&quot;&gt;
        &lt;div class=&quot;row align-items-center justify-content-center justify-content-lg-between&quot;&gt;
          &lt;div class=&#39;col-12 col-lg-6&#39;&gt;
            &lt;a href=&quot;tel:+3630&quot; class=&quot;text-white_disabled header_full_width_link text-white_redundant&quot; title=&quot;H&#237;v&#225;s&quot;&gt;&lt;i class=&quot;fa fa-phone mr-1 mr-md-2&quot;&gt;&lt;/i&gt;K&#233;rd&#233;sed van? +36 30 123 4567&lt;/a&gt;
          &lt;/div&gt;
          
          &lt;div class=&#39;col-12 col-lg-6&#39;&gt;
            &lt;span class=&quot;text-white_disabled header_full_width_link text-white_redundant&quot;&gt;&lt;i class=&quot;fa fa-envelope mr-1 mr-md-2&quot;&gt;&lt;/i&gt;info@emailcimem.hu&lt;/span&gt;
            &lt;span class=&quot;nav_separator_2 text-white mx-1&quot;&gt;|&lt;/span&gt;
            
            &lt;a href=&quot;kapcsolat&quot; class=&quot;text-white_disabled header_full_width_link text-white_redundant&quot; title=&quot;El&#233;rhetős&#233;geink&quot;&gt;&lt;i class=&quot;fa fa-phone-square mr-1 mr-md-2&quot;&gt;&lt;/i&gt;Kapcsolat&lt;/a&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/section&gt;

huangapple
  • 本文由 发表于 2023年6月26日 20:56:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定