简体   繁体   English

如何使用引导程序类将div居中放置在其他div中?

[英]How can I center div, inside other div with bootstrap classes?

I'm working on a responsive web page using Bootstrap and SASS. 我正在使用Bootstrap和SASS处理响应式网页。

How can I center the maintext div and Logo , when the screen has smaller size? 当屏幕较小时,如何将maintext divLogo 居中 I've tried a variety of options, such as: 我尝试了多种选择,例如:

@media (max-width: 750px) {

    #maintext {
     margin-left: 0;
     margin-right: 0;

    }
}

I also tried to place maintext div inside another div centerhelper , and to do so: 我还尝试将maintext div放置在另一个div centerhelper中 ,并这样做:

@media (max-width: 750px) {
    .centerhelper {
        left: 50%;
        border: solid blue 2px;
    }

    #maintext {
     left: -50%;

    }
 }

But the height of the centerhelper div was equal to zero, I tried different "display" CSS for the centerhelper div and maintext div , but did not succeed. 但是centerhelper div的高度等于零,我为centerhelper divmaintext div尝试了不同的“ display” CSS,但是没有成功。

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

 <!-- NAVIGATION TOP -->
        <nav class="navbar my-navbar navbar-inverse">
          <div class="container">
            <div class="navbar-header">

     <!-- LOGO -->
              <a href="#" class="my-logo pull-left"><img src="img/Logo.png"></a> 
      <!-- LOGO -->          
               <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>

            </div>
            <div id="navbar" class="collapse navbar-collapse pull-right">
              <ul class="nav navbar-nav">
                <li><a href="#"><button class="navbutton active">Home</button></a></li>
                <li><a href="#"><button class="navbutton">About</button></a></li>
                <li><a href="#"><button class="navbutton">Services</button></a></li>
                  <li><a href="#"><button class="navbutton">Portfolio</button></a></li>
                <li><a href="#"><button class="navbutton">Blog</button></a></li>
                <li><a href="#"><button class="navbutton lastbutton">Contact</button></a></li>
              </ul>
            </div><!--/.nav-collapse -->
          </div>
        </nav>

        <!-- NAVIGATION TOP END -->
        <!-- SHOWCASE -->  
          <section class="showcase">
            <div class="container showcase-content">

<!-- MAINTEXT DIV -->
               <div class="centerhelp"> <div id="maintext" ></div></div>
<!-- MAINTEXT DIV -->                
             </div>
          </section>
        <!-- SHOWCASE END -->

And CSS: 和CSS:

.showcase {

  background-image: url(...);
  height: 380px;
  width: 100%;

#maintext {
    padding: 0px;
    display: inline-block;
    float: left;

  margin-top: 91px;
  width: 430px;

    h1 {
        padding: 0;
        margin: 0;
    }
    p {
        padding: 0;
        margin: 0;
        }
    }

Here you have a working jsfiddle of your example 在这里,您可以使用示例的jsfiddle

@media (max-width: 1000px) {
    .centerhelper {
        left: 50%;
        border: solid blue 2px;
     }
     div#maintext {
       text-align: center;
     }
     .navbar-header{
       text-align: center;
     }
    .pull-left{
      float:none!important;
    }
}

Why don't you try this, This would be in absolute center in all screen sizes. 您为什么不尝试此操作呢?在所有屏幕尺寸中,这绝对是中心。 :) :)

 .popup{ position:fixed; left:50%; } .popup .wrapper{ position:relative; left:-50%; /*popup-styles*/ background-color:#fff; padding:10px; border:solid 2px #444; border-radius:10px; } html{background-color:#aaa;} 
 <div class="popup"> <div class="wrapper"> <img src="http://farm4.staticflickr.com/3721/8826906676_501192b1c4.jpg"> </div> </div> 

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

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