Dynamically Change the Height of a tr - html

I am creating a calendar that contains tasks/events. When I add more events to the table row, I expect the row to re-size automatically to fit the contents but it is not. Instead, the contents spill out of the row.
If I remove my "events-wrapper" div, I can fix this problem but I need to wrap my events in this div so I can position them in the row so they do not overlap the date as more is added.
Here is the code below.
HTML
<tr class="week">
<td><div class="date">20</div></td>
<td><div class="date">21</div></td>
<td><div class="date">22</div></td>
<td>
<div class="date">23</div>
<div class="events-wrapper">
<div class="event">
<span class="fa fa-check-square-o"></span>
<p class="event-name">Test Event</p>
</div>
<div class="event">
<span class="fa fa-check-square-o"></span>
<p class="event-name">Test Event</p>
</div>
</div>
</td>
</tr>
CSS
.week td{
text-align: right;
position: relative;
font-size: 14px;
padding: 2px 5px;
width: 14.28%;
}
.week .date{
position: absolute;
top: 4px;
right: 5px;
}
.events-wrapper{
position:absolute;
top:20px;
}
.event{
width: 95%;
height: 20px;
border: 1px solid blue;
background-color: lightblue;
display: inline-block;
top: 5px;
position:relative;
margin-bottom: 5px;
padding-left: 5px;
text-align: left;
}
.event .event-name{
display:inline;
font-weight:bold;
}
Any suggestions on how to fix this?

If I got you. You used position absolute to you code so the elemnt don't get any size. your td item cant get size for 2 absolute divs within hi,
so i cahnges the code and give you on fiddle:
https://jsfiddle.net/1tt63h85/
changes:
.event{
position: static;
}
.week .date{
position: static;
}

Welcome to StackOverflow!
The Main Deal: Don't do this.
There's quite a few foundational issues with what is going on here. Right off the bat, I'd strongly recommend having something that can easily have data written in with ease. Editing markup will be a terrible way of adding data to the calendar, and many unexpected/undesirable results may occur.
At any rate, I was compelled to spend a couple minutes and crack out something that I think would show you what you're trying to work towards. I made two examples for you: a simple calendar that has the dates running vertically (my assumption for what your goal was from the example) and another, much more robust calendar (Guy Ytzhak previously answered and assumed you were looking to have the dates run horizontally across the screen)
Neither calendar is complete in any way; they are simply working examples (that I've styled horribly) to show you how I'd structure such a frail system, if I was forced to go about it the way you currently are.
http://codepen.io/anon/pen/ZObOaR - Simple Vertical Date Example
http://codepen.io/anon/pen/oLjLoK - Robust Horizontal Date Example
Some Problems
Your original issue for layout and placement really came from the structure of your table. Your attempts at corrective styling have also further muddled up the issue:
<tr class="week"> <!-- This is a row -->
<td><div class="date">20</div></td> <!-- (1st 'td') First Item in the row -->
<td><div class="date">21</div></td> <!-- (2nd 'td') Sits to the right of the 1st 'td'-->
<td><div class="date">22</div></td> <!-- (3rd 'td') Sits to the right of the 2nd 'td' -->
<td> <!-- (4th 'td') Sits to the right of the 3rd item -->
<div class="date">23</div>
<div class="events-wrapper">
<div class="event">
<span class="fa fa-check-square-o"></span>
<p class="event-name">Test Event</p>
</div>
<div class="event">
<span class="fa fa-check-square-o"></span>
<p class="event-name">Test Event</p>
</div>
</div>
</td>
</tr> <!-- (Still the 4th 'td') Sits to the right of the 3rd item -->
The core table structural issue is shown above. (Assuming no additional code affects the placement & layout beyond what you've provided)
What's Supposed To Happen:
The way a table works (in a very crude & basic sense with a focus on <table>, <tr>, and <td> without worrying about the other pieces) is that it:
Establishes a... well, a table. (<table> tag) - Think of this like a containing box.
Establishes a row (<tr> tag) within said table.
Establishes any number of table cells (<td> tag) within said row.
Repeat steps 2 and 3 until no more <tr> and <td> tags are left
That's how it's supposed to work (mostly). Your code, however, attempts to force a row (<tr>) tag to display its contained cells (<td>) tags in a column, not a row, and I've seen you've tried to do this through absolute positioning. Not a good thing to do.
Some More Problems
Additionally, if you were to throw this into a codepen and inspect it (right click the the table you've created and click inspect), you'll find that the <td> tags and <tr> tags have been stripped away in the rendered result, which is why your below styling does not get applied:
.week td{
text-align: right;
position: relative;
font-size: 14px;
padding: 2px 5px;
width: 14.28%;
}
If you're confused and would like to verify this, feel free to click the following link. I've taken the liberty of throwing your original code into this codepen:
http://codepen.io/anon/pen/WxQXZO
Even More Problems (30% bonus issues completely FREE!)
Your table tags being stripped (along with everything I've said previously) aren't the only core issue. In fact, you'll find that it still has plenty of issues if you were to throw the entirety of your HTML into a set of table tags so it looks like the below code block:
<table>
<tr class="week">
<td><div class="date">20</div></td>
<td><div class="date">21</div></td>
<td><div class="date">22</div></td>
<td>
<div class="date">23</div>
<div class="events-wrapper">
<div class="event">
<span class="fa fa-check-square-o"></span>
<p class="event-name">Test Event</p>
</div>
<div class="event">
<span class="fa fa-check-square-o"></span>
<p class="event-name">Test Event</p>
</div>
</div>
</td>
</tr>
</table>
This is why I've decided to make you two completely new examples. However, they're also very issue-riddled. I probably could have done them a bit better, but I'm doing this purely for example purposes.
The point is:
Could you make a calendar like this? Yes. You could probably make it work pretty well if you worked at it. With that said, I feel that HTML and CSS alone aren't fit to provide this solution with any form of reliability, especially if you want this thing to be interactive on the web. You'd need to take advantage of some javascript libraries, like jQuery, give you the functionality you need. Even then, however, maintaining this thing with new events, dates, etc. would be a total pain and take a lot of time for each edit.
Calendars are meant to quickly and easily provide a visual display of schedule data that can be changed/updated on the fly with ease. This does not do any of that.... and we're not even discussing browser support/device compatibility yet.
My Recommendation:
Grab a CMS (if you don't already use one) like Wordpress or Drupal and develop on there. You can easily download a plethora of beautiful calendars that work wonderfully (and some that don't work so well). A CMS would save you time, frustration, and you'll be able to create something better in the end.
Hope this helps and welcome to StackOverflow! :)

Related

HTML table: text in 1st column is under the image in 2nd column

Being a professional C++ programmer, I'm a very new to HTML :)
The problem is reproducible under Internet Explorer 11 Windows 10.
Other browsers (Edge, Firefox, Chrome) - works fine.
Code:
<table>
<tr>
<td width=500><h1>Free Monitor Manager</h1>
<br>
It's a simple utility.
<br><br>
</td>
<td width=20></td>
<td><img style="vertical-align:top;" src="main_window.png" /></td>
</tr>
</table>
Problem is shown on this screenshot:
Here is how it should be and how it is in non-IE browsers:
Addition:
Here is how it looks using DIV code below :( Image is above the text:
Here's a more simple version. And yes, please avoid use table tags for general layout:
.prod-img {
margin-left: 20px;
float: right;
}
/* this is optional if you want to set an overall width */
.outer-container {
width: 700px;
}
<div class="outer-container">
<img src="http://placehold.it/350x150" class="prod-img">
<h1>Free Monitor Manager</h1>
<p>It's a simple utility.</p>
</div>
Fiddle here:
http://jsfiddle.net/mark47/5mt66fpL/
The CSS "floats" the image to the right side of the parent container. We're adding a margin-left: 20px to it so there's always some space between it and the text.
Setting an overall width is optional. Could also use max-width
Trust me, you still want to do this sort of thing with DIVs rather than a table.
Like this:
<div style="width:350px;float:left">
<h1>Free Monitor Manager</h1>
<br>
It's a simple utility.
</div>
<div style="position:relative">
<img style="vertical-align:top;" src="http://maps.wunderground.com/data/images/ne_rd.gif" />
</div>
In short, this builds two divs, one with your text that is set to the left, and one with the right that is set relative to it.
You can put whatever you want in either side.
But more importantly, this sort of thing is much more cross-browser friendly.

How (and why) to use display: table-cell (CSS)

I have a site with a very active background (I'm talking 6 or so different z-indexes here 2 with animations). I wanted a in the foreground that had content but wanted a "window" through to the background in it. Some problems I had:
you can't "punch a hole" in a background, so...
I built a containing div, lets call it "srminfo"
Inside that I had a "top", "left", "window", "right" and "bottom"
the top, left, right, bottom all had opaque white backgrounds
while the srminfo and window divs had background:none;
No matter how hard I tried, the "right" div wouldn't fill the space between the "top" and "bottom" divs, I tried a lot of different things. The reason it had to be dynamic is that the text in the "left" div was dynamic based on the background colour, which was itself generated randomly with JavaScript.
How is display: table; and all the other related CSS code like tables? And how can it be used?
After days trying to find the answer, I finally found
display: table;
There was surprisingly very little information available online about how to actually getting it to work, even here, so on to the "How":
To use this fantastic piece of code, you need to think back to when tables were the only real way to structure HTML, namely the syntax. To get a table with 2 rows and 3 columns, you'd have to do the following:
<table>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
Similarly to get CSS to do it, you'd use the following:
HTML
<div id="table">
<div class="tr">
<div class="td"></div>
<div class="td"></div>
<div class="td"></div>
</div>
<div class="tr">
<div class="td"></div>
<div class="td"></div>
<div class="td"></div>
</div>
</div>
CSS
#table{
display: table;
}
.tr{
display: table-row;
}
.td{
display: table-cell; }
As you can see in the example below, the divs in the 3rd column have no content, yet are respecting the auto height set by the text in the first 2 columns. WIN!
#table {
display: table;
padding: 5px;
}
.tr {
display: table-row;
padding: 5px;
}
.td {
display: table-cell;
padding: 5px;
width: 150px;
border: #000000 solid 1px;
margin: 5px;
}
<div id="table">
<div class="tr">
<div class="td">Row 1,
<br />Column 1</div>
<div class="td">Row 1, Column 2</div>
<div class="td" style="background:#888888;"></div>
</div>
<div class="tr">
<div class="td">Row 2,
<br />Column 1</div>
<div class="td">Row 2, Column 2</div>
<div class="td" style="background:#888888;"></div>
</div>
</div>
It's worth noting that display: table; does not work in IE6 or 7 (thanks, FelipeAls), so depending on your needs with regards to browser compatibility, this may not be the answer that you are seeking.
It's even easier to use parent > child selector relationship so the inner div do not need to have their css classes to be defined explicitly:
.display-table {
display: table;
}
.display-table > div {
display: table-row;
}
.display-table > div > div {
display: table-cell;
padding: 5px;
}
<div class="display-table">
<div>
<div>0, 0</div>
<div>0, 1</div>
</div>
<div>
<div>1, 0</div>
<div>1, 1</div>
</div>
</div>
How (and why) to use display: table-cell (CSS)
I just wanted to mention, since I don't think any of the other answers did directly, that the answer to "why" is: there is no good reason, and you should probably never do this.
In my over a decade of experience in web development, I can't think of a single time I would have been better served to have a bunch of <div>s with display styles than to just have table elements.
The only hypothetical I could come up with is if you have tabular data stored in some sort of non-HTML-table format (eg. a CSV file). In a very specific version of this case it might be easier to just add <div> tags around everything and then add descendent-based styles, instead of adding actual table tags.
But that's an extremely contrived example, and in all real cases I know of simply using table tags would be better.
The display:table family of CSS properties is mostly there so that HTML tables can be defined in terms of them. Because they're so intimately linked to a specific tag structure, they don't see much use beyond that.
If you were going to use these properties in your page, you would need a tag structure that closely mimicked that of tables, even though you weren't actually using the <table> family of tags. A minimal version would be a single container element (display:table), with direct children that can all be represented as rows (display:table-row), which themselves have direct children that can all be represented as cells (display:table-cell). There are other properties that let you mimic other tags in the table family, but they require analogous structures in the HTML. Without this, it's going to be very hard (if not impossible) to make good use of these properties.
I don't have 10 years of web dev., but only a year or so and I have quickly came around a use case that does not work with table elements and work with and CSS table : forms.
Forms and tables do not go well together. A form is not allowed to be a child of a table element. So, to comment previous comment : divs and CSS table are useful at least when you want forms into table.
Jean-yves

Right Align Text at end of line without table

I spent a little while trying to figure out how to achieve the following effect without using a table but couldn't figure it out: http://jsfiddle.net/sKFzA/
CSS :
.header{width:100%;font:25px Arial,Helvetica,sans-serif;}
.titleCol{width:99%;}
.dateCol{vertical-align:bottom;white-space:nowrap;}
.dateText{font-size:12px;}
HTML :
<table class="header">
<tr>
<td class="titleCol">This is the blog title</td>
<td class="dateCol"> <span> </span><span class="dateText">1/23/2012</span>
</td>
</tr>
</table>
To explain it, I have a blog title and a blog date. The title could be long and wrap. At the end of the last line, wrapped or not, I want the blog date to be aligned to the right.
So I have two questions. Is there any reason not to use a table for this? If so, how would you achieve it without assuming static font sizes?
CSS has properties that allow any element to behave like specific components of a table.
http://cssdeck.com/labs/rjiesryc
<header>
<h1>This is the blog title</h1>
<time datetime="2012-01-23">1/23/2012</time>
</header>
CSS
header {
display: table;
width: 100%;
}
header h1, header time {
display: table-cell;
}
header time {
/*vertical-align: bottom;*/
}
With the help of cimmanon and the others, I've gathered that:
The only reason's not to use a table here is because layout is not technically a table's intended purpose and also by not using a table you can separate your layout (CSS) from your markup (HTML). However, if I were to use a table, I am not aware of of any negative effects.
There doesn't seem to be a good solution to this exact layout without the concept of table, but my table solution can be achieved without using an HTML table by applying styles to display other elements as the table. So I replaced my table elements with divs. The span with the space before the date allows the smaller sized date to stay aligned to the title's baseline without having to hard-code line height's or font sizes. So if the font sizes change, I don't have to worry about updating any other magic numbers hard-coded around them.
http://jsfiddle.net/K35gT/
HTML
<div class="header">
<div class="titleCol">This is the blog title</div>
<div class="dateCol">
<span> </span><span class="dateText">1/23/2012</span>
</div>
</div>
Styles:
.header{display:table;width:100%;font:25px Arial,Helvetica,sans-serif;}
.titleCol{display:table-cell;width:99%;}
.dateCol{display:table-cell;vertical-align:bottom;white-space:nowrap;}
.dateText{font-size:12px;}
You do not need tables at all, simply block elements with the right styles.
If it was my website, I would do this:
<header>
<h1>This is the blog title</h1>
<time datetime="2012-01-23">1/23/2012</time>
</header>
Combined with this CSS:
header {position:relative; width:100%; font:25px Arial,Helvetica,sans-serif;}
header > h1 {margin:0px;}
header > time {display:block; font-size:12px; text-align:right;}
You can decide if you want to use HTML5 elements, or general elements and if you want to hook in class names or not. Here's the jsFiddle for above: http://jsfiddle.net/sKFzA/13/
Something like this? I hope i got you right.
HTML:
<div id="titleRow">This is the blog title</div>
<div id="dateText"><span id="spandate">1/23/2012</span></div>
CSS:
#titleRow{width:80%; height: 25px; font:25px Arial,Helvetica,sans-serif;
float:left;text-align: left;}
#dateText{width:20%; height: 25px; font-size:12px;float:left; text-align: right; position: relative;}
#spandate { position: absolute; bottom: 0; right: 0;}
See here: http://jsfiddle.net/sKFzA/31/

How to correctly use tableless layout with borders using CSS?

I'm working on a new HTML page and I want to use a table-less layout. Bear in mind that what follows here is only part of the page, but I think it paints a clear picture of what I'm trying to do.
The HTML below is meant to render six cells with text inside. I want the cells to be sized appropriately to contain the text inside.
The problem I'm having is that the borders are drawn incorrectly. In both IE and Firefox, I see two problems:
1) One of the borders is drawn outside the table.
2) The borders between the cells in the first row are drawn incompletely.
<!DOCTYPE html>
<html>
<head>
<style>
html
{
}
.reviewRow
{
clear:both;
}
.reviewBlock
{
float:left;
border-top: 1px solid #444;
border-left: 1px solid #444;
}
.rightBorder
{
border-right: 1px solid #444;
}
.bottomBorder
{
border-bottom: 1px solid #444;
}
</style>
</head>
<body>
<div class='reviewRow'>
<div style="width:200px;" class='reviewBlock'>
THIS TEXT IS MUCH LONGER THAN THE TEXT IN THE OTHER CELLS
</div>
<div style="width: 225px" class='reviewBlock'>
ABC
</div>
<div style="width: 100px" class="reviewBlock rightBorder">
December 25, 2012
</div>
</div>
<div class='reviewRow bottomBorder'>
<div style="width:300px;" class='reviewBlock'>
Hello, World!
</div>
<div style="width: 125px" class='reviewBlock'>
123
</div>
<div style="width: 100px" class="reviewBlock rightBorder">
May 1, 2013
</div>
</div>
<div style='clear:both;'></div>
</body>
</html>
Don't go what others say, that don't make layouts using tables, but when it comes to tabular data and if you use div's for making a table doesn't make any sense to me, just don't use tables for designing layouts, but YOU SHOULD USE IT FOR TABULAR DATA
Still if you want to use you can refer this
By the way, to answer the question... You can use display:table, table-cell and table-row (in CSS3)
But I agree with Mr. Alien : just use tables for tabular data.
See the height of html elements are computed based on the content inside them unless you explicitly specify it.
Your second div has taken only the height necessary to show ABC and hence the border showed up only that much. To fix this you must specify a certain height to each of the div so that they appear just as you want.
If you are trying to show data in a tabular manner just use tables. They are there for that purpose only. You can obviously style them in order to make them better looking.
If you're trying to do this for tabular data, then by all means use a table that's what it's there for. Not doing so it about like trying to use Photoshop to make a spreadsheet when Excel would be the better tool.
You can change the number of columns a cell takes up by using the colspan attribute. By default, of course, a cell takes up 1 column (colspan="1"), but if you need it to take up more, you simply change the number. You can do the same thing for rows with rowspan.

css/html Simple progress bar using hr tag

I'm trying to make a simple progress bar using the width property of an HR representing percent complete. I did these years ago but for some reason I can't seem to make this one look right. The website uses tables so I am placing the bar within a table cell. I'm open to doing it with pure css but I would like this to be light as in a few lines of code as there is a lot of other stuff on this page that has to load quickly.
I can get the rule to display a nice solid bar.
<td width=200 style="border: 2px solid silver;padding:none"><hr style="color:#c00;background-color:#c00;height:15px; border:none;" align="left" width=50% /></td>
where the percentage complete is set on the server side with php. This is not a dynamic bar where polling is required. Just one that represents percentage of a profile that is complete.
Ideally, I'd like the bar to fill a percentage of the cell equal to that completed so that the solid bar shows what percent is done and the white space to the right what part remains. However, I can't get a nice looking rule around the whole cell for the bar to fill a portion of. Instead there is a lot of space around the bar. I've tried setting the padding to none but that doesn't seem to work.
Here is a jsfiddle.
http://jsfiddle.net/GqrnC/
Thanks for any suggestions.
You have to remove the margins from the <hr> (add margin: 0 to its styles) to get rid of the extra space: http://jsfiddle.net/GqrnC/1/
<table>
<tr>
<td width=200 style="border: 2px solid silver;padding:none">
<hr style="color:#c00;background-color:#c00;height:15px; border:none;
margin:0;" align="left" width=50% />
</td>
</tr>
</table>
However, I agree with the comments above, I don't see any reason to use an <hr> instead of a <div> for this. The <hr> has the semantics of a "separator", which is not the case here. A div has neutral meaning.
Does this fit your needs better: http://jsfiddle.net/GqrnC/16/
You should really use <div> instead of a <hr> since a <div> is a structural element.
HTML
<div class='outer'>
<div class='inner'>
</div>
</div>
CSS
.outer{
border: 2px solid silver;
width: 200px;
}
.inner{
background-color: #c00;
width: 50%;
height: 5px;
}
Alternatively, I would recommend looking into the jQuery UI progress bar. It is built exactly for this case and looks really great (plus if you want to later, you can update it using AJAX).
​