How can I make a fix positioned menu bar? - css

I would like to style my menu bar Like THIS.
It's fixed to the top of the site when you scroll down and it isn't fixed where it is when the page is loaded.
How can it be done with CSS?

What you're after is a 'sticky navbar/menu'.
The simplest way would be to add the below CSS to your menu/navbar
position:fixed;
top:0px;
That said, for an effect closer to the one you've posted, you'll probably want to look at using some jQuery, e.g.:
$(window).bind('scroll', function() {
if ($(window).scrollTop() > 50) {
$('.menu').addClass('fixed');
}
else {
$('.menu').removeClass('fixed');
}
});
What this does is 'fix' the menu bar to the top of the page once you scroll past a certain point (e.g. 50px) by adding the CSS class 'fixed' to the .menu element, the fixed class would simply be e.g. the CSS above.
There are some nice examples listed here.

Source: Creating a sticky nav css and jquery
HTML
<div id="menu">
<ul>
<li>Home</li>
<li>About</li>
<li>Blog</li>
<li>Shop</li>
<li>Contact</li>
</ul>
</div>
<div id="content">
This is some content 0<br/>
This is some content 1<br/>
This is some content 2<br/>
This is some content 3<br/>
This is some content 4<br/>
This is some content 5<br/>
This is some content 6<br/>
This is some content 7<br/>
This is some content 8<br/>
<div id="data" />
</div>
CSS
* {
font-family: Consolas,Sans-serif;
font-size: 10pt;
}
#menu {
position: absolute;
width: 100%;
}
#menu.out {
position: fixed;
}
#menu ul {
margin: 0;
padding: 1em .5em;
list-style: none;
background-color: #fc9;
}
#menu ul li {
display: inline-block;
}
#menu ul li a {
padding: 5px .5em;
}
#content {
background-color: #ebebee;
padding: 4em 1em 1em;
height: 900px;
}
JQuery:
var menu = $("#menu");
var ul = menu.find("ul");
var content = $("#content")[0];
var data = $("#data");
var menuHeight = menu[0].getBoundingClientRect().bottom;
var inView= true;
$(document).scroll(function(evt) {
evt.preventDefault();
var top = content.getBoundingClientRect().top;
var nextInView = top+menuHeight > 0;
if (inView ^ nextInView)
{
data.append("<div>Switching.</div>")
inView = nextInView;
if (inView)
{
menu.removeClass("out");
}
else
{
menu.addClass("out");
ul.hide().slideDown("fast");
}
}
});
Fiddle :Demo
Courtesy : Robert Koritnik
Hope this helps
Happy Coding

Related

Chrome - css img float listmark is not displayed on the right place

In the following coding, the listmark is placed on top of the image only if it is displayed in Chrome.
(Float is effective for text, it will be displayed on the right side of the image without problems)
Also, when refresh with F5, the listmark will be displayed on the right side of the image without problems.
To prevent the listmark from being placed on top of the image from the beginning, could someone please tell me what to do?
img {
float: left;
}
ul li {
margin-left: 20px;
}
<div>
<img src="images/sample.jpg" alt="sample" width="50%" />
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
<li>ddd</li>
<li>eee</li>
</ul>
</div>
Updates:
When the list gets long, I want the lists wrap images like below.
https://embed.plnkr.co/uo1u4YOQHAWpFEO117QG/
img {
display: inline-block;
}
ul{
display: inline-block;
}
ul li {
margin-left: 20px;
}
<div>
<img src="images/sample.jpg" alt="sample" width="50%" />
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
<li>ddd</li>
<li>eee</li>
</ul>
</div>
img {
display: inline-block;
}
ul{
display: inline-block;
}
Try this.
You can try to float your <ul> like so:
ul{
float: left;
width: calc(50% - 20px);
}
That should float everything and the ul will always be on the right side of the image.

Tab Width CSS not working in Firefox and Internet Explorer

I have the following css for tabs,
.columbia-browser-blackberry .ui-tabs .ui-tabs-nav li a, .columbia-browser-
chrome .ui-tabs .ui-tabs-nav li a, .columbia-browser-iemobile .ui-tabs .ui-
tabs-nav li a, .columbia-browser-safari .ui-tabs .ui-tabs-nav li a,
.columbia-browser-moz.ui-tabs .ui-tabs-nav li a
{
padding: .5em 1em;
width: 225px;
word-wrap: initial;
height: 25px;
text-align: center;
display: inline;
}
Html
/**
* Page Tabs Control
* #TODO guerrics: overflowing tabs
*
* #param array pages - array of pages (default: map.values(page.subpages))
* #param str tab - name of the selected tab (default: __request.args.tab)
*/
var pages = $pages ?? map.values(page.subpages);
let pages = [ (p is str) ? wiki.getpage(p) : p foreach var p in pages];
var selectedTab = $tab ?? __request.args.tab;
// find the active tab page
var activeTab = [ tabpage foreach var tabpage in pages where tabpage.name ==
selectedTab ][0];
// no selected tab found, use first tab
if (!activeTab) {
let activeTab = pages[0];
}
<div class="mt-tabs ui-tabs ui-widget ui-widget-content ui-corner-all"
id="tabs">
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-
header ui-corner-all">
foreach(var tabpage in pages) {
<li id=('tab-'..tabpage.name) class=('ui-state-default ui-
corner-top'..(tabpage.name == activeTab.name ? " ui-tabs-selected ui-state-
active " : ""))>
<a href=(page.uri & {tab: tabpage.name})>
tabpage.title ?? "#" .. (__count + 1)
</a><span></span>
</li>
}
</ul>
<div class=("ui-tabs-panel ui-widget-content ui-corner-bottom"..(#pages
> 0 ? "" : "ui-state-empty"))>
if (activeTab) {
var contents = activeTab.contents;
if (contents) {
contents;
} else {
wiki.page(activeTab.path);
}
}
</div>
</div>
It works for Google chrome, but it doesn't work in Mozilla and Internet Explorer. I am trying to giving the tabs a uniform width of 225px.
Any help would be appreciated?
To apply width, set css property 'display' to either 'block' or 'inline-block'.
block: the element will sit in a single line. In such case you may want to set float so links are in the same line;
inline-block; the element will have height, width, etc, and multiple elements will sit in the same line (block).
try with:
display:inline-block
This is how it worked.
body .ui-tabs .ui-tabs-nav li a
{
padding: .5em 1em;
width:225px;
word-wrap: initial;
height: 25px;
text-align: center;
display: inline-block;
}

Max-Height Firefox/Opera/IE

I'm trying to set a max-height to an image. It works well in Safari and Chrome, but not in Firefox/Opera/IE. Now I read that html and body heights should be put at 100%, and it did work when I used jsfiddle. However, it doesn't work in my page (memo-designs.com/portfolio.php).
The following is the source of the page:
<!DOCTYPE html>
<html>
<head>
<title>memodesigns</title>
<link rel='stylesheet' href='style/stylesheet.css'>
<script type = 'text/javascript'>
function displayImage(image, link) {
document.getElementById('img').src = image;
document.getElementById('mylink').href = link;
}
function displayNextImage() {
if (x < images.length-1){
x++;
} else {
x = 0;
}
displayImage(images[x], links[x]);
}
function displayPreviousImage() {
if (x > 0){
x--;
} else {
x = images.length-1;
}
displayImage(images[x]);
}
function startTimer() {
setInterval(displayNextImage, -1);
}
var images = [], links = [], x = 0;images[0] = "http://memo-designs.com/items/doublek-01.png"
links[0] = "http://memo-designs.com/items/doublek-01.png"
images[1] = "http://memo-designs.com/items/memodesigns.png"
links[1] = "http://memo-designs.com/items/memodesigns.png"
</script>
</head>
<body style = 'background-color: #000000'><div id = 'menucontainer'>
<div id = 'menu'>
<p>
<ul>
<li><a class = 'menu' href = '/'>HOME</a></li>
<li><a class = 'menu' href = 'about.php'>ABOUT</a></li>
<li><a class = 'menu' href = 'portfolio.php'>PORTFOLIO</a></li>
<li><a class = 'menu' href = 'rates.php'>RATES</a></li>
<li><a class = 'menu' href = 'contact.php'>CONTACT</a></li>
</ul>
</p>
</div>
</div>
<div id = 'contentcontainer' style = 'padding-top: 0%; max-height: 100%; overflow: hidden; background-color: #000000'>
<p>
<img id= 'img' src = 'http://memo-designs.com/items/doublek-01.png' style = 'max-height: 100%; max-width: 100%; display: block; margin-left: auto; margin-right: auto;'>
<img class = 'arrow' onclick = 'displayPreviousImage()' id= 'img' src = 'style/graphics/larrow.png' style = 'position: absolute; left: 0; top: 40%;'>
<img class = 'arrow' onclick = 'displayNextImage()' id= 'img' src = 'style/graphics/rarrow.png' style = 'position: absolute; right: 0; top: 40%;'> </p>
</div>
</body>
</html>
And the css stylesheet (only part of it is shown here):
*{
margin: 0;
padding: 0;
}
html{
margin: 0;
min-width: 100%;
height: 100%;
min-height: 100%;
}
body{
margin: 0px;
background-color: #f3f4f4;
min-width: 100%;
height: 100%;
min-height: 100%;
}
Would appreciate any help as to what I'm doing wrong :)
First of all I recommend you to start using a CSS-Reset like Normalize.css . It makes browsers render all elements more consistently and in line with modern standards.
Your HTML notation might also cause inconsistency across browsers. Turn things like <div id = 'menu'> into <div id="menu">. This makes it also more readable IMHO.
Inline style attributes make maintaining the pages a pain and may override things you didn't intent to. They also need to be applied to every single element thus also increasing download time. Using classes / id's is the way to go. Also pseudo-elements can't be used with inline styles. I advice to only use them for quick changes during development. I use the element inspector from Chrome / Firefox to change things quickly and instantly see how the changes look, copy/pasting the edits afterwards.
So, make sure to put all css into your stylesheet. It's also considered as a best practice for maintainability and better download speed (minify the files for production) of your pages.
You surely have heard about jQuery before. Try using it. jQuery makes developing things like image sliders a breeze (once you understand the syntax, but it's a low learning curve). Furthermore, there are LOTS of ready-to-use plugins for jQuery.
Another "good practice" is to put your javascripts at the very end of your document just before the </body> tag. Read more about this here and here.
Ok, enough tips. Let's get the hands dirty:
The HTML Part:
<!DOCTYPE html>
<html>
<head>
<title>memodesigns</title>
<link rel="stylesheet" href="/assets/css/normalize.css">
<link rel="stylesheet" href="/assets/css/style.css">
</head>
<body>
<div id="menuContainer">
<div id="menu">
<p>
<ul>
<!-- Instead of writing in CAPITALS use the text-transform:uppercase; css property -->
<li><a class="menu" href="/">Home</a></li>
<li><a class="menu" href="about.php">About</a></li>
<li><a class="menu" href="portfolio.php">Portfolio</a></li>
<li><a class="menu" href="rates.php">Rates</a></li>
<li><a class="menu" href="contact.php">Contact</a></li>
</ul>
</p>
</div>
</div>
<div id="contentContainer">
<p>
<!-- NOTE: Use IDs only once, else use classes to share css styles -->
<img id="img" src="http://memo-designs.com/items/doublek-01.png">
<img class="arrow left" src="style/graphics/larrow.png" onclick="displayPreviousImage()">
<img class="arrow right" src="style/graphics/rarrow.png" onclick="displayNextImage()">
</p>
</div>
<!-- Put the JavaScript at the end of the document just before the closing body tag -->
<script>
var images = [], links = [], x = 0,
baseUrl = "http://memo-designs.com/items/";
images[0] = baseUrl + "doublek-01.png";
links[0] = baseUrl + "doublek-01.png";
images[1] = baseUrl + "memodesigns.png";
links[1] = baseUrl + "memodesigns.png";
function displayImage(img, link)
{
document.getElementById('img').src = img;
document.getElementById('mylink').href = link;
}
function displayNextImage()
{
if (x < images.length-1) x++;
else x = 0;
displayImage(images[x], links[x]);
}
function displayPreviousImage()
{
if (x > 0) x--;
else x = images.length-1;
displayImage(images[x]);
}
function startTimer()
{
setInterval(displayNextImage, -1);
}
</script>
</body>
</html>
...and the CSS:
/* Assuming you'll use a CSS-Reset */
body {
background-color: #f3f4f4;
font:
...
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
#menuContainer { ... }
#menu { ... }
#menu ul { ... }
/* Making the menu labels all UPPERCASE */
#menu ul > li {
text-transform: uppercase;
}
#contentContainer {
background-color: #000;
padding-top: 0;
overflow: hidden;
/* IMPORTANT: Set a fixed pixel height here to make the images use up the given space */
height: 200px; /* change 200 to your needs */
}
#img {
display: block;
width: 100%;
height: 100%;
margin-left: auto;
margin-right: auto;
}
#contentContainer .arrow {
position: absolute;
top: 40%;
}
#contentContainer .arrow.left {
left: 0;
}
#contentContainer .arrow.right {
right: 0;
}
Ok, try out the suggestions and the code example. Tell us if and what helped.
Good luck and happy coding!

use bullet to indicate current page in navigation menu

I have a vertically displayed navigation menu. I would like a bullet point to appear to the left of the page currently being viewed. I've read a little bit about using background changes applied to li to indicate page, but I don't know how to apply that to using bullets.. Any ideas?
<nav>
<ul>
<li>home</li>
<li>about</li>
<li>lookbook</li>
<li>services</li>
<li>contact</li>
<li>blog</li>
</ul>
</nav>
nav {
position: fixed;
right: 13%;
top: 65%;
}
nav ul li {
text-align: right;
}
http://jsfiddle.net/jtRws/
Update
Live examples of all proposed solutions
Working with AMC it was determined that a JavaScript solution would work better given the requirements. All content would appear on one page so the CSS solutions would not work based on my findings. I proposed the simple jQuery code to show a bullet when the link was clicked.
jQuery
$(window).load(function(event) {
$('a').click(function() {
$('#Nav li').removeClass();
$(this).parent().addClass("current");
});
});
CSS
#navcontainer3 ul {
width: 200px;
list-style-type:disc;
}
#navcontainer3 ul li {
color: #ccc;
float:right;
clear:right;
}
#navcontainer3 ul li.current {
color: #000;
}
This page contains examples of all the solutions but you'll find the jQuery solution alone at this link: http://jsfiddle.net/jtRws/10/.
Interesting problem. Obviously, there are JavaScript solutions, but it looks like you want a pure CSS solution.
Solution 0
Hide the other list item bullets by setting the list style color to the background color.
All pages will have the same navigation HTML but each page's <body> will have an id unique for that page. For example: <body id="home">, <body id="products">, etc. Then, using some clever CSS, the current page will obtain the specific styling definition (last def below).
#navcontainer0 ul {
width: 200px;
list-style-type:disc;
}
#navcontainer0 ul li {
color: #fff;
float:right;
clear:right;
}
body#home #homenav0,
body#products #prodnav0 {
color: #000;
}
Solution 1
Use background to show and hide the bullet image. Based on this article I used the following code. The article explains the technique in greater detail. Below you'll find the same code as the live example.
CSS
#navcontainer ul {
width: 200px;
padding:0;
margin:0;
text-align:right;
}
#navcontainer ul li a:hover {
color: #930;
background: #f5d7b4;
}
body#home a#homenav,
body#products a#prodnav,
body#faq a#faqnav,
body#contact a#connav {
background:url(bullet.gif) 0 50% no-repeat no-repeat;
padding-left:15px;
}
Products Page
<body id="products">
<div id="navcontainer">
<ul id="navlist">
<li><a id="homenav" href="index.html">Home</a></li>
<li><a id="prodnav" href="products.html" >Products</a></li>
</ul>
</div>
</body>
I assume you're using anchors to navigate through page sections, so the only way I can think of right now is using javascript(jQuery).
I've updated your code example: http://tinyurl.com/a6ypyuz
In addition to #earthdesigner answer, if you don't want to add jquery then use this javascript instead:
http://jsfiddle.net/7uxcg/31/
window.onload = function() {
// var nav = document.getElementById('nav');
var nav = document.getElementsByTagName('NAV')[0].children[0];
for(c in nav.children) {
var li = nav.children[c];
if(li.nodeName == 'LI') {
li.onclick = function() {
changeClass(this.children[0].hash);
}
}
}
function changeClass(cur) {
var nav = document.getElementsByTagName('NAV')[0].children[0];
for(c in nav.children) {
var li = nav.children[c];
if(li.nodeName == 'LI' && li.children[0].hash == cur)
li.className = 'active';
else
li.className = ''
}
}
};
I understand that you want bullets only to current page. So first of all we're goind to eliminate normal bullets.
ul {list-style-type:none;}
Than i saw that you use id's for content divs on every page. Combining that with atribute selector here's an example of what you could do:
#home li a[href='#home']:before, #about li a[href='#about']:before {
content:"•";pointer-events:none; text-decoration:none;
}

How to make only one line list wider than screen?

I have a list, which length is variable,it could have 4 items or 30 items.
The issue is that this list is being rendered as a marquee, through a Javascript that is working, but items reach the screen width and then they pass to next line.
I want to have all the items on the same line, so then I could move the list to recreate the marquee effect.
<div class="submenu-container">
<div id="agrupador">
<ul style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;" class="submenu" id="moverlo">
<li> Item </li>
<li> Item </li>
<li> Item </li>
<li> Item </li>
<li> Item </li>
<li> Item </li>
</ul>
</div>
</div>
.submenu-container {
background:#F0F7FF;
border-bottom:1px silver solid;
width: 100%;
overflow: hidden;
}
.submenu li {
display: inline-block;
margin-left:4px;
background:none;
*display: inline; /* IE */
float:left;
}
.submenu {
margin:0;
padding-top:8px;
padding-bottom:8px;
padding-left:0;
padding-right:0;
clear:none;
width:auto;
float:left;
}
And with javascript:
function ClonarAvisos(){
var width = alertSize(); // Usable window width
var xVarScreen = width - $('moverlo').getWidth();
alert($('moverlo').getWidth());
$('moverlo').setStyle({ width: $('moverlo').getWidth() + xVarScreen });
$('agrupador').setStyle({ width: $('moverlo').getWidth() * 2 });
var clon = Element.clone('moverlo', true);
clon.id = 'moverlo2';
$('agrupador').insert(clon);
}
function MoveNoticias(p) {
//Función que imita el comportamiento del Tag "marquee"
new Effect.Move('agrupador', {
x: -3,
y: 0,
mode: 'relative',
duration: 0.1,
afterFinish: function() {
var offSetDistance = $('agrupador').offsetLeft ;
offSetDistance = Math.abs(offSetDistance);
var gettingWidth = $('moverlo').getWidth() ;
if ( offSetDistance >= gettingWidth ) {
Element.setStyle('agrupador', {left: '0px' });
}
MoveNoticias();
}
});
}
I hope you understand what I am trying to do! Thanks!
Try removing the float: left from the li, and adding white-space: nowrap to the 'ul'.
I tried that here: http://jsfiddle.net/76d6p/

Resources