So I'm trying to build a responsive navigation menu that'll scale with media queries.
The problem is (and it's a stupid one I can't work out) that I can't get the a tags to appear as block level elements(I think).
Here's the HTML:
<div class="navbuttons">
<ul>
<li>Home</li>
<li>About</li>
<li>Expertise</li>
<li>Capabilities</li>
<li>Case Studies</li>
<li>Contact</li>
</ul>
</div> <!-- end div navbuttons -->
And the CSS:
.navbuttons {
position: relative;
height: 100px;
width: 50%;
background-color: blue;
float: left;
transition:all .2s linear;
-o-transition:all .2s linear;
-moz-transition:all .2s linear;
-webkit-transition:all .2s linear;
}
.navbuttons ul {
list-style: none;
margin:0;
padding:0;
float: left;
}
.navbuttons li {
float:left;
}
.navbuttons li a {
background: #444;
display: block;
float: left;
line-height: 100px;
height: 100%;
text-align: center;
text-decoration: none;
text-transform: uppercase;
width: 16.5%;
}
Yet, if I use px to define width/height, I can get it looking roughly how I want (though it obviously isn't fluid, like I need).
.navbuttons {
position: relative;
height: 100px;
width: 50%;
background-color: blue;
float: left;
transition:all .2s linear;
-o-transition:all .2s linear;
-moz-transition:all .2s linear;
-webkit-transition:all .2s linear;
}
.navbuttons ul {
list-style: none;
margin:0;
padding:0;
float: left;
}
.navbuttons li {
float:left;
}
.navbuttons li a {
background: #444;
display: block;
float: left;
line-height: 100px;
height: 100px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
width: 100px;
}
Where am going wrong?
The A tags are indeed block level once you specify display: block; on them, however, the 16.5% is calculated from their natural width (i.e. their 100% width based on the inner text).
Depending on your needs, you may want to remove the width parameter and simply add padding. See: http://jsfiddle.net/bLS6B/. This way, the width is dependent on the navigation text, not some preset width. Alternatively, you can specify 120% which has a similar effect.
Related
I'm new to HTML and CSS, and this is my first project.
I have this "space" near the end of the navbar and it forces the next link to break into a new line. Also, I cannot find a solution that removes the gap between the background image and the navbar no matter the screen size.
I'm not able to pinpoint what is causing these problems, and I'm sure it's just some petty mistake.
navbar http://prntscr.com/g0xi6z
.topnavhome nav {
position: relative;
display: block;
margin: 0px;
background-color: #333333;
overflow: hidden;
text-align: justify;
font-size: 0px;
min-width: 500px;
width: 100%;
}
.topnavhome:after {
content: '';
display: inline-block;
width: 100%;
}
.topnavhome .li {
display: inline-block;
}
.topnavhome nav li a {
float: left;
display: block;
padding: 12px;
width: 180px;
margin-bottom: -1.5px;
color: #f2f2f2;
border-left: solid 3px #333333;
text-align: center;
text-decoration: none;
font-family: 'Lato', sans-serif;
font-size: 20px;
-webkit-transition: background .3s linear;
-moz-transition: background .3s linear;
-ms-transition: background .3s linear;
-o-transition: background .3s linear;
transition: background .3s linear;
}
.topnavhome nav a:hover {
background-color: #046A78;
color: white;
border-left: solid 3px #79CBD6;
-moz-transition: background-color 0.01s;
-webkit-transition: background-color 0.01s;
-o-transition: background-color 0.01s;
transition: background-color 0.01s;
}
<ul class="topnavhome" id="myTopnav">
<nav>
<li>Home</li>
<li>Network Security</li>
<li>Passwords</li>
<li>Firewalls</li>
<li>Encryption</li>
<li>Biometric Devices</li>
<li>References</li>
</nav>
</ul>
Fix Error:
* {
box-sizing: border-box;
}
More css for setting nav use :
.topnavhome {
padding: 0;
}
And define height for nav and a element.
body, ul {
margin:0;
}
* {
box-sizing: border-box;
}
.topnavhome {
padding: 0;
}
.topnavhome nav {
position: relative;
display: block;
margin: 0px;
background-color: #333333;
overflow: hidden;
text-align: justify;
font-size: 0px;
min-width: 500px;
width: 100%;
height: 90px;
}
.topnavhome:after {
content: '';
display: inline-block;
width: 100%;
}
.topnavhome li {
display: inline-block;
}
.topnavhome nav li a {
float: left;
display: block;
padding: 12px;
width: 180px;
margin-bottom: -1.5px;
color: #f2f2f2;
border-left: solid 3px #333333;
text-align: center;
text-decoration: none;
font-family: 'Lato', sans-serif;
font-size: 20px;
-webkit-transition: background .3s linear;
-moz-transition: background .3s linear;
-ms-transition: background .3s linear;
-o-transition: background .3s linear;
transition: background .3s linear;
height: 90px;
}
.topnavhome nav a:hover {
background-color: #046A78;
color: white;
border-left: solid 3px #79CBD6;
-moz-transition: background-color 0.01s;
-webkit-transition: background-color 0.01s;
-o-transition: background-color 0.01s;
transition: background-color 0.01s;
}
<ul class="topnavhome" id="myTopnav">
<nav>
<li>Home</li>
<li>Network Security</li>
<li>Passwords</li>
<li>Firewalls</li>
<li>Encryption</li>
<li>Biometric Devices</li>
<li>References</li>
</nav>
</ul>
About a "space" near the end of the navbar which forces the next link to break into a new line, you have this problem cause there is not enough place to fit in, you need to use "#media queries", so that when you would reduce width it would fit nicely. For example on my screen I do not have such problem
Here are a link for good examples of using #media-query
https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries
And next, you have your <ul> height greater than <nav> in it. And the reason why expanding height of nav wouldn't help is that you have content after your <ul>, which is taking a gap place after nav.
just delete that line:
.topnavhome:after {
content: ''; <------- delete this one
display: inline-block;
width: 100%;
}
you have fixed width of 180px to .topnavhome nav li a which making the li to stack down as they doesn't fit in the container width.
Try using flex which will equally divide the width to li
nav { display: flex;}
nav li { flex: 1;}
Please try below code.
Here your css is not working well it's due to all li element's are not fitting in container due to this reason displaying in new line.
Just add below classes.if it's already their then update the properties.
ul#myTopnav {
-webkit-padding-start: 5px;
}
Here i have only made changes in width & padding.
.topnavhome nav li a {
padding: 12px 6px 12px 6px;
width: 172px;
}
One more thing is used #media query if it's not fitting in small screen or larger screen.
Hope this helps.
I am a bit new when it comes to web development.
I have created a menu but its buttons width are different depending on the browser (Firefox or Chrome)
In Firefox, we can see that the end of the last button of the menu is aligned with the div below. We can also appreciate that the width of the button is 136.5px
However, in Chrome the fonts are bolder and the menu end is pushed a bit forward. Here, the width of the button is 139.281px
This is the site, the menu is on the top: http://www.metagame.gg/champions/
This is the HTML and the CSS code for the menu
.navigator {
margin: 0;
padding: 0;
display: flex;
padding-left: 39px;
background: #8C9BAA;
}
.navigator li {
display: inline-block;
position: relative;
z-index:100;
}
.navigator li a {
text-decoration: none;
font-size: 15px;
display: block;
line-height: 50px;
padding: 2px 27.75px 0px;
-webkit-transition: all 0.2s ease-in-out 0s;
-moz-transition: all 0.2s ease-in-out 0s;
-o-transition: all 0.2s ease-in-out 0s;
-ms-transition: all 0.2s ease-in-out 0s;
transition: all 0.2s ease-in-out 0s;
}
.navigator li a:hover, .navigator li:hover > a {
color:#fff;
background: #7AAFD1;
}
.navigator ul {
display: none;
margin: 0;
padding: 0;
width: 170px;
position: absolute;
left: 0px;
}
.navigator ul li {
display:block;
float: none;
background:none;
margin:0;
padding:0;
}
.navigator ul li a {
font-size:12px;
font-weight:bold;
display:block;
color:#797979;
border-left: 3px solid #ffffff;
background: #fff;
line-height: 42px;
transition: 0s;
}
.navigator ul li a:hover, .menu ul li:hover > a {
background:#EFEFEF;
border-left:3px solid #83BEE4;
color: #12303D;
}
.navigator li:hover > ul {
display: block;
}
.navigator ul ul {
left: 149px;
top: 0px;
}
#in {
color:#fff;
background: #7AAFD1;
}
.sub-menu {
border:1px solid #ddd;
border-top:0px;
border-left:0px;
box-shadow: 3px 5px 5px -5px #3B3B3B;
}
.navigatorSecond {
margin: 0;
padding: 0;
display: flex;
padding-left: 39px;
background: #d1d1d1 ;
}
.navigatorSecond li {
display: inline-block;
position: relative;
z-index:80;
}
.navigatorSecond li a {
font-weight:600;
text-decoration:none;
font-size: 13px;
display:block;
color:#4a4a4a;
line-height: 40px;
padding: 0px 35.3px;
-webkit-transition: all 0.2s ease-in-out 0s;
-moz-transition: all 0.2s ease-in-out 0s;
-o-transition: all 0.2s ease-in-out 0s;
-ms-transition: all 0.2s ease-in-out 0s;
transition: all 0.2s ease-in-out 0s;
}
#ini {
color:#fff;
background: #7AAFD1;
}
.navigatorSecond li a:hover, .navigatorSecond li:hover > a {
color:#fff;
background: #7AAFD1;
}
.navigatorSecond ul {
display: none;
margin: 0;
padding: 0;
width: 170px;
position: absolute;
left: 0px;
}
.navigatorSecond ul li {
display:block;
float: none;
background:none;
margin:0;
padding:0;
}
.navigatorSecond ul li a {
font-size:13px;
font-weight:bold;
display:block;
color:#797979;
border-left: 3px solid #ffffff;
background: #fff;
line-height: 42px;
transition: 0s;
}
<div class="menuWrap">
<ul class="navigator">
<li>HOME</li>
<li>CHAMPIONS</li>
<li>SOLO QUEUE <img class="iconMenu" src="/icon/expandir.png" alt=""/>
<ul class="sub-menu">
<li>Tier List</li>
<li>Basics & Tactics</li>
<li>Psychology</li>
<li>Picks & Bans</li>
<li>Division analysis</li>
</ul>
</li>
<li>PRO SCENE <img class="iconMenu" src="/icon/expandir.png" alt=""/>
<ul class="sub-menu">
<li>Sub-Menu 1</li>
<li>Sub-Menu 2</li>
<li>Sub-Menu 3</li>
</ul>
</li>
<li>IN-DEPTH ANALYSIS</li>
<li>STATISTICS</li>
</ul>
</div>
I think it is because the font is a bit bolder in Chrome and that causes the button width to be higher.
Thanks in advance
Update: This is because each browser has its own font rendering engine.
I replicated your results with your current sans-serif font. The case also occurred when I changed the font to monospace.
I believe you're better off using a non-system font. For instance, I wanted to see what would happen with a typical Google font such as Open Sans, and I found that the issue did not exist when using it.
By the way, good work on that website. It looks nice. TSM! TSM! TSM!
*{margin:0; padding:0;}
put this line in top of your css file. It helps me for different browser. Just let me know that it helps you or not.
.navigatorSecond li {
display: inline-block;
position: relative;
z-index:80;
}
instead of display inline-block use float left. that will not be different. or use .navigatorsecond{font-size=0;}
I'm trying to make the text on my nav bar grow in size on hover, I've managed to do this but when I do this the other text links move down slightly and push the others to the right (except the end one)
here's my code:
/* -----------------------------------------------------------------*/
html, body {
margin:0;
padding:0;
}
.header {
margin-top:-7px;
background-color:#333333;
height:70px;
}
.container {
max-width: 940px;
margin: 0 auto;
padding: 30px 10px;
}
.nav {
list-style-type: none;
margin: 0;
padding: 0px 0;
color:white;
}
.nav li {
color: #fff;
display: inline;
font-family: 'Raleway', sans-serif;
font-weight: 600;
font-size: 15px;
margin-right: 25px;
text-transform: uppercase;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
-ms-transition: all 0.5s ease;
transition: all 0.5s ease;
}
/* NAV PROPERTIES */
.nav li:hover{
cursor: pointer;
font-size:18px;
text-shadow:0px 0px 5px white;
margin-top: 10px;
padding: 6px;
}
Define a line-height on .nav li
.nav li {
line-height: 10px;
}
https://jsfiddle.net/gnd6q0sp/1/
You are changing the size and margins of the list items...of course the other links move...that's what is supposed to happen.
One option is to just visually scale the list item using a transform instead.
.nav li:hover{
transform:scale(1.1);
cursor: pointer;
text-shadow:0px 0px 5px white;
}
JSfiddle Demo
Note: I am not recommending this option, I think there are better methods available to you.
I'm trying to create a button in CSS with an image as a background.
So far I have been able to get it to work, as long as the Text is only one line.
Now I'm trying to get it to work with a two line button, but it is either braking the layout or like in the jsfiddle not showing the second line.
Here is my CSS section:
#font-face {
font-family: 'easy_street_eps';
src: url('http://www.fontsaddict.com/fontface/easy-street-eps.ttf');
}
#gallery {
list-style: none;
list-style-type: none;
}
#gallery a {
text-decoration: none;
color: #FFF;
}
#gallery a:hover { text-decoration:underline;}
#gallery ul { -webkit-padding-start: 0px; }
#gallery li {
padding: 0 20px 0 0;
display: inline;
background:url("http://imageshack.com/a/img843/2751/gidy.png") no-repeat scroll 0% 0%;
font-family: "easy_street_eps";
font-size:35px;
color: #FFF;
text-align: center;
text-decoration: none;
cursor: pointer;
border: none;
min-width: 200px;
min-height: 140px;
float:left;
line-height : 140px;
padding-top: 4.25px;
transition: all 0.5s ease 0s;
-webkit-transition: all 0.5s ease 0s;
-moz-transition: all 0.5s ease 0s;
-o-transition: all 0.5s ease 0s;
}
.gallery2lb {
line-height: 20px;
width: 100px;
max-height: 140px;
}
Here is the jsfiddle: http://jsfiddle.net/2vMPs/
Any help is very much appreciated.
thanks,
Alex
Demo
Defining the parent <li> as display: table and the element <a> itself with vertical-align:middle & display:table-cell, and removing the other unwanted styles as the this will take care of issue.
CSS
li {
display: table;
}
a {
display: table-cell;
vertical-align: middle;
}
#font-face {
font-family:'easy_street_eps';
src: url('http://www.fontsaddict.com/fontface/easy-street-eps.ttf');
}
#gallery {
list-style: none;
list-style-type: none;
}
#gallery a {
text-decoration: none;
color: #FFF;
}
#gallery a:hover {
text-decoration:underline;
}
#gallery ul {
-webkit-padding-start: 0px;
}
#gallery li {
/*padding: 0 20px 0 0;*/
/* display: inline; */
background:url("http://imageshack.com/a/img843/2751/gidy.png") no-repeat scroll 0% 0%;
font-family:"easy_street_eps";
font-size:35px;
color: #FFF;
text-align: center;
text-decoration: none;
cursor: pointer;
border: none;
min-width: 200px;
min-height: 150px;
float:left;
/* line-height : 140px;
padding-top: 4.25 px; */
transition: all 0.5s ease 0s;
-webkit-transition: all 0.5s ease 0s;
-moz-transition: all 0.5s ease 0s;
-o-transition: all 0.5s ease 0s;
}
.gallery2lb {
/* line-height: 20px; */
width: 100px;
max-height: 140px;
}
HTML
<div style="text-align: center;">
<div id="gallery">
<ul>
<li><a title="Candyland" href="#">Candyland</a>
</li>
<li class="gallery2lb"><a title="Diese Seite befindet sich noch im Aufbau" href="#">Alice in Wonderland</a>
</li>
<li><a title="Arielle" href="#">Arielle</a>
</li>
<li><a title="Rainbow" href="#">Rainbow</a>
</li>
</ul>
</div>
</div>
You can use em to adjust the font-size on your parent container where the text resides.
#gallery a {
text-decoration: none;
color: #FFF;
font-size: 0.6em;
}
You can also use instead of font-size. vh is CSS new viewport sized typography.
font-size:4.2vh;
Another technique, however, not consistent (for all buttons is) to change this on #gallery li:
line-height : 30px;
padding-top: 30px;
This will give you something like this:
You can't vertically center align multiline text with line-height. Instead, if ancient browser support is not an issue, you can use css3 flexible boxes for centering by adding the following to <li>
display: flex;
justify-content: center;
align-items: center;
Updated fiddle
If older browser support is a requirement you can go with the display:table method mentioned in this answer
I would like a topbar navigation, similar to one that you see with Foundation. The problem i'm having is that i've set margins for the page and dont know how to override them?
So for most of page (body) I need these margins but for the top bar i'd like it extending the full width of the browser.
Here's the code:
body {
font-family: 'Droid Sans', sans-serif;
font-size: 1.2em;
color: #000000;
background-color: white;
margin: 0em 6.5em 3.5em;
#nav ul {
width: 100%;
background-color: #212121;
font-size: 12px;
font-weight: bold;
color: white;
text-align: left;
display: block;
margin: 0;
padding: 15px 4px 17px 0;
list-style: none;
}
#nav ul li {
display: inline-block;
margin-right: -4px;
height: inherit;
margin-left: 20px;
position: relative;
padding: 15px 20px;
background: #212121;
cursor: pointer;
-webkit-transition: all 0.2s;
-moz-transition: all 0.2s;
-ms-transition: all 0.2s;
-o-transition: all 0.2s;
transition: all 0.2s;
}
#nav ul li:hover {
background: #212121;
color: #fff;
}
#nav ul li ul {
padding: 0;
position: absolute;
top: 48px;
left: 0;
width: 150px;
-webkit-box-shadow: none;
-moz-box-shadow: none;
box-shadow: none;
display: none;
opacity: 0;
visibility: hidden;
-webkit-transiton: opacity 0.2s;
-moz-transition: opacity 0.2s;
-ms-transition: opacity 0.2s;
-o-transition: opacity 0.2s;
-transition: opacity 0.2s;
}
#nav ul li ul li {
background: #212121;
display: block;
color: #fff;
}
#nav ul li ul li:hover { background: #212121; }
#nav ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
Html:
<body>
<div id="nav">
<ul>
<li>Home</li>
<li>Venue</li>
<li>Events</li>
<li>Stalls
<ul>
<li>Food</li>
<li>Arts & Crafts</li>
</ul>
</li>
<li>Contact</li>
</ul>
</div>
Besides using absolute positioning, you can just use negative margins for the #nav like this:
#nav {
margin-left:-6.5em;
margin-right:-6.5em;
}
Demo
Apply the following css for the header,
#nav{
position:absolute;
left:0;
right:0;
}
It's take the nav out of normal flow and stretch it from left to right.
Check this JSFiddle
If you set position:fixed; width:100%; then the header will be taken out of the normal flow and will be positioned relative to the window. It'll stay where it is even if the user scrolls down the page.
Use fixed positioning for the navigation bar you want at the top of your page.
#nav {
min-width:100%;
position:fixed;
top:0;
left:0;
}
Adding a min-width of 100% should ensure the navigation bar stretches across the width of your page. Setting top and left to zero, in conjunction with position:fixed, would anchor the nav div to the top-left.