Whitespace appearing using CSS :after and :content - html

I am trying to style the output of wp_list_categories using CSS to put commas in between items. However, there is a whitespace appearing before the comma and I seriously cannot comprehend where it is coming from!
I have made a jsbin to demonstrate and compare.
Screenshot:
HTML:
<ul id="category-listing">
<li class="cat-item cat-item-6">Branding
</li>
<li class="cat-item cat-item-4">Environment
</li>
<li class="cat-item cat-item-5">Exhibition
</li>
<li class="cat-item cat-item-8">Lecture
</li>
<li class="cat-item cat-item-9">Photography
</li>
<li class="cat-item cat-item-10">Print
</li>
</ul>
CSS:
li {
font-size: 46px;
display: inline;
margin: 0;
padding: 0;
}
#category-listing li:after {
content: ', ';
}

The white space is appearing because it is there in your HTML code.
The closing </li> tag is on a new line. Carriage returns are counted as white space in HTML, and therefore you have white space at the end of the list item element.
The reason it is showing up is because you're using display:inline. When usign inline (or inline-block), white space is relevant because inline means "treat this element as plain text", and therefore any white space is considered an intentional part of the text.
The best way to get rid of this is to simply put the </li> closing tag on the same line as the rest of the text, so that there is no white space there.
There are a number of other ways around it, but most of them involve quite hacky CSS; simply closing up the space is by far the easiest option.
The next best alternative is to switch to using float:left instead of display:inline. This will also deal with the problem, but will change the way the whole thing is rendered, which will require you to make various other changes to your CSS to compensate.

FLoating the anchor inside the list item will solve this issue:
li a {float:left;}

It is because you have spaces with inline display. You have two choices:
Remove spaces:
<ul id="category-listing">
<li class="cat-item cat-item-6">Branding
</li><li class="cat-item cat-item-4">Environment
</li><li class="cat-item cat-item-5">Exhibition
</li><li class="cat-item cat-item-8">Lecture
</li><li class="cat-item cat-item-9">Photography
</li><li class="cat-item cat-item-10">Print</li>
</ul>
Use float:
ul {overflow: hidden;}
ul li {float: left;}

Related

Need help in adding space to a nav list in CCS3

I need to add a space in between some of the items in a nav list but not sure how. Some help would be appreciated.
Here is the HTML
<nav>
<ul>
<li>Home Page</li>
<li>Running Class</li>
<li>Cycling Class</li>
<li>Swimming Class</li>
<li>Coaches</li>
<li>Active.com</li>
<li>Runner's World</li>
<li>endomondo.com</li>
<li>Strava</li>
<li>Bicycling Magazine</li>
<li>VeloNews</li>
<li>Bicycle Tutor</li>
<li>Swim Smooth</li>
<li>Swimming World</li>
<li>USA Swimming</li>
<li>triathlon.org</li>
<li>usatriathlon.org</li>
<li>Texas Triathlons</li>
<li>CapTex Triathlon</li>
<li>Triathlon Calendar</li>
<li>Triathlete.com</li>
<li>Trifuel.com</li>
</ul>
</nav>
I need to put a space between the 5th and 6th line and between the 15th and 16th. Not sure how to do it in the CCS.
This can be achieved in CSS3 by using the nth-child
nav ul li:nth-child(6), nav ul li:nth-child(16)
{
margin-top: 5px;
}
Give 5th & 15th an ID ...
Then write in CSS Codes :
<style>
nav ul li.one {margin-bottom:25px;}
nav ul li.two {margin-top:25px;}
</style>
In HTML:
<li class="one">Coaches</li>
<li class="two">Active.com</li>
do the same with 15 and 16
In the <li> put a margin like so <li style="margin-top: 20px">. That will give you extra spacing above. You can change top to bottom/left/right and the pixels for customizing.
try to add padding/margin to those li tags
<li style="padding-right:3px">Coaches</li>
<li style="padding-left:3px">Active.com</li>
EDIT: Add a class to those li elements you want space around. Do this like so:
<li class="space">
Then You could add padding or margins to the CSS file or between
<style></style>
tags in the html document.
.space { margin: 10px; }
.space { padding: 10px; }
There is a good reason why you want exactly add some space to this positions, so:
To avoid to modify CSS file each time an entry is added
Add some :nth-child is not a good solution. If tomorrow you add a new item, your space will not be in the correct place.
To Avoid a semantic meaning confusion
Add some <li> </li> is not a good solution too because a <li> (list item) must contain same semantic thing (in <ol> (ordered list) or <ul> (unordered list) each item are the same purpose, else use <div> insteed). If there is some « empty » item, this is not a list item anymore. For example, if you list link from a database, create empty item just for « design » is not good, because this force you to add some « empty data ».
To allow Semantic and Design to fit
You do first define « why this item need more space », « why this item is different » ? For example: « because this item is a very important item » so tag item as important (or others).
After that, just apply your specific CSS for .important item on your list for example.
/**
* List all link to navigate into website.
* #component .main-navigation
*/
.main-navigation {}
/**
* Set an item as important in a list of item.
* #pattern .important
* #partof .main-navigation
* #example <ul>
* <li class="important">This is important</li>
* <li>This is less important</li>
* </ul>
*/
.main-navigation .important {
margin-bottom: 10px;
}
<nav class="main-navigation">
<ul>
<li>Home Page</li>
<li>Running Class</li>
<li>Cycling Class</li>
<li>Swimming Class</li>
<li class="important">Coaches</li>
<li>Active.com</li>
<li>Runner's World</li>
<li>endomondo.com</li>
<li>Strava</li>
<li>Bicycling Magazine</li>
<li>VeloNews</li>
<li>Bicycle Tutor</li>
<li>Swim Smooth</li>
<li>Swimming World</li>
<li class="important">USA Swimming</li>
<li>triathlon.org</li>
<li>usatriathlon.org</li>
<li>Texas Triathlons</li>
<li>CapTex Triathlon</li>
<li>Triathlon Calendar</li>
<li>Triathlete.com</li>
<li>Trifuel.com</li>
</ul>
</nav>
Name the .important class as you want but not .space for example, because if tomorrow you want change the design not with space but with color differenciation, your class will not be correctly named. But an item important will always be important. If it's not the case anymore, just « untag » it by removing class.
If tomorrow or later you want add some link between your existing link, no change in CSS will be needed.

How can I target a block after another block with css?

My html looks like this:
<ul>
<li class="myItemClass">item foo</li>
<li class="myItemClass specialItemClass">item foo</li>
<li class="myItemClass">item foo</li>
<li class="myItemClass">item foo</li>
<li class="myItemClass specialItemClass">item foo</li>
<li class="myItemClass specialItemClass">item foo</li> <!-- problem here -->
<li class="myItemClass">item foo</li>
<li class="myItemClass">item foo</li>
</ul>
Now I want to define a css-style for an item that has the 'specialItemClass' and is located after an item that has the 'specialItemClass', too. How can I do that?
Further explanation: The 'specialItemClass' includes having a 2px coloured border. But if two special items are right beneath each other, the borders are doubled. So I don't want to add the special border at the top [or bottom] if there's another special item above [or below].
Use the adjacent sibling selector +
.specialItemClass + .specialItemClass{
/*styles here*/
}
use adjecent css3 selector to work around your issue.
its better to have mere specific selector.
Please see the fiddle
li.specialItemClass + li.specialItemClass{
border-top:none;
}
https://jsfiddle.net/0ou3a02u/

How to keep <li> elements on single line in fixed width?

I am developing a website and I want a menu like the above image. This is what I have tried: jsFiddle
HTML
<div class="">
<ul class="header-menu-ul">
<li class="header-menu menu-link1-color">ABC's Office</li>
<li class="header-menu menu-link2-color">Benefits</li>
<li class="header-menu menu-link3-color">NEW Brand</li>
<li class="header-menu menu-link4-color">Editorial</li>
<li class="header-menu menu-link5-color">Manger</li>
<li class="header-menu menu-link6-color">Extra</li>
</ul>
</div>
The problem I'm facing is it's taking a space between <li> tags.
I just floated the list items to the left, like this:
.header-menu-ul li {
float: left;
}
WORKING FIDDLE: link.
DEMO
This will create an always single line menu unliek the floating solution where often items fall from position on small sizes + no spaces between the items.
Code:
.header-menu-ul{
display: table;
width: 100%;
}
.header-menu{
display: table-cell;
}
my approach would be using inline-block and min-width. can also use percent in width (1oo% / total_li ).

Prevent HTML Tidy adding li elements

I am using HTML Tidy to output pretty HTML source and as a final encoding check.
However it is taking a very simple list such as:
<ul id="navigation">
<li>Summary</li>
<li>Upload</li>
<li>Accounts</li>
</ul>
and converting it to this monstrosity:
<ul id="navigation">
<li style="list-style: none">
</li>
<li id="active">Summary
</li>
<li style="list-style: none">
</li>
<li>Upload
</li>
<li style="list-style: none">
</li>
<li>Accounts
</li>
</ul>
How do I gracefully tell it to leave my list alone?
I have searched these configuration options, however I often find they are not named intuitively or explained properly.
It's actually trying to correct your markup to make it conform to standards, your <li> tags should be around the <a> tags, not the other way around, maybe if you fix up that then it won't try to add extra items to the list.
You can remove the style part though, just modify your css to have:
ul.navigation li
{
list-style: none;
}
The only answer: give it valid markup to start with. The only legal child element of a ul is an li. An a element cannot be a child of a ul.
<ul id="navigation">
<li>Summary</li>
<li>Upload</li>
<li>Accounts</li>
</ul>
If you want the whole li to be clickable, style the a element as display: block:
#navigation li a {
display: block;
}
Your list is invalid markup; an ul element may only contain li elements. Tidy is actually applying the most sensible general approach to fixing such broken markup: it turns any non-li content to an li element for which the list bullter is suppressed.
So manually change markup like
<li>Summary</li>
to
<li>Summary</li>
which is probably what you want. This may require changes to CSS or JavaScript code, if they expect the current markup.
Move your <a> tags into <li>-s:
<li>Summary</li>

Is it sound to wrap a list item in an anchor?

I have a group of images which each have their own links. I want the images to be in a list (<ul><li> .. etc) and have each item have a different background-image.
Would I run into any issues with something like this?
<ul>
<li class="1"></li>
<li class="2"></li>
<li class="3"></li>
<li class="4"></li>
<li class="5"></li>
<li class="6"></li>
</ul>
You would do better to write it like this
<ul>
<li class="1"></li>
<li class="2"></li>
<li class="3"></li>
</ul>
Then you could add the background-image to either the a or the li.
However, you would style the as as display:block and give them the same height and width of the li. That way the background-image would show and the entire li would be clickable.
It is not valid HTML because the only thing allowed in an <ul> element is <li>s.
It's not valid HTML.
<!ELEMENT UL - - (LI)+ -- unordered list -->