Long paragraph breaks list layout - html

I have an unordered list with list-style-type:none to which I manually added bullets using :before.
The problem here is that if a paragraph inside one of the <li>s gets long enough to wrap around, the width of the paragraph itself will be too long to fit behind the bullet and so it gets pushed to a new line:
ul {
list-style-type: none;
padding: 0;
}
li {
padding:0 0 0 .5em;
}
li:before {
content:"- ";
position:relative;
vertical-align: top;
}
li > p {
display:inline-block;
margin:0;
/*max-width:calc(100% - 2em);*/
}
<ul>
<li><p>Looks normal</p></li>
<li><p>Looks normal with a<br>line break too.</p></li>
<li><p>If a line is long enough, it wraps (as it should) and the layout is somehow destroyed. Just adding some more text to make sure the text wraps.</p></li>
</ul>
http://jsfiddle.net/1xaamone/
As you can see in my example, I already have a workaround, but I wonder if there's a solution that works without calc() to improve browser compatibility even further.

Set white-space: nowrap on the parent li element and then reset that by setting white-space: normal on the child p element:
Updated Example
ul {
list-style-type: none;
padding: 0;
}
li {
padding: 0 .5em;
white-space: nowrap;
}
li::before {
content: "- ";
position: relative;
vertical-align: top;
}
li > p {
display: inline-block;
margin: 0;
white-space: normal;
}
<ul>
<li><p>Looks normal</p></li>
<li><p>Looks normal with a<br>line break too.</p></li>
<li><p>If a line is long enough, it wraps (as it should) and the layout is somehow destroyed. Just adding some more text to make sure the text wraps.</p></li>
</ul>
Alternatively, you could also absolutely position the pseudo element relative to the parent li element and then displace it with padding.
Updated Example
ul {
list-style-type: none;
padding: 0;
}
li {
padding: 0 .5em;
position: relative;
}
li::before {
content: "- ";
position: absolute;
top: 0;
left: 0;
}
li > p {
display: inline-block;
margin: 0;
}
<ul>
<li><p>Looks normal</p></li>
<li><p>Looks normal with a<br>line break too.</p></li>
<li><p>If a line is long enough, it wraps (as it should) and the layout is somehow destroyed. Just adding some more text to make sure the text wraps.</p></li>
</ul>

Simply removing the <p> tags also works.
Example
<ul>
<li>Looks normal</li>
<li>Looks normal with a<br>line break too.</li>
<li>If a line is long enough, it wraps (as it should) and the layout is somehow destroyed. Just adding some more text to make sure the text wraps.</li>
ul {
list-style-type: none;
padding: 0;
}
li {
padding:0 0 0 .5em;
}
li:before {
content:"- ";
position:relative;
vertical-align: top;
}
li > p {
display:inline-block;
margin:0;
/*max-width:calc(100% - 2em);*/
}

Related

I am having alingment issue in navigation bar css

In first image was taken from IE, its having full width for every content, but if u see in second image last menu content, not taking full width. how to solve this in both browser
HTML:
<div class="menu-section clearfix">
<div class="menu-element clearfix">
<ul>
<li class="active">Home</li>
<li>about us</li>
<li>administration</li>
<li>academics</li>
<li>research</li>
<li>activities</li>
<li>examination</li>
<li>facilites</li>
<li>contact us</li>
</ul>
</div>
</div>
CSS:
.menu-section {
background-color:#900000;
height: 56px;
}
.menu-element {
background-color: #400;
height: 50px;
}
.menu-element li {
float:left;
}
.menu-element li:hover {
background-color:#900000;
}
.menu-element li.active {
background-color:#900000;
}
.menu-element li a {
color:#fff;
text-transform:uppercase;
display: block;
padding: 18px 21px;
text-decoration:none;
font-weight: bold;
}
You need to add style to the ul as well:
.menu-element > ul {
list-style: none;
margin: 0; padding: 0;
}
Maintaining consistency across browsers is bit difficult, but you could ensure same rendering by two methods.
Specify a valid doctype on your html to ensure standards mode, and
Specify a box-sizing typically border-box in your stylesheet.
-
* {
box-sizing: border-box;
}
If you want to justify the menu options across the width, then you will have to make a few adjustments and a hack.
Apply a fixed width to the wrapping div, text-align:justify on the ul and display:inline-block on li are required.
Note 1: The display: inline-block is required, however it generates html white-spaces. In order to get rid of those white-spaces, html comments can be used in the markup of list items.
Note 2: The :after pseudo element in the hack is what seems to do the trick. However, that will create an unintended space below the ul. This space seems to be there because the elements are flushed across. If not justified, then this space does not appear.
.menu-element {
width: 100%; /* fixed width required on wrapping container */
}
.menu-element > ul {
list-style-type: none; /* getting rid of bullets */
margin: 0px; padding: 0px; /* getting rid of default indents */
text-align: justify; /* important to justify contents */
}
.menu-element li {
display: inline-block; /* required. float won't work. */
text-align: left; /* to properly align list items */
white-space: no-wrap; /* to prevent wrapping of list items if required */
}
.menu-element > ul:after {
/* this is the hack without which the list items won't get justified */
content:''; display: inline-block; width: 100%; height: 0;
}
Demo: http://jsfiddle.net/abhitalks/mv7qnfLe/4/
Full Screen Demo: http://jsfiddle.net/abhitalks/mv7qnfLe/4/embedded/result/
.
Try this:-
.menu-element ul {
padding: 0;
}
Try This
Give some width to ul element and add this style rule in your css:
.menu-element ul {
clear: both;
list-style:none;
margin: 0 auto;
text-align: center;
width: 92%;
}
I hope it works for you.

HTML List classes

So for my Tafe work, one requirment is to have an unordered list.
I have a menu, but it clashes with the list I'm attempting to make.
Here's the fiddle: http://jsfiddle.net/tHLY7/1/
If you remove:
li {
display: inline;
}
It shows the list how I want but ruins my menu.
Any idea?
You need to tell the display:inline to be on the nav only.
#Menubar ul li { display: inline; }
your styling li { display: inline } will apply to ALL <li> on the page, no matter where they are. I would suggest targeting only the <li> that are part of your menu. In your case,
#menu li { display: inline; }.
Or maybe,
#Menubar li { display: inline }.
(one word of note though, ID's and classes in HTML are by convention, all lowercase, so you should change <div id="Menubar"> to <div id="menubar">.
I've made some improvement overall: http://jsfiddle.net/oneeezy/tHLY7/4/
Here are a few tips
1.) You should never use "#ID" for styling purposes, just use #ID for javascript hooks, always use ".class" for styling and like someone else said, keep it lowercase.
2.) Always use a "reset.css" file. I've attached the best reset file I know that exists from HTML5 boilerplates website. You can take care of a lot of your "base" styles in that file. Use a stylesheet.css file after your reset.css file
3.) Like someone else said, if you have multiple elements on a page (in this case, ul's) then you must target that specific ul through a class name and tell it specifically what you want it to do.. otherwise it will take the style from the reset.css file.
4.) 2 very important styles have been added!
Clear Fix (I'm calling this ".row", This is the best way to make things drop to the next line (like hitting the "return" key in microsoft word)
Box sizing is you're best friend! It makes "padding" act correctly and doesn't add space to your elements that have it. I gave it the "*" to apply on everything.
/* Box sizing is you're best friend! It makes "padding" act correctly and doesn't add space to your elements that have it. */
*, *:after, *:before { margin:0; padding:0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
/* Clear Fix - This is the best way to make things drop to the next line (like hitting the "return" key in microsoft word ) */
.row:before, .row:after { content: " "; display: table; }
.row:after { clear: both; }
.row { *zoom: 1; clear: both; }
/* This "wrapper" goes around everything and makes your content stay in the middle of the page */
.wrapper { width: 90%; margin: 0 auto; }
/* Navigation */
.menu { background: #000; width: 100%; float: left; display: block; }
.menu ul { color: #fff; float: right; }
.menu ul li { float: left; display: block; }
.menu ul li a { display: block; color: #fff; padding: .25em 1em; border-left: 1px solid #fff; }
.menu ul li a.active { background: #333333; display: block; color: #fff; padding: .25em 1em; border-left: 1px solid #fff; }
.menu ul li a:hover { background: #333333; color: #fff; }
/* Main Content */
.main { padding: .5em 0; }
.main h1 { margin: .5em 0; }
.main ul { }
.main ul li { list-style: inside; }
I hope this helps!

Keeping list inline with text while keeping list vertically positioned

Right now this displays as: text 1 2 3 4 with the list appearing horizontally inline with the text. Is there a way to make the list display vertically while still remaining inline with the text? (instead of text 1 2 3 4, the 2 should appear below the 1, 3 below the two, etc. but the list will still appear to the right of "text" and in the same line as "text")
Any help is appreciated. Thanks!
HTML
<div id = "text">text</div>
<ul id="ulist">
<li id="contents">1</li>
<li id="contents">2</li>
<li id="contents">3</li>
<li id="contents">4</li>
</ul>
CSS
#ulist {
display: inline-block;
margin: 0;
padding: 0;
}
#contents {
display: inline-block;
}
#text {
display: inline;
}
#text2 {
display: inline;
}
Sure, get rid of your rules and just float the div to the left:
#text {
float:left;
}
Note that IDs must be unique.
jsFiddle example
Add display:block; to your li elements, modify #text to display:inline-block and vertical-align: top, and that should cause them to stack.
#ulist {
display: inline-block;
margin: 0;
padding: 0;
}
#ulist li {
display: block;
}
#contents {
display: inline-block;
}
#text {
display:inline-block;
vertical-align:top;
}
#text2 {
display: inline;
}

Vertically align smaller bullets with larger text

I have a list with bullets. I made the bullets smaller by putting the li text inside a span and making the font-size of the li smaller than that of the span. The problem is that now the bullets are not vertically aligned in relation to the text. How do I fix that?
jsFiddle: http://jsfiddle.net/tXzcA/
li {
font-size: 15px;
}
li span {
font-size: 25px;
}
<ul>
<li><span>text1</span></li>
<li><span>text2</span></li>
<li><span>text3</span></li>
<li><span>text4</span></li>
</ul>
You could just make your own bullet point and make it whatever size you want.
li{
font-size: 15px;
list-style-type:none;
}
li span{
font-size: 25px;
}
ul li:before {
content: "•";
font-size: 80%;
padding-right: 10px;
}
Just change around the font-size to the size you want.
jsFiddle
Try this:
li span{
font-size: 25px;
vertical-align:middle;
padding-bottom:5px;
}
See it here: http://jsfiddle.net/tXzcA/19/
This is what I used, it centers on both the bullet & the content
Jsfiddle: http://jsfiddle.net/VR2hP/14/
CSS:
ul {
padding-left: 5em;
list-style: none;
}
li.twitter::before,
li.fb::before {
font-family: 'FontAwesome';
margin: 0 .2em 0 -1.5em;
font-size: 3em;
vertical-align: middle;
}
li.twitter::before {
content: '\f081';
}
li.fb::before {
content: '\f082';
}
li {
list-style-type: none;
}
li span {
display: inline-block;
vertical-align: middle;
}
Use an unordered list and display the list items as tables.
Take a look at this example: https://jsfiddle.net/luenib/jw1ua38v/
The icon, number, or whatever you want to place is going to be located inside a span. The content of the span is centered horizontally and vertically, very useful if you don't want to display your icon on the top. The text inside the paragraph will keep a space to its left, so it won't go under the icon in case the text extends in more than one line.
CSS:
ul {
padding-left: 0;
}
li {
display: table;
}
li span {
width: 40px;
display: table-cell;
text-align: center;
vertical-align: middle;
}
HTML:
<ul>
<li><span>1</span>
<p>Some text here. Some text here. Some text here. Some text here. Some text here. Some text here.</p>
</li>
<li><span>2</span>
<p>Some text here. Some text here. Some text here.</p>
</li>
</ul>

"a" element inside "li" element overflows the "li" element

I am trying to create a very simple "no-frills" tab using html & css. For this, I have a bunch of li elements and inside each of these, there is a "a href" element. Now, when i look at the output in IE & Firefox (after setting the styles to make the list display horizontally with proper border and everything), I can see that the "a" element overflows the "li" element. How do i make the "li" element resize based on the "a" element?
CSS and html as follows
#tabs ul
{
list-style:none;
padding: 0;
margin: 0;
}
#tabs li
{
display: inline;
border: solid;
border-width: 1px 1px 1px 1px;
margin: 0 0.5em 0 0;
background-color: #3C7FAF;
}
#tabs li a
{
padding: 0 1em;
text-decoration: none;
color:White;
font-family: Calibri;
font-size: 18pt;
height: 40px;
}
<div id="tabs">
<ul>
<li><span>One</span></li>
<li><span>Two</span></li>
<li><span>Three</span></li>
</ul>
</div>
You forgot the "#" in the CSS declarations. You've an id="tabs" in you html code which needs to be referenced as
#tabs {
....
}
in the CSS. The rest is fine-tuning ;)
And try
#tabs {
display: inline-block;
}
instead of the display: inline;
Try settings the the display on the li element as "inline-block".
http://www.quirksmode.org/css/display.html
give style to anchor as
display:block
I give
display:block
to both the li and a tags. Then float the li. You can add this code to make the li enclose the a completely:
overflow: hidden; zoom: 1; word-wrap: break-word;
This will clear anything inside.
You could also simply give your li's some padding:
#tabs li {
padding: 8px 0 0;
}
Inline-block is a good way to go (as suggested).
But if you want this to be cross-browser, you need to add som CSS-hacking "magic" :)
One very good tutorial on the subject is http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/
Using the method from that article, you'd end up with the following CSS:
/* cross browser inline-block hack for tabs */
/* adapted from:
/* http://blog.mozilla.com/webdev/2009/02/20/cross-browser-inline-block/ */
#tabs ul,
#tabs li,
#tabs li a {
display: -moz-inline-stack;
display: inline-block;
zoom: 1;
*display: inline;
vertical-align: bottom;
margin:0; padding:0; /* reset ul and li default settings */
}
/* The rest is "window dressing" (i.e. basically the tab styles from your CSS) */
#tabs li a {
margin: 0 0.5em 0 0;
background-color: #3C7FAF;
padding: 0 1em;
text-decoration: none;
color:white;
font-family: Calibri;
font-size: 18pt;
height: 40px;
}
Simply display:inline-block on both li & a did the trick for me man. Lists stretched to accommodate whatever I did with the links.