简体   繁体   English

我怎样才能使按钮固定在顶部并将内容折叠到其中

[英]How can i make the button fix at the top and the content collapse into it

I tried this code but when i click on the button the content collapses and the button moves down. 我尝试了这段代码,但是当我单击按钮时,内容折叠并且按钮向下移动。 I want the content to collapse and the button remains at the top inline with the other button. 我希望内容折叠,并且该按钮与其他按钮保持在同一行的顶部。

Here is my HTML and CSS code: 这是我的HTML和CSS代码:

 #fixed { margin-top: 60px; } #block { border: solid #BDC3C7 1px; width: 360px; margin-left: 60px; display: inline-block; } .ab { margin: 5px; } #demo { max-width: 350px; color: blue; border: solid #ddd 1px; padding: 5px 12px 0px 12px; margin: 2px -10px 2px 0px; vertical-align: top; white-space: nowrap; font-size: 12px; text-align: left; font-family: Arial, Helvetica, sans-serif; } .list { list-style: none; border-bottom: solid 1px #eee } .as { margin: 0 0 10px 0; padding: 0; } 
 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="js/bootstrap.min.js"></script> <link rel="stylesheet" href="style.css"> </head> <body data-spy="scroll" data-target=".navbar-collapse"> <div class="navbar navbar-default navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a href="http://getbootstrap.com/getting-started/#template" target="_blank" class="navbar-brand">THINK</a> </div> <div class="collapse navbar-collapse"> <ul class="nav navbar-nav navbar-right"> <li><a href="#div1">About Us</a> </li> <li><a href="#div2">Contact Us</a> </li> <li><a href="#div3">Pricing</a> </li> </ul> </div> </div> </div> <div class="container"> <div id="fixed"> <div id="block"> <div class="ab"> <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo">Simple collapsible</button> <div id="demo" class="collapse in"> <ul class="as"> <li class="list">hello</li> <li class="list">hi</li> <li class="list">hello</li> </ul> </div> </div> </div> <div id="block"> <div class="ab"> <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo1">Simple collapsible</button> <div id="demo1" class="collapse in"> <ul class="as"> <li class="list">hello</li> <li class="list">hi</li> <li class="list">hello</li> </ul> </div> </div> </div> </div> </div> </body> </html> 

Give the blocks a vertical align rule. 给块一个垂直对齐规则。

Also never duplicate an html id. 也不要重复html id。 Use classes instead. 改用类。

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <link rel="stylesheet" href="style.css"> <style> #fixed { margin-top: 60px; } /* Changed to class here */ .block { border: solid #BDC3C7 1px; width: 360px; margin-left: 60px; display: inline-block; /* added vertical-align */ vertical-align: top; } .ab { margin: 5px; } #demo { max-width: 350px; color: blue; border: solid #ddd 1px; padding: 5px 12px 0px 12px; margin: 2px -10px 2px 0px; vertical-align: top; white-space: nowrap; font-size: 12px; text-align: left; font-family: Arial, Helvetica, sans-serif; } .list { list-style: none; border-bottom: solid 1px #eee } .as { margin: 0 0 10px 0; padding: 0; } </style> </head> <body data-spy="scroll" data-target=".navbar-collapse"> <div class="navbar navbar-default navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a href="http://getbootstrap.com/getting-started/#template" target="_blank" class="navbar-brand">THINK</a> </div> <div class="collapse navbar-collapse"> <ul class="nav navbar-nav navbar-right"> <li><a href="#div1">About Us</a> </li> <li><a href="#div2">Contact Us</a> </li> <li><a href="#div3">Pricing</a> </li> </ul> </div> </div> </div> <div class="container"> <div id="fixed"> <!-- changed "block" from id to class here --> <div class="block"> <div class="ab"> <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo">Simple collapsible</button> <div id="demo" class="collapse in"> <ul class="as"> <li class="list">hello</li> <li class="list">hi</li> <li class="list">hello</li> </ul> </div> </div> </div> <!-- changed "block" from id to class here --> <div class="block"> <div class="ab"> <button type="button" class="btn btn-info" data-toggle="collapse" data-target="#demo1">Simple collapsible</button> <div id="demo1" class="collapse in"> <ul class="as"> <li class="list">hello</li> <li class="list">hi</li> <li class="list">hello</li> </ul> </div> </div> </div> </div> </div> </body> </html> 

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

相关问题 如何使用 Bootstrap 切换按钮(复选框)折叠内容? - How can I collapse content with a Bootstrap toggle button (checkbox)? 当单击登录按钮并且登录表单折叠时,如何隐藏注册表? - How can i make the register form hide when the login button is clicked and the login form collapse vice versa 我将如何仅使按钮折叠该部分? - How would I make just the button collapse the section? 我该如何修复这个按钮 - How can I fix this button 导航栏展开时,如何使它滚动并固定到页面顶部? - How can I make the navbar scroll and fix to the top of the page when it is expanded? 默认情况下,如何使bootstrap multiselect optgroup崩溃? - How can I make bootstrap multiselect optgroups collapse by default? jQuery设置的margin-top使内容跳转。 如何使它动画流畅? - margin-top set by jQuery makes content jump. How can I make it smoothly animate? 如何确保jQuery加载的可滚动模式内容始终滚动回到顶部? - How can I make sure the jQuery loaded scrollable modal content always scrolls back to the top? 我想让文本内容不隐藏在顶部标题后面,我应该修复哪个 css 属性? - I am trying to make the text content not hide behind the top header, which css property I should to fix it? 如何使按钮内的href崩溃 - How to make href in button inside collapse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM