我试图在Bootstrap 4移动屏幕和桌面屏幕中定位三个不同的元素。我需要按照以下方式在移动设备中占据一席位置:
左侧图像,中央徽标图像和右侧切换导航栏按钮。
我试过但我可以找到正确的切换按钮和中心徽标图像。如果我试图将左图像弄乱整个导航栏。请参阅下面的代码。
<nav class="navbar fixed-top navbar-toggleable-md navbar-inverse" id="mainNav">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarExample" aria-controls="navbarExample" aria-expanded="false" aria-label="Toggle navigation">
<i class="fa fa-bars"></i>
</button>
<div class="container nav_div">
<ul class="nav navbar-nav navbar-logo mx-auto">
<li class="nav-item">
<a class="nav-link" href="#">Brand</a>
</li>
</ul>
<div class="collapse navbar-collapse" id="navbarExample">
<ul class="navbar-nav mr-auto">
<?php
foreach ($this->session->userdata('menu') as $key => $value) {
if($value->label == 'Home'){
$function_nm = 'index';
}elseif ($value->label == 'My info') {
$function_nm = 'myInfo';
}elseif ($value->label == 'Inbox') {
$function_nm = 'inbox';
}elseif ($value->label == 'Settings') {
$function_nm = 'setting';
}
?>
<li class="nav-item"><a class="nav-link" href="<?php echo base_url() ?>customerController/<?php echo $function_nm ?>"><?php echo $value->label ?></a></li>
<?php
}
?>
<li class="nav-item"><a class="nav-link" href="<?php echo base_url() ?>customerController/logout">Logout</a></li>
</ul>
</div>
</div>
<!-- <div style="text-align: center;">test</div>-->
</nav>
---------- -------------- CSS
#mainNav
{
background: #00a3d6;
}
.navbar-toggler
{
font-size: 1.85rem;
}
.navbar-logo
{
font-size: 16px !important;
text-align: center !important;
margin-top: 0px;
}
在桌面视图中打开此菜单时,左侧图像应位于右侧。徽标图像旁边有导航菜单。目前,徽标和导航菜单处于完美位置。我只需要在移动设备和桌面上定位额外的图像,如上所述。
答案 0 :(得分:0)
实现这一目标唯一需要的是将“其他图像”定位为HTML中的第一个项目(在导航栏中),然后为其提供课程
order-md-last
。这将在中等(md
)或更大的屏幕上将图像重新排序为 last 项目(并在较小的屏幕上恢复为默认值)。
另外,请确保使用实际的Bootstrap 4类和导航栏组件。您目前没有按照您的代码进行判断。
这是一个正常工作的代码段(点击下面的“运行代码段”并展开到完整页面进行测试):
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<nav class="navbar fixed-top navbar-expand-md navbar-dark bg-dark">
<div class="navbar-brand order-md-last">
<!-- the 'other image' comes here -->
<img class="" src="https://placehold.it/130x30" alt="other image">
</div>
<a class="navbar-brand" href="#">
<!-- the 'logo image' comes here -->
<img src="https://placehold.it/60x30" alt="logo image">
</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
</div>
</nav>
您还会注意到我将“其他图片”包含在类navbar-brand
的div中。这是为了使其垂直对齐与徽标图像相同。