CSS Issue, space between elements makes no sense [duplicate] - html

This question already has answers here:
How to remove the space between inline/inline-block elements?
(41 answers)
Closed 7 years ago.
I have got a weird space in two elements on this page and it doesn't make any sense! There should be no white gap between the border on the ul on the left, and the div on the right. The blue sections should touch, load the page and it will make sense.
This is the HTML for that part:
<div class="messages">
<ul class="messageList">
<li style="background-color:#2e89e6;color:#FFF;">
<div>Martyn Ball</div>
<p>This is a brief mess...</p>
<span>07/12/2015</span>
</li>
<li>
<div>Martyn Ball</div>
<p>This is a brief mess...</p>
<span>07/12/2015</span>
</li>
<li>
<div>Martyn Ball</div>
<p>This is a brief mess...</p>
<span>07/12/2015</span>
</li>
</ul>
<div class="messageContent">
<div class="messageContentTitle">
<span class="name">Martyn Ball</span>
<span class="date">07/12/2015 02:24PM</span>
</div>
This is a brief message, more will be loaded here!
</div>
</div>
And here is the CSS:
/* Messages Styles */
.messageList {
list-style:none;
list-style-type:none;
margin:0; padding:0;
display:inline-block;
border:solid #2e89e6;
border-width:0px 5px 0px 0px;
width:20%;
}
.messageList li p {
font-size:14px;
margin:0px;
}
.messageList li div {
font-weight:bold;
}
.messageList li span {
font-size:12px;
}
.messageList li {
border:solid #e5e5e5;
border-width:0px 0px 1px 1px;
padding:8px;
cursor:pointer;
}
.messageList li:hover {
background-color:#2e89e6;
color:#FFF;
}
.messageContent {
vertical-align:top;
overflow: hidden;
display:inline-block;
text-align:left;
width:75%;
}
.messageContentTitle {
color:#FFF;
background-color:#2e89e6;
margin:0;
padding:5px;
width:100%;
}
.messageContentTitle name { float:left; }
.messageContentTitle date { float:right; }

This is because both the div and ul are inline-block elements, which means that they display the whitespace in between.
This is my favorite solution to such an issue. Set the font-size of the parent to 0, and then set the font-size of the inline-block elements to the size that you need [I set it using rems, but you can use whichever unit you need in your project].
.messages {
font-size: 0;
}
.messageList, .messageContent {
font-size: 1rem;
}
The other options are to comment out the whitespace between the elements.
<div class="messages">
<ul class="messageList">
...
</ul><!--
--><div class="messageContent">
...
</div>
</div>
Or, to remove all the whitespace, altogether.
<div class="messages">
<ul class="messageList">
...
</ul><div class="messageContent">
...
</div>
</div>
I prefer the first solution over the others, as it keeps the HTML more readable.

Related

Remove random padding from my UL LI?

I have some sort of space in between my li tags I don't where it's coming from? How can i remove this?
Also, I'd like to change the color of the font to white on hover of the li
JSFIDDLE http://jsfiddle.net/omarel/tfyxL66c/
CSS
.nav_container {
text-align: center;
width:100%;
}
.nav_container ul {
/* margin-top:15px; */
margin-left:30px;
}
.nav_container ul li {
display: inline-block;
text-align: center;
padding-left:40px;
padding-right:40px;
margin:0px;
height:80px;
cursor:pointer;
}
.nav_container ul li:hover {
background-color:#08298A;
}
.nav_container a:hover {
color:#fff;
}
header {
width:100%;
margin: auto;
box-shadow:0 0 8px rgba(0,0,0,0.1);
min-width:410px;
}
.navlogo {
z-index:99;
}
.navlogo img {
width:100px;
margin:10px 10px 10px 10px;
}
.floatleft {
float:left;
}
.floatright {
float:right;
}
.centerdiv {
margin:0 auto;
}
#media only screen and (min-width:700px) {
header {
max-width:1250px;
}
.container {
max-width:1250px;
}
.box2 {
width:32%;
height:300px;
float:left;
}
.box2left {
width:65%;
height:600px;
float:left;
}
}
div {
border:solid 1px #E6E6E6;
position:relative;
}
ul li {
border:solid 1px #E6E6E6;
}
HTML
<div class="navlogo floatleft">
<img src="images/logo.png" />
</div>
<div class="floatleft">
<div class="nav_container">
<ul>
<li>Link 1</li>
<li>Link 2</li>
</ul>
</div>
</div>
<div class="floatright">
<div class="nav_container">
<ul>
<li>Profile</li>
<li>Sign out</li>
</ul>
</div>
</div>
</header>
<div style="clear:both;"></div>
Answering your second question first as the answer is shorter: use the :hover pseudo class.
EXAMPLE
li:hover a{color:#fff;}
More information on pseudo classes
To answer your first question, then; setting an element's display property to inline or inline-block will cause the white space surrounding it to be treated just like the space surrounding any other inline element.
You can workaround it in a number of ways
Remove all line breaks from within your list:
<ul><li>Item 1</li><li>Item 2</li><li>Item 3</li></ul>
Use comments to hide the line breaks from the browser:
<ul><!--
--><li>Item 1</li><!--
--><li>Item 2</li><!--
--><li>Item 3</li><!--
--></ul>
Use CSS to set the font-size of the parent element to 0 and then "reset" it for the child elements:
html{font-size:20px;}
ul{font-size:0;}
li{font-size:1rem;}
Alternatively, if you're not 100% set on using display:inline-block, you can use floats or flexbox instead.
To change the color of the links to white, use this css:
.nav_container ul li:hover a {
color:white;
}
However, only the text will be clickable, the li element won't be clickable. Another way to do the same thing is to apply all width/height/background styling to the link, instead of the li.
As Shaggy mentioned, to eliminate extra spacing when using inline-block you should remove all spaces in your html between your menu li items.
As for changing the link color on hover you should add the following to your css code:
.nav_container li:hover a {
color:#FFF;
}

Position text to center of div left and right

I'm trying to create something that looks like this:
so far I have: http://jsfiddle.net/ePse6/
Without using something like: margin-top:-25px;, how can I position the Edit/Delete links to be on the right of the title (the part that says "iPhone" or "Android") and have both the title and links halfway between the borders?
Thanks!
just like most of answers, here i come with text-align:right and float:left .
I reduced code to minimal and plain CSS for your actual structure and to make it clear to you : http://jsfiddle.net/ePse6/7/
ul , a { /* basic reset we need */
padding:0;
margin:0;
color:gray;
text-decoration:none;
}
.mini > ul > li {
display:block;/* reset from list-item */
border-bottom:solid;
text-align:right;
overflow:hidden;/* wraps floatting element within */
}
.mini > ul > li> h3 {
float:left;
margin:0;
}
.mini > ul > li ul,
.mini > ul > li li {
display:inline-block;
}
Why not use something simple and really handy?
I have removed all of your messy code, and have created a new fiddle for you.
http://jsfiddle.net/afzaal_ahmad_zeeshan/ePse6/4/
I have used just a few lines of code, I have used a div and inside that, I have used 2 paragraphs to seperate each of them. Then inside that I used span element to seperate the right and left floating elements.
Using CSS I selected the classes and then styled them to get the desired input!
Here is the code:
<div>
<p>
<span class="left">Android</span><span class="right">Delete Edit</span>
</p>
<p>
<span class="left">iPhone</span><span class="right">Delete Edit</span>
</p>
</div>
CSS is as:
p {
border: 1px solid #333; // border that you wanted!
padding: 20px; // padding all around the element
padding-bottom: 40px; // padding at the bottom of the element
}
.left {
float: left; // making the elements float at the left
}
.right {
float: right; // floating elements at the right side
}
You can go to the fiddle page, and check for the design of the layout now. It was a simple thing. Hope its what you wanted.
This is without the lists. Just some CSS to do the trick: http://jsfiddle.net/Lg96p/
CSS:
.wrap{
width:100%;
border-bottom:solid 1px #666666;
margin-bottom:20px;
padding-bottom:10px;
}
.title{
font:bold 16px arial;
}
.fl{
float:left;
}
.fr{
float:right;
}
.lnk{
color:#6c6c6c;
display:inline-block;
text-align:right;
margin:0 10px 0 0;
text-decoration:none;
font:normal 14px arial;
}
HTML:
<div class="wrap fl">
<div class="title fl">iPhone</div>
<div class="fr"><a class="lnk" href="">Edit</a><a class="lnk" href="">Delete</a></div>
</div>
<div class="wrap fl">
<div class="title fl">Android</div>
<div class="fr"><a class="lnk" href="">Edit</a><a class="lnk" href="">Delete</a></div>
</div>
You should create two columns that fill the parent div. Make them both float:left; and for the right column you can align the text to the right text-align:right; or put two divs in it with float:right; for edit and delete.
Here is my fiddle: http://jsfiddle.net/ePse6/5/
Whatever you put into the columns or how to format it is up to you. But from here you have 2 columns independently next to each other.
If you want multiples of these stacked on top of each other i would change the container to a class and just add multiple of these containers with the columns to keep it tidy and readable. Like in this fiddle: http://jsfiddle.net/ePse6/6/
HTML:
<div class='container'>
<div class='leftCollumn'>
Iphone
</div>
<div class='rightCollumn'>
<a hreft="">Edit</a><a hreft="">Delete</a>
</div>
</div>
<div class='container'>
<div class='leftCollumn'>
Iphone
</div>
<div class='rightCollumn'>
<div class="button">Edit</div><div class="button">Delete</div>
</div>
</div>
CSS:
.container
{
width:600px;
margin:auto;
}
.leftCollumn
{
float:left;
width:400px;
background-color:#999;
}
.rightCollumn
{
float:left;
width:100px;
text-align:right;
background-color:#CCC;
}
.rightCollumn a
{
margin-left:10px;
margin-right:5px;
}
.button
{
margin-left:10px;
margin-right:5px;
background-color:#000;
color:#FFF;
float:right;
}

Vertically center DIV inside LI without px-measure

I am trying to make a list with square bullets in different colors with square size independant of the font size.
I need to use font sizes in em or %.
That's my best try so far: http://jsfiddle.net/3GMjp/29/
<ul>
<li>
<div>
<span class='li_box green'></span>
<span>Element 1</span>
</div>
</li>
<li>
<div>
<span class='li_box red'></span>
<span>Element 2</span>
</div>
</li>
<li>
<div>
<span class='li_box blue'></span>
<span>Element 3</span>
</div>
</li>
<ul>
css:
ul {
padding:0;
margin:0;
}
li {
font-size: 1.5em;
list-style-type:none;
line-height: 2em;
}
.li_box {
float:left;
width:10px;
height:10px;
vertical-align:middle;
margin-right:10px;
}
.red{ background-color:red}
.green{ background-color:green}
.blue{ background-color:blue}
Could someone help me to center the boxes without adding px-measures?
Any working solution (would be appreciated)!
See I have done modification in the CSS and HTML :
Please see URL : http://jsfiddle.net/3GMjp/33/
HTML code:
<li>
<div>
<span class='li_box green'></span>
<span class='spn'>Element 1</span>
</div>
</li>
CSS changes :
ul {
padding:0;
margin:0;
}
li {
font-size: 1.5em;
list-style-type:none;
line-height: 2em;
}
li div {
display:table;
}
.spn{
display:table-cell;
}
.li_box {
display:table-cell;
float:left;
width:10px;
height:10px;
vertical-align:middle;
margin-right:10px;
}
.red{ background-color:red}
.green{ background-color:green}
.blue{ background-color:blue}
I've updated your fiddle here.
If you float an element, it will become a block element, and thus vertical align won't work. The span with the text, unless floated as well, will wrap to the next line.
Inline block seems to work just fine:
.li_box {
display: inline-block;
width:10px;
height:10px;
margin-right:10px;
}
If you're simply looking to center the content of the divs, this should be all you need:
div { text-align: center; }
try to use this:
li {
...
text-align:center;
}
Adding margin top to .li_box will fix your issue.
.li_box {
...
margin-top: 1%;
}

Reduce a white gap between two <hr> tags

I have the following menu
The two lines are both 'hr' tags and the menu is a div containing a ul. I have been googling for a while now and trying adjusting the css with margin and padding but I want to reduce the white space between the lines and the text bringing them closer to the text.
HTML:
<hr id="header_line"/>
<div id="menu_bar">
<ul>
<li>Add new Form</li>
<li>View old forms</li>
<li>Reports</li>
<li>Site Administration</li>
</ul>
</div>
<hr id="under_menu_line"/>
CSS:
#menu_bar ul {
list-style:none;
padding-left:0px;
}
#menu_bar ul li {
display:inline;
padding-left:10px;
}
#menu_bar ul li a {
text-decoration:none;
color:Black;
font-family:Century Gothic;
font-size:12pt;
}
#menu_bar ul li a:hover {
color:#007C5A;
}
#header_line {
margin-top:5px;
}
#under_menu_line {
margin-top:5px;
}
Any ideas?
The best solution would be to drop the <hr>s, and use border-top and border-bottom in conjunction with padding on the div.
<hr> should be used as a horizontal rule. For instance, a hard separation of paragraphs or a long break. And not as a visual element.
Just like with any other element, the <hr> is controlled by CSS. The space you want to control is just the margin. This is the default from Firefox:
hr {
-moz-box-sizing: border-box;
-moz-float-edge: margin-box;
border: 1px inset;
color: gray;
display: block;
height: 2px;
margin: 0.5em auto;
}
So, the following will make the space 0.1em instead of 0.5em:
hr { margin: 0.1em auto; }
Try this and tell me if this is what you wanted.
#header_line { margin-top:5px; margin-bottom:-10px;}
#under_menu_line { margin-top:-10px; }
Use
#header_line{
margin-bottom:0px;
}
#under_menu_line{
margin-top:0px;
}

Facebook-like friend selection checkbox

I'm trying to code a selection like the Facebook "invite friends" selection. I like it because it uses the default checkboxes and not a background simulating the selection of each item.
The problem is that I cannot figure out how can they make the text (friend's name) break into two lines if needed (text is too big)!
I have a simplified example of what I'm trying to achieve. Here's the HTML:
<div class="modal">
<div class="modal-body">
<ul class="unstyled">
<li class="store_selectable">
<input class="checkbox" type="checkbox">
<a href="#">
<div class="store-text">
<div class="iblock"><img alt="Missing" src="http://placehold.it/30x30"></div>
<div class="text"><div>This is the store</div></div>
</div>
</a>
</li>
</ul>
​
And here's the CSS:
body {
padding: 20px;
}
li.store_selectable {
width:161px;
float:left;
margin:0 0px 6px 0px;
overflow:hidden;
font-size:0.9em;
}
li.store_selectable .checkbox {
float:left;
display:inline-block;
margin:12px 6px 0;
}
li.store_selectable .store-text {
display:block;
height:30px;
}
li.store_selectable img {
float:left;
display:block;
width:30px;
height:30px
}
li.store_selectable a {
display:block;text-decoration:none;
padding:2px;
outline:none;
color:#333;
border:1px solid #fff;
border-width:1px 0;
}
li.store_selectable a:hover {
background:cyan;
border:1px solid blue;
border-width:1px 0;
}
li.store_selectable .iblock {
display:inline-block;
vertical-align:middle;
overflow:hidden;
text-align:left;
}
For an easier testing and understanding I've created a jsfiddle here: http://jsfiddle.net/rikas/tpqHd/1/
Now, what I'm trying to do is breaking the text in two lines if the width of the list item is small enough, changing this line: width:161px;. Messing with facebook "invite friends" modal window CSS I can see that they do it, but I can't!
Thanks for your help!
Not quite sure of the interface you are trying to achieve, but the reason for text not wrapping is the css
overflow:hidden;
after removing that consider putting your checkbox and
the text its own div element with class .fl
.fl { display:block; }
to line them side by side
directly under the two create another empty div element with the class of .clear
.clear { display:block; clear:both; line-height:0; }
to prevent the next element inheriting the float:left