Remove padding from unordered list - css

Something has bugged me for years. If you look at this fiddle
you'll see a simple unordered list with some padding on the a element and a background colour to create a box.
There is white space between each item in the list. How can you get rid of it so the boxes are touching horizontally?
Html is:
<div id="dvLinks">
<ul>
<li>One
</li>
<li>Two
</li>
<li>Three
</li>
</ul>
</div>
css is:
#dvLinks ul {
margin: 0;
padding: 0;
list - style - type: none;
}
#dvLinks ul li {
display: inline;
}
#dvLinks ul li a {
text - decoration: none;
padding: .1em 1em;
color: #000;
background-color: # 33EEDD;
}

There are several ways. A few are:
1) Remove thew white space between the list item elements:
<li>One</li><li>Two</li><li>Three</li>
jsFiddle example
2) Put HTML comments between the list item elements
<li>One</li><!--
--><li>Two</li><!--
--><li>Three</li>
jsFiddle example
3) Float them left:
#dvLinks ul li {
display: inline;
float:left;
}
jsFiddle example

It's very simple:
CSS
#dvLinks ul li { display: table-cell; }
RESULTS

inline leaves white-space between elements.
Write elements on same line rather than writing them on different lines.
Change
<li>One</li>
<li>Two</li>
<li>Three</li>
to
<li>One</li><li>Two</li><li>Three</li>
Updated fiddle here.

You have to set the ul font-size to 0 and then you have to set the font-size of the li in what ever your like
#dvLinks ul
{
margin: 0;
padding: 0;
list-style-type: none;
font-size: 0;
}
#dvLinks ul li { display: inline; font-size: 16px; }
#dvLinks ul li a
{
text-decoration: none;
padding: .1em 1em;
color: #000;
background-color: #33EEDD;
}
See the Demo here
This is a common problem with the inline/inline-block.
Another solution is the following:
// All elements in one line
<ul><li>Element #1</li><li>Element #2</li>...</ul>
// Or
// No space between li elements
<ul><li>
Element #1</li><li>
Element #2</li>...
</ul>
// Or
// Comments between li elements
<ul><li>
Element #1</li><!--
--><li>Element #2</li><!--
...-->
</ul>
// Or by using CSS
// Change the li display attribute to
ul li
{
display : table-cell;
}
Anyway, best solution for me is the float left. You can do it like that:
<ul id="list" class="clearfix">
<li>Element #1</li>
<li>Element #2</li>
<li>Element #3</li>
</ul>
and in CSS
/* Clear fix resource : http://www.webtoolkit.info/css-clearfix.html */
.clearfix:after
{
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix
{
display: inline-block;
}
html[xmlns] .clearfix
{
display: block;
}
* html .clearfix
{
height: 1%;
}
#list li
{
display : block;
float : left;
}

You can use negative margins.
#dvLinks ul li {
margin: 0 -1px;
}

You could add
li { margin-left: -5px;}
http://jsfiddle.net/3gmZa/6/

Related

Full width background in Flexbox navigation

I have a nav element with an unordered list and several items. I am using Flexbox to ensure sufficient width between the items and account for Media Queries. However, My background-color property isn't spanning the entire width/height of the navbar. Do I need to set these properties on a different element?
nav {
background-color: #dddddd;
& ul {
display: flex;
width: 100%;
list-style-type: none;
& li {
flex-grow: 1;
flex-shrink: 1 auto;
flex-basis: 100%;
}
& li a {
text-decoration: none;
}
}
}
<nav>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
<ul>
</nav>
1st. Close your ul with </ul>
2nd. Are you talking about the margin at left and right? By default, body have an 8px margin.
If you add this will remove it:
body {
margin: 0;
}
Also, I compiled your SASS to CSS just showing the example here.
The following could be really helpful to understand the default CSS value for each HTML elements.
Default CSS Values for HTML Elements
https://www.w3schools.com/cssref/css_default_values.asp
body {
margin: 0;
}
nav {
background-color: #dddddd;
}
nav ul {
display: flex;
width: 100%;
list-style-type: none;
}
nav ul li {
flex-grow: 1;
flex-shrink: 1 auto;
flex-basis: 100%;
}
nav ul li a {
text-decoration: none;
}
<nav>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
</nav>

How to Clear float for ul

I have a similar problem found here - Using :after to clear floating elements
and its demo solution:
http://jsfiddle.net/EyNnk/1/
However it still does not solve my situation:
What I m trying to do is to pass background of ul to li, by using float ul as well.
As a result I have no way to clear float except to add a div outside ul to clear the float. Is there a better way?
HTML
Text Before
<ul class="wrapper">
<li>test1</li>
<li>test2</li>
<li>test3</li>
<li style="background:#555">test4</li>
</ul>
Text After
Here is an updated problem:
http://jsfiddle.net/EyNnk/456/
What I m trying to get is that:
"Text Before" should be before the ul
and "Text After" should be after the ul
Thanks all for the solution, the best goes for connexo:
ul: display:table-cell for above/below
ul: display:inline-block for before/after
no need to float, so that there is no need to clear float, the less the better
Instead of float: left;, use display: inline-block; on your ul.wrapper.
https://jsfiddle.net/EyNnk/460/
For new lines between elements, use display: table-cell; for your ul.wrapper. Instead of making your li { float: left; }, use display: inline-block;. To avoid unwanted whitespace issues, set the parent's font-size: 0; and reset to the font-size you need on the li.
https://jsfiddle.net/EyNnk/464/
See Brett DeWoody's comment. In case you wish you place <ul> element inline between these texts, you could place them in its container. For example, let it sit in the span like this:
<p>Text Before</p>
<ul class="wrapper">
<li>test1</li>
<li>test2</li>
<li>test3</li>
<li style="background:#555">test4</li>
</ul>
<p>Text After</p>
CSS:
.wrapper {
list-style-type: none;
display: inline-block;
background: #888;
}
.wrapper li {
float: left;
padding: 10px;
}
.wrapper:after {
content: '';
display: block;
clear: both;
}
See fiddle: http://jsfiddle.net/EyNnk/462/
Remove float:left; from ul , Li
and add display:inline-block;
If you want the before and after text to be to the left/right of the <ul> add display:inline-block to .wrapper.
.wrapper {
list-style-type: none;
background: #888;
display:inline-block;
}
Here's a demo.
If you want the before and after text to be on the top/bottom of the <ul> adddisplay:blockto the.wrapper`.
.wrapper {
list-style-type: none;
background: #888;
display:block;
}
Here's a demo.
Try this
div{
background: #888;
}
.wrapper {
list-style-type: none;
background: #888;
display:inline-block;
padding:0;
margin:0;
}
.wrapper li {
float: left;
padding: 10px;
}
.wrapper:after {
content: '';
display: block;
clear: both;
}
<div>
Text Before
<ul class="wrapper">
<li>test1</li>
<li>test2</li>
<li>test3</li>
<li style="background:#555">test4</li>
</ul>
Text After
</div>

Stop text wrapping around css genareated content?

Im using CSS generated content on a list. When the list text is long enough to wrap it moves too far to the left. I want the text to stay inline with itself when it wraps.
<ul>
<li>List item text</li>
<li>List item with longer text</li>
</ul>
li {
list-style: none;
margin-bottom: 20px;
}
li:before {
content: "+";
color: red;
margin-right: 10px;
}
ul {
width: 120px;
}
http://codepen.io/anon/pen/uFchG
You could slightly change your CSS so that it's a position:aboluste; and the li (parent) is a position:relative; then move it slightly out.
CodePen Example
li {
list-style: none;
margin-bottom: 20px;
position:relative;
}
li:before {
content: "+";
position:absolute;
color: red;
left:-15px;
}
ul {
width: 120px;
}
Alternatively, you could give the li a margin-left and the :after left:-npx; so it's all still within the li element's boundaries.
Try to add these two lines
li {
list-style: none;
margin-bottom: 20px;
padding-left: 1em;
text-indent: -1em;
}
add this in ul:
text-align:center;

CSS unable to get rid of UL's first and last child's dividers

I have a nav that holds a ul with several ils. The problem is that im unable to get rid of the ul's first and last child's divider.
here's a demo: http://jsfiddle.net/Rvs3C/1/
i've added this code to my CSS file but seems like it makes no difference:
nav ul a li:first-child{
content: none;
}
nav ul a li:last-child{
content: none;
}
You markup is wrong. The corret way is <a> tag being the children of tag <li>.
The way you made ​​the correct selector would:
nav ul a:first-child li{
content: none;
}
nav ul a:last-child li{
content: none;
}
But remembering, this is not the right way to do.
The correct is change you html to:
<ul class="showing">
<li>home</li>
...
As mentioned elsewhere your HTML structure is off, li must me a direct child of ul, no other element as a child is valid. With this said, you also need to change your selectors per the below.
Updated Fiddle
HTML
<nav>
<ul class="showing">
<li>home
</li>
<li>about me
</li>
<li>content
</li>
<li>browse
</li>
<li>rate
</li>
<li class="nav_arr"><img src="http://www.irvinewelsh.net/images/arrow-left.png" />
</li>
</ul>
</nav>
CSS
nav {
margin-right: 150px;
}
nav ul {
background-color: #494A4C;
overflow: hidden;
text-align: right;
}
nav li img {
margin-left: 20px;
vertical-align: top;
}
nav ul li a {
color: white;
text-decoration: none;
font-family:arial;
font-size: 14px;
padding: 0px;
font-weight: bold;
}
nav ul li:first-child:before {
content: none;
}
nav ul li:last-child:before {
content: none;
}
nav li:before {
content:"|";
padding: 5px;
font-weight: bold;
color:lightblue;
font-size: 20px;
}
nav ul li {
display: inline-block;
}

Make inline-block element take up no vertical space

I have an evenly distributed menu like :
HTML
<nav>
<ul>
<li>Home
</li>
<li>About
</li>
<li>Contact
</li>
<li>Blog
</li>
</ul>
</nav>
CSS
nav ul {
padding:0;
margin:0;
text-align: justify;
text-transform: uppercase;
background-color: #000;
}
nav ul:after {
content:'';
width: 100%;
display: inline-block;
height: 0;
line-height: 0;
font-size: 0px;
}
nav ul li {
display: inline-block;
}
nav ul li a {
color: #fff;
}
This works great to spread the menu items accross the whole width of the bar as can be seen in this fiddle: http://jsfiddle.net/SjDEX/.
However, you can also see that a result of the ::after element the height of the ul is increased making extra space below the menu items.
Is there a way to get rid of this by making the after element not take up vertical space?
Setting its height to 0 does nothing and changing its display to block or something else breaks the layout.
It is the ul itself that has that height, not the :after, so just add
nav ul {
height: 20px;
}
Fiddle
And this code can be reduced to this:
nav ul:after {
content:'';
width: 100%;
display: inline-block;
}

Resources