I have this code:
nav li{
display: inline-block;
background-color:blue;
width: 70px;
padding: 5px;
}
nav li:first-child, nav li:last-child {
border-radius: 5px;
}
<nav>
<li>Home</li>
<li>Work</li>
<li>Contact</li>
</nav>
I would like to remove this right margin.
How can I do it? I tried:
margin:0
padding:0
It doesn't work.
It's a common problem of inline-blocks, you should remove white space between elements.
nav li{
display: inline-block;
background-color:blue;
width: 70px;
padding: 5px;
}
nav li:first-child, nav li:last-child {
border-radius: 5px 0 0 5px;
}
nav li:last-child {
border-radius: 0 5px 5px 0;
}
<nav>
<li>Home</li><!--
--><li>Work</li><!--
--><li>Contact</li>
</nav>
Reference: Fighting the Space Between Inline Block Elements
Please reset of parent element font-size: 0 and line-height: 0
then set required font-size and line-height on child element
Like this way
nav li{
display: inline-block;
background-color:blue;
width: 70px;
padding: 5px;
font-size: 14px;
line-height: 20px;
color: #fff;
text-align: center;
}
nav li:first-child {
border-radius: 5px 0 0 5px;
}
nav li:last-child{border-radius: 0 5px 5px 0;}
nav ul{
font-size: 0;
line-height: 0;
}
nav li:hover{
background: #333;
}
<nav>
<ul>
<li>Home</li>
<li>Work</li>
<li>Contact</li>
</ul>
</nav>
Related
What I'm looking for:
/
What I've got:
I want to include small vertical lines, evenly spaced with matching color, in between the links in my navbar. Below is the CSS code I've written. I'm new to coding and I've searched Google but I keep seeing the same answer which got me these huge lines that I don't want. ???
/* GLOBAL */
body {
margin: 0 auto;
background: grey;
font-family: 'Arial', 'Helvetica Neue', Helvetica, sans-serif;
font-weight: 300;
}
/* NAVBAR */
header {
background: #ffffff;
max-width: 100%;
border-bottom: 2px solid #777777;
}
header::after {
content: '';
display: table;
clear: both;
}
.container {
margin: 0 auto;
max-width: 960px;
}
.logo {
float: left;
background-color: #4aaaa5;
color: #ffffff;
margin: 0;
padding: 20px 30px;
display: inline-block;
font-family: 'Georgia', Times, 'Times New Roman', serif;
}
nav {
float: right;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
}
nav li {
display: inline-block;
padding-top: 30px;
margin-left: 20px;
**border-right: 1px solid #777777;**
}
nav a {
color: #777777;
text-decoration: none;
}
nav li:last-of-type {
**border-right: none;**
}
nav a:hover {
color: #4aaaa5;
}
===========================================================================
<body>
<header>
<div class="container">
<h1 class="logo">Name</h1>
<nav>
<ul>
<li><a class="active" href="about.html">About</a></li>
<li>Portfolio</li>
<li>Content</li>
</ul>
</nav>
</div>
</header>
</body>
You can consider using pseudo elements. That'll do the trick.
Have a look at this
ul {
list-style:none;
}
ul li{
display:inline-block;
padding:0 7px;
position:relative;
}
ul li:not(:last-child)::after{
content:"";
border:1px solid #e2e2e2;
border-width: 1px 1px 0 0;
position:absolute;
right:-3px;
top:0;
height:100%;
}
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
Try adding the border-right to each li tag except the last li tag. And add padding to the each li tag.
like the below code
.nav li{
border-right: 2px solid #c4c4c4;
display: inline-block;
float: left;
padding: 0 15px;
}
.nav li:last-child {
border-right:0;
}
Below is the full working demo here. Hope it helps you.
.nav{
list-style: none;
padding: 0;
margin: 0;
margin-top: 25px;
float: left;
}
.nav li{
border-right: 2px solid #c4c4c4;
display: inline-block;
float: left;
padding: 0 15px;
}
.nav li:last-child {
border-right:0;
}
<ul class="nav">
<li>About</li>
<li>Home</li>
<li>Contact</li>
</ul>
You can do this by using border-right or the way below.
.nav{
list-style: none;
padding: 0;
margin: 0;
margin-top: 25px;
float: left;
}
.nav li{
display: inline-block;
position: relative;
float: left;
padding: 0 15px;
}
.nav li::after {
position: absolute;
width: 2px;
right: 0;
content: ' ';
height: 100%;
background: rgba(0, 0, 0, 0.2);
}
.nav li:last-child::after {
width: 0;
}
<ul class="nav">
<li>About</li>
<li>Home</li>
<li>Contact</li>
</ul>
working demo
<ul class="nav">
<li>About</li>
<li>Home</li>
<li>Contact</li>
</ul>
.nav{
list-style: none;
padding: 0;
margin: 0;
margin-top: 25px;
float: left;
}
nav li {
display: inline-block;
padding-top: 30px;
padding-left:10px
padding-right:10px;
}
.nav li:after{
content:'|';
display:inline-block;
color:#dddddd;
vertical-align:middle;
}
.nav li:last-child:after {
display:none;
}
Add the separator image as a background image on the li.
#nav li + li {
background:url('seperator.gif') no-repeat top left;
padding-left: 10px
}
My unordered list seems to be going out of it's parent element and going under it. I'm sure this is an easy fix but I just can't figure it out. I'm new to html and css.
I've added my codes here in hope of someone figuring something out for me. I think i've asked this question before. I'm not sure, anyways. Thanks in advance.
header {
width: 100%;
height: 36px;
border-bottom: 1px solid #f0efef;
box-shadow: 0 0 5px 0 #f0efef;
}
.header-content {
width: 1030px;
height: 36px;
background-color: red;
}
.header-content p {
margin: 0;
font-size: 14px;
padding: 7px 15px 0 15px;
}
.header-content ul {
margin: 0;
float: right;
list-style: none;
padding: 5px 15px 0 15px;
}
.header-content ul li {
padding-left: 10px;
display: inline;
}
.header-content ul li a, a:visited {
color: #404040;
font-size: 14px;
text-decoration: none;
}
<header>
<div class="header-content">
<p>Welcome, please sign or register</p>
<ul>
<li>My Account</li>
<li>Gift Cards</li>
<li>Customer Care</li>
<li>Community</li>
</ul>
</div>
</header>
That paragraph element is a block level element so it's going to push your ul down. To solve the problem, you can float:left your p tag in the header
header {
width: 100%;
height: 36px;
border-bottom: 1px solid #f0efef;
box-shadow: 0 0 5px 0 #f0efef;
}
.header-content {
width: 1030px;
height: 36px;
background-color: red;
}
.header-content p {
margin: 0;
font-size: 14px;
padding: 7px 15px 0 15px;
float:left;
}
.header-content ul {
margin: 0;
float: right;
list-style: none;
padding: 5px 15px 0 15px;
}
.header-content ul li {
padding-left: 10px;
display: inline;
}
.header-content ul li a, a:visited {
color: #404040;
font-size: 14px;
text-decoration: none;
}
<header>
<div class="header-content">
<p>Welcome, please sign or register</p>
<ul>
<li>My Account</li>
<li>Gift Cards</li>
<li>Customer Care</li>
<li>Community</li>
</ul>
</div>
</header>
I'm trying to create a basic drop-down menu. Whenever I however over "blog", the "wiki" link seems to get dragged into it.
Here's a screenshot to show what I mean:
As you can see, "wiki" is below "march". I want to have "march" drop down from "blog" and keep "wiki" along the green line.
#header {
box-shadow: 0px 0px 5px 2px #000;
border-radius: 15px 15px 15px 15px;
width: 790px;
height: 30px;
line-height: 85%;
background: #002929;
word-spacing: 5px;
}
#header li {
display: inline;
}
#header ul ul {
display: none;
}
#header ul li:hover > ul {
display: block;
}
#header a:hover {
background: #0147FA;
border-radius: 15px 15px 15px 15px;
padding: 2px;
}
#header a {
text-decoration: none;
color: #ffff4c;
}
<div id="header">
<ul>
<li style="font-size:30px"><strong>Home</strong></li>
<li style="font-size:30px"><strong>Blog</strong>
<ul>
<li style="font-size:30px"><strong>March</strong></li>
</ul>
</li>
<li style="font-size:30px"><strong>Wiki</strong></li>
</ul>
</div>
Here's a JSFiddle.
So I cleaned up the css and it seems to work now.
Here is your html below
<div id="header">
<ul>
<li style="font-size:30px"><strong>Home</strong></li>
<li style="font-size:30px"><strong>Blog</strong>
<ul>
<li style="font-size:30px"><strong>March</strong></li>
</ul>
</li>
<li style="font-size:30px"><strong>Wiki</strong></li>
</ul>
</div>
Here is the new css below:
#header {
box-shadow: 0px 0px 5px 2px #000;
border-radius:15px 15px 15px 15px;
width: 790px;
height: 30px;
line-height: 85%;
background: #002929;
word-spacing: 5px;
}
#header li {
display: inline;
}
#header a:hover {
background: #0147FA;
border-radius: 15px 15px 15px 15px;
}
#header a {
text-decoration: none;
color: #ffff4c;
}
#header ul a
{
text-decoration:none;
font-weight:700;
line-height:32px;
padding:0 15px;
}
#header ul li
{
position:relative;
float:left;
margin:0;
padding:0
}
#header ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
padding:0
}
#header ul li:hover > ul
{
display:block
}
Be sure to compare to see the differences between your css and my css. If this is is not what you wanted please get back to me.
HTML:
<ul id="menu">
<li>Categories 1
<ul id="cat1">
<li class="first">temp1</li>
<li>temp2</li>
<li>temp3</li>
</ul>
</li>
</ul>
CSS:
#menu {
background-color: #0000FF;
height: 20px;
padding: 15px 0 10px;
margin: 5px;
font: 12px Tahoma;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 1px;
box-shadow: inset 0px 0px 10px #000;
text-align: center;
}
#menu > li{
display: inline;
}
li a {
color: #fff;
text-decoration: none;
}
.first{
margin-top: 12px;
}
#cat1 > li{
display: block;
background-color: #0000FF;
width: 150px;
margin-right: auto;
}
#cat1 > li > a{
display: block;
padding: 10px;
box-shadow: inset 0px 0px 2px #000;
}
When i use margin-left: auto in #cat > li it is woring properly. margin-right:auto on the other hand doesn't get the full margin, and I don't understand why this is.
Here's my fiddle: http://jsfiddle.net/ZfN7t/25/
Thank you for any and all help!
Mayb you should set <li> inline-block , maybe text-align:center and reset margin/padding of <ul> second level too :)
http://jsfiddle.net/ZfN7t/26/
ul {
margin:0 auto;
padding:0;
}
#menu > li {
display: inline-block;
}
Remove the default padding for html elements and it should work like expected.
ul,li
{
margin:0;padding:0;
}
FIDDLE
I have made a few attempts at this and they ended up just getting confusing. The current HTML and CSS seems to be working fine for a simple horizontal CSS menu. I am struggling with dropping subitems off the current <li> elements.
I am trying to making them show up exactly below the current menu items with the same hovering effects as I have in place now. Any assistance would be appreciated. I am admittedly no CSS pro.
Current HTML:
<div class="MenuTop">
<ul class="Nav">
<li>Home</li>
<li>Vehicles
<ul>
<li>SubItemOne</li>
</ul>
</li>
<li>About</li>
<li>Contact</li>
<li>News</li>
</ul>
</div>
Current CSS:
.MenuTop
{
width: 960px;
background-color: Black;
color: #fff;
margin: auto auto 0px auto;
padding: 5px;
height:auto;
font-family: Segoe UI, Arial;
font-weight:bold;
min-height:15px;
}
.MenuTop ul
{
float: left;
list-style: none;
margin: -5px ;
padding: 0px;
}
.MenuTop li
{
float: left;
font-size:12px;
font-family: Segoe UI, Arial;
margin: 0;
padding: 0;
}
.MenuTop a
{
background-color: Black;
color: #fff;
display: block;
float: left;
margin: 0;
padding: 4px 12px 4px 12px;
text-decoration: none;
}
.MenuTop a:hover
{
background-color: #404040;
color: #fff;
padding: 4px 12px 4px 12px;
}
You were close, but you're forgetting about positioning your submenu items absolutely to your parent li menu item and hiding it as well, by using display:none and then showing it on hover, so try something like this to achieve that effect:
CSS
.Nav li {
position:relative;
}
.Nav li ul {
display:none;
position:absolute;
top:30px;
}
.Nav li:hover ul {
display:block;
}
Also, your submenu ul is not properly closed so go ahead and close it.
Ninja Edit: demo, by the way you can greatly benefit from properly targeting your menu by using the class you have given it, .Nav, instead of its parent class of its container, .MenuTop, that way you can target your menu and your menu alone and not any other element you might place inside that container,
I have created a working example for you on jsFiddle.
The HTML is as follows:
<nav>
<ul class="cf">
<li>Home</li>
<li>Vehicles
<ul>
<li>Sub-menu Item 1</li>
<li>Sub-menu Item 2</li>
<li>Sub-menu Item 3</li>
</ul>
</li>
<li>About</li>
<li>News</li>
</ul>
</nav>
and the CSS:
nav ul {
background: #ddd;
list-style: none;
margin: 0;
padding: 0;
width: 100%;
}
nav li {
float: left;
margin: 0;
padding: 0;
position: relative;
min-width: 25%;
}
nav a {
font-family: Lucida Grande;
background-color: #666666;
-webkit-background-clip: text;
-moz-background-clip: text;
background-clip: text;
color: transparent;
text-shadow: rgba(255,255,255,0.5) 0px 3px 3px;
display: block;
font: bold 14px sans-serif;
padding: 0 25px;
text-align: center;
text-decoration: none;
}
nav li ul {
float: left;
left: 0;
opacity: 0;
position: absolute;
top: 1em;
visibility: hidden;
z-index: 1;
}
nav li:hover ul {
opacity: 1;
top: 1em;
visibility: visible;
}
nav li ul li {
float: none;
width: 100%;
}
.cf:after, .cf:before {
content:"";
display:table;
}
.cf:after {
clear:both;
}
.cf {
zoom:1;
}