Is it possible to change the length of a navbar made by CSS? If so, how is it done? Like instead of stretching across the whole page, it stops at a certain distance
Here is a JSFiddle to illustrate the sidebar, aka the distance i need the navbar not to cover http://jsfiddle.net/jAVQv/
Here is the code that I have for the navbar (from W3Schools with minor edits):
ul.topnav:not(.ignore) {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
ul.topnav li:not(.ignore) {float: right;}
ul.topnav li a:not(.ignore) {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
ul.topnav li a:hover:not(.ignore) {background-color: #555;}
ul.topnav li.icon {display: none;}
#media screen and (max-width:680px) {
ul.topnav li:not(:first-child):not(.ignore) {display: none;}
ul.topnav li.icon:not(.ignore) {
float: right;
display: inline-block;
}
}
#media screen and (max-width:680px) {
ul.topnav.responsive:not(.ignore) {position: relative;}
ul.topnav.responsive li.icon:not(.ignore) {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li:not(.ignore) {
float: none;
display: inline;
}
ul.topnav.responsive li a:not(.ignore) {
display: block;
text-align: right;
}
}
Related
Well I would like to align the content of the list to the bottom so they would align with eachother in a neatly way or if you guys got a other way to do it I would appreciate that to.
.container-fluid {
padding-right: 0px !important;
padding-left: 0px !important;
}
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
ul.topnav li {
float: right;
}
ul.topnav li a {
padding-top: 39px;
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
}
ul.topnav li a:hover {
background-color: #555;
}
ul.topnav li.icon {
display: none;
}
#media screen and (max-width:768px) {
ul.topnav li:not(:first-child) {
display: none;
}
ul.topnav li.icon {
float: right;
display: inline-block;
}
}
#media screen and (max-width:768px) {
ul.topnav.responsive {
position: relative;
}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
<!-- begin snippet: js hide: false console: true babel: false -->
<div class="container-fluid">
<header>
<ul class="topnav" id="myTopnav">
<li class="pull-left"><a><h1>Logo-Text</h1></a></li>
<li>Home</li>
<li>News</li>
<li>Find us</li>
<li>About</li>
<li class="icon">
☰
</li>
</ul>
</header>
</div>
.container-fluid {
padding-right: 0px !important;
padding-left: 0px !important;
}
ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
text-align: right;
padding-bottom: 5px;
}
ul.topnav li {
border-bottom: 1px solid #ffffff;
display: inline-block;
vertical-align: bottom;
}
ul.topnav li a {
padding-top: 39px;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition: 0.3s;
font-size: 17px;
line-height: 1;
}
ul.topnav li a h1{
margin: 0;
}
ul.topnav li a:hover {
background-color: #555;
}
ul.topnav li.icon {
display: none;
}
#media screen and (max-width:768px) {
ul.topnav li:not(:first-child) {
display: none;
}
ul.topnav li.icon {
float: right;
display: inline-block;
}
}
#media screen and (max-width:768px) {
ul.topnav.responsive {
position: relative;
}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
<!-- begin snippet: js hide: false console: true babel: false -->
<div class="container-fluid">
<header>
<ul class="topnav" id="myTopnav">
<li>Home</li>
<li>News</li>
<li>Find us</li>
<li>About</li>
<li class="icon">
☰
</li>
<li><a><h1>Logo-Text</h1></a></li>
</ul>
</header>
</div>
I just remove floating from li. ul will be text-align: right; then
li{
display: inline-block;
vertical-align: bottom;
}
It will make your list item bottom align. Hope this help.
I'm trying to make a responsive navigation bar so that when the device used to access the website is too small to fit the entire navigation bar, it collapses into a dropdown. Strangely, the media queries I'm using don't seem to be doing anything to the website. In the head of my HTML file, I have this : <meta name="viewport" content="width=device-width, initial-scale=0.8" >
Here is the css:
#media screen and (max-width: 800px) {
ul.topnav li:not(: first-child) {
display: none;
}
ul.topnav li.icon {
float: right;
display: inline-block;
}
ul.topnav.responsive {
position: relative;
}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
var main = function () {
function openNav() {
var x = document.getElementById("topnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
}
$(document).ready(main);
.jumbotron p {
font-weight: 400;
font-family: 'roboto', sans-serif;
}
.topnav {
font-family: 'Montserrat', sans-serif;
font-size: 16px;
background-color: transparent;
color: black;
transition: all 0.25s ease;
position: fixed;
top: 0;
width: 100%;
padding: 20px;
}
ul.topnav {
list-style-type: none;
margin: 0;
overflow: hidden;
display: flex;
align-items: center;
}
ul.topnav li {
float: left;
}
ul.topnav li.icon {
display: none;
}
.topnav a {
margin-left: 20px;
text-decoration: none;
color: black;
}
.topnav a:hover {
margin-left: 20px;
text-decoration: none;
color: #FACD00;
}
#media screen and (max-width: 800px) {
ul.topnav li:(not: first-child) {
display: none;
}
ul.topnav li.icon {
float: right;
display: inline-block;
}
ul.topnav.responsive {
position: relative;
}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<!DOCTYPE html>
<body>
<!--navbar-->
<ul class="topnav">
<li>Home</li>
<li>Events</li>
<li>About</li>
<li>Volunteer</li>
<li>Contact</li>
<li class="icon">
☰
</ul>
</body>
Just put the media query on the bottom of the css file. The CSS rules are applied by specificity and when having the same specificity (this is your case) by the order the selectors appear.
Also corrected the syntax mistake that Artem Gorlachev pointed out.
.jumbotron p {
font-weight: 400;
font-family: 'roboto', sans-serif;
}
.topnav {
font-family: 'Montserrat', sans-serif;
font-size: 16px;
background-color: transparent;
color: black;
transition: all 0.25s ease;
position: fixed;
top: 0;
width: 100%;
padding: 20px;
}
ul.topnav {
list-style-type: none;
margin: 0;
overflow: hidden;
display: flex;
align-items: center;
}
ul.topnav li {
float: left;
}
ul.topnav li.icon {
display: none;
}
.topnav a {
margin-left: 20px;
text-decoration: none;
color: black;
}
.topnav a:hover {
margin-left: 20px;
text-decoration: none;
color: #FACD00;
}
#media screen and (max-width: 800px) {
ul.topnav li:not(:first-child) {
display: none;
}
ul.topnav li.icon {
float: right;
display: inline-block;
}
ul.topnav.responsive {
position: relative;
}
ul.topnav.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
ul.topnav.responsive li {
float: none;
display: inline;
}
ul.topnav.responsive li a {
display: block;
text-align: left;
}
}
I use the following code to style my navigation: External codePen
/*--- Navigation Main ---*/
.nav {
margin: 0px;
background-color: rgb(200,216,239)
}
ul.topnav {
display: block;
list-style-type: none;
margin: 0 450px 0;
padding: 0;
overflow: hidden;
position: relative;
border-bottom: solid 1px
}
ul.topnav li {
float: left;
}
ul.topnav li a {
display: block;
text-align: center;
padding: 1px 15px;
text-decoration: none;
height: 65px;
line-height: 70px;
}
ul.right {
margin: auto;
float: right;
list-style-type: none;
border-bottom: 1px
}
#club-name {
border-bottom: 1px firebrick;
}
/*--- Navbar colors ---*/
ul.topnav li a:hover:not(.active) {
background-color: rgb(144,149,158);
color: rgb(198, 218, 238);
}
ul.topnav li a:active:not(.active) {
background-color: rgb(46,53,66);
color: rgb(198, 218, 238);
}
ul.topnav li a.active {
background-color: firebrick;
}
/*--- Pseudoclasses ---*/
nav a:link {
color: rgb(144,149,158);
text-decoration: none;
}
a:visited {
color: rgb(144,149,158);
}
/*--- Small Screen view ---*/
#media (max-width: 600px){
nav {
margin: 0px;
}
ul.right,
ul.topnav li {
float: none;
text-align: center;
}
}
/*--- fonts ---*/
#club-name {
font-family: Bevan;
font-size: 20px;
color: aliceblue
}
ul.topnav ul.right li {
font-family: Pontano Sans;
font-size: 20px;
font-weight: bolder;
}
<div class="nav">
<ul class="topnav">
<li><a id="club-name" class="active" href="index.html">Hercules</a></li>
<ul class="topnav right">
<li>The Team</li>
<li>Facilities</li>
<li>Membership</li>
</ul>
</ul>
</div>
My styling works as I want until I set ul.topnav { margin: 0 350px 0; }.
If you update this margin to 0 and make the window smaller, you will see the effect I achieved before, but once I applied margins, the collapsible effect does not work anymore.
Hope someone can help, or offer me a different solution.
When you set the ul.topnav margin to 350px, the ul.topnav li elements start to run into each other and wrap before the breakpoint is reached as the screen size gets smaller. You can achieve an effect similar to the one you achieved before you applied the margins by setting the margins for ul.topnav to 0 auto, and then setting the max-width for this element to 800px or so. This provides some space on either side of the ul.topnav at larger screen sizes, ensures the ul.topnav li elements don't get too squished at smaller screen sizes, and allows your breakpoint to work as expected.
The CSS for ul.topnav in this solution looks like this:
ul.topnav {
display: block;
list-style-type: none;
margin: 0 auto;
max-width: 800px;
padding: 0;
overflow: hidden;
position: relative;
border-bottom: solid 1px
}
I have a navigation bar, but when the page is resized too small the navigation bar extends to a second line below it. I'd like it so even when the webpage is resized, the navbar stays the exact same (same size and everything). Here is some of my CSS that has to do with the navbar:
nav ul {
margin-top:-200px;
list-style: none;
padding: 0;
}
nav a{
text-decoration: none;
color: inherit;
}
nav ul li {
padding: 20px;
float: left;
background-color: #fff;
position: relative;
}
nav ul ul {
display: none;
position: absolute;
top: 250px;
left: 5;
padding: 0;
}
nav ul ul li {
margin-top: 0px;
width: 200px;
background: #FFF;
padding: 10px;
}
nav ul li:hover ul {
color: #4169E1;
display: block;
}
SCREENSHOT: http://gyazo.com/86c490863c3f1149098b6600c1ab276b
Add a min-width to your navigation bar to force it to maintain a certain length. Here's the JSFiddle: http://jsfiddle.net/t8h50p2g/1/
nav {
min-width: 1000px;
}
Add one more line of css
nav {
min-width: 800px;
max-width: 1200px;
}
I'm trying to use the following responsive navigation: http://codepen.io/Goatsy/pen/dqnKv
/*Border surrounding entire navigation - NAV CONTAINER*/
.navborder {
border-top: 10px solid #6e6f1f;
border-right: 20px solid #6e6f1f;
border-bottom: 10px solid #6e6f1f;
border-left: 20px solid #6e6f1f;
background: #6e6f1f;
}
/*Last nav li padding appears*/
.last {
padding-right:14px;
}
/*White bg & top spacing btwn nav*/
#site-content {
background-color: #fff;
padding-top:20px;
margin-top:20px;
width: 100%;
}
#media screen and (max-width : 1240px){
/*Border disappears*/
#site-content {
background-color: #fff;
}
}
/*Slider element*/
.slidebehind {
margin-top:-60px;
}
#media screen and (max-width : 768px){
/*Slider element jumps down - keep it above nav*/
.slidebehind {
margin-top:-40px;
}
}
#media screen and (max-width : 762px){
/*Border disappears*/
.navborder {
border: none;
}
/*Logo disappears*/
li img {
visibility: hidden;
}
/*Last nav li padding disappears*/
.last {
padding-right:0px;
}
/*Slider element jumps down - keep it above nav*/
.slidebehind {
margin-top:-106px;
}
}
#media screen and (max-width : 1200px){
/*UCR Logo margin - come inward*/
.ucrlogo {
margin-right:20px;
}
/*Breadcrumbs margin - come inward*/
.breadcrumb {
margin-left:20px;
}
}
/*Menu shrinks*/
#media screen and (max-width : 1200px){
/*Border disappears*/
#menu {
margin-right: 20px;
margin-left: 20px;
}
}
/* BEGIN NAV STYLES!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
/*Strip the ul of padding and list styling*/
ul.navlist {
list-style-type:none;
margin:0;
padding:0;
position: absolute;
z-index:99999;
}
/*Create a horizontal list with spacing*/
li {
display:inline-block;
float: left;
margin-right: 0px;
}
/*Style for menu links*/
li a {
display:block;
padding-right: 18px; padding-left:18px;
height: 40px;
text-align: left;
line-height: 45px;
font-family: 'TradeGothicLTStd-Cn18', sans-serif;
font-size: 20px;
color: #fff;
background: #6e6f1f;
text-decoration: none;
opacity: .99;
}
/*Hover state for top level links*/
li:hover a {
background: #fff;
color: #6e6f1f;
}
/*Style for dropdown links*/
li:hover ul a {
background: #f5f1e6;
color: #6e6f1f;
height: 40px;
line-height: 45px;
}
/*Hover state for dropdown links*/
li:hover ul a:hover {
background: #fff;
color: #6e6f1f;
}
/*Hide dropdown links until they are needed*/
li ul {
display: none;
}
/*Make dropdown links vertical*/
li ul li {
display: block;
float: none;
}
/*Prevent text wrapping*/
li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
display: block;
}
/*Style 'show menu' label button and hide it by default*/
.show-menu {
text-decoration: none;
color: #fff;
background: #6e6f1f;
text-align: center;
padding: 10px 0;
display: none;
border: 10px solid #6e6f1f;
margin-bottom: 1px;
margin-right: 20px;
margin-left: 20px;
opacity: .99;
}
.show-menu:hover {
background: #898a2c;
border: 10px solid #898a2c;
cursor: pointer;
}
/*Hide checkbox*/
input[type=checkbox]{
display: none;
-webkit-appearance: none;
}
/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu{
display: block;
}
/*Responsive Styles*/
#media screen and (max-width : 762px){
/*Make dropdown links appear inline*/
ul {
position: static;
display: none;
margin-right: 20px;
margin-left:20px;
}
/*Create vertical spacing*/
li {
margin-bottom: 1px;
}
/*Make all menu links full width*/
ul li, li a {
width: 100%;
}
/*Display 'show menu' link*/
.show-menu {
display:block;
}
}
However, I want the nav list to have a specific name, to differentiate its styles from other list styles.
I've tried using something this: ul.navlist {nav css here} (shown in the codepen link)
and applied it to my html <ul class="navlist">, but for some reason, this breaks the navigation when resizing below 762px.
How do I apply a unique name to the nav list ul, without "breaking it" when resizing to small screens?
(You can see what I mean if you view the codepen, and see how the nav becomes "broken" after resizing to mobile view. If you remove the .navlist class, you'll see that it works fine, but has no class).
UPDATE - 7/29/14
Let me explain this a different way; I can see how my wording was confusing.
This is the EXACT menu I'm trying to achieve:
http://codepen.io/Goatsy/pen/GFDou
Here's the original CSS:
/*Strip the ul of padding and list styling*/
ul {
list-style-type:none;
margin:0;
padding:0;
position: absolute;
z-index:99999;
}
And here is the menu I have (after adding a class, navlist, to the ul):
http://codepen.io/Goatsy/pen/dqnKv
Here's the CSS with class added:
/*Strip the ul of padding and list styling*/
ul.navlist {
list-style-type:none;
margin:0;
padding:0;
position: absolute;
z-index:99999;
}
I'm trying to give a specific class name to the navigation ul.
For some reason, when I do this, it "breaks" the mobile-view of the navigation.
I don't know how else to explain when something "breaks", except to say that it's not working the way it should work (it should work like the first link).
See how in the first link, when switching to smaller/mobile view, the navigation doesn't look the same as the second? The only difference is the second one has a class added to the ul, but it's "breaking" the original style/functionality of the smaller navigation. How to I add a class to the ul without "breaking" the original style/functionality?
Hopefully that clears things up. :(
I'm not sure what you're trying to do, a hover menu only works on non-touch devices, so this menu may work on IOS by clicking and double clicking on Android, I'm guessing since :hover is not something I use on menus for about 2 years now.
I've assumed you're hiding sub menus on smaller viewports and that the top levels are links.
I've cleaned this up based on best guesses, you'll need to learn by it. Read the information below this code chunk. Anything shared by ALL viewports is outside a media query, styles only affecting larger viewports are inside a min-width, stuff that's only max-width (smaller viewports) and not shared between all sizes is inside a max-width.
http://jsbin.com/wafuso/1/edit
http://jsbin.com/wafuso/1/
Here's one with the hover on smaller viewports and other adjustments:
http://jsbin.com/roway/1/edit
This is to learn by.
.navlist,
.navlist * {
list-style: none;
margin: 0;
padding: 0;
}
.navlist li a {
display: block;
height: 40px;
text-align: left;
line-height: 45px;
font-family: 'TradeGothicLTStd-Cn18', sans-serif;
font-size: 20px;
color: #fff;
background: #6e6f1f;
text-decoration: none;
opacity: .99;
}
input[type=checkbox] {
display: none;
-webkit-appearance: none;
}
#media (min-width:763px) {
ul.navlist {
position: absolute;
z-index: 99999;
}
.navlist li { float: left }
.navlist li:hover ul a:hover {
background: #fff;
color: #6e6f1f;
}
.navlist li > a { padding: 0 18px }
.navlist li li a {
display: block;
padding: 5px;
height: auto;
line-height: normal;
font-size: 16px;
min-width: 100px;
padding: 10px 18px;
}
.navlist li ul {
display: none;
width: 250px;
}
.navlist li ul li {
display: block;
float: left;
width: 100%;
}
/*Prevent text wrapping*/
.navlist li a:hover + .hidden,
.navlist .hidden:hover { display: block }
/*Hover state for top level links*/
.navlist li:hover a {
background: #fff;
color: #6e6f1f;
}
.navlist li:hover ul a {
background: #f5f1e6;
color: #6e6f1f;
}
}
.show-menu {
text-decoration: none;
color: #fff;
background: #6e6f1f;
text-align: center;
padding: 10px 0;
display: none;
border: 10px solid #6e6f1f;
margin-bottom: 1px;
margin-right: 20px;
margin-left: 20px;
opacity: .99;
}
.show-menu:hover {
background: #898a2c;
border: 10px solid #898a2c;
cursor: pointer;
}
#media screen and (max-width:762px) {
input[type=checkbox]:checked ~ #menu { display: block }
.navlist {
position: static;
display: none;
margin-right: 20px;
margin-left: 20px;
}
.show-menu { display: block }
.disappear { display: none }
}
What you have here is general selectors like:
ul {styles}
and
ul li {styles}
which will affect every ul and li in the entire site.
If you add a class to a ul, you would then refer to it in the css as:
.navlist {toplevel style on the ul}
.navlist li {more styles for the lists}
and so forth
Also, if you don't want something to affect other viewport widths, such as everything above 700px is one style and everything under is another, you use min-width and stack in the order of smallest to largest, so if you want the font size at 700px to be 20px but it's 30px at 1200px and up, but the base (the small viewport) is 15px it would be like this:
.myselector {font-size:15px;}
#media (min-width:700px) {
.myselector {font-size:20px;}
}
#media (min-width:1200px) {
.myselector {font-size:30px;}
}
Max-width media queries stack in the reverse, where the smallest is last. Please read up on responsive design further.