Inline Block Elements Overflowing Parent Container - html

I have a list of 4 menu items sitting side by side using display:inline-block;. Each item is 120px, therefore I should be able to set the parent container to be 480px wide, however this sends the last item into the next row, why is this ??
Here is a jsfiddle :http://jsfiddle.net/htdgdhxn/
My html:
<section id="nav">
<div id="nav-wrapper">
<ul id="nav-list">
<li id="nav-home">Home
</li>
<li id="nav-clothes"><a class="category All">Clothes</a>
</li>
<li id="nav-about">About Us
</li>
<li id="nav-contact">Contact Us
</li>
</ul>
</div>
</section>
CSS:
* { margin: 0px; padding: 0px; }
#nav { background-color: #fff; }
#nav-wrapper { text-align: center; height: 74px; }
#nav-list { height: 100%; width: 480px; }
#nav-list li { display: inline-block; vertical-align: top; width: 120px; height: 100%; }
#nav-list li a { text-decoration: none; color: #000; font-size: 1.6em; display: block; height: 100%; line-height: 74px; }
#nav-list li a:hover { background-color: #F0ECE1; cursor: pointer; }
I have tested and this happens in Chrome, IE and Firefox.

Remove the whitespace between each <li>
<li></li> <...space here...> <li></li>
Inline block elements create a gap between li elements.
<ul id="nav-list">
<li id="nav-home">Home
</li><li id="nav-clothes"><a class="category All">Clothes</a>
</li><li id="nav-about">About Us
</li><li id="nav-contact">Contact Us
</li>
</ul>
See fiddle

The inline-block value is incredibly useful when wanting to control margin and padding on
"inline" elements without the need to block and float them.One problem that arrises
when you use inline-block is that whitespace in HTML becomes visual space on screen.
Gross.There are a few ways to remove that space; some of them are just as gross, one is
reasonably nicer.
Solution 0: No Space Between Elements:
The only 100% solution to this issue is to not put whitespace between those elements in the HTML source code:
<ul><li>Item content</li><li>Item content</li><li>Item content</li></ul>
Solution 1: font-size: 0 on Parent
The best white-space solution is to set a font-size of 0 on the parent to the inline block
elements.
.inline-block-list { /* ul or ol with this class */
font-size: 0;
}
.inline-block-list li {
font-size: 14px; /* put the font-size back */
}
Solution 2: HTML Comments
This solution is a bit gangsta but also works. Using HTML comments as spacers between the elements works just as placing no space between elements would:
<ul>
<li>Item content</li><!--
--><li>Item content</li><!--
--><li>Item content</li>
</ul>
It might help you.

Just increase the width of your container to 500px
#nav-list { height: 100%; width: 500px; }
or remove the white spaces between consecutive li tags
or
apply display:initial in #nav-list { height: 100%; width: 480px;}
i.e #nav-list { height: 100%; width: 480px; display: initial;}

Reason:-
1. The font size of the text in the li element might be causing the problem.
You can modify it by reducing the font-size.
#nav-list li a { text-decoration: none; color: #000; font-size: 1.2em; display: block; height: 100%;line-height: 74px; }
Instead of using this
#nav-list li { display: inline-block; }
You can do like this:-
#nav-list li { display: inline; font-weight:bold;}
Please let me know if this helps.

Related

Adding an image in front of List Item

How can I add an icon in front of a specific list item?
<ul class="rightNav2">
<li id="homeLnk">Home</li>
</ul>
I have the following style for the list items already and I want to add a specific icon in front of one of the items. The image however does not appear.
.rightNav2 li {
display: inline;
padding-right: 6px;
padding-left: 6px;
color: white;
}
.rightNav2 #homeLnk {
list-style-image: url('/images/homeIcon.png');
}
There are several methods to add an image to a list item.
Here is one using a background image. http://jsfiddle.net/p05g14zm/
rightNav2 li {
display: inline;
padding-right: 6px;
padding-left: 20px;
color: white;
}
.rightNav2 #homeLnk {
background-image: url('http://i.stack.imgur.com/vQ4nM.jpg?s=32&g=1');
background-repeat: no-repeat;
background-size: contain;
}
Try
.rightNav2 #homeLnk:before {
content: url('/images/homeIcon.png');
}
Also you might want to make sure that the image path is correct.
Please check out my codepen... I believe this may help you:
http://codepen.io/anon/pen/myRWmZ
html:
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.css" rel="stylesheet">
<ul>
<li>Link 1</li>
<li class="home">Link 2</li>
<li>Link 3</li>
</ul>
CSS:
ul {
list-style-type: none;
}
li.home::before {
font-family: FontAwesome;
content: "\f015";
margin-right: 3px;
}
li.home {
margin-left: -18px;
}
So what I did was place an icon using the :before selector. The margin adjustments are meant to ensure that each of the list items still align properly.
The css below would add an icon to the left of the home li element.
.rightNav2{
list-style:none;
padding:0;
margin:0;
}
.rightNav2 li{
padding:0;
margin:0;
}
.rightNav2 #homeLnk {
padding-left: 35px;
/* padding-left above is the width of the icon plus any whitespace between text */
min-height:10px;
/* min-height above is the height of the icon */
background-image: url('/images/homeIcon.png') no-repeat center left;
}
I would as in the answer above recommend considering icon fonts if this a responsive site.
Background images on zoom can become very grainy.
Problem
The list-style-image property determines whether the list marker is
set with an image, and accepts a value of "none" or a URL that points
to the image: ~css tricks
This means that, rather than applying this styling to the li, you're meant to apply it to the parent ul. Something like:
ul {
list-style-image: url(images/bullet.png);
}
So you can't place it on a single element using just this syntax (unless you wanted to use the :first-child selector (not tested))
My Solution
This solution may or may not be of use to you, but it's using pseudo effects (meaning no real 'extra' elements need to be added). The pseudo element would also be clickable, too (with no need of worrying about image sizing, as this would do it for you):
.rightNav2 li {
display: inline;
padding-right: 6px;
padding-left: 6px;
position: relative;
display: block;
/*only for demo*/
}
.rightNav2 #homeLnk a:before {
content: "";
height: 100%;
width: 20px;
left: -20px;
top:0;
position: absolute;
background: url(http://placekitten.com/g/20/20);
}
<ul class="rightNav2">
<li id="homeLnk">Home
</li>
<li>another link
</li>
<li>and another link
</li>
</ul>

Element only shows sometimes

I'm trying to make a menubar for my website with home, about, and contact links. They are aligned on the right side of the page. For some reason, when I reload the page, sometimes contact seems to return to the next line. I don't have to change anything for this to happen.
Here is the relevant css:
ul#menubar {
display: inline-block;
list-style-type: none;
font-family: "Cantarell";
float: right;
margin-right: 5%;
margin-top: 0;
margin-bottom: 0;
}
li.menubar_item {
display: inline-block;
padding-right: 5%;
}
li.menubar_item a {
color: #D2F2FE;
text-decoration: none;
}
div.menubar_div {
height: 150px;
padding-right: 5%;
}
And the HTML:
<ul id="menubar">
<li class="menubar_item">
<div class="menubar_div">Home</div>
</li>
<li class="menubar_item">
<div class="menubar_div">About</div>
</li>
<li class="menubar_item">
<div class="menubar_div">Contact</div>
</li>
</ul>
It's your padding-right: 5% that ruins it. Since you haven't set a width for the ul#menubar, it's automatically set to 100%. The problem is that the width expands with the padding, causing it to be a total of 115% (100 + 5x3).
You can add this to your ul#menubar:
width: 85%;
text-align: right;
Try adding this line in your CSS :
ul#menubar {
white-space:nowrap;
}
The property white-space:nowrap force the li elements to be side by side

changing the line-height of an individual li tag

I want to have the phone number and email address vertically align with the little icons next to them. I'm trying to change their line-height, but that changes the line height of all the li's in that area. I think that is because they are inline. Here is the site and the css.
LINK: www.matthewtbrown.com/newsite
HTML:
<ul class="contact">
<li><img src="http://s7.postimg.org/64ux9a1if/email.png"></li>
<li class="contacttext">mbrown74#rocketmail.com</li>
<li><img src="http://s7.postimg.org/g0w08x7af/phone.png"></li>
<li class="contacttext">978-761-1205</li>
</ul>
CSS:
.contact {
color: #fff;
text-align: center;
float:none;
}
.contact > li {
display: inline;
}
.contacttext {
font-size: 19px;
padding-left: 5px;
}
That's where vertical-align comes into play which aligns inline elements to each other:
In your case the following should work:
.contacttext{
vertical-align:text-top;
}
Also note, that if you want to have your li contain the img properly, it needs a display-type other than the default inline - inline-block might be suited most.
Try something like this:
li {
display: inline;
vertical-align: top;
line-height: 25px;
}
after this i have this result:
try this
<ul class="contact">
<li class="contacttext">
<img src="http://s7.postimg.org/64ux9a1if/email.png">mbrown74#rocketmail.com
</li>
<li class="contacttext">
<img src="http://s7.postimg.org/g0w08x7af/phone.png">978-761-1205
</li>
I just kept the icons and the text in the same li's
I got it. I did:
.contacttext {
font-size: 19px;
padding-left: 5px;
display:inline-block;
vertical-align:middle;
}
You could add margin-bottom to your <img> to solve this issue.
li img {
margin-bottom: 3px;
}

Horizontal list items

So, I have attempted to create a horizontal list for use on a new website I am designing. I have attempted a number of the suggestions found online already such as setting 'float' to left and such - yet none of these have worked when it comes to fixing the problem.
ul#menuItems {
background: none;
height: 50px;
width: 100px;
margin: 0;
padding: 0;
}
ul#menuItems li {
display: inline;
list-style: none;
margin-left: auto;
margin-right: auto;
top: 0px;
height: 50px;
}
ul#menuItems li a {
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
font-weight: bolder;
color: #000;
height: 50px;
width: auto;
display: block;
text-align: center;
line-height: 50px;
}
<ul id="menuItems">
<li>
Home
</li>
<li>
DJ Profiles
</li>
</ul>
Currently I am unsure of what is causing this issue, how would I go about and resolve it?
Updated Answer
I've noticed a lot of people are using this answer so I decided to update it a little bit. No longer including support for now-unsupported browsers.
ul > li {
display: inline-block;
/* You can also add some margins here to make it look prettier */
}
<ul>
<li> some item
</li>
<li> another item
</li>
</ul>
This fiddle shows how
http://jsfiddle.net/9th7X/
ul, li {
display:inline
}
Great references on lists and css here:
http://alistapart.com/article/taminglists/
I guess the simple solution i found is below
ul{
display:flex;
}
A much better way is to use inline-block, because you don't need to use clear:both at the end of your list anymore.
Try this:
<ul>
<li>
some item
</li>
<li>
another item
</li>
</ul>
CSS:
ul > li{
display:inline-block;
}
Have a look at it here : http://jsfiddle.net/shahverdy/4N6Ap/
You could also use inline blocks to avoid floating elements
<ul>
<li>
some item
</li>
<li>
another item
</li>
</ul>
and then style as:
li{
/* with fix for IE */
display:inline;
display:inline-block;
zoom:1;
/*
additional styles to make it look nice
*/
}
that way you wont need to float anything, eliminating the need for clearfixes
Here you can find a working example, with some more suggestions about dynamic resizing of the list.
I've used display:inline-block and a percentage padding so that the parent list can dynamically change size:
display:inline-block;
padding:10px 1%;
width: 30%
plus two more rules to remove padding for the first and last items.
ul#menuItems li:first-child{padding-left:0;}
ul#menuItems li:last-child{padding-right:0;}
strong tex
ul {
list-style: none;
display: flex;
justify-content: space-around;
}
<ul>
<li>bla</li>
<li>blabla</li>
<li>blablabla</li>
</ul>

Unordered list navbar elements have strange gaps

I'm trying to write a navigation bar using an <ul> with inline elements, but the elements all have a gap between them that seem to come from nowhere. That is when hovering a link, the shaded box should snap to the surrounding boxes. The page currently looks like this: http://wictorht.at.ifi.uio.no/. What is causing these gaps?
HTML:
<body>
<div id="main">
<ul class="header">
<li class="title">wictorht</li>
<li class="header">
<a class="header" href="https://bitbucket.org/htor/dwmst/src">dwms</a>
</li>
<li class="header">
<a class="header" href="https://bitbucket.org/htor/linux/src">linux</a>
</li>
<li class="header">
<a class="header" href="http://www.fsf.org/register_form?referrer=10397">fsf</a>
</li>
<li class="header">
<a class="header" href="http://stackexchange.com/users/1006063">stackexhange</a>
</li>
</ul>
</div>
</body>
CSS:
body {
background: #666666;
color: #c0c0c0;
margin: 0;
}
a.header {
text-decoration: none;
padding: 10px;
margin: 0;
}
a.header:hover, a.header:active {
background-color: #666666;
color: #c0c0c0;
}
ul.header {
background-color: #c1c1c1;
color: #666666;
list-style: none;
padding: 10px 10px 10px 0;
margin: 0 0 10px 0;
}
li.header {
display: inline;
}
li.title {
background-color: #000000;
color: #bada55;
display: inline;
padding: 10px;
}
This is because all white-space, including new-lines, between elements is collapsed down to a single space when rendered by the client's browser. To hide the spaces you can either:
Remove the spaces between li elements:
<li><!-- content --></li><li><!-- more content --></li>
Set the font-size of the parent ul to 0, and redefine the font-size of the li element:
ul {
font-size: 0;
}
ul li {
font-size: 14px;
}
Comment out the gaps between the li elements:
<li>Content</li><!--
--><li>Next li</li>
Float the li elements instead of using display: inline, which removes the spaces by taking the elements out of the normal flow:
ul {
overflow: hidden; /* to keep the li 'visibily' within the bounds of the ul */
}
ul li {
float: left;
}
Close the li tag on the next line, before the next li opening tag this feels slightly wrong to me, but it is valid:
<li>First li</li
><li>Second li</li>
(Or, obviously, place the next li opening-tag on the previous line, immediately after the previous element's closing tag:
<li>First li</li><
li>Second li</li>
)
The gaps are caused by the whitespace between the <li></li> tags.
Try <li>...</li><li>...</li> as a comparison.
Anyways, avoid this with display:block and using float:left
This is a great post explaining what is happening and the work arounds that have already been mentioned by the previous answers.
http://css-tricks.com/fighting-the-space-between-inline-block-elements/
You also have a little trouble with your css selector names, you actually only need one class and you can take advantage of the nature of CSS to do the rest.
.header {
background-color: #c1c1c1;
color: #666666;
list-style: none;
padding: 10px 10px 10px 0;
margin: 0 0 10px 0;
}
Now target all the 'li' tags that are children of the .header class
.header li {
display: inline;
}
Now target all the 'a' tags that are children of the .header class (these happen to be inside your 'li' tags)
.header a {
/* etc */
}