Cant Center border bottom below dynamically - html

I have a simple navbar and i have few menu items via ul and what I'm trying to do is that whenever i hover over the li a border-bottom appears in center of the text but im having a few problems first is that the border-bottom's width is leaving the text and sometimes it doesn't it cover the full width of the text above it
body{
margin: 0px;
padding: 0px;
background: #2c3e50;
}
.header__middle ul{
list-style-type: none;
display: flex;
}
.header__middle ul li a{
margin-right: 10px;
text-decoration: none;
font-size: 1.2em;
color: black;
text-align: center;
}
.header__middle ul li {
position: relative;
}
.header__middle ul li:hover ::after{
content: "";
position: absolute;
left: 4%;
bottom: -13%;
width: 40px;
border-bottom: 2px solid black;
}
<div class="header__middle">
<ul>
<li >Home</li>
<li>Join</li>
<li>FAQs</li>
<li>About Us</li>
</ul>
</div>
HTML

Here, try this. The only issue I see is that your 40px border is too wide for some items like "Join". (Notes are in the code)
body {
margin: 0px;
padding: 0px;
background: #2c3e50;
}
.header__middle ul{
list-style-type: none;
display: flex;
}
/* Remove your margin here and re-add it as a padding on the parent li */
.header__middle ul li a{
text-decoration: none;
font-size: 1.2em;
color: black;
text-align: center;
}
.header__middle ul li {
position: relative;
padding-right: 10px;
}
/* Change to display block and position relative */
.header__middle ul li:hover ::after{
content: "";
display: block;
position: relative;
left: calc(50% - 20px); /* To center, set your left for 50% minus half of your bar's width */
width: 40px;
border-bottom: 2px solid black;
}
<div class="header__middle">
<ul>
<li >Home</li>
<li>Join</li>
<li>FAQs</li>
<li>About Us</li>
</ul>
</div>
Edit: Sorry, I may have misunderstood. I thought you were wanting your 40px bar to be centered on each object.
If you want your bar to be the same width as each item, just remove the left and width properties from my code above, as shown here:
.header__middle ul li:hover ::after{
content: "";
display: block;
position: relative;
border-bottom: 2px solid black;
}

Related

Changing Position of Dropdown Menu

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>

CSS Styling for boxed border floating to right (and preferrebly next to the cursor) corner

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.

Navigation bar not centering

HTML :
<nav>
<ul>
<li>Home</li>
<li>History</li>
<li>Appointments</li>
<li>Contact us</li>
</ul>
</nav>
CSS :
nav {
position: relative;
margin: auto;
padding: 0;
list-style: none;
width: 100%;
display: center;}
nav ul li {
display: inline;
}
nav ul li a {
display: inline-block;
text-decoration: none;
padding: 5px 0;
width: 100px;
background: #Cc3399;
color: #eee;
float: left;
text-align: center;
border-left: 1px solid #fff;
}
nav ul li a:hover {
background: #a2b3a1;
color: #000
}
Basically, I've managed to make this navigation bar, that fits my specifications. However, it's not centered, it's in position vertically, but horizontally it's way left and no where near the center of the page.
One solution is to replace inline to inline-block and use text-align: center to parent(also display: center is not valid css):
nav {
position: relative;
margin: auto;
padding: 0;
list-style: none;
width: 100%;
text-align: center;/*add text align-center*/
}
nav ul li {
display: inline-block;/*replace inline to inline-block*/
}
nav ul li a {
display: inline-block;
text-decoration: none;
padding: 5px 0;
width: 100px;
background: #Cc3399;
color: #eee;
float: left;
text-align: center;
border-left: 1px solid #fff;
}
nav ul li a:hover {
background: #a2b3a1;
color: #000
}
<nav>
<ul>
<li>Home
</li>
<li>History
</li>
<li>Appointments
</li>
<li>Contact us
</li>
</ul>
</nav>
Since you have given margin as 100% for it will not have impact on margin.. So try giving 50% width to and it should work.
You can change like this...
nav
{
position: relative;
margin: auto;
padding: 0;
list-style: none;
width: 50%;
display: center;
}
to horizontally center an element, this element needs to be display:block; and it should have a width that is less than 100%
Here is the css, slight modification based on what you have
nav {
position: relative;
/*** centers the nav block ****/
margin-left: auto;
margin-right: auto;
width: 80%;
}
nav ul{
padding: 0;
list-style: none;
}
nav ul li {
display: inline;
}
nav ul li a {
display: inline-block;
text-decoration: none;
padding: 5px 0;
width: 20%; /*** make the centering look more vivid ****/
background: #Cc3399;
color: #eee;
float: left;
text-align: center;
border-left: 1px solid #fff;
}
nav ul li a:hover {
background: #a2b3a1;
color: #000
}
You can view it on jsfiddle

How can I get an anchor inside these list items to fill the space of the list item?

I'm using a fairly typical nested UL setup to create a dropdown menu, however I can't get the anchorlinks inside the li to expand to their height.
The HTML
<div id="navbar-container">
<ul id="navbar">
<li>Home</li>
<li>Lessons</li>
<ul>
<li>sub item1sdfsdfsdfsdfsdf</li>
<li>sub item2</li>
<li>sub item3</li>
</ul>
</li>
<li>Custom Fitting</li>
</ul>
</div>
In the CSS I'm using display:block on the anchor tags which does make them expand to the width of the li but not the height. I have tried using padding but it does not work correctly across all browsers. #navbar is using display: table and the children lis are using display: table-cell. This is so the navbar can expand and contract to fit the screen size. I suspect display: table-cell may have something to do with the anchors not expand vertically.
Here is a JSFiddle so you can see what I'm talking about.
The CSS
#navbar-container {
min-width: 768px;
height: 32px;
position: relative;
background-color: #bb4212;
}
#navbar {
list-style-type: none;
display: table;
width: 100%;
font : 14px"Arial", sans-serif;
height: 100%;
}
#navbar li {
text-transform:uppercase;
display: table-cell;
text-align: center;
}
#navbar li a {
color: #f2f2f2;
display: block;
border-left: 1px solid #c17455;
}
#navbar > li:first-child a {
border: 0;
}
#navbar li ul {
list-style-type: none;
background-color: #f2f2f2;
position: absolute;
right: -9999px;
top: 32px;
margin-left: 1px;
-moz-box-shadow: 0px 6px 4px 0px #898989;
-webkit-box-shadow: 0px 6px 4px 0px #898989;
box-shadow: 0px 6px 4px 0px #898989;
}
#navbar li ul li:hover {
background-color: #bb4212;
}
#navbar li ul a:hover {
color: #f2f2f2;
}
#navbar li:hover {
background-color: #f2f2f2;
}
#navbar li:hover a {
color: #000;
}
#navbar li:hover ul {
right: auto;
}
#navbar li ul li {
display: block;
text-align: left;
}
#navbar li ul li a {
border: 0;
white-space:nowrap;
margin: 0 5px;
text-decoration: none;
display: block;
}
My favorite technique for filling up a parent container 100% width and height is to use absolute positioning:
parent {
position: relative; /* unless it's already positioned */
}
child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
Here it is applied to your JSFiddle: http://jsfiddle.net/Kgz5p/

Aligning DIV at bottom of another element causes menus to not be displayed in IE7

I've added markup on a webpage to align a element at bottom of another element. However when I do this the menus on my page aren't displayed in IE7. Here's the markup:
<div id="header">
<div class="panel">
<h1>Heading</h1>
<div class="nav">
<ul>
<li class="hdr"><a class="hdr" href="#">Submenu One</a>
<ul>
<li class="menuitem">Submenu one</li>
<li class="menuitem">Submenu 2</li>
</ul>
</li>
<li class="hdr"><a class="hdr" href="#">Submenu 2</a></li>
<li class="hdr"><a class="hdr" href="#">Submenu 3</a></li>
</ul>
</div>
</div>
</div>
The associated style sheet has the following:
#header
{
position: relative; /* Move to bottom */
height: 100px;
width: 100%;
}
.nav
{
position: absolute; /* Move to bottom */
bottom: 0; /* Move to bottom */
}
#header ul
{
padding-left: 0;
margin-left: 0;
margin: 12px 0px 0px 0px;
list-style: none;
position: relative;
left: -10px;
float: left;
}
#header ul li.hdr
{
display:-moz-inline-stack;
display: inline-block;
zoom: 1;
*display: inline; /* IE Hack */
margin-right: 15px !important;
font-size: 16px;
z-index: 1000;
}
#header ul li a.hdr
{
display: block;
color: white !important;
text-decoration: none;
padding: 9px 11px 11px 11px;
}
#header ul li a.hdr:hover
{
background: #505050;
border: solid 1px #606060;
padding: 8px 10px 10px 10px;
text-shadow: 2px 2px 2px #111;
}
#header ul ul
{
display: none;
border: 1px solid #a0a0a0;
background: #f5f5f5;
position: absolute;
top: 27px;
left: 0px;
zoom: 1;
padding: 10px;
line-height: 20px;
}
#header ul li:hover > ul
{
display: block;
}
#header ul ul li
{
display: block;
}
#header ul ul li a
{
font-size: 12px;
border: none;
color: #000;
background: #f5f5f5;
text-decoration: none;
padding: 8px;
}
The lines with the comment /* Move to bottom */ are responsible for moving the nav div to the bottom of the header. I've tried putting z-index's everywhere, as well as other attributes to ensure IE sees the elements with hasLayout equal to true, but to no avail. I'm pulling my hair out over this, any help much appreciated.
Your IE hack is wrong:
use
*+display: inline; /* IE Hack */
instead of
*display: inline; /* IE Hack */
(star) hack is for IE6 only.
[See here][1]
http://css-tricks.com/how-to-create-an-ie-only-stylesheet/