简体   繁体   English

在页面上以全宽显示 div 时出现问题

[英]Having issue displaying div to full width on a page

I am trying to display divs side by side on a page.我试图在页面上并排显示 div。 However, if there is only one col-md-6 div class on the page, then I am trying to display it full width(100%) rather than 50%.但是,如果页面上只有一个col-md-6 div class,那么我会尝试以全宽 (100%) 而不是 50% 显示它。 Currently it's using only 50% even if there is only one col-md-6 .目前即使只有一个col-md-6它也只使用了 50%。 Is there a way to do this using CSS?有没有办法使用 CSS 来做到这一点? Here is the my HTML and CSS:这是我的 HTML 和 CSS:

   <div class="col-md-6 textcontent">
   </div>

   <div class="col-md-6 service">
   </div>

   <div class="col-md-6 textcontent">
   </div>

CSS CSS

.col-md-6{
width50%;
}

Flexbox is the way to go here. Flexbox 是这里通往 go 的路。 Here is an example:这是一个例子:

 .wrap { display: flex; flex-direction: row; justify-content: space-evenly; margin-bottom: 2rem; }.item { flex-grow: 1; /* For display purposes */ padding: 1rem; }.pink { background-color: pink; }.blue { background-color: lightblue; }.green { background-color: lightgreen; }.orange { background-color: coral; }
 <section class="wrap"> <div class="item pink"> Content 1 </div> <div class="item blue"> Content 2 </div> <div class="item green"> Content 3 </div> </section> <section class="wrap"> <div class="item orange"> Content Solo </div> </section>

https://jsfiddle.net/willihyde/aqrs410u/1/ https://jsfiddle.net/willihyde/aqrs410u/1/

If you are using the bootstrap, You can use the col-md-12 class to add a full width column.如果您使用的是引导程序,则可以使用 col-md-12 class 添加全宽列。 bootstrap grid system is coming as a fraction system and your number will be divide by 12. So if we divide the 6 by 12 the answer is 0.5 and that means 50%.引导网格系统作为分数系统出现,你的数字将除以 12。所以如果我们将 6 除以 12,答案是 0.5,这意味着 50%。 That's why it's adding a 50% width column.这就是它添加 50% 宽度列的原因。 So when we add 12 it will divide by 12/12 and the width will be 100%.所以当我们加 12 时,它将除以 12/12,宽度将为 100%。 You can follow the Bootstrap grid system guideline to learn more about the various width ratios and how to make a responsive grid.您可以按照Bootstrap 网格系统指南来了解有关各种宽度比例以及如何制作响应式网格的更多信息。

<div class="col-md-12 service"></div>

If you are trying to build this by yourself, Define another class with a name and add the width: 100%;如果您尝试自己构建它,请使用名称定义另一个 class 并添加宽度:100%;

.col-md-12{
   width:100%;
}

Then add that class name to your html class attribute然后将该 class 名称添加到您的 html class 属性中

<div class="col-md-12 service"></div>

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

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