2017-10-04 28 views
0

我对Bootstrap相当新,我想知道如何使用九个网格的element s。我有一个菜单保留在我的网页顶部,它有九个element s,五个text/buttonelement s和四个间距element s(我发现这是获得我想要的最简单的方法),并且本身,它看起来像这样:如何使用九个元素的列大小调整

Menu1

这看起来不错,但是,我在引导学会了如何使我的网页调整大小取决于屏幕尺寸。下面是它看起来像在非大版(不带调整大小的变化):

Menu2

现在,如果你知道如何调整工作(我会这样想,你这样做),你知道,它从这里变得更糟。我想知道如何使用grid来允许我的菜单根据屏幕大小自动调整大小。

代码:

#menuBar { 
 
    height: 85px; 
 
    width: 100%; 
 
    background-color: #F2F2F2; 
 
    position: relative; 
 
} 
 
.titleButton, #contactButton { 
 
    font-family: 'Lato', sans-serif; 
 
    font-size: 30px; 
 
    border-bottom: 3px groove; 
 
    border-radius: 2px; 
 
    line-height: 2.5; 
 
} 
 
.titleSpacer { 
 
    margin-right: 10%; 
 
} 
 
.titleButton, .titleSpacer, #contactButton { 
 
    display: inline; 
 
} 
 
.titleButton:hover, #contactButton:hover { 
 
    cursor: pointer; 
 
    border-bottom: 2px groove black; 
 
}
<html> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>CyanCoding | Home</title> 
 
    <link href="index.css" rel="stylesheet" type="text/css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 
    <script src="index.js"></script> 
 
    <link href="https://fonts.googleapis.com/css?family=Lato|Montserrat|Roboto" rel="stylesheet"> 
 
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> 
 
    </head> 
 
    <body> 
 
    <div class = "container-fluid"> 
 
     <div id = "menuBar"> 
 
      <center> 
 
      <div class = "row-fluid"> 
 
       <div class = "titleButton col-lg-offset-1 col-md-offset-1 col-sm-offset-6 col-xs-offset-12" id = "homeButton">Home</div> 
 
       <div class = "titleSpacer col-lg-1 col-md-1 col-sm-6 col-xs-12"></div> 
 
       <div class = "titleButton col-lg-1 col-md-1 col-sm-6 col-xs-12" id = "programsButton">Programs</div> 
 
       <div class = "titleSpacer col-lg-1 col-md-1 col-sm-6 col-xs-12"></div> 
 
       <div class = "titleButton col-lg-1 col-md-1 col-sm-6 col-xs-12" id = "newsButton">News</div> 
 
       <div class = "titleSpacer col-lg-1 col-md-1 col-sm-6 col-xs-12"></div> 
 
       <div class = "titleButton col-lg-1 col-md-1 col-sm-6 col-xs-12" id = "aboutButton">About</div> 
 
       <div class = "titleSpacer col-lg-1 col-md-1 col-sm-6 col-xs-12"></div> 
 
       <div id = "contactButton" class = "col-lg-1 col-md-1 col-sm-6 col-xs-12">Contact</div> 
 
       <div class="col-lg-1 col-md-1 col-sm-1 hidden-xs"></div> 
 
      </div> 
 
      </center> 
 
     </div> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
 
    </body> 
 
</html>

P.S我真的只是改变了结果col-lg因为我在大屏幕上观看它,想通后,我能解决别人。如果你可以在你的答案中得到其他尺寸的工作,这是非常受欢迎的。

P.P.S查看代码片段时,它可能看起来很奇怪。即使您看到整页结果,也会看到在十二个columngrid中使用九个element时会发生什么情况。

+0

你不需要'col-lg- *'类,因为'col-md- *'类是用相同的数字定义的。我的意思是,如果你有'col-md-1','col-lg-1'没用。 –

回答

0

还有就是要实现这一目标的一种方法:

代码:

#menuBar { 
 
    background-color: #F2F2F2; 
 
    position: relative; 
 
    font-family: 'Lato', sans-serif; 
 
} 
 
.title-button { 
 
    font-size: 30px; 
 
    border-bottom: 2px groove; 
 
    text-align: center; 
 
    line-height: 2.5; 
 
} 
 
.title-button:hover { 
 
    cursor: pointer; 
 
    border-bottom: 2px groove black; 
 
} 
 
@media(min-width: 768px) and (max-width: 991px) { 
 
    .title-button { 
 
    font-size: 20px; 
 
    } 
 
}
<div class="container-fluid"> 
 
    <div id="menuBar" class="clearfix"> 
 
    <div class="row-fluid"> 
 
     <div class="title-button col-sm-2 col-sm-offset-1">Home</div> 
 
     <div class="title-button col-sm-2">Programs</div> 
 
     <div class="title-button col-sm-2">News</div> 
 
     <div class="title-button col-sm-2">About</div> 
 
     <div class="title-button col-sm-2">Contact</div> 
 
    </div> 
 
    </div> 
 
</div>

Codepen here


正如你可以看到:

  • 我删除了分隔符,因为它不是一个好办法。如果你不想让你的border-bottom填充所有的宽度,你可以创建一个spanelement并将border应用于它。

  • 在从768px991px大小我申请一个media-query降低您的font-size因为5 element S IN两行不填都行。但是,如果您想要最后一个element,请填写所有最后一行,将您的class es更改为col-sm-12 col-md-2

  • 请勿使用<center>标记。它已被弃用。More info

  • 请勿为您的类名使用骆驼案例,请用短划线分隔这些单词。