I have aligned a span next to div as shown below but there is a spacing between the elements. Can someone tell me what is causing it to appear and how to remove it?
<table>
<tr>
<td>
<div id="headerDiv" style="">DIV</div>
<span id="labelSpan">test</span>
</td>
</tr>
JSFiddle
Using inline-block will create a space when elements are on a new line. (The most frustrating example is when you want li to be side-by-side.
Either do this:
<div id="headerDiv" style="">DIV</div><span id="labelSpan">test</span>
Or this:
<div id="headerDiv" style="">DIV</div><!--
--><span id="labelSpan">test</span>
Alternatively, you can do float:left; instead.
JSFiddle
The inline-block always gives gap. If you don't want that, you can do either:
Remove the Space
<table>
<tr>
<td>
<div id="headerDiv" style="">DIV</div><!--
--><span id="labelSpan">test</span>
</td>
</tr>
</table>
Fiddle: http://jsfiddle.net/praveenscience/w3sjrgc4/2/
Or use float:
<table>
<tr>
<td>
<div id="headerDiv" style="">DIV</div>
<span id="labelSpan">test</span>
</td>
</tr>
</table>
td {
overflow: hidden;
}
td > span {
float: left;
border:2px solid;
}
td > div {
float: left;
border:2px solid;
}
Fiddle: http://jsfiddle.net/praveenscience/vt4kr35w/
This spacing is caused by white spaces (both elements have inline layout), you can get read of them by :
pushing elements next to each other in one line in the HTML markup;
Example:
<td>
<div id="headerDiv">DIV</div><span id="labelSpan">test</span>
</td>
using <!-- --> tags;
Example:
<td>
<div id="headerDiv">DIV</div><!--
--><span id="labelSpan">test</span>
</td>
using word-spacing CSS property.
CSS:
td {word-spacing: -100%;}
It's called white space and occurs between inline (or inline-block) elements when there's space (on the same line or a new line) in the markup.
Here are a few solutions for you:
Solution 1- Remove the white space in the markup:
<div id="headerDiv" style="">DIV</div><span id="labelSpan">test</span>
Demo: http://jsfiddle.net/w3sjrgc4/7/
Solution 2- Set the font-size to 0 on the container then retrieve it on the children.
Demo: http://jsfiddle.net/w3sjrgc4/9/
Solution 3- Comment out the white space in the markup:
<div id="headerDiv" style="">DIV</div><!--
--><span id="labelSpan">test</span>
Demo: http://jsfiddle.net/w3sjrgc4/10/
Solution 4- Left float the children and clear the container to maintain normal document flow on the height.
Demo: http://jsfiddle.net/w3sjrgc4/11/
Related
I am trying to put a red rectangle icon followed by some text within a HTML Table cell and I am getting very strange behavior here. I am using just a DIV to draw the red rectangle as shown in the example here. I want the height of rectangle to be the height of the cell so I set the height: 100%
https://jsfiddle.net/pm43k26w/1/
<table border="1">
<td>
<div style="width:10px;height:100%;background:red;display:inline-block"></div>
Height in percentage
</td>
<td>
<div style="width:10px;height:10px;background:red;display:inline-block"></div>
Fixed Height
</td>
</table>
The solution kind of works in Chrome but not in FireFox. FireFox just shows a blank space. It appears it does not like it when I set the height to 100% Can anyone explain why? What's the best way to accomplish this if DIV isn't the right way to go for the rectangle?
Thanks.
Firefox needs content in the div. The following modification will do. The numerical entity is Unicode's 'zero width space character'. A non-breaking space ( ) will do as well, of course.
<div style="width:10px;height:100%;background:red;display:inline-block"></div>
See this fiddle.
Try setting the height of the parent element.
<td style="height:20px">
That should help with the Firefox problem.
Edit: JSFiddle:
https://jsfiddle.net/prove64m/
First of all you forgot the <tr> tag.
So this should be the correct HTML:
<table>
<tr>
<td>
<div></div> first text
</td>
<td>
<div></div> second text
</td>
</tr>
</table>
Then the CSS part:
table {
border:1px solid;
}
td {
height:40px;
}
div {
display:inline-block;
vertical-align:bottom;
width:10px;
height:100%;
background:red
}
Pay attention that the height is ALWAYS evaluated, so, if there isn't any explicitily set, there is nothing "to compute"; we did this here:
td {
height:40px;
}
Other important thing; i guess you would like to control the position of the text after the <div> element; this is possible with online-block elements in this way:
div {
...
vertical-align:bottom;
...
}
Other possible values are: middle, top,...
here the Fiddle: https://jsfiddle.net/pm43k26w/5/
Firstly, you need to understand the problem here. CSS Properties such as height are "Computed". In this particular case, the computed height of the first div (let's call it unseenForce, shall we?) is 0 while its cousin, aptly named seenForce is 10px. See this below :
http://jsfiddle.net/gvo4kf41/
$(document).ready(function() {
$('.Info').html('The computed height of the unseenForce is ' + $('#unseenForce').height() + 'px <br />');
$('.Info').append(document.createTextNode('The computed height of the seenForce is '+ $('#seenForce').height() + 'px'));
});
.Info {
color: red;
margin-top : 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<table border="1">
<td>
<div id="unseenForce" style="width:10px;height:100%;background:red;display:inline-block"></div>
Height in percentage
</td>
<td>
<div id="seenForce" style="width:10px;height:10px;background:red;display:inline-block"></div>
Fixed Height
</td>
</table>
<div class="Info">
</div>
This is because none of the ancestors of the unseenForce have a specific height to them. Hence, Firefox is unable to attach a height to it.
What you need to do it force the Computed value of height to be greater that 0. There are many ways to do it and all the answers here show you different ways of doing it. Choose the one which suits your needs.
Here's the way I would do it. Just add height to the row (<td>).
table td {
height: 10px;
}
<table border="1">
<td>
<div id="unseenForce" style="width:10px;height:100%;background:red;display:inline-block"></div>
Height in percentage
</td>
<td>
<div id="seenForce" style="width:10px;height:100%;background:red;display:inline-block"></div>
Fixed Height
</td>
</table>
<div class="Info">
</div>
Hope this helps!!!
I have a table with multiple columns. In one of the column rows I want to add 2 elements which will be next to each other. One text element and one icon. The icon has a fixed with, the text element needs to be dynamic and has to be truncated with ... when the column cannot stretch anymore.
This is the HTML:
<table>
<tr>
<td>
</td>
<td>
<span>Truncated text goes here</span>
<i class="icn sprite icn-name></i>
</td>
<td>
</td>
</tr>
</table>
How do I do this? Using display: table; will make the HTML all buggy.
As said in comments, if you allow text and image to stay in adiacent cells, you can try the following.
<table>
<tr>
<td>Truncated text goes here</td>
<td><img src="imageURL" /></td>
</tr>
</table>
You can use vertical-align:top; in td style to align text on the top of the cell. And then you can use the following to set image width.
td>img {
vertical-align:top;
display:inline-block;
width:80px;
}
Fiddle
UPDATE
If you don't want to add extra cells to your table, you can create an internal div inside the cell, display it with display:table; property, and then display both span and img with display:table-cell; property.
Fiddle
I added the <i> element in front of the <span> element and gave the <i> element a float: right; and the <span> element the truncate styles.
Works fine now!
I have dynamic text content inside all first tds. So the trs and tds get expanded automatically. Now for all second tds there is div with background image and dont have any text inside. I need this divs with background image to be expanded or collapsed automatically along with the cells in table so that the red image will span over the height of each cell. Any way to do this purely with css as I dont want to introduce script here? jsfiddle
<table border="1">
<tr>
<td>
This content makes the trs and tds to expand/contract horizontally automatically
</td>
<td>
<div class="img"></div>
</td>
</tr>
<tr>
<td>
xyz
</td>
<td>
All divs inside second tds should get expanded automatically which is not happening
</td>
</tr>
</table>
CSS
td
{
width:200px
}
.img{
width:30px;
height:40px;
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAAXCAIAAABmo+ONAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAApSURBVDhPY3gro0IG+v///6hOvGhUJyE0qpMQGtVJCFGmE4jJAf//AwBnlUxAq2HzYQAAAABJRU5ErkJggg==) no-repeat;}
Just set background for that td with 100% width and height.
Fiddle: http://jsfiddle.net/mttSA/
.empty{
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAAXCAIAAABmo+ONAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAApSURBVDhPY3gro0IG+v///6hOvGhUJyE0qpMQGtVJCFGmE4jJAf//AwBnlUxAq2HzYQAAAABJRU5ErkJggg==) no-repeat;
background-size: 100% 100%;
}
HTML:
<td class="empty">
</td>
A typical css alignment problem:
<table style="width:100px"> <!-- Not actually necessary; just makes the example text shorter -->
<tr>
<td style="padding:0;">
<div style="height:100%; width:100%; background-color:#abc; position:relative;">
test of really long content that causes the height of the cell to increase dynamically
</div>
</td>
<td>
<div>text</div>
<div ></div>
<img style="vertical-align: bottom;" src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/1118727_100000298033362_1412277170_q.jpg"/>
</td>
</tr>
</table>
http://jsfiddle.net/R5TAY/
How would I make the image always appear on the bottom of the table, and the text to stay in the middle?
Thanks
Based on your example, you could use positioning to achieve what you want:
td {
position:relative;
}
img {
position:absolute;
bottom:0;
}
jsFiddle example
You can set the vertical-align css property:
#your_td {
vertical-align: bottom;
}
See this FIDDLE
Set valign to bottom
<td valign="bottom">
Hope this helps
let's say I have something like this:
<table>
<tr>
<td>
<span class="text_1">Text 1</span>
<span class="text_2">Text 2</span>
</td>
</tr>
</table>
How can I tell each span inside my td to take 50% of the width of my td and also center align the text of my span? I also want the texts to appear next to each other on the same line inside my td. I tried something like on the spans:
...
<span class="text_1" style="display:inline-block;width:50%;">Text 1</span>
<span class="text_2" style="display:inline-block;width:50%;">Text 2</span>
...
But that didn't work
Thank you
They will each take 50% of the width of the parent element, the problem is the newline between the elements that also takes up a portion of that space. 50% + a space + 50% equals more than 100%.
Remove the new-line, or use float: left (or right).
With float: left: JS Fiddle demo.
Without the new-line: JS Fiddle demo.