I'm working with Bootstrap. I'm trying to add an image as number icon to my list. It is kind of a bad practice to add <div> tag inside <ul> tag, so is it possible to add icon with ::before?
Here's my code:
<ul class="lizt" id="sub-lizt">
<li class="item">1. Bunch of geebrish</li>
<li class="item">2. Bunch of geebrish</li >
</ul>
ul {
list-style-image: url('/your_img_path.jpg');
}
You can use this in your CSS. For in-depth just take a look Here.
Putting a div inside an li tag is not a bad practice. It would be wrong to put a div directly inside a list ol/ul, but not inside a list item.
That being said, you don't need a div to put a numbered image as bullet for your list, because, in fact, list-style property allows using an image as bullet.
.item {
list-style: url('http://via.placeholder.com/32x32/000/fff?text=1');
}
.item:nth-child(2) {
list-style: url('http://via.placeholder.com/32x32/000/fff?text=2');
}
.item:nth-child(3) {
list-style: url('http://via.placeholder.com/32x32/000/fff?text=3');
}
<ol class="lizt" id="sub-lizt">
<li class="item">Bunch of geebrish</li>
<li class="item">Bunch of geebrish</li >
<li class="item">Bunch of geebrish</li >
</ol>
You'd probably want your images vertically aligned in the middle with your text. In that case, you have many options, like moving up your a elements from its relative position.
.item a {
display: inline-block;
position: relative;
top: -0.75em;
}
Assume the following list:
<div class="menublock">
<ul class="menu">
<li>Home</li>
<li>Google</li>
<li>Porn Hub</li>
<li>Toronto Star</li>
<li>Stack Overflow</li>
<li>Example Site</li>
<li>York University</li>
</ul>
</div>
with the following CSS
.menublock .menu {
margin-top: 35px;}
.menublock .menu li {
display: inline;
list-style-type: none;
padding-right: 20px;
}
.menublock .menu li a {
font-size: 16pt;
}
How do I stop the list from wrapping in the middle of a list item?
That is, I want it to line break between "Toronto Star" and "Stack Overflow" (if necessary), but not between "Stack" and "Overflow"
Line Breaks should be responsive to actual screen width, not force using child-after selectors or other such fixed points.
Make the a tag display as inline-block
.menublock .menu li a {
font-size: 16pt;
display:inline-block;
}
Instead of having every list item display as "inline", have them display as "block" and have them float "left".
This will prevent breaking of text inside of each item.
I tend to make horizontal navbars this way because it eliminates issues like these as well as awkward spacing issues that result from inline elements when they are next to each other.
Edit: floating an element automatically makes it display as "block", so just eliminate "display: inline" and have your list items float left.
.menublock .menu li a {
font-size: 16pt;
white-space:nowrap;
}
I have made a horizontal navigation bar using styles, but I've encountered a major issue... Since <li> is a block element, I can't align it using text-align:right, which makes me unable to align it properly. I've tried using the display:inline; syntax for the list-item element, but that doesn't make any difference either (which makes sense actually).
My question being, is there any way of aligning horizontal <li>, without having to use float:right;? I want it to fit the current list's format (which I've adjusted to fit a parent div), and using float isn't really a good or safe method. Here's a screenshot of what I got so far (layout is slightly messed up due to recent addition of image). As you can see, I have managed to get the "My page" and "Log out" properly placed, but as soon as I add something more "complex" (like the "+", which now is placed in the normal list), it gets screwed up... I really don't get how other websites manages to get this right.
You must define text-align: right for the containing element
HTML:
<ul class="nav">
<li class="menu">1</li>
<li class="menu">2</li>
<li class="menu">3</li>
<li class="menu">4</li>
<li class="menu">5</li>
</ul>
CSS:
.nav {
text-align: right;
}
.menu {
display: inline;
}
JSFiddle
You can split the menu to a left and right part, if you like. Add or remove padding and margin as needed
HTML:
<ul class="nav left-nav">
<li class="menu">1</li>
<li class="menu">2</li>
<li class="menu">3</li>
</ul>
<ul class="nav right-nav">
<li class="menu">4</li>
<li class="menu">5</li>
</ul>
CSS:
.nav {
margin: 0;
padding: 0;
}
.left-nav {
text-align: left;
}
.right-nav {
text-align: right;
}
.menu {
display: inline;
}
JSFiddle
Here you go i think this is what you are looking for:
jsfiddle.net/Sdw5h/
i know this might seem straightforward, but i can't solve it, im trying to make the whole list item linkable, rather than just the word home
the html code:
<li class="current">Home</li>
p.s. when you hover the li, background color changes, so i want that whole list item to be hyperlinked, if you get what i mean, not just the word
Wrapping a block level element (the li) within an inline-element (a), is invalid mark-up, which has the potential to throw errors, depending on your doctype. That being the case, first change your mark-up around to:
<li>link text</li>
Assuming that your li is display: block then the following will cause the a to 'fill' the available space:
a {
display: block;
width: 100%; /* this may, or may not, be necessary */
}
If you're using this for a horizontal list, and the li are display: inline, then you can use the following:
a {
display: inline-block;
}
And style-to-taste.
Do it in reverse
<li class="current">Home</li>
not sure what your other styles are but you can change the tag with something like
li.current a{
display:block;
}
This should do it:
HTML:
<li class="current">Home</li>
CSS:
li a {
display: block;
}
// or
li.current a {
display: block;
}
I have an (XHTML Strict) page where I float an image alongside regular paragraphs of text. All goes well, except when a list is used instead of paragraphs. The bullets of the list overlap the floated image.
Changing the margin of the list or the list items does not help. The margin is calculated from the left of the page, but the float pushes the list items to the right inside the li itself. So the margin only helps if I make it wider than the image.
Floating the list next to the image also works, but I don't know when the list is next to a float. I don't want to float every list in my content just to fix this. Also, floating left messes up the layout when an image is floated to the right instead of left of the list.
Setting li { list-style-position: inside } does move the bullets along with the content, but it also causes lines that wrap to start aligned with the bullet, instead of aligned with the line above.
The problem is obviously caused by the bullet being rendered outside the box, the float pushing the contents of the box to the right (not the box itself). This is how IE and FF handle the situation, and as far as I know, not wrong according to the spec. The question is, how can I prevent it?
I have found a solution to this problem. Applying an ul { overflow: hidden; } to the ul ensures that the box itself is pushed aside by the float, instead of the contents of the box.
Only IE6 needs an ul { zoom: 1; } in our conditional comments to make sure the ul has layout.
Adding an improvement to Glen E. Ivey's solution:
ul {
list-style: outside disc;
margin-left: 1em;
}
ul li {
position: relative;
left: 1em;
padding-right: 1em;
}
http://jsfiddle.net/mblase75/TJELt/
I prefer this technique, since it works when the list needs to flow around the floating image, while the overflow: hidden technique will not. However, it's also necessary to add padding-right: 1em to the li to keep them from overflowing their container.
This is where the "display" property comes into its own. Set the CSS below to make the list work alongside the floated content.
display: table; works alongside floated content (filling the gap) but without hiding content behind it. Much like a table does :-)
.img {
float: left;
}
.table {
display: table;
}
<img class="img" src="https://via.placeholder.com/350x350" alt="">
<ul>
<li>Test content</li>
<li>Test content</li>
<li>Test content</li>
</ul>
<ul class="table">
<li>Test content</li>
<li>Test content</li>
<li>Test content</li>
</ul>
EDIT: Remember to add a class to isolate which lists you wish to do this for. E.g. "ul.in-content" or more generally ".content ul"
Try list-style-position: inside to change the layout of the bullets.
Why overflow: hidden works
The solution is as easy as:
ul {overflow: hidden;}
A block box with overflow: other than visible establishes a new block formatting context for its contents. W3C recommendation: http://www.w3.org/TR/CSS2/visuren.html#block-formatting
Example
The buttons on my website, which are <li> in disguise, are made like this. Make the viewport (window) of your browser smaller to see the indenting in action.
Related answers
https://stackoverflow.com/a/710264/2192488
https://stackoverflow.com/a/16041390/2192488
Article with examples
Overflow – a secret benefit
At http://archivist.incutio.com/viewlist/css-discuss/106382 I found a suggestion that worked for me: style the 'li' elements with:
position: relative;
left: 1em;
Where you replace "1em" with the width of the left padding/margin that your list items would have if the float weren't present. This works great in my application, even handling the case where the bottom of the float occurs in the middle of the lists--the bullets shift back over to the (local) left margin just right.
By adding overflow: auto; to your ul works for me at least.
Update
I've updated my jsfiddle to visualize what's going on. When having the ul beside the floating img, the content of the ul will be pushed by the float, but not the actual container. By adding overflow: auto the whole ul-box will be pushed by the float instead of only the content.
You could assign position: relative; left: 10px; to the li. (You may additionally want to give it a margin-right: 10px;, otherwise it might become too wide on the right side.)
Or, if you want to use float for the ul -- as suggested by others -- you can probably stop the rest from floating right of the ul by using clear: left on the element that follows the ul.
Disclaimer
Lists next to floated elements cause issues. In my opinion, the best way to prevent these sorts of floating issues is to avoid floating images that intersect with content. It'll also help when you have to support responsive design.
A simple design of having centered images between paragraphs will look very attractive and be much easier to support than trying to get too fancy. It's also one step away from a <figure>.
But I really want floated images!
Ok, so if you're crazy persistent enough to continue down this path, there are a couple techniques that can be used.
The simplest is to make the list use overflow: hidden or overflow: scroll so that the list is essentially shrink wrapped which pulls the padding back to where it's useful:
img {
float: left;
}
.wrapping-list {
overflow: hidden;
padding-left: 40px;
}
<img src="http://placehold.it/100x100"/>
<ul class="wrapping-list">
<li>lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
<li>amet</li>
</ul>
This technique has a few problems though. If the list gets long, it doesn't actually wrap around the image, which pretty much defeats the entire purpose of using float on the image.
img {
float: left;
}
.wrapping-list {
overflow: hidden;
padding-left: 40px;
}
<img src="http://placehold.it/100x100"/>
<ul class="wrapping-list">
<li>lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
<li>amet</li>
<li>lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
<li>amet</li>
<li>lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
<li>amet</li>
</ul>
But I really want wrapping lists!
Ok, so if you're even crazier more persistent and you absolutely must continue down this path, there's another technique that can be used to wrap the list items and maintain bullets.
Instead of padding the <ul> and trying to get it to behave nicely with bullets (which it never seems to want to do), take those bullets away from the <ul> and give them to the <li>s. Bullets are dangerous, and the <ul> just isn't responsible enough to handle them properly.
img {
float: left;
}
.wrapping-list {
padding: 0;
list-style-position: inside;
}
.wrapping-list li {
overflow: hidden;
padding-left: 25px;
}
<img src="http://placehold.it/100x100"/>
<ul class="wrapping-list">
<li>lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
<li>amet</li>
<li>lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
<li>amet</li>
<li>lorem</li>
<li>ipsum</li>
<li>dolor</li>
<li>sit</li>
<li>amet</li>
</ul>
This wrapping behavior can do weird things to complex content, so I don't recommend adding it by default. It's much easier to set it up as something that can be opted into rather than something that has to be overridden.
I am using this to solve this problem:
ul {
display: table;
}
Try the following on your UL tag. This should take care of the bullets overlaying your image and you don't have to mess up your left allignment caused by list-position: inside.
overflow: hidden;
padding-left: 2em;
Struggled with this myself. Best I've managed is the following:
ul {
list-style-position: inside;
padding-left: 1em;
text-indent: -1em;
}
The text is not actually indented but the bullet shows.
After fighting with this interesting issue in several projects, and investigating why it happens, I finally believe I found both: a working and 'responsive' solution.
Here is the magic trick, live example: http://jsfiddle.net/superKalo/phabbtnx/
ul {
list-style: none;
position: relative;
padding-left: 0; /* remove any left padding */
margin-left: 0; /* remove any left margin */
left: 35px;
}
li {
padding-left: 0; /* remove any left padding */
margin-left: 0; /* remove any left margin */
text-indent: -19px; /* adjust as much as needed */
}
li:before {
content: '•\00a0\00a0\00a0';
color: #000; /* bonus: you can customize the bullet color */
}
Edited to update based on OP's comment
ok, then just break it up into 2 divs nested together
ul {background: blue; position:static;}
.therest {position:relative; width:100%}
.indent {float:left; }
<div class="therest">
<p>
Est tincidunt doming iis nobis nibh. Ullamcorper eorum elit lius me delenit.
</p>
<hr />
<h3>Lorem</h3>
<div class="indent">
<ul>
<li>list element</li>
<li>list element</li>
<li>list element</li>
</ul>
<div>
the rest now under the UL
</div>
try changing the ul li css to
ul {float:left; background: blue; }
Working inside an LMS without access to head of doc, found it easier to go with margin-right: 20px as an inline style for the image. Which I owe to this site.
try this:
li{
margin-left:5px;
}
If you want them to go left, just put in a -##px value.
or you could do this:
#content ul {
background:none repeat scroll 0 0 #AACCDD;
float:left;
margin-right:10px;
padding:10px;
and then remove all the styling from the li
How about this?
ul{float:left; clear:right}
width: 300px;
height: 30px;
margin-left: auto;
right: 10px;
margin-left: auto will cause the element itself to be right aligned.
Set height and width of the element you want - in my it's a background image in a div inside
You could try also floating the ul to the left, and define an appropriate width for it, so that it floats next to the image.
Something a little like this jsfiddle?
I fixed it with
div.class-name ul {
clear: both;
}
width: auto; overflow: hidden;
Add display:table; to ul:
ul{display:table;}