如何在MVC 5中的Bootstrap 3导航中将链接左右对齐

huangapple go评论109阅读模式
英文:

How do I align links to left and right in a Bootstrap 3 navigation in MVC 5

问题

我正在尝试使我的导航栏看起来像这样。然而,无论我对基本布局代码进行了什么更改,文本似乎都无法对齐到屏幕的右侧。

我的HTML代码如下:

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        @if (Session["Logged_In"] == null)
        {
            <li>@Html.ActionLink("Home", "Index", "OUsers")</li>
            <li class="navbar-right">@Html.ActionLink("Create", "Create", "OUsers")</li>
            <li class="navbar-right">@Html.ActionLink("Login", "Login", "OUsers")</li>                       
        }
        @if (Session["Logged_In"] != null)
        {
            <li>@Html.ActionLink("Home", "LoggedIndex", "OUsers")</li>
            <li>@Html.ActionLink("Delete Account", "DeleteProfile", "OUsers")</li>
            <li>@Html.ActionLink("Edit Profile", "EditProfile", "OUsers")</li>
            <li style="float:right">@Html.ActionLink("Sign Out", "SignOut", "OUsers")</li>
        }
    </ul>
</div>

这并不是完整的布局代码,尽管如此,这是我实际编辑过的部分,似乎与文本对齐相关。此外,我的 "navbar-right" 类仅包含 float: right;text-align: right;

奇怪的是,当我检查网站上的登录和创建操作链接时,“navbar-right” 类被应用,但没有对齐更改。值得注意的是,我尝试将CSS类放在actionlink参数中,但没有任何变化,我还尝试了一个新的包含navbar-right的div,但这似乎将登录对齐到Home actionlink下面,这对我来说也很奇怪。

英文:

I'm trying to get my Navigation Bar to look like this. However the text won't align to the right side of the screen seemingly no matter what changes I make to the base layout code.

My html code is as follows:

&lt;div class=&quot;navbar-collapse collapse&quot;&gt;
    &lt;ul class=&quot;nav navbar-nav&quot;&gt;
        @if (Session[&quot;Logged_In&quot;] == null)
        {
            &lt;li&gt;@Html.ActionLink(&quot;Home&quot;, &quot;Index&quot;, &quot;OUsers&quot;)&lt;/li&gt;
            &lt;li class=&quot;navbar-right&quot;&gt;@Html.ActionLink(&quot;Create&quot;, &quot;Create&quot;, &quot;OUsers&quot;)&lt;/li&gt;
            &lt;li class=&quot;navbar-right&quot;&gt;@Html.ActionLink(&quot;Login&quot;, &quot;Login&quot;, &quot;OUsers&quot;)&lt;/li&gt;                       
        }
        @if (Session[&quot;Logged_In&quot;] != null)
        {
            &lt;li&gt;@Html.ActionLink(&quot;Home&quot;, &quot;LoggedIndex&quot;, &quot;OUsers&quot;)&lt;/li&gt;
            &lt;li&gt;@Html.ActionLink(&quot;Delete Account&quot;, &quot;DeleteProfile&quot;, &quot;OUsers&quot;)&lt;/li&gt;
            &lt;li&gt;@Html.ActionLink(&quot;Edit Profile&quot;, &quot;EditProfile&quot;, &quot;OUsers&quot;)&lt;/li&gt;
            &lt;li style=&quot;float:right&quot;&gt;@Html.ActionLink(&quot;Sign Out&quot;, &quot;SignOut&quot;, &quot;OUsers&quot;)&lt;/li&gt;

        }
    &lt;/ul&gt;
&lt;/div&gt;

This isn't the full Layout code granted however it's what I've actually edited and seems relevant to text alignment. Also my "navbar-right" class consists of only float: right; and text-align: right;

The strange thing is that when I inspect the Login and Create actionlinks on the website the "navbar-right" class is applied yet there is no alignment changed. It may be worth noting I tried putting the css class within the actionlink parameters and nothing changed and I also attmepted a new division with navbar right but that seemed to align Login under the Home actionlink which was also bizarre to me.

答案1

得分: 2

看起来你正在使用Bootstrap 3。那么这将起作用:

<div class="collapse navbar-collapse" id="navbar-collapse">
  <ul class="nav navbar-nav">
    <li><a href="#">链接</a></li>
    <li><a href="#">链接</a></li>
    <li><a href="#">链接</a></li>
    <li><a href="#">链接</a></li>
  </ul>
  <ul class="nav navbar-nav navbar-right">
    <li><a href="#">链接</a></li>
    <li><a href="#">链接</a></li>
  </ul>
</div>
英文:

It seems you are using Bootstrap 3. Then this will work

&lt;div class=&quot;collapse navbar-collapse&quot; id=&quot;navbar-collapse&quot;&gt;
  &lt;ul class=&quot;nav navbar-nav&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul class=&quot;nav navbar-nav navbar-right&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Link&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;

答案2

得分: 0

navbar-nav类具有float:left属性,它会将所有导航都对齐到左侧
尝试覆盖
如何在MVC 5中的Bootstrap 3导航中将链接左右对齐

英文:

The navbar-nav class have float:left property with it which align all the nav to left side
try to override
如何在MVC 5中的Bootstrap 3导航中将链接左右对齐

huangapple
  • 本文由 发表于 2020年1月6日 18:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定