CSS Centering Child Regardless of Padding/Margin - css

Basically, I have this website:
http://www.ug.it.usyd.edu.au/~sgre9702/week3/dropDownMenu/semantics.html
I want to centre the drop-down list items on the nav-bar, I know I can centre it with:
left:-11px;
However, I don't want to use a value I have calculated. Instead I would like it to automatically centre, taking the margin/padding values into consideration. I don't know if this is possible after googling around a bit.
My related HTML code is:
<nav>
<ul id="nav">
<li>Tours
<ul>
<li>New South Wales
<li>Australian Capital Territory
<li>Queensland
<li>Western Australia
<li>Northen Territory
<li>Tasmania
<li>South Australia
<li>Victoria
</ul>
<li>Attractions
<li>Food
<li>Resources
<li>About
<li>Contact
<ul>
<li>Online
<li>Phone
<li>Facimile
</ul>
</ul>
</nav>
The related CSS:
/* general nav list */
nav ul li {
background-color: #EEEEEE;
border-color: #000000;
border-style: solid;
border-width: 1px;
display: inline-block;
margin: 0px 5px;
padding: 5px;
position: relative;
text-align: center;
width: 120px;
}
/* nav sub list */
nav ul li ul {
display: none;
}
/* nav sub list shown */
nav ul li:hover ul {
display: block;
width: 142px;
position: absolute;
list-style-type: none;
}
/* nav sub list shown - list item */
nav ul li:hover ul li {
display:block;
background-color: #AACCFF;
border: solid 1px #000000;
position: relative;
/*left:-11px;*/
}

Remove the padding from the ul>li elements and apply to the anchors themselves (they will need display:block). Remove the margins from the sub-li elements
Then give the child ul width: auto. The submenu block will still be offset 1px to the right, though, as it will takes its left edge from where its parent's left border ends. You can get around that by either replacing the borders without outlines (which don't effect the widths of their host elements), putting borders on the child anchor/li elements or finally trying a left:-1px value on the child UL.

Related

How do I vertically align my list items with the bullets?

So I have an unordered list with custom bullet images. They are triangles pointing to the right at the list. I would like the point to be aligned with the vertical center of the first line of text in the list item. How can I achieve this?
This is what I am currently viewing:
<ul>
<li>Photography for events and portraits</li>
<li>Image editing and restoration</li>
<li>Video and audio production</li>
</ul>
main ul {
list-style-image: url(../img/bullet.png);
margin-top: 25px;
}
main ul li {
line-height: 35px;
}
The line-height doesn't seem to do anything.
you can use pseudo-element before \ after instead, take a look at this example below:
main ul {
margin-top: 25px;
}
main ul li {
list-style: none;
}
main ul li:before {
content: url("http://www.milksmarter.co.nz/images/basement_platform/grey_triangle_bullet_point_large.png");
position: relative;
top: 10px;
left: -10px
}
<main>
<ul>
<li>Photography for events and portraits</li>
<li>Image editing and restoration</li>
<li>Video and audio production</li>
</ul>
</main>
It's really hard to actually provide you with finalized code without access to your image, but try merging the following code with your own. The first Padding value (currently 3px) should be the item you need to update.
li {
background: url(images/bullet.gif) no-repeat left top;
padding: 3px 0px 3px 10px;
/* reset styles (optional): */
list-style: none;
margin: 0;
}
src: Adjust list style image position?

Working with centering floated li elements with no width in CSS

I'm trying to work with an issue I've had before with CSS with Wordpress and would like to get an extra few sets of eyes on it to see if in fact what I'm doing is the best way for it or is there is a better way.
I'm setting a social media section within my main in the header and footer of my website. My "social media bar" is however not a set size as it is a wordpress website and there may a few more socialmedia buttons added to it. This is the basis of my code:
This is how it appears in my header and footer.
<li id="header-widget-area">
<ul class="icons-medium">
<li class="site-icon"><a target="_blank" href="#">Icon 1</a></li>
<li class="site-icon"><a target="_blank" href="#">Icon 2</a></li>
<li class="site-icon"><a target="_blank" href="#">Icon 3</a></li>
<li class="site-icon"><a target="_blank" href="#">Icon 4</a></li>
</ul>
</li>
#header-widget-area ul {
float: right;
padding: 0;
width: 300px;
border: 1px solid red;
}
#header-widget-area ul.icons-medium li, #footer-widget-area-right ul.icons-medium li {
background: none repeat scroll 0 0 transparent;
float: right;
padding: 0;
width: 60px;
list-style-type: none;
}
My jsfiddle - http://jsfiddle.net/nejsgyp3/
I want to have it floated to the right to align with some boxes and content I have there for both my header and footer (easy peasy!) but then for media queries I'd like to have the element centered and social media icons inside to be centered as well. So I've done this but I still have to keep a width on this or it won't center.
Added slight modifications to CSS for this
#header-widget-area ul {
float: none;
padding: 0;
width: 300px;
border: 1px solid red;
overflow:auto;
margin: 0 auto;
}
#header-widget-area ul.icons-medium li, #footer-widget-area-right ul.icons-medium li {
background: none repeat scroll 0 0 transparent;
float: left;
padding: 0;
width: 60px;
list-style-type: none;
}
My jsfiddle - http://jsfiddle.net/wswvokpu/
So my goal is to allow for more to be added but not have that extra space to the right in my media queries so this is properly centers. Also, is there a way for my "Icon 1" to remain positioned to the right in the media query? Or is the only way to do what I'm trying to do is to always have a set width and then when a new icon is added it would follow suit underneath as long as I keep the height auto? Which would then mean if a new icon was added to the header the height of the would expand thus pushing the box that is below it down?
Thanks in advance!
I think I understand what you are asking. Does this look right to you?
http://jsfiddle.net/nejsgyp3/1/
I have given the list elements a width of 25% each, floated left with centered text. (if you plan on using borders you will also need to set box-sizing:border-box as well - with the various vendor prefixes).
I have also centered the UL which I believe you are trying to do in your media queries.
CSS:
#media only screen and (max-width:600px) {
ul {
list-style:none;
padding:0px;
margin:0px;
}
#header-widget-area ul {
float: none;
padding: 0;
width: 300px;
border: 1px solid red;
margin:0 auto;
list-style:none;
}
#header-widget-area ul:after {
content: " ";
display:block;
height:0px;
clear:both;
float:none;
}
#header-widget-area ul.icons-medium li, #footer-widget-area-right ul.icons-medium li {
background: none repeat scroll 0 0 transparent;
float: left;
padding: 0;
width: 25%;
list-style: none;
text-align:center
}
}

Pure css tree with borders

I am trying to create a tree with indentations in pure CSS. I have been trying using something like:
ul.tree ul {
padding-left: 5px;
}
However I would like to have a separation between each item in the list. If I use the code above the separating bar gets indented as well so it's not too good.
Here is my current code (I do the indent directly in js, which I don't like): jsfiddle
Ultimately, I want to create something that basically looks like that:
Any idea how to do this in pure CSS? kudos for the simplest answers.
Simple with Multi-level Depth Support
UPDATED: Tweaked to accommodate hover
No extra HTML needed, no having to limit depth because of css selector chaining, as it supports any number of levels deep without having to adjust your css at all for those levels (no keeping track of "padding" to set on the next level deep).
This works well with only a two minor limitations (which I don't believe will factor into affecting you).
See fiddle demo.
Add a position: relative to your ul.tree, but keep all the child elements the default static position. Then change/add the following css:
ul.tree a {
display: block;
height:30px;
line-height: 30px;
padding-left: 15px;
}
/* this is making our bottom border, but sizing off the .tree ul width */
ul.tree a:before {
content: '';
height: 30px; /* match your <a> height */
position: absolute;
left: 0;
right: 0;
z-index: -1;
border-bottom-width: 1px;
border-bottom-color: lightgray;
border-bottom-style: solid;
}
ul.tree a + ul {
padding-left: 15px; /* this is your spacing for each level */
}
ul.tree a:hover:before {
background-color: #DDDDDD;
}
The limitations are that no child elements can have a position set and we are using a pseudo-element (which means it cannot be used for some other feature, but that is probably not an issue either).
For lists with unknown depths, I've used an absolutely positioned element for separating lines. It adds a little extra markup, but seems to work.
div.separator {
position:absolute;
left:0px;
right:0px;
border-top:1px solid lightgray;
}
<ul class="tree">
<li><a>Item1</a><div class="separator"></div></li>
<li><a>Item2</a><div class="separator"></div>
<ul>
<li><a>Item3</a><div class="separator"></div></li>
<li><a>Item4</a><div class="separator"></div></li>
<li><a>Item5</a><div class="separator"></div>
<ul>
<li><a>Item6</a><div class="separator"></div></li>
</ul>
</li>
</ul>
</li>
</ul>
http://jsfiddle.net/7u87c/20/
This CSS makes the link inside a nested li have a padding-left of 30px, and I add another nested li link have padding-left: 60px.
ul.tree li ul li a {
padding-left: 30px;
}
ul.tree li ul li ul li a {
padding-left: 60px;
}
http://jsfiddle.net/7u87c/5/
No extra markup and use of icon image.
Pretty simple and dynamic based on the content.
Sample HTML:
<ul class="tree">
<li><span>public</span></li>
<li><span>server.js</span></li>
<li>
<span>server</span>
<ul>
<li><span>webfs</span></li>
</ul>
</li>
<li><span>specs</span></li>
<li>
<span>src</span>
<ul>
<li>
<span>core</span>
<ul>
<li><span>CellAddress.js</span></li>
</ul>
</li>
</ul>
</li>
</ul>
CSS:
ul.tree {
border-top: 1px solid grey;
}
ul.tree, ul.tree ul {
padding: 0;
margin: 0;
list-style: none;
}
ul span {
display: block;
padding-left: 25px;
border-bottom: 1px solid #666;
height: 25px;
line-height: 25px;
background: url("http://lorempixel.com/10/8/") no-repeat scroll 5px 8px transparent;
}
ul ul span {
padding-left: 35px;
background-position: 15px 8px;
}
ul ul ul span {
padding-left: 45px;
background-position: 25px 8px;
}
Please see example
Note: You can convert the spans into a tags

Problems with rounded horizontal CSS navigation menu

I'm designing a navigation menu for a website.
The menu must have rounded corners, I've done this using 'border-radius'.
I've set the width as 800px as that's the rough width the menu needs to be, if I remove the width or put width: auto the width goes to 100%.
There is a gap before the first button and after the last button in my navigation menu and what I need to get rid of this gap without losing the curved edges.
How can I make the first and last buttons maintain rounded outside corners and remove the gap between each side of the navigation.
CSS:
/* CSS MENU */
#menu {
/* DISPLAY SETTINGS */
text-align: center;
height: 40px;
width: 800px;
margin: 0;
padding: 0;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
/* APPEARANCE SETTINGS */
border-top: 2px solid #356AA0;
border-left: 2px solid #356AA0;
border-bottom: 2px solid #204061;
border-right: 2px solid #204061;
background: #628ddb;
/* FONT SETTINGS */
color: #15387a;
font-family: Arial, sans-serif;
font-weight: bold;
text-transform: uppercase;
font-size: 12px;
}
/* LIST SETTINGS */
#menu li {
display: inline-block;
}
/* HYPERLINK SETTINGS */
#menu li a {
text-decoration: none;
display: block;
padding: 0 15px;
line-height: 40px;
}
/* HOVER AND ACTIVE BUTTON SETTINGS */
#menu li a:hover, #menu li.active a {
color: #15387a; background: #3D7BBB; border-bottom: 2px solid #204061
}
HTML
<ul id="menu">
<li class="active end">Home</li>
<li>Our Services</li>
<li>Testimonials</li>
<li>Get A Quote</li>
<li>Drive For Us</li>
<li>Terms & Conditions</li>
<li class="end">Contact Us</li>
</ul>
So there are several things that need to happen in order to maintain your design:
1.) The UL tag needs to have display: table
2.) Like what #Netsurfer You'll need to set the LI to have display: table-cell so that the list items flush to the edges
3.) Now that UL has rounded corners, any child elements with squared corners will stick out. You can either:
a.) Resolve this by applying overflow: hidden to both the UL and LI or
b.) Apply the rounded corners to the LI and A tags.
4.) Your :hover & active state applies a bottom border -- the table-cell will cause this to look strange. It might be better to remove it altogether.
You can check out the code here: http://jsfiddle.net/vuAVV/
Remove text-align: center; from #menu.
You might also want to include padding-left: 10px; to make sure when the first link is highlighted it does not overlap with the rounded corner of the menu.
See this working jsFiddle.
Change the display setting for the LIs to display: table-cell.
By doing so you are also not "trapped" by the white-space issues when using display: inline-block.
See jsFiddle
PS: Forgot the rounded corners ..., now also included. ;-)

CSS How to hide current child menu items when hovering over parent's sibling items without javascript

I'm trying to create a two-level horizontal navigation menu (or menubar) that displays the child submenu items when you hover over the parent menu item. If one of the child items is selected, the parent item has a visual indicator that the current page corresponds to one of its child items, and its child items remain displayed.
Child2 is current page:
parent1 *parent2* parent3
|
child1 *child2* child3
When I hover over parent1 or parent3, their children are displayed as desired. My challenge is that I can't figure out how to hide parent2's children when the other parents' children are being displayed. Is there a CSS way to accomplish this? I know I can use jquery to hide parent2's children when mousing out of parent2 and parent2's children, but I'd rather not have to use javascript for maximum usability.
Here is a live example
CSS:
ul.AspNet-Menu
{
position: relative;
}
ul.AspNet-Menu,
ul.AspNet-Menu ul
{
margin: 0;
padding: 0;
display: block;
}
ul.AspNet-Menu li
{
position: static;
list-style: none;
float: left;
}
ul.AspNet-Menu li a,
ul.AspNet-Menu li span
{
display: block;
text-decoration: none;
}
ul.AspNet-Menu ul
{
visibility: hidden;
position: absolute;
}
ul.AspNet-Menu li:hover ul ul,
ul.AspNet-Menu li.AspNet-Menu-Hover ul ul
{
visibility: hidden;
}
ul.AspNet-Menu li:hover ul,
ul.AspNet-Menu li li:hover ul,
ul.AspNet-Menu li li li:hover ul,
ul.AspNet-Menu li.AspNet-Menu-Hover ul,
ul.AspNet-Menu li li.AspNet-Menu-Hover ul,
ul.AspNet-Menu li li li.AspNet-Menu-Hover ul
{
visibility: visible;
}
.main-nav2 .AspNet-Menu-Horizontal{
margin: 0;
padding: 0;
font: bold 13px/16px Arial, sans-serif;
position: absolute;
top: 21px;
left: 290px;
}
.main-nav2 ul.AspNet-Menu li {
display: inline;
}
.main-nav2 ul.AspNet-Menu li a,
.main-nav2 ul.AspNet-Menu li span.AspNet-Menu-NonLink {
color: #fff;
background: url(../../nav-bg.gif) no-repeat 0 -24px;
height: 24px;
text-decoration: none;
margin: 0 1px 0 0;
}
.main-nav2 ul.AspNet-Menu li a span,
.main-nav2 ul.AspNet-Menu li span.AspNet-Menu-NonLink span {
background: url(../../nav-bg-right.gif) no-repeat 100% -24px;
padding: 4px 12px 4px 12px;
cursor: pointer;
}
.main-nav2 ul.AspNet-Menu li a:hover,
.main-nav2 ul.AspNet-Menu li a.active {
background-position: 0 0;
color: #1b8db3;
}
.main-nav2 ul.AspNet-Menu li a:hover span,
.main-nav2 ul.AspNet-Menu li a.active span {
background-position: 100% 0;
}
.main-nav2 ul.AspNet-Menu li.AspNet-Menu-Selected a,
.main-nav2 ul.AspNet-Menu li.AspNet-Menu-ChildSelected a
{
background-position: 0 0;
color: #1b8db3;
}
.main-nav2 ul.AspNet-Menu li.AspNet-Menu-Selected a span,
.main-nav2 ul.AspNet-Menu li.AspNet-Menu-ChildSelected a span
{
background-position: 100% 0;
}
.main-nav2 ul.AspNet-Menu li.AspNet-Menu-ChildSelected ul
{
visibility: visible;
}
.main-nav2 ul.AspNet-Menu ul{
width:500px;
}
.main-nav2 ul.AspNet-Menu ul li {
font: 12px/20px Arial, sans-serif;
padding: 0 5px 0 0;
display: inline;
}
.main-nav2 ul.AspNet-Menu ul li a {
text-decoration: none;
color: #1b8db3;
padding: 0 0 0 12px;
background-image:none;
}
.main-nav2 ul.AspNet-Menu ul li a:hover {
text-decoration: underline;
}
HTML:
<div class="main-nav2" id="ctl00_MainMenu">
<div class="AspNet-Menu-Horizontal">
<ul class="AspNet-Menu">
<li class="AspNet-Menu-Item">
<a href="javascript:return false;#1">
<span> A Menu Option</span></a>
<ul>
<li class="AspNet-Menu-Item">
<a href="/CSSMenu/A1.aspx">
A1 SubMenu Option</a>
</li>
<li class="AspNet-Menu-Item">
<a href="/CSSMenu/A2.aspx">
A2 SubMenu Option</a>
</li>
</ul>
</li>
<li class="AspNet-Menu-Item">
<a href="javascript:return false;">
<span> B Menu Option</span></a>
<ul>
<li class="AspNet-Menu-Item">
<a href="/CSSMenu/B1.aspx">
B1 SubMenu Option</a>
</li>
<li class="AspNet-Menu-Item">
<a href="/CSSMenu/B2.aspx">
B2 SubMenu Option</a>
</li>
</ul>
</li>
<li class=" AspNet-Menu-Selected">
<a href="/CSSMenu/C.aspx">
<span> C Menu Option</span></a>
</li>
</ul>
</div>
</div>
Many thanks in advance for any tips or help!
Terry
The simple solution is attempt to degrade as best as possible. In this case, I'd set a background color on the child menu and jack up z-index on hover only, so the bg will cover other child menus--[edit] they'll still be visible, but the text won't overlap. Then use javascript to make it as you really want.
The more complicated solution means you have to make all child menus take up identical space--one way is to use negative margin and then padding to cover up that space--and let whatever child menu that's displayed cover up the open one, again by greater z-index (applied to the parent on hover).
edit Another thing I use all the time to handle this kind of situation is to do something like the following
ul:hover ul { display:none; } //or in your case, set to invisible
ul li:hover ul { display:block; } //in your case, set to visible
This means the submenu will disappear when the UL is hovered over and, because the li:hover is lower in the cascade and more specific (I usually have to deal with lots of here-state class names--don't think you will), should allow for the submenu to reappear. It's not quite as fine-grained as you want, but nearly.
If what you want is that when the user clicks a child2 selection the response produces a page with child2 displayed, but child2 should disappear when the user hovers over parent1 or parent3, then you'll need to use JavaScript. The reason being that it's an event that affects more than one node in the DOM in different ways. CSS only affects 1+ nodes in the DOM in the same way, and usually only at page load. The exception are pseudo-classes like :hover which can affect display after page load.
If you need a CSS multi-menu solution, or just want to look at a good one that might help you find what your answer, check out this GRC CSS. I learned a lot from it, and hacked it into a solution I've used numerous times.
There is no pseudo class in css to trigger mouse out equivalent event. You have to use javascript to accomplish what you are trying to do. There are lot of menus/plugins available which does exactly what you are doing (What I mean by the statement is no need to reinvent the wheel).
I'm positive you won't be able accomplish this level of fine-grained control solely through CSS. You'll need to change states on your DOM elements with JS.
Its possible to do without javascript. Check the answer in this page.
Horizontal CSS subnav issues!
Giving higher z-index to siblings child items and less z-index to active menu's child items.

Resources