Not full width css horizontal menu - css

Relatively new to CSS, so please bear with my inexperience. I'm trying to create a menu, and after much searching and comparison and reading and copying, I've mostly come up with the format I want. The problem is that I want my menu to be the width of its content, not full width, and the code below (adapted from various examples) yields a full width menu. I've played around with things, but can't seem to identify what makes it full width or not -- it may be that what I want requires a more substantial rewrite.
In case it helps, what I want is a horizontal menu with an outer rectangular border, with width determined by its contents, not automatically full width (or even, not automatically a specified width).
This is my first time posting a question here, so thanks in advance for your help and patience!
<style type="text/css">
*/#menu ul,#menu li,#menu a{
list-style:none;
margin:0;
padding:0;
border:0;
font-family: Arial}
#menu{
border:1px solid #000000;
border-radius:5px}
#menu ul{
background:#ffffff;
padding:5px 10px;
border-radius:5px}
#menu ul:before{
content:'';
display:block}
#menu ul:after{
content:'';
display:block;
clear:both}
#menu li{
float:left;
margin:0px 0px 0px 0px;
border:0px}
#menu li a{
border-radius:5px;
padding:5px 10px 5px;
display:block;
text-decoration:none;
text-align:center;
color:#000000;
border:0px;
font-size:15px}
</style>
<div id="menu">
<ul>
<li><span>Link1</span></li>
<li><span>Link2</span></li>
<li><span>Link3</span></li>
<li><span>Link4</span></li>
</ul>
</div>

ul are block level elements are so are 100% wide by default.
Make the ul display as inline-block and it will collapse to the width of it's contents.
Add text-align to the parent as required. Here I used center.
#menu ul,
#menu li,
#menu a {
list-style: none;
margin: 0;
padding: 0;
border: 0;
font-family: Arial
}
#menu {
border: 1px solid #000000;
border-radius: 5px;
text-align: center;
}
#menu ul {
display: inline-block;
background: lightblue;
padding: 5px 10px;
border-radius: 5px
}
#menu ul:before {
content: '';
display: block
}
#menu ul:after {
content: '';
display: block;
clear: both
}
#menu li {
float: left;
margin: 0px 0px 0px 0px;
border: 0px
}
#menu li a {
border-radius: 5px;
padding: 5px 10px 5px;
display: block;
text-decoration: none;
text-align: center;
color: #000000;
border: 0px;
font-size: 15px
}
<div id="menu">
<ul>
<li><span>Link1</span>
</li>
<li><span>Link2</span>
</li>
<li><span>Link3</span>
</li>
<li><span>Link4</span>
</li>
</ul>
</div>

Related

Centering float:left thing vertically

I can't figure it out how to center float:left object vertically.
I imagine that I could set the position of this menu bar vertically (The height of Y-axis) I think that would be the answer
// html part
<div class="menu_simple">
<ul>
<li>One</li>
<li>Two</li>
</ul>
//css
.menu_simple ul {
float:left;
margin: 0;
padding: 0;
width:100px;
list-style-type: none;
box-shadow: 5px 8px 5px #888888;
}
.menu_simple ul li a {
border: 1px solid #0066cc;
text-decoration: none;
color: white;
padding: 10.5px 11px;
background-color: #3b3b3b;
display:block;
}
.menu_simple ul li a:visited {
color: white;
}
.menu_simple ul li a:hover, .menu_simple ul li .current {
color: white;
background-color: #5FD367;
}
Fiddle example
First you set position: absolute for the menu div, then set top to 50% and the transform option to -50%.
Source: https://css-tricks.com/centering-css-complete-guide/
Hope this helps
Use the CSS position property.
Set the page hold, in your case the <body> to position: relative; and the part you wish to move, in your case; .menu_simple to the following:
.menu_simple {
position: absolute;
top: 50%;
left: 0;
}
Flexbox works nicely for this type of thing:
http://codepen.io/simply-simpy/pen/rVwXyz
menu_simple {
display:flex;
justify-content: flex-start;
align-items: center;
height: 500px;
border: 1px solid red;
}

CSS navigation sub-menu help... Block goes off screen

I am working on a site for my brother's new plumbing company, and I am having two issues with my navigation menu. I copied some CSS from a tutorial site and tried altering it to my taste, but I can't figure out why this is happening. When I hover over the top-level menu item ("Services" in this case), the sub-menu appears as it should, but the background of the LI's extend outside of the screen. I have tried changing/setting widths on different areas of the CSS, but my limited knowledge has me scratching my head bald. I have a temporary site up, but I am testing this new version with Media Queries locally.
It won't let me post a screenshot without 10 reputation, so here is a link: http://www.sourceplumbing.com/Capture.png
The HTML:
<nav>
<ul>
<li>About Us</li>
<li>Services
<ul>
<li>Leak Detection</li>
<li>Water Heaters</li>
<li>Plumbing Fixtures</li>
</ul>
</li>
<li>Reviews</li>
<li>Contact Us</li>
</ul>
</nav>
The CSS:
nav ul li:hover > ul {
display: block;
}
nav ul {
width:100%;
height:auto;
min-width:550px;
background: -webkit-linear-gradient(#d5d5d5, #595959); /* For Safari */
background: -o-linear-gradient(#d5d5d5, #595959); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(#d5d5d5, #595959); /* For Firefox 3.6 to 15 */
background: linear-gradient(#d5d5d5, #595959); /* Standard syntax */
-webkit-border-radius: 25px;
-o-border-radius: 25px;
-moz-border-radius: 25px;
border-radius: 25px;
font-size: 16px;
font-family: "Times New Roman", Times, serif;
font-weight: bold;
text-align: center;
text-shadow: 2px 2px 2px #cccccc;
text-decoration:none;
list-style: none;
position: relative;
display: table;
}
nav a:link, a:visited {
text-decoration: none;
color: #000000;
padding: 8px 0px 8px 8px;
}
nav ul:after {
content: "";
clear:both;
display: block;
}
nav ul li {
display:inline-block;
width:auto;
}
nav ul li:hover a {
text-decoration:none;
color: #fff;
}
nav ul li a:link, a:visited {
display: block;
padding: 5px;
color:#000;
text-decoration: none;
}
nav ul ul {
background: #999;
border-radius: 0px;
padding: 0;
position: absolute;
width:150px;
}
nav ul ul li {
width:150px;
border-top: 1px solid #ccc;
position: relative;
display:block;
}
nav ul ul li a {
width:150px;
padding: 5px;
text-align:left;
color:#000;
display:block;
}
nav ul ul li a:hover {
color:#FFF;
}
I do have another issue, but if I can get this figured out, I will move onto that one next. I would appreciate any help you can offer!
What you are looking for is min-width: 550px;. This is causing ALL <ul> to expand to a min width of 550px.
Taking this out will fix the problem. I guess that was there for the nav to stop it getting to small, in that case you should be about to put that under nav and not nav ul.
DEMO HERE
Putting the min-width in the right place:
nav {
min-width: 550px;
}
DEMO HERE

CSS margin-top on inner div pushing parent div down [duplicate]

This question already has answers here:
Margin-Top push outer div down
(8 answers)
Closed 8 years ago.
I am trying to push the child div down 5px inside of the parent div. When I put margin-top:5px; on the inner div it pushes down both the inner div and the parent div from the div above the parent div, but does not push the inner div down from the parent div. How to I set it so that only the inner div is pushed down 5px from the parent div? I do not want the parent div to push down from the div above it. Thanks for any help.
html for Navigation:
<nav>
<div class="nav-container">
<div id="cat_14623_divs">
<ul id="nav_14623">
<li><a href="#" onclick="return false;">AKINA & RED LAKE</a</li>
<li>FRESH WILD CAUGHT FISH
<ul id="navsub_14623_2326">
<li>WALLEYE</li>
<li>PERCH</li>
<li>CRAPPIE</li>
<li>NORTHERN</li>
<li>WHITEFISH</li>
</ul></li>
<li>NEWS FROM THE FISHERY</li>
<li>CONTACT US</li>
<li class="last">FAQs</li>
</ul>
</div>
</div>
</nav>
CSS for Navigation:
nav{
position:relative;
width:960px;
background-color:#660000;
height:40px;
}
div.nav-container{
position:relative;
width:100%;
}
#cat_14623_divs{
margin-top:5px;
height:30px;
background-color:#520000;
width:960px;
}
#nav_14623{
list-style:none;
display:block;
padding:0;
width:80%;
margin:0 auto;
}
#nav_14623 li{
position:relative;
float:left;
padding: 0.5em 1.5em;
margin: 0px;
font-size:12px;
border-right:solid 2px #fff;
text-align:center;
}
#nav_14623 li.last{
border-right:none;
padding-right:5px;
}
#nav_14623 li a{
color:#FFF;
text-decoration:none;
}
#nav_14623 ul{
position: absolute;
left:0;
top: 100%;
display: none;
padding: 0 1000em; /* trick from css-tricks comments */
margin: 0 -1000em; /* trick from css-tricks comments */
list-style: none;
margin-left: 0px;
margin: 0;
padding: 5px;
width:200px;
z-index: 1000;
background: #660000;
border-left: 1px solid #336699;
border-right: 1px solid #336699;
border-bottom: 1px solid #336699;
border-top: none;
}
#nav_14623 ul li{
position: relative;
float: none;
border-right: none;
padding:0;
margin: 0;
}
#nav_14623 ul li a{
color: #fff;
font-size: 12px;
vertical-align: middle;
line-height:32px;
width: 100%;
height:35px;
display:block;
}
#nav_14623 ul li a:hover{
background: #520000;
width: 100%;
}
#nav_14623 li:hover > ul{
display: block;
}
#nav_14623:after {
content: ""; clear: both; display: block;
}
Here is the url in case you want to see the site:
http://redlakewalleye.designangler.com/
Instead of a margin-top on the child, you should use a padding-top to the parent.
UPDATED (as a reaction on your comment)
A margin needs something to bounce on. Since the parent div has nothing to bounce on, it will bounce on the element above it. Therefor you should use padding.
The issue is collapsing margins when margins touch - http://www.sitepoint.com/web-foundations/collapsing-margins/

How to place this tab in horizontal in IE 7?

The image is the menu list items which displays vertically in IE7 but I want to display it horizontally. I am using smart wizard plugins (framestyle.css) which displays perfectly in all other browsers except IE7.
framestyle.css: this is the css for smart wizard plugins.
.swMain ul.anchor {
position:fixed;
z-index:1099;
display:inline;
float:left;
list-style: none;
padding: 0px;
margin: 5px 5px 0 0;
}
.swMain ul.anchor li{
position: relative;
margin: 0;
padding: 0;
padding-top:3px;
padding-bottom: 3px;
clear:both;
display:inline;
float: none;
}
.swMain ul.anchor li a {
display:block;
position:relative;
float:left;
margin:0;
padding:3px;
height:35px;
width:146px;
text-decoration: none;
outline-style:none;
}
IE7.css: this is the css for the menu list item for IE7 browser
.swMain ul.anchor {
display:inline;
position:relative;
list-style: none;
padding: 0px 0px 0px 0px;
margin: 10px 0px 0px 10px;
zoom:1;
float:left;
clear:both;
}
.swMain ul.anchor li{
margin: 0;
padding: -10px;
padding-top:0px;
padding-bottom: 0px;
clear:both;
display:inline;
float:left;
}
.swMain ul.anchor li a {
display:block;
position:relative;
float:left;
margin:0px 0px 0px 0px;
padding:3px 3px 3px 3px;
text-decoration: none;
outline-style:none;
}
If you want something to display inline horizontally, you shouldn't have clear: both; in the first place, because that will put the floating items on a new line.
You should also remove the float: left; from .swMain ul.anchor li in your IE7 stylesheet.
For clarity, the following code will put your list-items inline horizontally:
li {
display:inline;
}
If you want to style the list-items with some padding and other fancy stuff, you can try inline-block, but this will cause a few issues with IE7. You will find a solution here.
By the way, I was required to support IE7 for some specific purpose projects, but in general you may very well drop IE7 support altogether.
Hope this helps.

Add a separator between buttons in a menu bar (HTML/CSS)

I'm making a mobile website and having some difficulty with making a few changes to my menu bar. I'm not an expert on this field so your help would be greatly appreciated.
Below is the codes to the menu bar.
CSS
<style type="text/css">
* { padding: 0; margin: 3; }
body { padding: 5px; font-family: Helvetica, Arial, sans-serif; width:95%; font-size:12px}
ul { list-style: none; }
ul li {
float: left;
padding: 1.5px;
position: relative;
margin: auto;}
ul a { display: table-cell; vertical-align: middle; width: 75%; height: 50px; text-align:center; background: #FFF; color:#000; border-style: solid; border-width:2px; border-color:#1570a6; text-decoration: none; }
ul a:hover {background-color:#5A87B4; }
HTML
<div>
<ul>
<li>
<div align="center"><a href="../Software.html" >Software</a>
</div>
</li>
<li>
<div align="center">Products</div>
</li>
<li>
FAQ</li>
</ul>
This is a basic menu bar and i want to adjust this to the center and also have horozontal lines to break each button apart while all this is centered and fits a 100% on a mobile screen. All your help is greatly appreciated
EDIT: Its like having some space after each button but instead theres a horizontal line
EDIT: Changed the width from 75% to 80px. Note that i also changed the div ID of my code because i was having some other problems with identification. :) Hope this wont confuse you
#menubar * { padding: 0; margin: 2; }
body { padding: 5px; font-family: Helvetica, Arial, sans-serif; width:95%; font-size:12px}
#menubar ul{text-align:center;}
#menubar ul li { display:inline-block; padding: 2px; position: relative; }
#menubar ul a { display: table-cell; vertical-align: middle; width: 80px; height: 50px; text-align:center; background: #FFF; color:#000; border-style: solid; border-width:2px; border-color:#1570a6; text-decoration: none; }
I added below lines in your css code. I hope this is what you want.
ul{
display:inline-block;
overflow:hidden;
}
div{
text-align:center;
}
li:after{
border-right:50px solid black;
content:"";
position:relative;
left:10px;
top:-27px;
z-index:-1;
display:block;
height:1px;
}
li:last-child{
margin-right:-14px
}
Working Fiddle
Now just remove float:left in your li and add display:inline-block; and add text-align center in your ul tag
as like this
ul{
text-align:center;
}
ul li{
display:inline-block;
vertical-align:top;
float:left; // remove this line
}
Demo
from your current css remove float:left; on li's and add text-align:center; and it should work:
ul li {
text-align: center;
padding: 1.5px;
position: relative;
margin: auto;
}
here is a working JSFiddle.
Update
In that case you can change the CSS to.
ul li{
text-align:center;
display:inline-block;
}
ul li:before {
content: " - ";
}
ul li:first-child:before {
content: none;
}
Here is a working JSFiddle

Resources