How do I stop my divs from moving around while zooming? - css

Good day everyone,
I am working on a website for a local astronomy club that I am doing for free to build up both my portfolio and skill set.
I have a navigation bar within my header at the top of the page. When I zoom out to about 90% the elements within the navigation bar move around and cause the layout to look a little funky.
If it helps, I have a live example at
http://www.JamesRobertCook.com/BCAAS/
Here is the HTML for my navigation bar:
<div id="header_bar">
<div id="nav">
<div id="cssmenu">
<ul>
<li class="active"><span>Home</span></li>
<li class="has-sub"><span>Products</span>
<ul>
<li class="has-sub"><span>Product 1</span>
<ul>
<li><span>Sub Item</span></li>
<li class="last"><span>Sub Item</span></li>
</ul>
</li>
<li class="has-sub"><span>Product 2</span>
<ul>
<li><span>Sub Item</span></li>
<li class="last"><span>Sub Item</span></li>
</ul>
</li>
</ul>
</li>
<li><span>About</span></li>
<li class="last"><span>Contact</span></li>
</ul>
</div>
</div>
And here is the CSS associated:
#header_bar {
background: rgba(44, 44, 44, 0.75);
height: 36px;
margin: 0 auto;
}
#nav {
height: 36px;
margin: 0 auto;
width: 960px;
}
#cssmenu {
height: 36px;
background-color: rgb(44, 44, 44)s;
}
#cssmenu ul {
margin: 0;
padding: 0;
}
#cssmenu > ul > li {
float: left;
margin-left: 15px;
position: relative;
#cssmenu > ul > li > a {
color: rgb(160,160,160);
font-family: Verdana, 'Lucida Grande';
font-size: 15px;
line-height: 36px;
padding: 15px 85px;
-webkit-transition: color .15s;
-moz-transition: color .15s;
-o-transition: color .15s;
transition: color .15s;
I thought that sticking it inside of a container and giving it a fixed height/width would do the trick, but I tried that locally and had no success.
Thanks for any help! If possible, could you please explain what I was doing wrong? I'd like to learn from my mistakes!
Thanks!
EDIT
I just noticed that when zooming, the content in my three 'featured' boxes, overlaps the respective div it lives in as well! I certainly broke something.

I am sooooooo sorry. I got side-tracked looking at a content div down further on the page and totally lost focus on your true problem. I hope a simple solution to your original problem will help you forgive me.
#header_bar {
background: rgba(44, 44, 44, 0.75);
height: 36px;
margin: 0 auto;
min-width: 960px;
}
That min-width: 960px; (the 960px is 960 pixels which is the same width as the rest of the primary content of the page) means that the <div> with id="header_bar" can not be any smaller than 960px which makes it fit with the rest of your content even if someone views on a super low resolution, resizes the browser window or zooms in really far.
Again, so sorry for totally screwing that one up, but on a side note, I believe I could give you some pointers on a few things if you are interested. Just for example, you seem to overuse <div>'s and <span>'s in your page. Those two elements are not exclusive to receiving some of the styling which you have applied to them and the child elements in some cases or the parent elements of those elements could receive the style and you could eliminate those elements entirely.
If you want more detailed explanation on the tips and hints and such, I love playing sounding board for ideas and discussing programming/development and I think I could show you a few things I've learned over the years that would help you.

Your div elements need to grow taller or you need to set the overflow on them if you don't want the text to run through the borders, but aside from that I don't see where your div elements are doing anything unusual. They are static sized and they don't change size for anything. They "move" because the user is resizing/zooming and they can't stay static during that. You would have to find a way to keep your users from resizing/zooming to prevent anything changing.

Try using % as the unit versus px to assign width for the <li> (list item)
(i.e) if you are using five <li> in ur menu, assign width:20% to <li>

Related

Learning Div placement

Did a lot of research on all the separate components. However, I don't understand how the components work together. Several placement issues have plagued me on different occasions. I would like to understand why it behaves like it does.
Designing a site with a fixed header, containing some buttons. I want the buttons to be placed on a colored row (NAV). That's why I made a child of NAV. However I can't seem to place the buttons over the bar.
Html
<body>
<nav class="row">
<ul class="menu">
<li id="link1">Link 1</li>
<li id="link2">Link 2</li>
<li id="link3">Link 3</li>
<li id="link4">Link 4</li>
<li id="link5">Link 5</li>
</ul>
</nav>
<div class="row main">
#RenderBody()
</div>
CSS
nav, div, li {
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
box-sizing: content-box;
border: 1px dashed black;
}
.row {
width: 100%;
padding-left: 20px;
padding-right: 20px;
}
nav {
position: fixed;
top: 80px;
height: 40px;
z-index: 100;
background-color: Green;
border-bottom: solid greenyellow 2px;
}
.menu li {
display: block;
background-color: darkgreen;
float: left;
height: 40px;
width: 60px;
}
.menu a {
color: white;
}
Result
It can be fixed by several things, like button margin or placing the buttons relative with a negative Top offset. However, these solutions feel 'dirty', like it's not the right way to do it. Why are the LI's not on top of NAV?
because your broswer applies by default some margin to the ul tag
try adding
ul {
margin: 0;
}
you could avoid these issues by using a css reset (Eric Meyer is the authority here: http://meyerweb.com/eric/tools/css/reset/) or Necolas' Normalize.css: http://necolas.github.io/normalize.css/
the first one zeroes all the values of all elements - you have to rebuild the style of some elements like lists.
The second one normalizes the values of elements to fix browsers inconsistencies
When you use the "float" property on some elements (here the "LI"), the parent (here the "menu") ignore his floating children to calculate his height.
So you have to specify a valid height to your menu, or probably better, use "overflow:auto" on it to remember him his children.
So remove your
nav {
height:40px;
}
and add in your CSS :
.menu {
overflow:auto;
}
As in this fiddle : http://jsfiddle.net/bE3QH/
When using the element ul it sometimes creates whitespace on browsers. By making the margin 0px you are removing the whitespace decreasing the area used by element. hope this helps. The following code can be used...
ul {
margin:0px
}
You can use this instead of your code.
You will get ready made menu control on this website.
You can modify as you want & you will get your menu control available in a moment.
Here's the link.
http://cssmenumaker.com
http://tympanus.net/codrops/2010/07/16/slide-down-box-menu/
http://cssmenumaker.com/builder/1666948
Please check it out.
These are very useful and it will definitely save your time as well.
I hope this will resolve your issue.
Add this to your CSS:
ul{
margin:0;
padding:0;
}
This clears the default properties for ul elements
You would be better off if you didn't specify a width and a height for the list items, but rather displaying the anchor tags as blocks, and giving those a width and height.

CSS, UL menu with pop-up LI elements that appear over upper banner

I'm trying to create a CSS based menu for a site I'm creating, and not having much luck. Below you can see an image that shows what it is that I'm trying to achieve (the entire menu and header are in one block):
The upper half of the screen which is shown in a steel-blue colour in the image, will have contained in it, a div with a background image, and a heading one tag positioned absolutley to place the text over the top of the image.
The part below that, shown in an off-yellow colour in the example image, is a standard un-ordered list element containing normal li list tags.
Something like the following:
<ul>
<li>Home</li>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
</ul>
Each of the list elements in the unordered list parent container will contain an anchor tag along the lines of:
<ul>
<li>Home</li>
<li>Page 1</li>
<li>Page 2</li>
<li>Page 3</li>
</ul>
When a list item containing an anchor, has a href that matches the actual page being viewed, the intention is that it's height get's increased, such so that it grows outside of the un-ordered list parent that contains it. In doing this it overlays the steel-blue area of the parent page, something similar to the bright purple rectangle in the example image.
A list item that is not matched, and not hovered, shown as blue rectangles in the example image, should remain inside the parent unordered list and with its default background styling and colours.
When a list item is hovered, it should change to a style that is the same as one that is matched to the page, height wise, and be outside the parent list, but it should take on a different colour to show that it's hovered and not matched, and it should return to it's previous state (The blue rectangles) after the hover is finished.
I sort of have things partially working, this is how it currently looks:
As you can see, the actual unordered list element will not flow down and each of the list items inside of it overlap each other, I suspect this is because I have each list item set up with absolute positioning.
If I change the list items so they use relative positioning, it solves the overlap issue, however that prevents the list items from popping up over the top of the parent list element and overlaying the steel-blue section of the image.
There is also one other complication. I have very little control of the generated mark-up as this is being produced by a CMS (mojoPortal to be exact). The mark-up that is being generated by ASP.NET is currently as follows:
<nav class="span12" id="ctl00_SiteMenu1_ctl00">
<ul id="ctl00_SiteMenu1_ctl00_UL" class="root-nav">
<li class="current">
Home
</li>
<li>
Contact Us
</li>
<li>
Frogs
</li>
</ul>
</nav>
I know that the classes root-nav and current are defined elsewhere, and I do have the ability to override ANY CSS rules existing with any of my own.
The example above with the overlapping, this is defined as follows:
.mainhead
{
background: url(img/menu-banner.png);
height: 170px;
}
ul.root-nav
{
background: #B3A90E;
border-radius: 0px;
z-index: 1000;
position: relative;
width: 1170px;
}
ul.root-nav li.current > a
{
background-color: #B30D9E;
background-image: none;
height: 100px;
z-index: 1010;
position: absolute;
bottom: -30px;
border-radius: 25px 25px 0px 0px;
width: 100px;
display: block;
}
ul.root-nav li > a
{
background-color: #1C10B3;
background-image: none;
z-index: 1010;
position: absolute;
bottom: -30px;
border-radius: 25px 25px 0px 0px;
width: 100px;
}
ul.root-nav li > a:hover
{
background-color: #0BB312;
background-image: none;
height: 100px;
z-index: 1010;
position: absolute;
bottom: -30px;
border-radius: 25px 25px 0px 0px;
width: 100px;
}
I'd like to get this working with the overlay, instead of my alternitive, which is to make the yellow area the maximum height I expect any of the pop-up items to be and fix it in place so nothing moves when heights change.
I'm doing this in HTML5 so HTML5 markup and CSS rules are perfectly fine.
Thanks in advance for any ideas anyone comes up with.
and in about an hour... I'm back.
and here's something that definitely should be a top question on stack overflow...
WHY? when ever you post a question on stack overflow do you usually end up solving it yourself, in a pretty short time afterwards.
Anyway, enough of that.
The reason I was getting the overlap I was getting above was beacuse I wasn't setting the LI sizes.
Simply adding the following rules:
ul.root-nav li.current
{
min-width: 100px;
}
ul.root-nav li
{
min-width: 100px;
}
to my stylsheet, just before ul.root-nav li-current > a resolved my issue.
I was correct about my thoughts on the height not being set for the UL due to the absolute positioning, but setting a fixed height on that did the trick.

Do CSS Sprite images have to have X amount of space between each item?

I am having a problem, for a while I have been trying to figure out how to resolve this issue. I will describe it very well below...
I am trying to use an image as a sprite image for an UL list. It should show an icon with text next to it, both the icon and the text should link to somewhere.
My first example look how I want it to be. When the font-size is set to 10px it looks ok...
As soon as I change the font-size from 10px to 16px...
Here is the CSS and HTML
CSS
#post-meta-wrapper{
list-style: none;
margin:20px 0 20px 20px;
width:400px;
}
#post-meta-wrapper li {
width:100%;
color: #44495B;
border-top: 1px dotted #CCC;
color: #999;
font-size: 10px;
line-height: 28px;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
text-indent: 0px;
}
#post-meta-wrapper li a{
background: url(http://i.imgur.com/Bcps8.png) no-repeat 0px -183px;
padding-left:15px;
}
#post-meta-wrapper .meta-img {
background:#fff;
width: 15px;
height: 10px;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 8px;
overflow: hidden;
}
#post-meta-wrapper a:hover,
#post-meta-wrapper .active{
background: url(http://i.imgur.com/Bcps8.png) no-repeat 0px -195px;
width: 15px;
height: 10px;
}
The HTML
<ul id="post-meta-wrapper">
<li class="author">
<span class="meta-img">Test link
</li>
<li class="author">
<span class="meta-img">Test link
</li>
<li class="author">
<span class="meta-img">Test link
</li>
<li class="author">
<span class="meta-img">Test link
</li>
<li class="author">
<span class="meta-img">Test link
</li>
<li class="author">
<span class="meta-img">Test link
</li>
</ul>
JSFiddle Examples
This is the first one with the font-size: 10px http://jsfiddle.net/jasondavis/Mt87G/4/
This is the MESSED UP one with the font-size: 16px
http://jsfiddle.net/jasondavis/Mt87G/5/
Help me?
Ok I know I could just change the sprite image to have huge spaces around each object in the image and then you wouldn't notice this problem, but I would really like to resolve this issue the right way. I mean is it possible to do what I am trying to do with just CSS or does the image need to be spaced out? I have seen other sprite images where they are together close like mine and I have seen some where everything is like 100px apart.
Please help me if you know how to resolve this, I have tried eveything I can think of without any luck yet. I need to do this on a mass scale so I would like to get it done correctly now before I do that. Thank you for any help
I would suggest using the :before pseudo element. Add the class to your anchor link or the list item and style the pseduo element to hold the sprite image. This gives the same effect as being able to set a specific size on a span, without the need of extra markup.
<li class="icon">
<a href="#">Test Link<a/>
<li>
.icon:before{
content: '';
width: 10px;
height: 10px;
display: block;
background: url(http://i.imgur.com/Bcps8.png) no-repeat 0px -183px;
/* other css for positioning */
}
If you want the icon to be part of the link, the before should to be on the anchor. It will be underlined if the anchor is underlined.
If you put it on the li, the link can still be underlined without the icon being underlined, but to make it clickable you need to work some magic with padding on the anchors.
Note: As mentioned by jimplode, this will not work in IE <= 7 So if you need it to work there (sad for you) better to use the extra markup.
Close the span
Set width, height, float and margin-top of this span to align with base of line height
Try this:
http://jsfiddle.net/Mt87G/19/
CSS
#first a{
font-size: 10px;
padding-left: 5px;
line-height: 12px;
}
#second a{
font-size: 22px;
line-height: 25px;
padding-left: 5px;
}
.meta-img{
background: url(http://i.imgur.com/Bcps8.png) no-repeat 0px -183px;
width: 13px;
height: 12px;
float: left;
}
#first .meta-img{
margin-top: 4px
}
#second .meta-img{
margin-top: 7px
}
HTML
<p id="first">
<a href="#">
<span class="meta-img"></span>Test link
</a>
</p>
<p id="second">
<a href="#">
<span class="meta-img"></span>Test link
</a>
</p>
I think you can give more spaces between the images.
If you have individual images then you can use http://spritegen.website-performance.org/ to create sprite images and you can provide custom space between sprite images.
If you are changing the font-size dynamically then also you can use the sprite with more space and can only change the background-position.
Well written question! I simplified your two sample fiddles into one fiddle:
http://jsfiddle.net/Mt87G/6/
It seems in the 16px-variant the image is just showing a bit more as the line-height becomes bigger because of the larger font size.
Edited: apparently SO uses sprites as well, see for example this image. If I use firebug to increase the line-height for the "vote-up" button for example, at some point the next sprite starts showing up.
So, if you would follow their lead, it seems you could:
set a fixed height on your element
leave some space between sprites, just to be sure
Strict answer though to your question then seems "no, not necessary".
As you have your background image on an anchor, you could always put a span inside the anchor for the image, this way you can control the width and height of the span which in turn will only show the portion of the image you want it to use.
I generally leave a few pixels in between sprites due to rounding errors that I've seen on mobile Safari. If there is just 1 pixel in between the sprites, sometimes there is a hair-width sliver of the next sprite over at the edge of the element. Adding a little more buffer room avoids this problem. If you are using PNG files, there isn't really much additional file size (extremely small increase) with more spacing due to the way they compress.

Dropdown Menu - Make the <ul> submenu 100% width

I am going a bit crazy trying to achieve something my client wants. I could tell them it's not possible but I love a good challenge ;)
Basically, I'm trying to do a dropdown menu in which the dropdown <ul>, or:
ul.menu li ul
is surrounded by a div. Kind of:
<ul class="menu">
<li>
Item
<div class="submenu">
<ul>.....</ul>
</div>
</li>
</ul>
I want that div to have width:100% and fill the whole width of the page but have the UL inside aligned to the appropriate <li>.
The problem is the <div class="submenu"> will be as wide as the relative container, be it the main <ul class="menu"> or a <div> wrapping the <ul class="menu">.
The website itself has 1000px width and is centered width margin:0 auto;
I hope I have explained myself properly :S Here is a link to a mock up I have put together: Dropdown Menu Mock up
Any help highly appreciated.
Thanks,
Alex
Old question, but hopefully answer will help someone. I had to work on something similar to this a month or so ago.
Here is a fiddle of what I basically did (note: you have to do some extra work for this to work the same in older IEs): http://jsfiddle.net/doubleswirve/xbLrW/2/
I didn't use a nested div and instead stuck with nested lists. With a basic markup like the following:
<div class="nav">
<ul>
<li>
Products
<ul>
<li>Widget A</li>
<li>Widget B</li>
<li>Widget C</li>
</ul>
</li>
<li>
Locations
<ul>
<li>Location A</li>
<li>Location B</li>
<li>Location C</li>
</ul>
</li>
<li>
Staff
<ul>
<li>President</li>
<li>VP</li>
<li>Manager</li>
</ul>
</li>
</ul>
</div>
You can use the following styling:
/* GENERAL */
body { overflow-x: hidden; } /* trick from css-tricks comments */
/* FIRST LEVEL */
.nav > ul > li {
display: inline-block;
position: relative;
padding: 3px 10px 3px 0;
z-index: 100;
}
/* SECOND LEVEL */
.nav > ul > li > ul {
position: absolute;
left: 0;
top: 100%;
padding: 0 1000em; /* trick from css-tricks comments */
margin: 0 -1000em; /* trick from css-tricks comments */
z-index: 101;
visibility: hidden;
opacity: 0;
background: rgba(255, 240, 240, 0.8);
}
.nav > ul > li:hover > ul {
visibility: visible;
opacity: 1;
}
.nav > ul > li > ul > li {
padding: 3px 0;
}
If you wanted to get snazzy with the CSS, you could add this to the second level ul:
.nav > ul > li > ul {
...
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
-o-transition: all 0.3s ease;
transition: all 0.3s ease;
}
If anyone is interested in making this work similarly in old IEs or wants deeper nested lists, let me know.
To give you a head start, here are some useful links that helped me:
Full browser width bars (CSS tricks article/comment)
Fixing z-index (helpful for IE7)
Chris Coyier really covers us at work lol.
You're quite right, in that that box model doesn't work that way.
There is one thing I can think of, and that is to set your divs to
position:absolute
and use the top, left, right attributes to position them. But, as you say, that won't work if you have position: relative on a parent element.
To be honest, it'll be difficult to achieve this without a horrible mess of workarounds which will probably break between browsers. I've seen peers and colleagues spend ages trying to implement things like this, building more and more precarious code 'fixes' to get it to work cross-browser, receiving complaints from clients about it not working in IE6 and Firefox 1.5, only to give up on that 'feature' entirely.

Trying to center a dynamic width jquery menu

I have a menu built with jquery from apycom.com that I am trying to center.
The menu items are from a cms and dynamically created when the page loads. So this means that the menu isn't a fixed width.
I have tried several methods using just css, but without having a width set for the menu, they don't want to work.
I have found some information that leads me to believe that there may be a way to do it with javascript.
Is there is a way to dynamically set the width of the div element around the menu and then set the left and right margins to auto to center the menu?
If there is a better way to accomplish this, I am open to ideas.
Thanks in advance
Bjorn
Here is a sample of what I have thus far.
I have already tried using 'margin: 0 auto;' but without a width setting that doesn't work. Because the menu is created by looping over the menu items available from the cms, I don't know the width of the menu.
I've tried using 'display: inline-block;' as well, and that get's me to a point that the block space the menu takes up is only the width of the menu. Now I just need to be able to center that block. I thought that there might be a way that once the menu has been created and the width is then known that you could then apply the margin settings.
Maybe similar to the way jquery is able to apply and change style settings on the fly.
<div class="top_navigation_bar">
<div id="menu">
<ul class="menu">
<li><a class="parent" href="/en/"><span>Home</span></a></li>
<li><a class="parent" href="/en/web-design"><span>Web Design</span></a>
<div>
<ul>
<li><span>Design Packages</span></li>
<li><span>Website Maintenance</span></li>
<li><span>Redesign Website</span></li>
<li><span>Design Fundamentals</span></li>
<li><span>Design Key Elements</span></li>
</ul>
</div>
</li>
<li><a class="parent" href="/en/website-business-solutions"><span>Business Solutions</span></a></li>
<li><a class="parent" href="/en/internet-marketing"><span>Internet Marketing</span></a>
<div>
<ul>
<li><span>Small Business Marketing</span></li>
<li><span>Leveraging the Internet</span></li>
</ul>
</div>
</li>
<li><a class="parent" href="/en/doing-business"><span>About Us</span></a>
<div>
<ul>
<li><span>Design Team</span></li>
</ul>
</div>
</li>
<li><a class="parent" href="/en/blog"><span>Blog</span></a></li>
<li><a class="parent" href="/en/contact-us"><span>Contact</span></a></li>
<li class="last"><span>FAQ</span></li>
</ul>
</div>
.top_navigation_bar {
height: 46px;
padding-top: 4px;
background-color: #3a8658;
}
div#menu {
height: 46px;
padding-left: 24px;
background: url(/site_media/template_images/images/left.png) no-repeat;
_background: url(/site_media/template_images/images/left.gif) no-repeat;
width:auto;
}
div#menu ul {
margin: 0;
padding: 0;
list-style: none;
float: left;
}
Without a sample makes harder to see what exactly is happening. It would be nice if you post a sample for HTML and CSS you are using. But going blind...
For horizontal centering an element with CSS, you can do:
element {margin: 0px auto;}
This is enough to correctly center an element.
Note that block elements (like div, ul, li and p) tends to fill 100% horizontally. Floating elements or absolute positioning them makes they loose this fullfillment characterist. If this is the case, the elements will wrap to minimum comfortable size that allows the content to be displayed, unless you set width and/or overflow properties.
If you set width, and content is larger than the declared width, it will or overflow, or wrap. You have CSS properties to handle those cases too.
I recommend doind this with CSS, because makes layout more accessible. But if you prefer, you can code width with javascript or jquery, making your life a bit easier.
To process that with javascript, you'll need something like:
myMenuElement.style.width = "200px";
with Jquery (width method):
$('#myMenuElement').width(200);
Cheers.
EDIT
Not sure what is exactly the desired effect, but I made a few changes in your css. Check.
.top_navigation_bar {
height: 46px;
padding-top: 4px;
background-color: #3a8658;
}
div#menu {
height: 46px;
padding-left: 24px;
}
div#menu ul {
margin: 0;
padding: 0;
list-style: none;
}
ul.menu>li {
display: inline;
position: relative;
}
ul.menu>li>div {
display: block;
position: absolute;
left: 0%;
}
ul.menu span {
white-space: nowrap;
}
Follow a good reference from both, vertical and horizontal menus (I've learned from those).
If you are trying to center the #menu inside the .top_navigation_bar then you could use the margin:0 auto and additionally use jQuery like this
$(function(){
$menu = $('#menu');
$menu.width(
$('.menu').outerWidth() +
$menu.outerWidth() - $menu.width()
);
// added the following line, because the lavalamp plugin
// corrects itself when the window resizes..
// so we trigger a resize event, and the plugin fixes everything ;)
$(window).trigger('resize');
});
this will resize the #menu according to its contents, and will become centered because of the auto margin we set in css.
example at http://www.jsfiddle.net/MCnbr/

Resources