Html 如何在引导程序 4 中将按钮向右对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47658730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:59:51  来源:igfitidea点击:

How to align button to right in bootstrap 4

htmlcsstwitter-bootstrap

提问by Nathan Ruth

I tried looking this up here but I can't find anything related to this specific problem.

我尝试在此处查找此内容,但找不到与此特定问题相关的任何内容。

I'm trying to make a header which has a button on the left and right of the screen with a logo in the middle, but whenever I make the window smaller or larger the button on the right doesnt stay aligned.

我正在尝试制作一个标题,它在屏幕的左侧和右侧有一个按钮,中间有一个徽标,但是每当我使窗口变小或变大时,右侧的按钮就不会保持对齐。

Small window

小窗

Large window

大窗户

Here is the code:

这是代码:

<header>
        <div id="container-fluid">
            <div class="row">               
                <div class=" col-2">
                    Menu
                </div>
                <div class="col-8 text-center ">
                    Logo
                </div>
                <div class="col-2 float-right">
                    Contact
                </div>
            </div>
        </div>            
     </header>

What am I missing here?

我在这里缺少什么?

Thanks in advance.

提前致谢。



Fixed it, code now looks like this:

修复它,代码现在看起来像这样:

    <body> 
    <header>
        <div id="container-fluid">
            <div class="row">               
                <div class="col">
                    <span class="float-left">Menu</span>
                </div>
                <div class="col text-center">
                    Logo                       
                </div>
                <div class="col">
                    <span class="float-right">Contact</span>
                </div>
            </div>
        </div>            
     </header>          
</body>

回答by Dalin Huang

wrap your content so it can be aligned according to the parent container.

包装您的内容,以便它可以根据父容器对齐。

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwtheitroadesjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

<header>
  <div id="container-fluid">
    <div class="row">
      <div class="col-2">
        <span class="float-left">Menu</span>
      </div>
      <div class="col-8 text-center">
        Logo
      </div>
      <div class="col-2">
        <span class="float-right">Contact</span>
      </div>
    </div>
  </div>
</header>

回答by Nathan Ruth

You havn't defined what size of column grid you are using. Using col-xs,col-sm, col-md,col-lg will have different effects for the particular code. Run the snippet code in full page below and see how the col-size affects when the browser is resized.

您还没有定义您使用的列网格的大小。使用 col-xs,col-sm, col-md,col-lg 将对特定代码产生不同的影响。在下面的整页中运行片段代码,并查看调整浏览器大小时 col-size 的影响。

Update - Bootstrap 4 has replaced the col-xs with col- .

更新 - Bootstrap 4 已将 col-xs 替换为 col- 。

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwtheitroadesjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

<div id="container-fluid">
  Col:xs
  <div class="row">
    <div class=" col-2">
      Menu
    </div>
    <div class="col-8 text-center ">
      Logo
    </div>
    <div class="col-2 float-right">
      Contact
    </div>
  </div>
</div>
<br>
<br>

<div id="container-fluid">
Col:sm
  <div class="row">
    <div class=" col-sm-2">
      Menu
    </div>
    <div class="col-sm-8 text-center ">
      Logo
    </div>
    <div class="col-sm-2 float-right">
      Contact
    </div>
  </div>
</div>
<br>
<br>

<div id="container-fluid">
Col:md
  <div class="row">
    <div class=" col-md-2">
      Menu
    </div>
    <div class="col-md-8 text-center ">
      Logo
    </div>
    <div class="col-md-2 float-right">
      Contact
    </div>
  </div>
</div>
<br>
<br>


<div id="container-fluid">
Col:lg
  <div class="row">
    <div class=" col-lg-2">
      Menu
    </div>
    <div class="col-lg-8 text-center ">
      Logo
    </div>
    <div class="col-lg-2 float-right">
      Contact
    </div>
  </div>
</div>

回答by Black Sheep

You can use the flex-bootstrap-classand justify-contentfor this approach:

您可以使用flex-bootstrap-classjustify-content这种方法:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

<div class="container">
  <div class="row d-flex justify-content-between bg-light">
    <div class="p-2 bg-dark text-light">
      Menu
    </div>
    <div class="p-2 bg-dark text-light">
      Logo
    </div>
    <div class="p-2 bg-dark text-light">
      Contact
    </div>
  </div>
</div>