I am trying to make a website with a horizontal navigation bar. I found a useful reference, but I couldn't figure it out to be adopted to my current code since I am not an expert with HTML and CSS.
I like to create similar bar like this:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
/* Change the link color to #111 (black) on hover */
li a:hover {
background-color: #111;
}
And below are my current code:
{% block sidebar %}
<ul class="sidebar-nav">
<li>Home</li>
<li>All users</li>
<li>All productss</li>
<!-- <li>All vendors</li> -->
</ul>
{% endblock %}
.sidebar-nav {
margin-top: 20px;
padding: 0;
list-style: none;
}
================================================================
Thank you for your help, but none of below three answers worked.
Here I add more of my .css code. I am guessing "col-sm-2" ruled my current code, but don't know how to adjust it to make a horizontal navigation bar.
<div class="container-fluid">
<div class="row">
<div class="col-sm-2">
{% block sidebar %}
<ul class="sidebar-nav">
<li>Home</li>
<li>All users</li>
<li>All productss</li>
<!-- <li>All vendors</li> -->
</ul>
{% endblock %}
</div>
<div class="col-sm-10 ">{% block content %}{% endblock %}</div>
</div>
'''
The trick to get the list elements stack horizontally is to use the attribute float:left on the list items. Try adding something like this in your css-code
.sidebar-nav li {
float:left;
}
By writing .sidebar-nav li you apply the style to all list-items (li) that is a child of sidebar-nav.
Update:
Now that we know you're using bootstrap, you can make a horizontal navbar just by adding a few classes:
The following code gives you a navbar with beautiful padding and margins:
Bootstrap navbar reference: https://getbootstrap.com/docs/4.3/components/navs/
<div class="container-fluid">
<div class="row">
<div class="col-12">
<!--'col-sm-12' to 'col-12' for 100% width -->
{% block sidebar %}
<ul class="sidebar-nav nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'index' %">Home</a>
</li>
<li class="nav-item"><a class="nav-link" href="">All users</a></li>
<li class="nav-item">
<a class="nav-link" href="">All productss</a>
</li>
<!-- <li class="nav-item"><a class="nav-link" href="">All vendors</a></li> -->
</ul>
{% endblock %}
</div>
<div class="col-sm-10 ">{% block content %}{% endblock %}</div>
</div>
</div>
The previous 'non-bootstrap' answer:
The goal is to align the "li" items horizontally so a simple and easy way is :
.sidebar-nav li{
display: inline;
}
Display property reference:
https://www.w3schools.com/cssref/pr_class_display.asp
EDIT: I Assume you are using the bootstrap grid system.
The class col-sm-2 means your div will occupy 1/6th or 16.66% of the total container width on devices having screen width greater than 767px.
If you want your menu items to occupy the entire screen width, replace col-sm-2 with col-sm-12.
My preferred way to do this is using flex.
.sidebar-nav {
display: flex;
}
This will ensure all elements inside the ul are displayed in a single line and they all have the same height which is equal to the height of the ul. Hope this helps.
Related
I'm trying to have a drop down menu within a navbar; however, it only displays until the limit of my navbar when, ideally, id like it to be "in front" of the navbar so to speak.
Dropdown is cut off
I am new to html and CSS so my exploration is somewhat limited. I have implemented one of bootstrap's navbar's and also used their dropdown button html. I expected this to work okay but, as stated, the dropdown seems to be bound within the navbar (assuming this is because it is within the navbar div?) I have also attempted to follow w3schools guide but I didn't succeed with that either.
Sidenote: because of the limited visibility, the "my account" button logs the user out, this is intentional for now lmao.
<body>
<nav class="navbar navbar-expand-lg bg-light">
<div class="container-fluid">
<a class="logo" href="/"><img src="static/Logo_2.png" alt="Logo"></a>
<div class="navbar-nav">
{% if not session["id"] %}
<a class="nav-link" href="login">Log In</a>
{% endif %}
{% if session["id"] %}
<a class="nav-link" href="languages">Languages</a>
<a class="nav-link" href="faq">FAQs</a>
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Account
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="logout">My Account</a></li>
<li><a class="dropdown-item" href="logout">Log Out</a></li>
</ul>
</div>
{% endif %}
</div>
</div>
</nav>
{% if get_flashed_messages() %}
<header>
<div class="alert alert-primary mb-0 text-center" role="alert">
{{ get_flashed_messages() | join(" ") }}
</div>
</header>
{% endif %}
<main class="container-fluid py-5 text-center">
{% block main %}{% endblock %}
</main>
</body>
.navbar {
height: 100;
overflow: hidden;
}
.navbar-nav {
align-items: baseline;
display: flex;
float: right;
gap: 3em;
}
.nav-link {
color: black;
display: flex;
float: right;
}
.nav-link:hover {
color: red;
}
.btn-secondary {
background: none;
border: none;
color: black
}
.btn-secondary:hover {
background: none;
border: none;
color: red;
}
.logo {
display: flex;
scale: 0.4;
transform-origin: left;
}
your example should be cut down to just the minimum that displays the issue. you have extra classes, some ASP-like code, etc - none of that is needed and just makes it harder to diagnose.
the issue appears to be that your submenu is contained inside the top-level .navbar element, but you are constraining that to have a height of 100 (and you should include the metric here - 100px? 100cm?) with overflow being hidden. this means that the submenu gets cut off.
you could just remove those constraints.
a better solution would involve a rewrite, so the submenu items are positioned absolutely, in a relatively-positioned placeholder. there are examples of this online. example: https://gist.github.com/SamWM/901853
{% if not session["id"] %}
Log In
{% endif %}
{% if session["id"] %}
Languages
FAQs
Account
My Account
Log Out
{% endif %}
...
Remove the "overflow: hidden;" style from .navbar css, so your code should be-
.navbar {
height: 100;
}
I am newbie with html css and here is my problem.
I code a nav and subnav at html file as this one
<div id="header">
<!-- begin nav -->
<ul id="nav">
<li>Home</li>
<li>Bane</li>
<li>Tour</li>
<li>Contact</li>
<li>
<a href="">More
<i class="nav-arrow-down ti-arrow-circle-down"></i>
</a>
<ul class="subnav">
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
</li>
</ul>
<!-- end nav -->
<!-- begin search-->
<div class="search-btn">
<i class="search-icon ti-search"></i>
</div>
<!-- end search-->
</div>
And I want to make a block with color grey at block Merchandise, Extras, Media.
Here is my code at styles.css
#nav .subnav {
/*display: none;*/
position: absolute;
background-color: #fff;
min-width: 160px;
top: 100%;
left: 0;
}
My problem is, when I click to Merchandise, for example, the grey is not display fully all the block as I want. Here is the design
But here is what I got
As you can see in the second picture, the block become fell in.
I thought that I can use display: inline-block; to solve this problem , but when I add this command to #nav .subnav, it does not solve this problem.
They said that, I can use at #nav .subnav this command min-width: 160px;, but it still not well.
Could you please give me some ideas for this problem?
Thank you very much for your time.
I think you should give width:100% of ul tag.
<ul class="subnav" style="width:100%;">
<li>Merchandise</li>
<li>Extras</li>
<li>Media</li>
</ul>
Currently got the following setup by checking out other previously asked questions, although now I'm stuck as it won't span out
you mean you want it to extend to full screen width? if so you need to make its parent element width 100% too for example
body {
width: 100%;
}
it could be another parent like a div.container or whatever..
for 100% width of span
<style type="text/css">
.banner span{
width: 100% !important;
}
</style>
You can add img inside as this.
<div ng-controller="MainCtrl">
<header class="banner">
<div class="banner-wrapper">
<div class="banner-title">
<img src="https://upload.wikimedia.org/wikipedia/en/thumb/8/81/Williams_Companies_logo.svg/1280px-Williams_Companies_logo.svg.png" height="100px" width="100%">
</div>
<nav class="banner-nav">
{{ portal | welcome_navigation }}
</nav>
</div>
<nav class="nav-menu" id="header-tabs">
<ul>
<li class="nav-menu-title">Back to Main Site</li>
<li class="nav-contact">| Questions? Get in touch: <li class="nav-number">12345568693</li>
</ul>
</nav>
</header>
</div>
This actually has nothing to do with the width, as header elements are already supposed to span the whole width. There must be some default margin or padding in your html body. All you need to do is set default margin and padding on body tag to be 0.
(I usually follow setting margin/padding on body to 0 as a general practice)
You can remove your existing css code and replace it with this (I checked, it's working):
body {
padding: 0;
margin: 0;
}
<header class="banner">
<div class="banner-wrapper">
<div class="banner-title">
{{ portal | logo }}
</div>
<nav class="banner-nav">
{{ portal | welcome_navigation }}
</nav>
</div>
<nav class="nav-menu" id="header-tabs">
<ul>
<li class="nav-menu-title">Back to Main Site</li>
<li class="nav-contact">| Questions? Get in touch:
<li class="nav-number">12345568693</li>
</ul>
</nav>
</header>
I am developing a small app and have now to deal with the front end part of it. I am using bootstrap for it. For the navbar part, I am using nav-pills navbar, and I want it stretched to 100%. My navbar code:
<div class="navbar navbar-static-top" >
<div class="navbar-inner">
<ul class="nav nav-pills" >
<li >Home</li>
<li>Some</li>
<li>Items</li>
<li>Here</li>
<li>Register</li>
<li>Login</li>
</ul>
</div>
</div>
Now the css part:
.nav-pills{
margin-top:20px;
width:100%;
}
.nav-pills > li {
float: left;
}
Now if i change the 100% in nav-pills to say 1366px, it works. But when I scale it to 100%, the navbar is not stretched. I tried giving the width of .nav-pills > li to 25%, trying with just 4 tabs. That also gives the perfect result. But since the number of tabs vary from page to page, I don't want to fix the size of the .nav-pills > li class. I want to stretch the navbar to full width. What is that I should do?
Bootstrap 3 has a nav-justified class that does exactly what you want (at screens wider than 768px, on smaller screens the nav links are stacked).
Just add it to your .nav like this:
<ul class="nav nav-pills nav-justified">
<li >Home</li>
<li>Some</li>
<li>Items</li>
<li>Here</li>
<li>Register</li>
<li>Login</li>
</ul>
Demo fiddle
nav-pills nav-justified items not width fixed.
For this case:
HTML
<div class="gridProducts_box">
<ul class="nav nav-pill">
<li class="gridProducts_box__items">
One
</li>
<li class="gridProducts_box__items">
Two
</li>
<li class="gridProducts_box__items">
Three
</li>
<li class="gridProducts_box__items">
Four
</li>
<li class="gridProducts_box__items">
Five
</li>
</ul>
</div>
CSS
/* nav tabs fixed wodth */
.gridProducts_box > ul.nav.nav-pills {
display: table;
width: 100%;
table-layout: fixed;
}
.gridProducts_box > ul.nav.nav-pills > li {
float: none;
display: table-cell;
}
.gridProducts_box > ul.nav.nav-pills > li > a {
text-align: center;
}
.gridProducts_box__items {
width: 100%;
}
Demo
I'm designing a website using Twitter's Bootstrap, and I'm having difficulty with the responsive navbar. In bootstrap, the navbar allows a mobile user to click an icon to extend the navbar and display navigation links. I'm unable to find how to adjust the maximum height... when I try and use one of the drop down bars, the navbar doesn't change size accordingly, and the user is unable to see any of the links.
Thanks for any help you can provide.
-A
EDIT: here's the code that I'm using for the navbar (html) the css that I'm using is the standard bootstrap.css and bootrstrap-responsive.css:
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="brand" href="http://example.com">organization</a>
<div class="nav-collapse collapse">
<ul class="nav">
<li>Home</li>
<li class="active">Contact</li>
<li>Now Playing</li>
<li class="dropdown">
Public Resources <b class="caret"></b>
<ul class="dropdown-menu">
<li>Observatory</li>
<li>Planetarium</li>
<li class="divider"></li>
<li class="nav-header">For Teachers and Schools</li>
<li>Programs</li>
<li>Host an Event</li>
<li>Educational Initiatives</li>
</ul>
</li>
<li class="dropdown">
Undergraduate Resources <b class="caret"></b>
<ul class="dropdown-menu">
<li>Observatory</li>
<li>Planetarium</li>
<li class="divider"></li>
<li class="nav-header">Physics</li>
<li>Physics Homepage</li>
<li>Physics and Astronomy</li>
</ul>
</li>
<li>Volunteer</li>
<li>FAQ</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
The default navbar html code is the following:
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Title</a>
<ul class="nav">
<li class="active">Home</li>
<li>Link</li>
<li>Link</li>
</ul>
</div>
</div>
And if you inspect the navbar element you will find that the inside div (navbar-inner) has the class:
.navbar-inner {
min-height: 40px;
}
You can remove this value and find that the navbar remains the same height, that's normal, because the height of the navbar is set to "auto". Therefore if you remove the min-height the height will depend of the link tags padding, inside the <ul></ul>, and the <a class="brand"></a> padding as well.
.navbar .nav > li > a {
float: none;
**padding: 10px 15px 10px;**
color: #777;
text-decoration: none;
text-shadow: 0 1px 0 #FFF;
}
.navbar .brand {
display: block;
float: left;
**padding: 10px 20px 10px;**
margin-left: -20px;
font-size: 20px;
font-weight: 200;
color: #777;
text-shadow: 0 1px 0 #FFF;
}
If you change it, you will find that the height of the "navbar" parent will automatically change. You can also maintain the min-height property and just change its value, adjusting the padding of the elements I've mentioned above.
For the responsive part, you can edit all the styles in the bootstrap-responsive.css, inside the specific media query for the resolution that you want to edit.
EDIT: Just saw your HTML, check the padding of your link tags inside the navbar, reduce it, change the .navbar-inner min-height too, and play with the values.
Ok, the way I fixed the first part (having a header that didn't resize when I wanted to) was by changing the min width value in the bootstrap-responsive.css like so:
#media (min-width: 1200px) {
.nav-collapse.collapse {
height: auto !important;
overflow: visible !important;
}
}
where 1200px was changed to 1200 from 980. Tricky tricky... for the record, it's on line 1104.