Justified horizontal list with minified HTML - html

For a horizontal menu i want to justify the list items over the full width.
This works:
CSS:
ul {height: 1em;text-align: justify;overflow: hidden;padding-left: 0;}
li {display: inline-block;}
li:last-child {padding-left: 100%;}
HTML:
<ul>
<li>flexible number</li>
<li>and length of</li>
<li>list items</li>
<li>hidden</li>
</ul>
OUTPUT (the lines are showing the width of the UL):
|flexible number and length of list items|
If i delete all whitespaces and linebreaks to minify the HTML-output, it doesn't work any more.
SMALLER HTML:
<ul><li>flexible number</li><li>and length of</li><li>list items</li><li>hidden</li></ul>
It looks like this:
|flexible numberand length oflist items |
Is there any chance to get the "normal" behavior back with pure CSS?
Please have a look at this fiddle:
http://jsfiddle.net/Tfranz/HpP99/

Check out the jsFiddle below - you should be able to use % widths and floats to do this without padding or margins. Standard minification should not affect the results.
http://jsfiddle.net/MNally/CWjMz/
HTML:
<div id="container">
<ul id="the-list">
<li>item1</li>
<li>item1</li>
<li>item1</li>
</ul>
</div>
CSS:
div#container {
width:100%;
padding:0;
margin:0;
}
ul#the-list {
width:100%;
text-align:center;
}
li {
float:left;
display:inline;
padding:0;
margin:0;
background:#c9c;
width:33.333%;
}
I set the margin/padding to '0' to illustrate that they're not needed. You can remove these lines.

How about using display:table; and display:table-cell; ?
See here: http://jsfiddle.net/HpP99/2/

I ran into the same exact problem. HTML minification removes whitespaces as we all know, but in this case it breaks the CSS "text-align: justify" and "text-align-last: justify".
The solution is to implement a single line jQuery code to add white spaces back after the closing tags for the list items:
$('#the_list li').after(" ");
I hope someone finds this helpful.

Related

CSS Can't remove indentation from a bulleted list

I am trying to remove the indentation and bullets from a bulleted list using CSS. Here is what I am doing:
.entry-content ul{
list-style-type:none;
padding:0;}
The bullet points are removed from the list, but the indentation is not fixed. Here is the HTML:
<div class="entry-content">
<ul class=wp-block-categories wp-block-categories-list">
<li class="cat-item cat-item-8">Advice
</li>
</ul>
</div>
Here is an image before I apply the CSS:
https://imgur.com/Sw31pHJ
Here is an image after I apply the CSS:
https://imgur.com/Utnt5vI
Does anyone know why the indentation isn't being removed? I am doing this in wordpress.
You have an error in your HTML.
<ul class=:wp-block-categories wp-block-categories-list">
should be
<ul class="wp-block-categories wp-block-categories-list">
As for your CSS, one of these is the most likely:
The li may have a margin as well. try .entry-content ul li { margin-left: 0; }
Your selector isn't specific enough, try .entry-content ul.wp-block-categories-list instead
Your ul may have margin instead of padding (doubtful)
You can try and diagnose these with DevTools/your browsers inspector, it will show you all of the positions/margins/paddings and everything related to the element's bounding box:
You likely also need to apply:
.entry-content ul li {
margin-left: -20px;
}
The exact amount of margin will differ based on the size of your font, but 20px is the default.

Drop Down Alignment

I am working on a supposedly simple drop down menu using HTML and CSS, and have encountered an issue. After scouring google and the forums to no avail, figured it was time to ask. I am trying to get the drop down menu to line up with it's parent element.
I have experimented with a few different methods, so far the most hopeful seems to be setting the "left:" value to the necessary percentage.
This brings up another issue though:
Issue: when I set the left value, I end up with a bunch of blank space to the right of the item that I can't seem to get rid of. Can't get the width right.
Code located here: https://jsfiddle.net/c6mz3t08/5/
HTML
<div id="navbar-top">
<ul class="horizontal">
<li>Home</li>
<li>About
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
</ul>
</li>
<li>Header</li>
<li>Header</li>
<li>Header</li>
</ul>
CSS for dropdown
.horizontal li ul {
opacity:0;
visibility:hidden;
text-align:left;
position:absolute;
top:50px;
left:-38%; //end up with blank space on right?
}
.horizontal li ul li {
position:relative;
background-color:#BBB;
display:block;
width:100%;
}
It seems the alignment problem happens because the <ul> starts after the word "About" in the second <li>.
a.) for positioning adjust the leftparameter in .horizontal li ul (-39px seems to work well).
b.) for the width of the submenus adjust the width parameter in .horizontal li ul li (70px worked well here, but depends on the content)
Do not guess on the left. The reason it is pushed to the right is because the ul has by default some padding.
Setting the padding to 0 and the left to 0 will fix this.
The space on the right is added because you set the width to 100%. If you remove the width it will fit its container. But that might not be what you want because the text will wrap, it might be better to set white-space:nowrap on it.
.horizontal li ul {
opacity:0;
visibility:hidden;
text-align:left;
position:absolute;
top:50px;
padding:0;
left:0; //using the LEFT parameter to get it in to alignment--end up with "blank" space on right?
}
.horizontal li ul li {
position:relative;
background-color:#BBB;
display:block;
white-space:nowrap;
}
Updated demo at https://jsfiddle.net/c6mz3t08/6/

Spread list items evenly with equal padding (preferably without JavaScript)

I'm trying to make an HTML/CSS only <ul> layout in which the <li> items are evenly spread over the available width, and the padding between <a> and <li> elements are all the same. Here's my HTML-code (not very exciting):
<ul>
<li>item 1</li>
<li>item 2 with long name</li>
<li>item 3</li>
</ul>
The result should look like this:
The x of course represents the amount of padding, which is the same for every item in the list.
I tried to use display: table, but this doesn't give the desired result. When using display: table the spacing between the <li> items depends on the length of the text within the <a> element, so x is different for every element.
Since the available width and the amount of <li> items are variable, what is the best way to determine the value of x? I also also want x to have a maximum value of 100px, in this case the width of the <ul> isn't the same as the available width.
I presume this is possible with JavaScript, but since there's already a lot of JavaScript on the page I don't want to use anymore JavaScript than necessary. So I prefer a CSS/HTML only solution.
Here's the JavaScript i'm using:
var containerWidth = $('ul').outerWidth();
var liWidthTotal = 0;
$('li').each(function() {
liWidthTotal += $(this).outerWidth();
});
var padding = Math.round((containerWidth - liWidthTotal) / ($('.topNav > div > ul > li').length*2));
$('.li > a').each(function() {
$(this).css('padding', '0px ' + padding + 'px');
});
I'm basically checking how much space is available to fill (width of ul - width of li) and spread that equally to all A-Tags as padding. Why to the a-tags? So your hover effekts fill the whole width of that element. You can also give it to the LI's of course.
Of course you need to adjust your selectors in the jQuery call.
If you made your LI's inline-block you should also either write them in one line or give them float: left to prevent white-space between them.
In my example your UL needs to fill the whole width (display: block).
See this fiddle, you want to use display:table-cell; on the list items, and display:table; on the list element. On each list item then do padding: 0 100px;
CSS:
body{
margin:0;
padding:0;
width:100%;
}
ul{
margin:0;
padding:0;
width:100%;
display:table;
}
li{
border:1px solid red;
padding:0 100px;
text-align:center;
display:table-cell;
}
html
<ul>
<li>bipin</li>
<li>bipin kumar pal</li>
<li>pal</li>
</ul>
css
ul{
width:100%;
display:table;
}
li{
border:2px solid green;
display:table-cell;
text-align:center;
}
And see this link http://jsfiddle.net/bipin_kumar/t2B4B/5/
I just edited the below fiddles to match your requiremens.Try this fiddle : http://jsfiddle.net/t2B4B/9/
CSS:
body{
margin:0;
padding:0;
width:100%;
}
ul{
margin:0;
padding:0;
width:100%;
display:table;
}
li{
border:1px solid red;
padding-left:20px;
padding-right:20px;
display:table-cell;
}

Applying different style rules to different elements

I have a bunch of unordered list elements that are stacked side by side with each other. To accomplish this, the style rule typically applied is:
#slide ul,li{
float:left;
list-style-type:none;
}
I need to introduce another unordered list of elements that behave the way the ul and li element typically do; that is stacked on top of each other but without any list-style-type, and to achieve this:
.stack ul,li{
list-style-type:none
}
​
The problem is that the styles of stack class for ul,li do not apply and the elements stack next to each other as they are being in the case of ul,li for #slide.
Check it out on this js fiddle:
http://jsfiddle.net/G7JHK/
Are my selectors wrong?
P.S: I have tried this out with class/id and various combination of both but the result is always the same.
Because of the comma in your selector you were applying float left to all li elements. Try something like this:
<ul class="stack">
<li>element 1</li>
<li>element 2</li>
</ul>
<br/>
<ul id="slide">
<li>element 3</li>
<li>element 4</li>
</ul>
#slide li{
display:inline;
}
This css will make all list elements in the div 'slide' display in a row and all other list elements will continue to display like normal. It saves you having to use two different classes :)
​
Your CSS should be like so
ul.stack li{
display:block;
}
ul#slide li{
float:left;
}
​
I think you want something like:
ul.stack li{
display:block;
}
ul#slide li{
float:left;
}
Look at the selectors. You want to select a ul with class stack (ul.stack) and find its child li.
There is problem of your selector. class or id of same element never separated by a white space. They should be with no space and the child are separated by a space but no ',' will not be used there..
So you can try this in your code
ul.stack li{
display:block;
}
ul#slide li{
float:left;
}
Also you have to place the HTML tag name first and then the preceding attribute.
The problem is that you selected the ul that is a descendent of slide, but your ul has an id of slide, so it doesnt work, because there is no ul that has a container with an id of slide. Also by putting ,li you are selecting all list items on the page. You want to have #slide li, which will only select the list items with a container id of slide. You don't need the #slide ul so your final code should be
#slide li {
float:left;
}
http://jsfiddle.net/G7JHK/6/
As an alternative, you could use ul:nth-of-type(2) instead of an id to save some space in the html
http://jsfiddle.net/G7JHK/7/

Styling Lists with CSS

I am working on an un-ordered list. I have searched all over the internet for a good tutorial but I cannot find one. I want to display it inline and have a border line in between each <li> item. Now I am just unsure of the standards for styling lists. Do I use padding/margins to position the <li> items? If so do I apply it to the <ul> container or <li> or <a>?
Try using margins, borders, and float.
<html>
<head>
<style type="text/css">
.liClass
{
float:left;
padding-right:10px;
padding-left:10px;
border-right:thick double #ff0000;
}
.lastLi
{
float:left;
padding-left:10px;
}
</style>
</head>
<body>
<ul>
<li class="liClass">one</li>
<li class="liClass">two</li>
<li class="lastLi">three</li>
</ul>
</body>
</html>
Apply float:left; to <li> and style as needed.
Use display: inline-block to make the list items appear side-by-side (it is more reliable than float: left), then add borders or whatever else you want - maybe a width and some margin.
You can use display: inline-block to avoid having to mess with clearing floats:
http://jsfiddle.net/ZehJN/