I have a nav bar and below text I am putting a thin line using :after.
I want that line in the middle of the li tag.
ul {
background: black;
}
ul li {
display: inline-block;
padding: 20px 10px;
cursor: pointer;
}
ul li a {
text-decoration: none;
color: #FFFFFF;
}
ul li:hover {
background: #5249FF;
}
ul li:after {
content: '';
width: 10px;
height: 3px;
background: #fff;
position: absolute;
top: 20%;
left: 15px;
}
<ul>
<li>
HOME
</li>
<li>
WORLD
</li>
<li>
SPORTS
</li>
</ul>
I want white thin line in the center of each li tag.
Any help would be great.
Thank You.
I think you want to like this
ul {
background: black;
}
ul li {
display: inline-block;
padding: 20px 10px;
cursor: pointer;
position: relative;
}
ul li a {
text-decoration: none;
color: #FFFFFF;
}
ul li:hover {
background: #5249FF;
}
ul li::after {
background: #fff;
bottom: 10px;
content: "";
height: 3px;
left: 0;
margin: auto;
position: absolute;
right:0;
width: 10px;
}
<ul>
<li>
HOME
</li>
<li>
WORLD
</li>
<li>
SPORTS
</li>
</ul>
to position: absolute; something relative to it's parent - you need a positioned parent.
Let's use position:relative; for the a elements:
body{background:#444;}
ul {
background: black;
padding: 0;
}
ul li {
display: inline-block;
cursor: pointer;
/*padding: 20px 10px;*/
}
ul li a {
position:relative; /* add */
display: block; /* add */
padding: 20px 10px; /* add */
text-decoration: none;
color: #FFFFFF;
}
ul li a:hover { /* anchor hover! */
background: #5249FF;
}
ul li a:after { /* anchor after! */
content: '';
width: 10px;
height: 3px;
background: #fff;
position: absolute;
/*top: 20%; 20% of what*/
top: 40px;
/*left: 15px; 15px is not centering*/
left: 0;
right: 0;
margin: 0 auto /* center */
}
<ul>
<li>
HOME
</li>
<li>
WORLD
</li>
<li>
SPORTS
</li>
</ul>
Don't use paddings on LI elements - think about accessibility, rather set your paddings to the Anchor elements, that way their clickable size is expanded.
position:absolute elements are "absolute" relatively to the first position:relative parent, at your example, there is no relative parent, therefore they become relative to body.
What you need to do is to add position:relative to your li element.
You need to use left: 50%; transform:translateX(-50%); inside ul li:after and position:relative; inside ul li
ul {
background: black;
}
ul li {
display: inline-block;
padding: 20px 10px;
cursor: pointer;
position:relative;
}
ul li a {
text-decoration: none;
color: #FFFFFF;
}
ul li:hover {
background: #5249FF;
}
ul li:after {
content: '';
width: 10px;
height: 3px;
background: #fff;
position: absolute;
top: 20%;
left: 50%;
transform:translateX(-50%);
}
<ul>
<li>
HOME
</li>
<li>
WORLD
</li>
<li>
SPORTS
</li>
</ul>
Related
I have a dropdown menu in my website which I want to be exactly in line with the "Resources" tab (which opens up the dropdown menu when I hover over it). I have tried adding margins to the ul inside the "Resources" li, but it isn't changing the styling the way I want it to. Am I entering the CSS in the wrong selector or even using the wrong properties?
Here is my HTML and CSS:
* {
margin: 0;
padding: 0;
}
body {
background-color: orange;
}
nav {
width: 100%;
height: 60px;
background-color: black;
display: flex;
}
nav p {
text-align: center;
font-family: arial;
color: white;
font-size: 24px;
line-height: 55px;
float: left;
padding: 0px 20px;
flex: 1 0 auto;
}
nav ul {
position: absolute;
right: 0;
}
nav ul li {
float: left;
list-style: none;
position: relative; /* we can add absolute position in subcategories */
padding-right: 1em;
}
nav ul li a {
display: block;
font-family: arial;
color: white;
font-size: 14px;
padding: 22px 14px;
text-decoration: none;
}
nav ul li ul {
display: none;
position: absolute;
background-color: black;
padding: 5px; /* Spacing so that hover color does not take up entire chunk */
border-radius: 0px 0px 4px 4px;
}
nav ul li:hover ul {
/* This means when li is hovered, we want the unordered list inside list item to do something. */
display: block;
}
nav ul li ul li{
width: 130px; /* increases width so that all text can be fit */
border-radius: 4px;
}
nav ul li ul li a:hover {
background-color: #ADD8E6;
}
<nav>
<p> The Novel Column </p>
<ul>
<li> Resources
<ul>
<li> Book Reviews </li>
<li> Quotes and Principles </li>
<li> Community Aid </li>
</ul>
</li>
<li> About Us </li>
</ul>
</nav>
Add
nav ul li ul {
left: 50%;
right: auto;
transform: translateX(-50%);
}
left: 50% positions the left edge of the dropdown in the center of its parent. Then translateX(-50%) moves it to the left by half of the dropdown's width. Lastly right: auto ensures that the dropdown's width doesn't get messed up.
Demo:
https://jsfiddle.net/7Lzb8u6t/
Add translateX() CSS property to move your box, you can play with transform value and match your exact location,
for more information 1 rem = 16px;
* {
margin: 0;
padding: 0;
}
body {
background-color: orange;
}
nav {
width: 100%;
height: 60px;
background-color: black;
display: flex;
}
nav p {
text-align: center;
font-family: arial;
color: white;
font-size: 24px;
line-height: 55px;
float: left;
padding: 0px 20px;
flex: 1 0 auto;
}
nav ul {
position: absolute;
right: 0;
}
nav ul li {
float: left;
list-style: none;
position: relative; /* we can add absolute position in subcategories */
padding-right: 1em;
}
nav ul li a {
display: block;
font-family: arial;
color: white;
font-size: 14px;
padding: 22px 14px;
text-decoration: none;
}
nav ul li ul {
display: none;
position: absolute;
background-color: black;
padding: 5px; /* Spacing so that hover color does not take up entire chunk */
border-radius: 0px 0px 4px 4px;
transform: translateX(3rem);
}
nav ul li:hover ul {
/* This means when li is hovered, we want the unordered list inside list item to do something. */
display: block;
}
nav ul li ul li{
width: 130px; /* increases width so that all text can be fit */
border-radius: 4px;
}
nav ul li ul li a:hover {
background-color: #ADD8E6;
}
<nav>
<p> The Novel Column </p>
<ul>
<li> Resources
<ul>
<li> Book Reviews </li>
<li> Quotes and Principles </li>
<li> Community Aid </li>
</ul>
</li>
<li> About Us </li>
</ul>
</nav>
i wanna add that "arrow-down" image on hovering anchor tag element,like shown in the below image. how to achieve that?
here is my code:
HTML:
<div class="menu">
<ul>
<li>HOME</li>
<li>PROFILE</li>
<li>ACHIEVEMENTS</li>
<li>AWARDS</li>
<li>PUBLICATIONS</li>
<li>MEDIA</li>
</ul>
</div>
CSS:
.menu{float:left;margin-left:1%;}
.menu ul li{list-style:none;float:left;margin-right:18px;}
.menu ul li:last-child{margin-right:0;}
.menu ul li a{text-decoration:none;color:#fff; font-family: 'OpenSansSemibold';font-size:14px;border-bottom:3px solid transparent;padding-bottom:5px;}
.menu ul li a:hover{background:url("images/arrow-down.png")no-repeat 50% 100%;border-bottom:3px solid #fff;}
Expanding on Balvant Ahir's response
After you've digged up art of css trianlges
You can use pseudo elements to create a triangle, css below
.menu ul li a:after {
content: "";
width: 0;
height: 0;
border: 6px solid transparent;
border-top-color: white;
position: absolute;
bottom: -15px;
left: 50%;
margin-left: -3px;
}
The arrow is absolutely positioned, so need to set position relative on the link itself
.menu ul li a {
position: relative;
}
That and playing around with display:none and display:block on hover
.menu ul li a:after {
display: none;
}
.menu ul li a:hover:after {
display: block;
}
Should be enough
Demo: https://jsfiddle.net/Varinder/6sa3a4k4/
Something like this ? Don't go for image when you have css to do the job. Check the additional styles.
FIDDLE
.menu ul {
float: left;
margin-left: 1%;
}
.menu ul li {
list-style: none;
float: left;
margin-right: 18px;
}
.menu ul li:last-child {
margin-right: 0;
}
.menu ul li a {
text-decoration: none;
color: #fff;
font-family: 'OpenSansSemibold';
font-size: 14px;
border-bottom: 3px solid transparent;
padding-bottom: 5px;
}
.menu ul li a:hover {
background: url("images/arrow-down.png")no-repeat 50% 100%;
border-bottom: 3px solid #fff;
}
/* additional styles */
.menu {
padding: 5px 20px;
background: #3272B8;
}
.menu:after {
display: table;
clear: both;
content: '';
}
.menu li {
position: relative;
}
.menu li a:after {
border-left: 8px solid transparent;
border-right: 8px solid transparent;
border-top: 8px solid #fff;
width: 0;
height: 0;
content: '';
position: absolute;
bottom: -12px;
left: 50%;
transform: translateX(-50%);
visibility: hidden;
}
.menu li a:hover:after {
visibility: visible;
}
<div class="menu">
<ul>
<li>HOME
</li>
<li>PROFILE
</li>
<li>ACHIEVEMENTS
</li>
<li>AWARDS
</li>
<li>PUBLICATIONS
</li>
<li>MEDIA
</li>
</ul>
</div>
As per title, I have got the code almost working I think, but due to having limited knowledge on CSS, I am making stupid mistakes/assumptions. Any help with explanation would be much appreciated.
The fiddle link is here
ul {
padding: 0;
list-style: none;
background: #ffffff;
}
ul li {
color: #0000ff;
display: inline-block;
position: relative;
line-height: 21px;
text-align: left;
}
ul li a {
display: inline-block;
padding: 8px 25px;
background-color: #FFFFFF;
text-decoration: none;
width: 140px;
}
ul li a:hover {
background-color: #FFFFFF;
}
ul li ul.dropdown {
min-width: 125px;
/* Set width of the dropdown */
background: #f2f2f2;
display: none;
position: absolute;
z-index: 999;
left: 500;
}
ul li:hover ul.dropdown {
border: 2px solid #0000ff;
display: block;
padding-left: 50px;
background-color: #ffffff;
}
ul li:hover a:hover ul.dropdown {}
<div id="mylinks">
<ul id="mylists" href="#">
<li>MY LIST MENU
<ul class="dropdown">
<li><span id="level1" onclick="location.href='http://www.google.co.uk/'" title="Go Google"> First item<span>
</li>
<li> Second item
</li>
</ul>
</li>
</ul>
</div>
You need to set a correct position for the dropdown list:
ul li ul.dropdown {
min-width: 125px;
background: #f2f2f2;
display: none;
position: absolute;
z-index: 999;
left: 150px;
top: 0;
}
Notice that left was set to 150px and top was set to 0.
This question already has answers here:
How do CSS triangles work?
(23 answers)
Closed 7 years ago.
I am fiddling with the this menu for touch screens:
Somehow #nav span:after is causing a down arrow to appear on the spans - how? Where does it come from? How can I change this icon/character?
It does not seem to be from the content property and there is no image file anywhere. When I replicate this in my own system I also get the arrow, in IE and FF. It appears if we change the nav tag to a div and if we remove aria-haspopup. If I F12 in IE or FF I can't find it to see where it is being introduced, unless it is some weird quirk using the borders or something.
HTML:
<nav id="nav" role="navigation">
<span href="#nav" title="Show navigation">Show navigation</span>
<span href="#" title="Hide navigation">Hide navigation</span>
<ul class="clearfix">
<li>Home
</li>
<li> <span>Blog</span>
<ul>
<li>Design
</li>
<li>HTML
</li>
<li>CSS
</li>
<li>JavaScript
</li>
</ul>
</li>
<li> <span>Work</span>
<ul>
<li>Web Design
</li>
<li>Typography
</li>
<li>Front-End
</li>
</ul>
</li>
<li>About
</li>
</ul>
</nav>
CSS:
#nav {
width: 60em;
/* 1000 */
font-family:'Open Sans', sans-serif;
font-weight: 400;
position: absolute;
top: 25%;
left: 50%;
margin-left: -30em;
/* 30 480 */
}
#nav > span {
display: none;
}
#nav li {
position: relative;
}
#nav li a {
color: #fff;
display: block;
}
#nav li a:active {
background-color: #c00 !important;
}
#nav span:after {
width: 0;
height: 0;
border: 0.313em solid transparent;
/* 5 */
border-bottom: none;
border-top-color: #efa585;
content:'';
vertical-align: middle;
display: inline-block;
position: relative;
right: -0.313em;
/* 5 */
}
/* first level */
#nav > ul {
height: 3.75em;
/* 60 */
background-color: #e15a1f;
}
#nav > ul > li {
width: 25%;
height: 100%;
float: left;
}
#nav > ul > li > a {
height: 100%;
font-size: 1.5em;
/* 24 */
line-height: 2.5em;
/* 60 (24) */
text-align: center;
}
#nav > ul > li:not(:last-child) > a {
border-right: 1px solid #cc470d;
}
#nav > ul > li:hover > a, #nav > ul:not(:hover) > li.active > a {
background-color: #cc470d;
}
/* second level */
#nav li ul {
background-color: #cc470d;
display: none;
position: absolute;
top: 100%;
}
#nav li:hover ul {
display: block;
left: 0;
right: 0;
}
#nav li:not(:first-child):hover ul {
left: -1px;
}
#nav li ul a {
font-size: 1.25em;
/* 20 */
border-top: 1px solid #e15a1f;
padding: 0.75em;
/* 15 (20) */
}
#nav li ul li a:hover, #nav li ul:not(:hover) li.active a {
background-color: #e15a1f;
}
#media only screen and (max-width: 62.5em)
/* 1000 */
{
#nav {
width: 100%;
position: static;
margin: 0;
}
}
#media only screen and (max-width: 40em)
/* 640 */
{
html {
font-size: 75%;
/* 12 */
}
#nav {
position: relative;
top: auto;
left: auto;
}
#nav > span {
width: 3.125em;
/* 50 */
height: 3.125em;
/* 50 */
text-align: left;
text-indent: -9999px;
background-color: #e15a1f;
position: relative;
}
#nav > span:before, #nav > span:after {
position: absolute;
border: 2px solid #fff;
top: 35%;
left: 25%;
right: 25%;
content:'';
}
#nav > span:after {
top: 60%;
}
#nav:not(:target) > span:first-of-type, #nav:target > span:last-of-type {
display: block;
}
/* first level */
#nav > ul {
height: auto;
display: none;
position: absolute;
left: 0;
right: 0;
}
#nav:target > ul {
display: block;
}
#nav > ul > li {
width: 100%;
float: none;
}
#nav > ul > li > span {
height: auto;
text-align: left;
padding: 0 0.833em;
/* 20 (24) */
}
#nav > ul > li:not(:last-child) > span {
border-right: none;
border-bottom: 1px solid #cc470d;
}
/* second level */
#nav li ul {
position: static;
padding: 1.25em;
/* 20 */
padding-top: 0;
}
}
It looks like they are using CSS3 to draw an arrow in span:after.
They use the border property to get it to work.
#nav span:after {
width: 0;
height: 0;
border: 0.313em solid transparent;
border-bottom: none;
border-top-color: #efa585;
content: '';
vertical-align: middle;
display: inline-block;
position: relative;
right: -0.313em;
}
<div id="nav"><span>Test Arrow</span></div>
The CSS you are using for #nav span:after is the triangle.
Check out this site https://css-tricks.com/examples/ShapesOfCSS/ and scroll down to "Triangle Down".
The dropdown arrow appears to be attached to the :after pseudo-element of the span child element within the a link of the li.
This is where the span is in the markup:
<ul class="clearfix">
<li class="active">
<span>Blog</span>
This is the css rule:
#nav span::after
The content and border-color properties for this rule appear to be what is rendering and stylistically modifying the dropdown arrows.
hey i was learning how to use the drop down menu with css, but i faced two problems:
The length of my first drop down menu changes, even though i kept playing with their percentages.
I am not able to bring my second drop down menu, i guess i don't know how to call the second drop down menu in css even though i gave it a different class name.
Here is the HTML code just for the drop down menu:
<div class="list">
<ul class="style">
<li class="international">International
<ul class="sub1">
<li class> Top 10</li>
<li> All</li>
</ul>
</li>
<li class="pop">Pop
<ul class="sub1" >
<li> Top 10</li>
<li> All</li>
</ul>
</li>
<li class="electronic">Electronic
<ul class="sub1">
<li> Top 10</li>
<li> All
<ul class="sub2">
<li> English</li>
<li> European</li>
<li> International</li>
</ul>
</li>
</ul>
</li>
</div>
and here is the CSS code:
div ul li {
display:inline-block;
background-color:#B2B28F;
float:right;
text-align: center;
width: 22%;
position: relative;
z-index: 1;
bottom: 19px;
padding-top: 5px;
font-size: 18px;
padding-bottom: 3px;
font-weight: bold;
font-family: harrington;
margin-right: 12px;
border-bottom:5px;
margin-bottom: 10px;
text-align: center;
margin-top: 2px;
}
div ul li a {
display: block;
height: 30px;
text-align: center;
border-bottom:5px;
position: relative;
font-size: 20;
z-index: 1;
margin-top: 2px;
}
.sub1 li {
display: none;
position:relative;
width:100%;
height: 20px;
margin-bottom:-8px;
margin-top:12px ;
float: right;
font-size: 17;
margin-right: 4px;
padding: 2px;
text-align: center;
left:-20px;
}
.sub1 li a {
text-align: left;
margin-right: 15px;
}
.sub2 li {
position: relative;
left: 15px;
top: -30px;
width: 100%;
text-align: left;
margin-top: -3px;
margin-bottom: 4px;
display: none;
float: left;
}
div ul li:hover ul li{
display: block;
position: absolute;
top: 27px;
float: left;
width: 97%;
left: 0px;
height: 23px;
border-top: 5px;
text-align: center;
}
div ul li :hover ul li ul li {
display: block;
position: absolute;
text-align: center;
}
a:link {
text-decoration: none;
}
a:visited {
color:#520029;
}
a:hover {
color: #293D66;
}
also any comments on how i did would be appreciated!
Hope you like this..
Just made some changes on your html and css.
HTML:
<div class="nav">
<ul>
<li>International
<ul>
<li> Top 10</li>
<li> All</li>
</ul>
</li>
<li>Pop
<ul>
<li> Top 10</li>
<li> All</li>
</ul>
</li>
<li>Electronic
<ul>
<li> Top 10</li>
<li> All
<ul>
<li> English</li>
<li> European</li>
<li> International</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS:
.nav {
margin: 50px auto;
text-align: center;
}
.nav ul ul {
display: none;
}
.nav ul li:hover > ul {
display: block;
}
.nav ul {
background: #C0C0C0;
padding: 0 20px;
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;
}
.nav ul li:hover a {
color: #fff;
}
.nav ul li a {
display: block; padding: 10px 20px;
color: #757575; text-decoration: none;
}
.nav ul ul {
background: #5f6975; 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: 15px 40px;
color: #fff;
}
.nav ul ul li a:hover {
background: #4b545f;
}
.nav ul ul ul {
position: absolute; left: 100%; top:0;
}
Also Here is the fiddle, Check this working here
I would recommend not using absolute positioning and floats to elements that don't need it..
regarding your second sub, I got it working in this example..
div > ul > li {
display:inline-block;
background-color:#B2B28F;
float:right;
text-align: center;
width: 22%;
position: relative;
z-index: 1;
bottom: 19px;
padding-top: 5px;
font-size: 18px;
padding-bottom: 3px;
font-weight: bold;
font-family: harrington;
margin-right: 12px;
border-bottom:5px;
margin-bottom: 10px;
text-align: center;
margin-top: 2px;
}
div ul li a {
display: block;
text-align: center;
padding:5px 0;
position: relative;
font-size: 20px;
z-index: 1;
color:#212121;
padding-left:10px; box-sizing:border-box;
}
ul { padding:0;}
ul li { list-style:none;}
.sub1 { display:none; width:100%;}
.sub1 li {
position:relative;
width:100%;
clear:both;
font-size: 17px;
text-align: center;
}
.sub1 li a {
text-align: left;
}
.sub2 { display:none; position: relative; background:#B2B28F;}
.sub2 li {
width: 100%;
text-align: left;
margin-bottom: 4px;
padding-left:20px;
box-sizing:border-box;
}
.sub2 li a { font-size:14px;}
div ul li:hover ul li{
width: 100%;
left: 0px;
border-top: 5px solid;
text-align: center;
}
div ul li:hover > ._sub { display:block;}
a:link {
text-decoration: none;
}
a:visited {
color:#520029;
}
a:hover {
color: #293D66;
}
http://jsfiddle.net/2mSzr/
notice I make more specific rules with the '>' option in css, so the styling rules will not
apply to the wrong elements..
Also in the HTML I've added _sub class to all sub menus, and changed the behavior so the display:none/block will be on the actual ul._sub elements and not on their li's.. it just
makes more sense..
go over the example above, let me know if you have any questions.