What is causing my button to get pushed down? - html

I know I could fix this with relative positioning, but I'm more wanting to know the why behind my button getting pushed down when I give height to .toggle. I figured out that giving absolute positioning to #menu made it so I could give top margin to .toggle, but when I gave it a height, the button got pushed down. When I changed the height on the button, it changed the vertical height of the button itself. When I tried line-height, again, the button itself changed size.
What is causing the button to get pushed down, and how would I fix this outside of using relative positioning, or switching to in-line block instead of using floats? (I gave .toggle a background-color of blue so I could visualize what was pushing down the button)
http://jsfiddle.net/7rj67454/
HTML:
<div id="menu">
<div id="logo">Codeplayer</div>
<ul class="toggle">
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
<li>Result</li>
</ul>
<button>Run</button>
</div>
CSS:
/* ----------- UNIVERSAL -----------*/
a, body, html, ul, li, div {
margin: 0;
padding: 0;
list-style: none;
font-family: helvetica;
}
#logo, .toggle {
line-height: 50px;
}
/* ----------- MENU BAR -----------*/
#menu {
background-color: #EDEBED;
width: 100%;
height: 50px;
box-shadow: 1px 1px 1px #000000;
position: absolute;
}
/* ----------- LOGO -----------*/
#logo {
float: left;
font-weight: bold;
margin-left: 15px;
font-size: 20px;
height: 20px;
}
/* ----------- TOGGLE BAR -----------*/
.toggle li {
list-style: none;
float: left;
margin-left: 20px;
}
.toggle {
margin: 0 auto;
width: 250px;
background-color: blue;
height: 30px;
margin-top: 10px;
}
/* ----------- BUTTON -----------*/
button {
float: right;
margin-right: 15px;
}

This is happening because the li elements in your ul are floated. When you float an element it takes it out of the normal document flow. As far as a containing element is concerned a floated child element doesn't take up any space within it, so it collapses.
The only reason that the Run button isn't sitting all the way at the top of the page is because .toggle has margin-top: 10px; and is taking up the full width of .menu. Try removing margin-top and you'll see Run move up.
The Run button is sitting under your .toggle ul because .toggle is taking up the full width of the shared parent.
This is how your elements are currently laid out:
#######################################
# .toggle #
#######################################
# Run #
#######
By adding height to you .toggle you're simply pushing and element that is already below it further down.

http://jsfiddle.net/7rj67454/1/
If that's what you're looking for then it's because you're confusing or having a hard time grasping the concept of float, position: absolute, and inline-block.
You'd essentially want to avoid position absolute in your case as you don't really need to take anything out of the normal flow.
You want to display things as inline-blocks rather than their default block style because you want to put things inline.
Floating them to the left can achieve the same as inline-block styling but it wasn't meant to be used for what you're aiming for.
Here's a quote from the following page:
Advantages of using display:inline-block vs float:left in CSS
Inline Block
The only drawback to the display: inline-block approach is that in IE7
and below an element can only be displayed inline-block if it was
already inline by default. What this means is that instead of using a
<div> element you have to use a <span> element. It's not really a huge
drawback at all because semantically a is for dividing the page
while a is just for covering a span of a page, so there's not a
huge semantic difference. A huge benefit of display:inline-block is
that when other developers are maintaining your code at a later point,
it is much more obvious what display:inline-block and text-align:right
is trying to accomplish than a float:left or float:right statement. My
favorite benefit of the inline-block approach is that it's easy to use
vertical-align: middle, line-height and text-align: center to
perfectly center the elements, in a way that is intuitive. I found a
great blog post on how to implement cross-browser inline-block, on the
Mozilla blog. Here is the browser compatibility.
Float
The reason that using the float method is not suited for layout of
your page is because the float CSS property was originally intended
only to have text wrap around an image (magazine style) and is, by
design, not best suited for general page layout purposes. When
changing floated elements later, sometimes you will have positioning
issues because they are not in the page flow. Another disadvantage is
that it generally requires a clearfix otherwise it may break aspects
of the page. The clearfix requires adding an element after the floated
elements to stop their parent from collapsing around them which
crosses the semantic line between separating style from content and is
thus an anti-pattern in web development.

use display: table;
/* ----------- UNIVERSAL -----------*/
a, body, html, ul, li, div {
margin: 0;
padding: 0;
list-style: none;
font-family: helvetica;
}
.toggle {
line-height: 50px;
}
/* ----------- MENU BAR -----------*/
#menu {
background-color: #EDEBED;
width: 100%;
height: 50px;
box-shadow: 1px 1px 1px #000000;
display: table;
}
#menu .item{
display: table-cell;
vertical-align: middle;
width: 25%;
}
#menu .item-center{
width: 50%;
}
#menu .item-right{
text-align: right;
}
/* ----------- LOGO -----------*/
#logo {
font-weight: bold;
margin-left: 15px;
font-size: 20px;
height: 20px;
}
/* ----------- TOGGLE BAR -----------*/
.toggle li {
list-style: none;
display: inline-block;
margin-left: 20px;
}
.toggle {
margin: 0 auto;
width: 250px;
background-color: blue; /* I added this to show what's causing run to push down */
}
/* ----------- BUTTON -----------*/
button {
margin-right: 15px;
}
<div id="menu">
<div class="item item-left">
<div id="logo">Codeplayer</div>
</div>
<div class="item item-center">
<ul class="toggle">
<li>HTML</li>
<li>CSS</li>
<li>JS</li>
<li>Result</li>
</ul>
</div>
<div class="item item-right">
<button>Run</button>
</div>
</div>

Figured it out: I switched the placement of the .toggle and "run" button in my html so that the logo was first, then the run button, then the toggle. This way, when I floated the button and put top margin on .toggle, it wasn't pushed down.

Related

Nav bar with diving image

I'm really struggling to get this navigation-menu with a image that splits the navigation to work. Responsive is a big plus. I don not believe the current code is on the the right way to go, so please be open minded of how to approach this problem.
HTML
<div id="nav">
<ul class="nav-left">
<li>1</li>
<li>2</li>
</ul>
<div class="nav-logo"></div>
<ul class="nav-right">
<li>3</li>
<li>4</li>
</ul>
</div>
CSS
#nav {
width: 400px;
background: #f5f5f5;
border: 1px solid grey;
text-align: center;
}
.nav-left, .nav-right, .nav-logo {
display: inline-block;
height: 30px;
vertical-align:bottom;
}
.nav-left {
background: red;
}
.nav-right {
background: red;
}
.nav-right, .nav-left {
width: 100px;
line-height: 30px;
padding: 0;
}
.nav-logo {
background: yellow;
width: 30px;
height: 100px;
margin-left: 10px;
margin-right: 10px;
}
ul {
margin: 0;
text-align: justify;
list-style: none;
}
li {
display: inline;
width: 100%;
}
Update 05.05.14
Updated it with my current code as requested.
I have gone ahead and made a slightly responsive header for you: JSfiddle (Note: random coffeeshop logo borrowed from google image search... man I should go get a coffee)
The idea is to use the html5 nav element to contain your links, each with a width:20% so it resizes with the width of the screen.
The image is positioned with background:url('...') no=repeat center; to avoid sizing problems. It's in a separate div to allow you to stripe the <nav> element. This is possible with pure css.
As you want to position the links vertically they are nested in divs, and space for the image is made using an empty div with the same width:20%; property as the link containers.
The design breaks once you get close to small mobile device widths (as the links cover the image). For this you can use #media queries.
I hope this gives you a starting point, but design is very subjective and there are many different ways to do it.
It may be worth while to style your page with Bootstrap and inspect their CSS to see what's happening.
Can you please add your css and html codes into your qustion then I can help you with a best solution. Also if you want then email me your codes, I will send you the best solution.
Orherwise just add a class .navfix on the main navigation container div or ul. then add the style property z-index:9999999 for the class .navfix into your stylesheet. Also if you have use any other div for the background then you have to add same thing for the nav background container but here z-index property should be negative or lower then other one.
sample :
.navfix {
z-index:9999999;
}
for background container (if you have)
then
.navfix {
z-index:9999999;
}
.backgrounContainerClassName {
z-index:-1;
}

How can i set height of my banner and vertically align its content to center?

I'm trying to make a banner on my webpage, the part on the top that is 700px wide and 80px high.
Code looks like:
<div class="container-narrow" style="heigth: 80px;">
<img src="#" width="52" height="52" alt="my logo" />
<ul>
<li>About</li>
<li>Projects</li>
<li>Contact</li>
</ul>
</div>
Css:
.container-narrow
{
margin: 0 auto;
max-width: 700px;
background: yellow;
}
ul
{
float: right;
width: 100%;
padding: 0;
margin: 0;
list-style-type: none;
}
a
{
float: right;
width: 6em;
color: black;
text-decoration: none;
padding: 0.2em 0.6em;
}
a:hover {color: #ccc; text-decoration: none;}
li {display: inline;}
What I want is the image and the horizontal menu to be vertically aligned in the center of the 80px. the logo to the left and the menu to the right.
I've tried to set the height and then padd/margin my way to get the job done but it feels rubbish...
Problem:
ul has a width:100%; if you give it a black border you will see that its occupying the width of the page, means it has no space to reside on the left of the logo inside the yellow header.
Removing this width will give the following result: http://jsfiddle.net/YBVe6/
Now since the header has a fixed max width, which is 700px, there's many ways to center the logo and the menu.
Fastest way I can think of is the following:
Give ul a display: inline-block;, (remove float: right;) then give the header a text-align: center;, here's the result : http://jsfiddle.net/YBVe6/1/
And if you want the menu to be displayed in the upper part, just add vertical-align: top;.
To start of, it's a good practice if you have an external CSS, don't put additional CSS in your HTML blocks:
<div class="container-narrow">
and put the height style in your css sheet, as you have a class setup for your div there anyway.
Second, making typo's is a pain if you want your CSS to work properly, so instead of heigth you should use height, will make you div actually 80px high.
Third of all: margins are there the position elements. Use them!
.container-narrow
{
height: 80px;
margin: 0 auto;
max-width: 700px;
background: yellow;
}
img
{
margin-top:14px;
}
ul
{
float: right;
padding: 0;
margin: 0;
list-style-type: none;
margin-top:25px;
}
a
{
width: 6em;
color: black;
text-decoration: none;
padding: 0.2em 0.6em;
}
a:hover {color: #ccc; text-decoration: none;}
li {display: inline;}
Edit
This is mostly applicable for vertical alignment. If you want to auto-center horizontally, you can make use of the margin:auto concept. This is possible because a page can't extend beyond the browser width (browser height can extend as you have scrolling available as default behavior).

ul auto indents as well as only pads one side?

I know there have been many questions about ul auto indents, I've tried with no success many of those answers and I have the additional problem of when adding a container with padding, it seems to only pad one side.
Basically I'm trying to get a ul bar fixed to the bottom of the window. This bar is inside a container (main) to give it padding from either side of the window). It is designed to auto expand with the width of the window (liquidish) so there is no defined width beyond the initial width=device width.
When written by itself, I get a small auto indention on the left side of the ul. I've tried adding 0 padding and margins to the ul and li elements and that seems to have no effect as well as others. When I add in the container it seems to pad only the side with the indent problem. I'm sure its something simple and stupid, but I appreciate any help.
As an aside.. the width of the li elements being 33.3% is due to using a spacing box hack found on stackoverflow. The use of it doesn't change/help/hurt anything in this problem, so I omitted it to keep this a little more simple.
JSFiddle Link http://jsfiddle.net/XdHXf/1/
HTML
<div class="main">
<nav>
<ul>
<li>Main</li>
<li>Second</li>
<li>Third</li>
</ul>
</nav>
</div>
CSS
.main{
width: 100%;
padding: 0 1em;
}
nav{
position: fixed;
bottom: 0px;
background: #455868;
width: 100%;
z-index: 1000;
}
nav ul {
list-style: none;
width: 100%;
padding: 0;
margin: 0;
}
nav li{
position: relative;
float: left;
width: 33.3%;
text-align: center;
background: #455868;
padding: 0;
margin: 0;
}
nav li a{
text-decoration: none;
display: inline-block;
width: 100%;
padding: 15px 0;
}
This comes from the browser's builtin stylesheet. If you add
body {
margin: 0;
}
the small extra space goes away.
As an alternative, you get similar results, when you check Normalized CSS under Fiddle Options.

HTML & CSS: How to create four equal size tabs that fill 100% width?

I am trying to create a navigation panel for my website. I would like it to consist of:
Four tabs in equal size with text-centered in each tab.
They should fill the whole page width.
I would really like the design to be flexible and browser friendly. I have tried various float techniques, but I can't get it to work. I hope that you can help me out!
Thank you.
HTML
EDIT: it's 2015 and HTML5 has been there for a while; following code should be inside a nav element (html5doctor) with landmark ARIA attribute role="navigation" on it (and 99.9% of the time be unique in any given page).
A navigation panel should use an unordered list of links:
<ul id="nav">
<li>One</li>
<li> Second</li>
<li>Third</li>
<li>Fourth and last, so large that... worst case</li>
</ul>
CSS
EDIT2: It's 2017, just use Flexbox 😲 (with or without flex-wrap: wrap)
inline-block is useful but has one drawback: whitespace between two elements must be carefully managed. Whether removed or no </li> in HTML5 or </li> at the beginning of the following line stuck like </li><li>next item or other tricks, you still have to do something or it'll create a ~4px gap between 2 elements.
25% + 25% + 25% + 25% doesn't equal 100% on all browsers if the total isn't a multiple of 4. Each browser has its own rounding method.
If you want elements to total 100% width and equal width, another method is to use display: table (and table-cell) with table-layout: fixed to force browsers to use the other table algorithm, the one that doesn't try to adapt cells width to content but respect the widths wanted by the designer/developer as far as possible.
ul {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
#nav {
display: table;
table-layout: fixed;
text-align: center;
}
#nav li {
display: table-cell;
width: 25%;
padding-right: 1px;
height: auto;
vertical-align: bottom;
}
#nav a {
display: block;
min-height: 100%;
padding: 4px 10px;
background-color: #222;
color: white;
border-radius: 6px 6px 0 0;
}
Fiddle
http://jsfiddle.net/PhilippeVay/aHCy3/1/
edit: http://jsfiddle.net/PhilippeVay/aHCy3/2/ with another method for space between each tab, courtesy of my colleague.
You don't need floats for this. Just set the width to 25%, or a tiny bit less than 25%. If you're using this on a block level element, set display: inline-block. This will work for all browser sizes, as well as respond to window resize.
HTML
<div class="nav">Nav 1</div>
<div class="nav">Nav 2</div>
<div class="nav">Nav 3</div>
<div class="nav">Nav 4</div>​
CSS
body, html {
width: 100%;
padding: 0;
margin: 0;
}
.nav {
width: 24%; /*Slightly less than 1/4th of the width*/
display: inline-block;
padding: 0;
margin: 0;
text-align: center;
}​​
Live demo
css:
.tab {
float: left;
width:25%;
height:25px;
background:black;
border:1px solid #fff;
box-sizing: border-box;
}​
html:
<div class="tab"></div>
<div class="tab"></div>
<div class="tab"></div>
<div class="tab"></div>​
jsfiddle: http://jsfiddle.net/zP7Xh/6/

Adding an image to the end of a <li> with text, centering the image vertically with the text

I don't use CSS that often so every time I use it I have to experiment again and figure things by trial and error.
What I'm trying to do is place icon_32 in the same line as: "I made this." The image is 32px high so I wanted to move it down a bit to center it with the line.
After experimenting a bit I found a solution that works on Safari and Chrome but doesn't work in Firefox.
Here's what I'm doing:
CSS
ul, li {
/* spacing */
margin: 0;
padding: 0;
border: 0;
outline: 0;
/* list */
list-style: none;
}
#work_list li {
display: block;
/* spacing */
margin-bottom: 10px;
}
#work_list .icon_32 {
display: inline-block;
position: absolute; /* This doesn't work in Firefox. */
cursor: pointer;
/* spacing */
margin-top: -8px;
margin-left: 8px;
}
HTML
<ul id="work_list">
<li> I made this. <div class="icon_32"></div></li>
<li>I write, sometimes.</li>
</ul>
Because one can't add a padding-top to inline elements, I made the icon's position absolute. The problem with just having it absolute is that I have to manually set the margin-left because the icon doesn't move to the left automatically with the text. By setting display to block on the <li> and display to inline-block on the icon, what I was trying to achieve worked (although I don't fully understand why). This however, doesn't work in Firefox (and I haven't even tested in IE yet).
Is there a proper way to achieve what I'm truing to do that works correctly on all browsers?
Update: How I ended up solving this by using the suggestions from the answers. Thanks a lot for the help!
I didn't want to use line-height because I wanted to preserve the original height of the li. I just got rid of the unnecessary position: absolute and added vertical-align: middle. After doing so, the image was still affecting the height of the li, so I just added an id to the affected li and overwrote the margin-bottom so the sum of its height + bottom margin would equal the sum from the li without the 32px image.
This is how it ended up being:
CSS
ul, li {
/* spacing */
margin: 0;
padding: 0;
border: 0;
outline: 0;
/* list */
list-style: none;
}
#work_list li {
display: block;
/* spacing */
margin-bottom: 10px;
}
#work_list #work_item_icon {
margin-bottom: 3px;
}
#work_list span.icon_32 {
display: inline-block;
cursor: pointer;
/* spacing */
vertical-align: middle;
margin-top: -8px;
margin-left: 8px;
}
HTML
<ul id="work_list">
<li id="work_item_icon">I made this. <span class="icon_32"></span></li>
<li id="work_item_writing">I write, sometimes.</li>
</ul>
There are many ways to do this:
You can use negative margins to even out the difference between the line height and image height. The height of one line of text appears to be 20px; so a margin-top: -6px and margin-bottom: -6px will make the 32px tall image act as if it were 20px:
#work_list img.icon_32
{
vertical-align: middle;
margin-top: -6px;
margin-bottom: -6px;
}
#work_list span.icon_32
{
/* //// SPAN AS IMAGE \\\\ */
display: inline-block;
width: 32px;
height: 32px;
background-image: url(http://dummyimage.com/32x32/cf0/000&text=32x);
/* \\\\ SPAN AS IMAGE //// */
vertical-align: middle;
margin-top: -6px;
margin-bottom: -6px;
}
demo
Alternately, you can set a line height equal to the height of the image and set vertical-align: middle for the image. You can adjust a few pixels using a negative margin-top:
ul, li
{
line-height: 32px;
}
#work_list img.icon_32
{
vertical-align: middle;
margin-top: -4px;
}
#work_list span.icon_32
{
/* //// SPAN AS IMAGE \\\\ */
display: inline-block;
width: 32px;
height: 32px;
background-image: url(http://dummyimage.com/32x32/cf0/000&text=32x);
/* \\\\ SPAN AS IMAGE //// */
vertical-align: middle;
margin-top: -4px;
}
demo
This also work in firefox also please give position:relative to your LI
or it's better to edit your HTML like this:
<ul id="work_list">
<li><span>I made this.<span> <div class="icon_32"></div></li>
<li>I write, sometimes.</li>
</ul>
CSS:
li span, icon_32{
display:inline-block;
}
Try adding this css rule to your li,
li {line-height: #px;}
play around with the line height to get what you want. Also if using line height affects the image add float: left; to it, or take it out of normal flow.
I think your solution will be line-height. You can set this on the li, and it will center the text vertically.
#work_list li {
line-height: 32px;
}
#work_list .icon_32 {
height: 32px;
width: 32px;
}
Give the element containing the icon div relative position and it will make the positioning of the icon relative to that container.
http://jsfiddle.net/9HeNy/
Make the icon_32 element position : relative and it's parent li element line-height : 32px. A demo is above.
Docs for line-height: https://developer.mozilla.org/en/CSS/line-height