/    Sign up×
Community /Pin to ProfileBookmark

CSS problems

Hi Y’All,
I am trying my teeth at a CSS nav bar and I have two questions:
1. How can I spread the nav bar across the entire width of the page?
2. Can I have different background colors for the lines “Active Members” and any of the “Name” lines?
3. How can I get the arrow image behind “Name1” on the same line as the name? Right now it’s one line below.
Thanks,
Ed

<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Main Nav Bar</title>
<link href=”../resources/navmain.css” rel=”stylesheet”>
</head>
<body>
<nav>
<ul>
<li><a href=”../index.html”>The Club</a></li>
<li><a href=”../chefs.htm”>The Chefs</a>
<ul>
<li><a href=”../chefs.htm”>Active Members</a></li>
<li><a href=”../chefs/name1.htm”>Name1</a><img src=../arrow.gif></li>
<li><a href=”../chefs/name2.htm”>Name2</a></li>
</ul>
</li>
<li><a href=”../index.html”>Menus</a></li>
<li><a href=”../recipes.htm”>Recipes</a>
<ul>
<li><a href=”../recipes/appetizers/appetizers.htm”>Appetizers</a></li>
<li><a href=”../recipes/salads/salads.htm”>Salads</a></li>
</ul>
</li>
<li><a href=”../index.html”>Pictures</a></li>
<li><a href=”../index.html”>Links</a></li>
</ul>
</nav>
</body>
</html>

body, div, h1, h2, h3, h4, h5, h6, p, ul, ol, li, dl, dt, dd, img, form, fieldset, input, textarea, blockquote {
margin: 0; padding: 0; border: 0;
}
body {
background: #909eab url(bg.png);
font-family: Verdana, sans-serif; font-size: 11px; line-height: 12px;
}
nav {
margin: 10px auto;
text-align: left;
}
nav ul ul {
display: none;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
background: #efefef;
background: linear-gradient(top, #efefef 0%, #bbbbbb 100%);
background: -moz-linear-gradient(top, #efefef 0%, #bbbbbb 100%);
background: -webkit-linear-gradient(top, #efefef 0%,#bbbbbb 100%);
box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
padding: 0 50px;
border-radius: 10px;
list-style: none;
position: relative;
display: inline-table;
}
nav ul:after {
content: “”; clear: both; display: block;
}
nav ul li {
float: left;
}
nav ul li:hover {
background: #4b545f;
background: linear-gradient(top, #4f5964 0%, #5f6975 40%);
background: -moz-linear-gradient(top, #4f5964 0%, #5f6975 40%);
background: -webkit-linear-gradient(top, #4f5964 0%,#5f6975 40%);
}
nav ul li:hover a {
color: #fff;
}

/* nav bar */
nav ul li a {
display: block; padding: 5px 5px;
color: #757575; text-decoration: none;
}
nav ul ul {
background: #5f6975; border-radius: 0px; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
float: none;
border-top: 1px solid #6b727c;
border-bottom: 1px solid #575f6a; position: relative;
}
nav ul ul li a {
padding: 5px 5px;
color: #fff;
}
nav ul ul li a:hover {
background: #4b545f;
}
nav ul ul ul {
position: absolute; left: 100%; top:0;
}

to post a comment
CSS

21 Comments(s)

Copy linkTweet thisAlerts:
@TrainMay 09.2017 — It would sure help if you were to use the BBcode.

http://www.webdeveloper.com/forum/misc.php?do=bbcode
Copy linkTweet thisAlerts:
@ZorgMay 09.2017 — To answer your first question, create a class in your css stylesheet, like in this example. Width is 100% and since your nav ul element has padding of 50px on left and right, adding box-sizing: border-box prevents your navbar from exceeding the viewport. To see what i mean, comment out the box-sizing.

<i>
</i>.widenav {
width: 100%;
box-sizing: border-box;
}

Next in your html, add the class name we created, into your nav's first ul element.
[code=html]
<nav>
<ul class="widenav">
[/code]


OK, now your second question... Yes you can have different background colors, font colors, etc. Same process as before. Create a new class name in your stylesheet and assign its properties. Names are not important, call them whatever you want, use something meaningful.

Example:
[CODE]
.navitem {
background: darkslateblue; // change to suit
}
[/CODE]

Now add this to your page element. In this case add it to all your nested li elements that you want to have a this styling.

[code=html]
<ul>
<li class="navitem"><a href="../chefs.htm">Active Members</a></li>
[/code]


Question 3, simply move the img element into the link element. Make it part of the link.

[code=html]
change this:
<li class="navitem"><a href="../chefs/name1.htm">Name1</a> <img src=../arrow.gif></li>

to:
<li class="navitem"><a href="../chefs/name1.htm">Name1 <img src=../arrow.gif></a></li>

[/code]


After this, add a new selector to the stylesheet, to handle alignment of the image with the link text.
[CODE]
nav li img {
vertical-align: middle;
}
[/CODE]


Note: when creating a class, a class name selector always has a dot before the name. An element selector has no dot like in last example above. You can also assign id selectors with a # instead of a dot. Id selectors are used when you want to target a specific, unique page element by its ID. In other words a class selector can be used for many page elements, an id selector for one page element.

For more in depth info, I suggest reading up on CSS. Here is a link:

https://www.w3schools.com/css/
Copy linkTweet thisAlerts:
@busterauthorMay 10.2017 — Zorg,

I'm working on your suggestions and everything works fine so far, thanks a lot.

I realize that I have to learn a lot about CSS, will take a while.

Another question re your first answer: the nav bar stretches across the whole width but the 6 buttons are aligned left. How do I get them to stretch across the whole width?

And where is the font color of the main bar set? I can't seem to find that in the style sheet.

Ed
Copy linkTweet thisAlerts:
@ZorgMay 11.2017 — Glad it works!

Your nav selector in the stylesheet is already set to text-align: center but you're floating it to the left; this needs to change:

[code=html]
nav ul li {
float: left; // remove this
display: inline-block; // add this instead will align each li side by side
width: 10vw; // adding this will make the width responsive to the viewport width, 10vw is 10% relative to width of viewport - change to suit
}

// create a new submenu class and inherit the width of main nav item
.submenu {
width: inherit;
}
[/code]


You want to add the submenu class to the nested ul elements:

[code=html]
<li><a href="../chefs.htm">The Chefs</a>
<ul class="submenu">
...
[/code]


Menu items should now be aligned in the center of page with menu items spaced out.

Text color is defined by the color property:

[CODE]
nav ul li a {
display: block;
padding: 5px 5px;
color: #757575; // this is the text color, look online for color HEX values
text-decoration: none;
}
[/CODE]
Copy linkTweet thisAlerts:
@busterauthorMay 11.2017 — Thanks again, you've been very helpful, much appreciated.

Ed
Copy linkTweet thisAlerts:
@busterauthorMay 11.2017 — Zorg,

Please check the attached screen shot.

The nav bar has spread out but it does not seem to be centered.

I'm sure I did something wrong, or?

Thanks again,

Ed[ATTACH]17489[/ATTACH]

[canned-message]attachments-removed-during-migration[/canned-message]
Copy linkTweet thisAlerts:
@ZorgMay 11.2017 — Make sure you removed the float: left from the nav ul li selector. If this doesn't work, what else was added to the css?

I'm glad to help, but i really can't tell you forsure from a screen shot. It's like sending a mechanic a pic of your engine.
Copy linkTweet thisAlerts:
@sin123May 12.2017 — nice post,thanks for sharing.
Copy linkTweet thisAlerts:
@busterauthorMay 12.2017 — I have removed the float:left selector but I have also tried to customize the nav bar a bit and may have, inadvertently, screwed things up a bit. I have attached the files again.

There are a couple of things I couldn't figure out despite several hours of messing with CSS:

1. The centering of the nav bar

2. Is there any way to make the pull down cells not so high as the main nav bar?

3. Same goes for the font size, when I make the main bar font bigger, the pulldown font gets bigger too.

Thanks again for all your help and your patience with me.

Ed

body, div, h1, h2, h3, h4, h5, h6, p, ul, ol, li, dl, dt, dd, img, form, fieldset, input, textarea, blockquote {

margin: 0; padding: 0; border: 0;

}

body {

font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px;

}

.navitem {

background: #FFB263;

}

nav {

margin: 10px auto;

text-align: left;

}

.widenav {

width: 100%;

box-sizing: border-box;

}

nav ul ul {

display: none;

}

nav ul li:hover > ul {

display: block;

}

nav ul {

background: #738610;

background: linear-gradient(#738610 0%, #bbbbbb 100%);


background: -moz-linear-gradient(#738610 0%, #bbbbbb 100%);

background: -webkit-linear-gradient(#738610 0%,#bbbbbb 100%);

box-shadow: 0px 0px 2px rgba(0,0,0,0.15);

padding: 0 50px;

border-radius: 5px;


list-style: none;

position: relative;

display: inline-table;

}

nav ul:after {

content: ""; clear: both; display: block;

}

nav ul li {

float: center;

display: inline-block;

width: 14vw;

}

.submenu {

width: inherit;

}

nav ul li:hover {

background: #8B4513;

background: linear-gradient(#4f5964 0%, #BBBBBB 50%);

background: -moz-linear-gradient(#4f5964 0%, #BBBBBB 50%);

background: -webkit-linear-gradient(#4f5964 0%,#BBBBBB 50%);

}

nav ul li:hover a {

color: #0A4877;

}

/* nav bar */

nav ul li a {

display: block; padding: 5px 5px;

color: #FFFFFF; text-decoration: none;

}

nav ul ul {

background: #FFFBC6; border-radius: 0px; padding: 0;

position: absolute; top: 100%;

}

nav ul ul li {

float: none;

border-top: 1px solid #FF0000;

border-bottom: 0; position: relative;

}

nav ul ul li a {

padding: 5px 5px;

color: #fff;

}

nav ul ul li a:hover {

background: #FFB263;

}

nav ul ul ul {

position: absolute; left: 100%; top:0;

}


</head>

<body>

<ul>

<nav>

<ul class="widenav">

<li><a href="../index.html">The Club</a></li>

<li><a href="../chefs.htm">The Chefs</a>

<ul class="submenu">

<li class="navitem"><a href="../chefs.htm"><img src=../arrow2.gif>&nbsp;<b>Active Chefs</b></a></li>

<li><a href="../chefs/name1.htm"><img src=../arrow.gif>Name1</a></li>

<li><a href="../chefs/name2.htm"><img src=../arrow.gif>Name2</a></li>

</ul>

</li>

<li><a href="../index.html">Menus</a></li>

<li><a href="../recipes.htm">Recipes</a>

<ul class="submenu">

<li><a href="../recipes/appetizers/appetizers.htm"><img src=../arrow.gif>Appetizers</a></li>

<li><a href="../recipes/salads/salads.htm"><img src=../arrow.gif>Salads</a></li>

<li><a href="../recipes/soups/soups.htm"><img src=../arrow.gif>Soups</a></li>

<li><a href="../recipes/pasta/pasta.htm"><img src=../arrow.gif>Pasta, Rice, etc.</a></li>

<li><a href="../recipes/beef/beef.htm"><img src=../arrow.gif>Beef, Veal, etc.</a></li>

<li><a href="../recipes/pork/pork.htm"><img src=../arrow.gif>Pork</a></li>

<li><a href="../recipes/poultry/poultry.htm"><img src=../arrow.gif>Poultry</a></li>

<li><a href="../recipes/seafood/seafood.htm"><img src=../arrow.gif>Seafood</a></li>

<li><a href="../recipes/salads/salads.htm"><img src=../arrow.gif>Salads</a></li>

<li><a href="../recipes/veggies/veggies.htm"><img src=../arrow.gif>Veggies</a></li>

<li><a href="../recipes/desserts/desserts.htm"><img src=../arrow.gif>Desserts</a></li>

</ul>

</li>

<li><a href="../index.html">Pictures</a></li>

<li><a href="../index.html">Links</a></li>

</ul>

</nav>

</body>
Copy linkTweet thisAlerts:
@TrainMay 12.2017 — For easier reading PLEASE use the bbcode.

http://www.webdeveloper.com/forum/misc.php?do=bbcode
Copy linkTweet thisAlerts:
@busterauthorMay 12.2017 — Train,

I do not understand what you are asking me.

I have submitted clean code that should be easily understood.

Maybe you could send me an example of what you exactly mean?

Thanks,

Ed
Copy linkTweet thisAlerts:
@ZorgMay 13.2017 — Ed, for what you're trying to do, we need to rewrite and combine some of your CSS. What i did is add text sizing and alignment. Combined, removed and renamed some selectors that were not needed.

Create a new CSS file (keep your current one for backup) and copy/paste this:
[CODE]
body, div, h1, h2, h3, h4, h5, h6, p, ul, ol, li, dl, dt, dd, img, form, fieldset, input, textarea, blockquote {
margin: 0; padding: 0; border: 0;
}

body {
font-family: Verdana, sans-serif; font-size: 12px; line-height: 20px;
}

/*-- nav elements --*/
nav {
margin: 10px auto;
text-align: center;
}

/* nav images */
nav img {
vertical-align: middle;
}

/*--- widenav class ---*/
.widenav {
width: 100%;
box-sizing: border-box;
background: #738610;
background: linear-gradient(#738610 0%, #bbbbbb 100%);
background: -moz-linear-gradient(#738610 0%, #bbbbbb 100%);
background: -webkit-linear-gradient(#738610 0%,#bbbbbb 100%);
box-shadow: 0px 0px 2px rgba(0,0,0,0.15);
padding: 0 50px;
border-radius: 5px;
list-style: none;
position: relative;
display: inline-table;
}

/* widenav line item elements */
.widenav li {
display: inline-block;
width: 14vw;
}

/* widenav links */
.widenav a {
display: block;
padding: 5px 5px;
color: #FFFFFF;
text-decoration: none;
text-align: center;
font-size: 1.5em;
}

/* widenav link on line item hover */
.widenav li:hover a {
color: #0A4877;
}

/* widenav line item hover */
.widenav li:hover {
background: #8B4513;
background: linear-gradient(#4f5964 0%, #BBBBBB 50%);
background: -moz-linear-gradient(#4f5964 0%, #BBBBBB 50%);
background: -webkit-linear-gradient(#4f5964 0%,#BBBBBB 50%);
}

/* submenu show on line item hover */
.widenav li:hover > .submenu {
display: block;
}

/*--- submenu class ---*/
.submenu {
background: #FFFBC6;
border-radius: 0px;
padding: 0;
position: absolute;
width: inherit;
display: none;
}

/* submenu line item */
.submenu li {
border-top: 1px solid #FF0000;
border-bottom: 0;
position: relative;
}

/* submenu link */
.submenu a {
padding: 0;
text-align: left;
font-size: 1em;
}

/* submenu line item hover */
.submenu li:hover {
background: #FFB263;
}

/*--- nav item class ---*/
.navitem {
background: #FFB263;
}
[/CODE]


I added comments to each css selector block to help define its purpose. I should of explained before, that a class selector is used to style specific page elements. The element selector (css selector without a dot) is used to target all matching page elements for that selector. Typically this is done to create the base style for the element, then you create a class to style it even further.

example:
[CODE]
this targets all ul page elements
ul {
...

this targets all ul elements within submenu class
.submenu ul {
...
[/CODE]


FYI: What Train was saying is, when posting your code on these forums, you should wrap your code in BBcode, kind of like html (follow the link in Train's reply). See when i'm posting code, it looks different from when you're posting your code? It helps distinguish and to read it.

Here is how, wrap in code blocks:

[noparse][CODE]Your Code Here[/CODE][/noparse]

Hope this helps. Navbar looks great, keep it up!
Copy linkTweet thisAlerts:
@busterauthorMay 13.2017 — Zorg,

It looks great and I really appreciate the comments. Makes it a lot easier for me to play around with it.

Thanks a bunch, I am very grateful.

Ed
Copy linkTweet thisAlerts:
@ZorgMay 13.2017 — Awesome! Glad to help.
Copy linkTweet thisAlerts:
@busterauthorMay 15.2017 — Zorg,

I have finished my nav bar according to your suggestions and it works fine.

Now I am trying to pull it into an iframe that has basically the same height as the nav bar itself and it shows fine. However the pull-downs only show when I make the iframe unreasonably high, which does, of course, make no sense for a nav bar. Is that normal?

Thanks again, Ed
Copy linkTweet thisAlerts:
@ZorgMay 15.2017 — What are you using the iframe for, what effect are you trying to achieve? Are you using the iframe as a container for the navbar? So the iframe height is as tall as the navbar, that would make sense. The contents within the iframe will not exit its frame bounds.
Copy linkTweet thisAlerts:
@busterauthorMay 15.2017 — Yes, I am trying to use the iframe as a container for the nav bar to avoid having that nav bar html on each page.

I'm sure there are other ways to pull the nav bar (incl the pull-downs) onto all pages, my knowledge is just too limited in that respect.
Copy linkTweet thisAlerts:
@ZorgMay 15.2017 — We can do this without an iframe using php. Help me understand a few things first... are you familiar with php? PC or Mac? Do you have a local web server on your machine? What is your current workflow, how are you viewing your changes?
Copy linkTweet thisAlerts:
@busterauthorMay 16.2017 — Unfortunately, I am not familiar with php.

I am working on a PC, using Coffeecup HTML editor, which allows me to look at my html/css changes.

But I can upload any changes to a server and check there, if that helps.
Copy linkTweet thisAlerts:
@ZorgMay 16.2017 — Ok, that will work.

You won't need the iframe. We are going to separate some elements and bring them in with php. PHP can be used with html. So the content will not need to change, we are only adding to it.

[LIST=1]
  • [*] First rename your [I]index.html[/I] to [B]index.php[/B]

  • [*] Create a new file called [B]navbar.php[/B] within the same directory as index.php and cut/paste the navbar only. Don't worry about the css, keep it in the index page.

  • [*] Paste code below into [B]index.php[/B] after the [I]body[/I] tag

  • [/LIST]


    Within your [B]index.php[/B] file, where you want the navbar to appear; paste the following: (make sure it is after the body tag so it appears where you had it originally)
    [code=php]
    <?php include_once "navbar.php";?>
    [/code]


    Do this on every page you want the navbar. You can include any other page content you create later this way, on multiple pages.

    You can also take it a step further and separate out the header and footer of your page into separate php files and include those.

    Example [I]index.php[/I]:
    [code=html]
    <?php
    include_once "header.php";
    include_once "navbar.php";
    ?>
    <div id="wrapper">
    <?php include "home-content.php";?>
    </div>
    <?php include_once "footer.php";?>
    [/code]


    In the example above, the [B]header.php[/B] we would move everything from the <body> tag and up; [B]footer.php[/B] would have everything from the closing </body> tag down, from your current [B]index.php[/B]. Then on every new page include your header, navbar, content and footer. The home-content.php in the example, would be your main content.

    SIDENOTE: The only thing is, in order for you to see the pages locally on your machine you might need a php server running locally on your machine.

    I recommend you look into installing a local php server on your machine. This makes testing your code easier and without having to ftp changes to your live server every time. A free and easy to setup solution is XAMPP. Here is a link with more info:

    https://www.apachefriends.org/index.html

    Hope this isn't confusing
    Copy linkTweet thisAlerts:
    @busterauthorMay 17.2017 — Well, it is a bit confusing. ?

    I'll try my luck with your suggestions and report back.

    Thanks again.

    Ed
    ×

    Success!

    Help @buster spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.21,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...