Major Edit: Turns out my issue was not repeated exactly the same here. My issue appears when using bootstrap, and is not fixed when using margin:0px;. See the updated code for the persisting issue.
I am building an ordered list of text, and noticed a slight issue. When I nest other elements inside of a li element, I get a good amount of extra white space between the individual elements that is not controlled by margin or padding. Throwing a border onto these elements reveals that each of the nested elements does not control that white space (seemingly).
My question is how do I control the amount of extra white space?
Demo of this issue:
#questionList li {
font-size: 3em;
margin-left: 3em;
}
#questionList li h2 {
border: 1px solid red;
font-size: 45px;
margin:0px;
}
#questionList li small {
border: 1px solid red;
font-size: 14.72px;
margin:0px;
}
#questionList li p {
border: 1px solid red;
font-size: 16px;
margin:0px;
}
<link href="http://bootswatch.com/paper/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<ol id="questionList" class="col-sm-8 col-sm-offset-2">
<li>
<h2>Qg</h2>
<small>sub header</small>
<p>P1</p>
</li>
<li></li>
<li></li>
</ol>
</div>
</div>
Paragraphs and headers do indeed have default margin, as demonstrated below, if we set the margin for each equal to zero.
ol li h2 {
border: 1px solid red;
}
ol li small {
border: 1px solid red;
}
ol li p {
border: 1px solid red;
}
h2,
p {
margin: 0;
}
<ol>
<li>
<h2>
Header
</h2>
<small>
Sub header
</small>
<p>
paragraph of text
</p>
</li>
<li>
<h2>
Header2
</h2>
<small>
Sub header2
</small>
<p>
paragraph of text2
</p>
</li>
</ol>
This is indeed controlled by the margin on the individual elements.
Fiddle for example here: https://jsfiddle.net/25f3bcwr/
ol li h2 {
border: 1px solid red;
margin:0px;
}
ol li small {
border: 1px solid red;
margin:0px;
}
ol li p {
border: 1px solid red;
margin:0px;
}
This is because headers if not defined, by default they posses some default characteristics e.g their size, spacing, font-weight.
I believe, those are not white spaces White spaces are created using .
Try to use display:inline-block; or display:inline; and then ad break line in the css style and then add break line code at the end of your sentence<br> see some difference
ol li h2 {
border: 1px solid red;
display: inline;
}
ol li small {
border: 1px solid red;
display: inline;
}
ol li p {
border: 1px solid red;
display: inline;
}
<ol>
<li>
<h2>
Header
</h2><br>
<small>
Sub header
</small><br>
<p>
paragraph of text
</p>
</li>
<li>
<h2>
Header2
</h2><br>
<small>
Sub header2
</small><br>
<p>
paragraph of text2
</p>
</li>
</ol>
Result
Related
I'm building a list widget where I have multiple <li> with two sibling elements wrapped inside.
A <span> for the title and a <div> for the bottom line. That line must vary depending the width of their adjacent span element.
.limit-area {
width: 250px;
height: 350px;
background-color: #96ceb4;
}
.limit-area ul {
font-size: 0;
list-style-type: none;
-webkit-padding-start: 0;
}
.limit-area ul li {
background-color: #ffeead;
display: table;
margin-bottom: 20px;
}
.limit-area ul li .wrapper span {
font-size: 24px;
color: #010101;
}
.limit-area ul li .wrapper .line {
height: 6px;
margin-top: 12px;
background-color: #ff6f69;
}
<div class="limit-area">
<ul>
<li>
<div class="wrapper">
<span>One line only</span>
<div class="line"></div>
</div>
</li>
<li>
<div class="wrapper">
<span>Bigger one line only </span>
<div class="line"></div>
</div>
</li>
<li>
<div class="wrapper">
<span>Long line and go crazyyyyyy</span>
<div class="line"></div>
</div>
</li>
</ul>
</div>
Full example on jsfiddle: https://jsfiddle.net/arnauth/55josw6e/
In the upper example, I can easily accomplish my goal on the first two list items.
The trick here is to have the same effect when I have a long line that break into two or more lines - that third line on the example.
The line element must have the same width of the text, so in the example must stop near the word 'go' - not in the end of the container as displayed.
The idea is try to approach the problem using only CSS rules avoiding javascript based code.
Thanks for your attention.
Try adding this inside of your span:
border-bottom: 6px solid #ff6f69;
so that it ends up looking like this:
span {
font-size: 24px;
color: #010101;
border-bottom: 6px solid #ff6f69;
}
it should come out like this
https://jsfiddle.net/55josw6e/3/
Hope this answers your question.
I am trying to use the CSS :not selector to select all list elements that do not contain a link. Even though the first element contains a link, for some reason it is still following the CSS rules of the :not selector. Here is the example on CSSDesk: http://www.cssdesk.com/bFrgz
Here is the HTML
<ul>
<li>One</li>
<li>Two</li>
<ul>
Here is the CSS:
li {display: inline; border: 2px solid lightgrey; padding: 15px 20px 15px 20px; background: white;}
li:not(a) {color: white; border: 2px solid lightgrey; padding: 15px 20px 15px 20px; background: light grey;}
Any help would be greatly appreciated
First off, as per the original question, this is not possible. You're trying to both select an LI that is not also an A. No HTML element can be two types of elements like that.
There is also no way using pure CSS to reach inside of an LI, detect it has an A, then traverse back up the tree and apply a style to the LI.
HOWEVER, you COULD set the A to fill the LI with color by converting it to an inline-block level element. Here's what your code would look like, and please note that you were overwriting your background color in one case, and had a space in the 'lightgrey' in the second. I fixed that assuming you wanted your LIs with A tags in in them to have the white background. I used a negative margin to pull the edges back in, and zeroed out the border on the A so that it just uses the LI's border. If you wanted no border at all, then you'd need to remove the border from the LI.:
li a {
display: inline-block;
border: 0px solid lightgrey;
padding: 15px 20px;
background: white;
margin: -15px -20px;
}
li {
display: inline-block;
color: white;
border: 2px solid lightgrey;
padding: 15px 20px;
background: lightgrey;
}
You could also implement a jQuery solution whereas you detect all LIs with an A and apply a class to the LI that you can then style differently:
$('li a').parent().addClass('has-anchor');
Sorry but this can't be done.
You need to rethink the HTML structure because CSS selectors currently don't work with child elements targetting parent.
In this particular case, don't style the LI, style the A as block element, and elements wich don't have links on it, wrap them around a SPAN or DIV, then style them like you did with the anchor:
<ul>
<li>One</li>
<li><span>Two</span></li>
<ul>
CSS:
li {
display: inline;
margin: 0;
padding: 0;
}
li a, li span {
display: block;
border: 2px solid lightgrey;
padding: 15px 20px 15px 20px;
background: lightgrey;
}
li a {
color: red;
}
li span {
color: black;
}
You don't need the :not() selector at all; simply put the text styling for non-anchor elements in the li {} class.
li {
display: inline;
border: 2px solid lightgrey;
padding: 15px 20px 15px 20px;
background: lightgrey;
color: white;
}
<li>One</li>
<li>Two
</li>
I am using this software (Swiiit Website Builder) purchased by my company, im doing intranet page for my department (im a total learn on the job dude being tasked to do this) please help!
<style><!--
<html>
<head>
<style>
ul {
float: left;
width: 100%;
padding: 0;
margin: 0;
list-style-type: none;
}
a {
float: center;
width: 6em;
text-decoration: none;
color: white;
background-color: #000080;
padding: 0.6em 4.5em;
border-right: 1px solid white;
}
a:hover {
background-color: #0000cd;
}
li {
display: inline;
}
--></style>
the style will affect other area of the page; how do i edit it?
i tried to understand this Apply different css stylesheet for different parts of the same web page but im still cracking my head...
I have amended according to the recommendation (see below) and turn out great!!! Thanks!!
doesnt affect other parts of page now, but now the space between blocks of links are so wide apart when i launch the code... looks fine in preview mode though... please assist :)
.something ul {
float: left;
width: 100%;
padding: 0;
margin: 0;
list-style-type: none;
}
.something li {
display: inline;
}
.something a {
float: center;
width: 6em;
text-decoration: none;
color: white;
background-color: #000080;
padding: 0.6em 4.5em;
border-right: 1px solid green;
}
.something a:hover {
background-color: #0000cd;
}
<div class="something">
<ul>
<li>Organisation</li>
<li>XXX</li>
<li>Organisational Chart</li>
<li>Contact Us</li>
</ul>
</div>
<p><img style="display: block; text-align: center; vertical-align: top;" src="/wbn/slot/u3733/Org%20Chart.png" alt="" width="800" height="473" /></p>
Set class for every ul, a and li otherwise
<div class="something">
<ul>
<li>....</li>
<li>....</li>
</ul>
...
</div>
Give styles using the class name like,
.something ul { }
.something li { }
.something a { }
Set specific class or id to the elements like
text
You can style this a in your css by
#a{
style
}
Rather than adding styling to your elements, add them to a class or id
class definition:
html
<div class="myClassIsReusableToAllWithThisClass"></div>
css
.myClassIsReusableToAllWithThisClass{
/*add styling to all which have a class called 'myClassIsReusableToAllWithThisClass'*/
}
ID definition:
html
<div id="uniqueID"></div>
css
#uniqueID{
/*add styling to only one element*/
}
element definition: (what you're doing presently)
html
<div></div>
css
div{
/*add styling to all 'div' elements*/
}
As #James Donnelly has already mentioned, you should also remove these:
<!-- and -->
as these are 'comments' in html, and will render all within them void/ go unnoticed by your browser.
DEMO:
#myID {
color: red;
}
.myClass {
background: blue;
}
a {
font-weight: bold;
}
<div >I'm just a div</div>
<br/>
<div id="myID">I have a specific id</div>
<br/>
<div class="myClass">I have a specific class which is reusable to all with this class</div>
<br/>
<div class="myClass">I have the myClass class</div>
<br/>
<a>I'm an a tag with no class or id. But all 'a' tags will have this styling</a>
I wonder if there exists an easy and solid way to style ul and li elements so that when using list-style: outside , the li elements lign up with the content above it, or with the content box in which it is in without margins or padding.
Condider this: ( http://jsfiddle.net/Um5L9/2/ )
<div id = "container1">
<span>Something</span>
<ul>
<li>
<div>One</div>
<div>Some more text or content here</div>
</li>
<li>
<div>One</div>
<div>Some more text or content here</div>
</li>
<li>
<div>One</div>
<div>Some more text or content here</div>
</li>
</ul>
</div>
body {
margin:20px;
border: 1px solid #333;
}
ul {
list-style: square outside none;
}
The result will be this:
What I want is this:
And it's easy to do by just adding some padding:
ul {
list-style: square outside none;
padding-left:15px;
}
But there has to be a better way surely than setting pixel margins. Maybe something that would work for all font sizes?
Thanks!
EDIT
Just want to add that I need both children of the li to line up underneath each other
Here is one way of doing it that appears to be fairly robust and uses pseudo elements.
Apply the following CSS:
body {
margin:20px;
border: 1px solid #333;
}
ul {
list-style: none;
margin:0px;
padding:0px;
font-size: 1.0em;
}
ul li {
margin-left: 0em;
position: relative;
padding-left: 1.0em;
}
ul li:before {
content:"\2022";
font-size: 1.0em;
position: absolute;
top: 0;
left: 0;
}
See demo at: http://jsfiddle.net/audetwebdesign/SfGbW/
Note: Look up the ISO code for the desired list marker.
I suggest using something like this:
ul {
list-style-position: inside;
margin:0px;
padding:0px;
}
http://jsfiddle.net/Um5L9/4/
I'm trying to write a navigation bar using an <ul> with inline elements, but the elements all have a gap between them that seem to come from nowhere. That is when hovering a link, the shaded box should snap to the surrounding boxes. The page currently looks like this: http://wictorht.at.ifi.uio.no/. What is causing these gaps?
HTML:
<body>
<div id="main">
<ul class="header">
<li class="title">wictorht</li>
<li class="header">
<a class="header" href="https://bitbucket.org/htor/dwmst/src">dwms</a>
</li>
<li class="header">
<a class="header" href="https://bitbucket.org/htor/linux/src">linux</a>
</li>
<li class="header">
<a class="header" href="http://www.fsf.org/register_form?referrer=10397">fsf</a>
</li>
<li class="header">
<a class="header" href="http://stackexchange.com/users/1006063">stackexhange</a>
</li>
</ul>
</div>
</body>
CSS:
body {
background: #666666;
color: #c0c0c0;
margin: 0;
}
a.header {
text-decoration: none;
padding: 10px;
margin: 0;
}
a.header:hover, a.header:active {
background-color: #666666;
color: #c0c0c0;
}
ul.header {
background-color: #c1c1c1;
color: #666666;
list-style: none;
padding: 10px 10px 10px 0;
margin: 0 0 10px 0;
}
li.header {
display: inline;
}
li.title {
background-color: #000000;
color: #bada55;
display: inline;
padding: 10px;
}
This is because all white-space, including new-lines, between elements is collapsed down to a single space when rendered by the client's browser. To hide the spaces you can either:
Remove the spaces between li elements:
<li><!-- content --></li><li><!-- more content --></li>
Set the font-size of the parent ul to 0, and redefine the font-size of the li element:
ul {
font-size: 0;
}
ul li {
font-size: 14px;
}
Comment out the gaps between the li elements:
<li>Content</li><!--
--><li>Next li</li>
Float the li elements instead of using display: inline, which removes the spaces by taking the elements out of the normal flow:
ul {
overflow: hidden; /* to keep the li 'visibily' within the bounds of the ul */
}
ul li {
float: left;
}
Close the li tag on the next line, before the next li opening tag this feels slightly wrong to me, but it is valid:
<li>First li</li
><li>Second li</li>
(Or, obviously, place the next li opening-tag on the previous line, immediately after the previous element's closing tag:
<li>First li</li><
li>Second li</li>
)
The gaps are caused by the whitespace between the <li></li> tags.
Try <li>...</li><li>...</li> as a comparison.
Anyways, avoid this with display:block and using float:left
This is a great post explaining what is happening and the work arounds that have already been mentioned by the previous answers.
http://css-tricks.com/fighting-the-space-between-inline-block-elements/
You also have a little trouble with your css selector names, you actually only need one class and you can take advantage of the nature of CSS to do the rest.
.header {
background-color: #c1c1c1;
color: #666666;
list-style: none;
padding: 10px 10px 10px 0;
margin: 0 0 10px 0;
}
Now target all the 'li' tags that are children of the .header class
.header li {
display: inline;
}
Now target all the 'a' tags that are children of the .header class (these happen to be inside your 'li' tags)
.header a {
/* etc */
}