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
}
Related
I have a navbar that contains elements, and I'm not able to center them in the middle of the navbar. I do not want to use bootstrap. Whatever I do, it still starts from the left side. I originally had the ul as the .topnav but it still wasn't centering the elements.
HTML:
<html lang="en">
<body>
<div class="topnav">
<ul >
<li><a class="active" href="index.html">Home</a></li>
<li class="dropdown">Products
<div class="dropdown-content">
Computers
Tablets
Cell Phones
Wearable Technologies
Accessories
</div>
</li>
<li class="dropdown">Brands
<div class="dropdown-content">
Apple
Samsung
Lenovo
Dell
HP
Sony
Panasonic
Motorola
HTC
</div>
</li>
<li>Deals</li>
</ul>
</div>
</body>
</html>
CSS:
.topnav{
list-style-type: none;
margin: 0 auto;
padding: 0;
overflow: hidden;
background-color: #333;
text-align:center;
align-items: center;
}
.topnav ul{
list-style-type: none;
margin: 0 auto;
}
.topnav li {
float: left;
}
.topnav li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
width:100px;
}
.topnav li a:hover, .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.topnav .dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.topnav .dropdown-content a:hover {background-color: #f1f1f1;}
.dropdown:hover .dropdown-content {
display: block;
}
JSFiddle: https://jsfiddle.net/29r9d18a/1/
Update the class definition
.topnav ul{
list-style-type: none;
margin: 0 auto;
display:inline-block;
}
Updated jsFiddle
.topnav ul{
list-style-type: none;
margin: 0;
display: inline-block;
-webkit-padding-start: 0;
}
You will no longer need the auto x-margin on the ul element with inline-block positioning and the parents center aligning, but you may want to override Safari's auto padding which will push it to the right slightly on Macs.
Use display: flex AND text-align: center
.topnav{
list-style-type: none;
margin: 0 auto;
padding: 0;
overflow: hidden;
background-color: #333;
display: flex; //HERE
text-align: center !important; //HERE
}
change the '.topnav ul' class to the following in your css code :
.topnav ul{
list-style-type: none;
margin: 0 auto;
margin-left: 27%;
}
This will shift your list to a bit right side and thus show it on the center.
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;
}
}
I have a CSS responsive menu that I made. I would like to add a button that show menu when the screen is smaller. This is my link to my current code pen
here is the code in the stackoverflow code snippet
.nav ul {
list-style: none;
background-color: #00FFFF;
text-align: center;
padding: 0;
margin: 0;
}
.nav li {
font-family: Georgia;
font-size: 1.2em;
line-height: 40px;
height: 40px;
border-bottom: 1px solid #ffffff;
}
.nav a {
text-decoration: none;
color: #000000;
display: block;
transition: .3s background-color;
}
.nav a:hover {
background-color: #036;
color: #ffffff;
}
#media screen and (min-width: 600px) {
.nav li {
width: 120px;
border-bottom: none;
height: 50px;
line-height: 50px;
font-size: 1.4em;
}
/* Option 1 - Display Inline */
.nav li {
display: inline-block;
margin-right: -4px;
}
/* Options 2 - Float
.nav li {
float: left;
}
.nav ul {
overflow: auto;
width: 600px;
margin: 0 auto;
}
.nav {
background-color: ffffff;
}
*/
}
<div class="nav">
<ul>
<li>Home</li>
<li>Resume</li>
<li>Biography</li>
<li>Contact</li>
</ul>
</div>
You mean like a Hamburger Menu?
You'll need to use javascript for that. I don't think there's a way to hide and show a menu on button click with pure css. I could be wrong.
HTML :
<nav>
<ul>
<li>Home</li>
<li>History</li>
<li>Appointments</li>
<li>Contact us</li>
</ul>
</nav>
CSS :
nav {
position: relative;
margin: auto;
padding: 0;
list-style: none;
width: 100%;
display: center;}
nav ul li {
display: inline;
}
nav ul li a {
display: inline-block;
text-decoration: none;
padding: 5px 0;
width: 100px;
background: #Cc3399;
color: #eee;
float: left;
text-align: center;
border-left: 1px solid #fff;
}
nav ul li a:hover {
background: #a2b3a1;
color: #000
}
Basically, I've managed to make this navigation bar, that fits my specifications. However, it's not centered, it's in position vertically, but horizontally it's way left and no where near the center of the page.
One solution is to replace inline to inline-block and use text-align: center to parent(also display: center is not valid css):
nav {
position: relative;
margin: auto;
padding: 0;
list-style: none;
width: 100%;
text-align: center;/*add text align-center*/
}
nav ul li {
display: inline-block;/*replace inline to inline-block*/
}
nav ul li a {
display: inline-block;
text-decoration: none;
padding: 5px 0;
width: 100px;
background: #Cc3399;
color: #eee;
float: left;
text-align: center;
border-left: 1px solid #fff;
}
nav ul li a:hover {
background: #a2b3a1;
color: #000
}
<nav>
<ul>
<li>Home
</li>
<li>History
</li>
<li>Appointments
</li>
<li>Contact us
</li>
</ul>
</nav>
Since you have given margin as 100% for it will not have impact on margin.. So try giving 50% width to and it should work.
You can change like this...
nav
{
position: relative;
margin: auto;
padding: 0;
list-style: none;
width: 50%;
display: center;
}
to horizontally center an element, this element needs to be display:block; and it should have a width that is less than 100%
Here is the css, slight modification based on what you have
nav {
position: relative;
/*** centers the nav block ****/
margin-left: auto;
margin-right: auto;
width: 80%;
}
nav ul{
padding: 0;
list-style: none;
}
nav ul li {
display: inline;
}
nav ul li a {
display: inline-block;
text-decoration: none;
padding: 5px 0;
width: 20%; /*** make the centering look more vivid ****/
background: #Cc3399;
color: #eee;
float: left;
text-align: center;
border-left: 1px solid #fff;
}
nav ul li a:hover {
background: #a2b3a1;
color: #000
}
You can view it on jsfiddle
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.