I've been looking for a way to produce the following HTML table with CSS:
<style type="text/css">
table.frm tr td { vertical-align: top; padding-right: 10px; }
</style>
<table class="frm">
<tr>
<td rowspan="2">Label 1:</td>
<td><input type="text" /></td>
<td rowspan="2">Label 2:</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Validation Message 1</td>
<td>Validation Message 2</td>
</tr>
<tr>
<td rowspan="2">Label 3:</td>
<td><input type="text" /></td>
<td rowspan="2">Label 4:</td>
<td><input type="text" /></td>
</tr>
<tr>
<td>Validation Message 3</td>
<td>Validation Message 4</td>
</tr>
</table>
The problem I've been having when I try to replace this with divs is that I can't align both the columns and the rows. I've tried using floats to align the columns, but then I loose the vertical alignment of the rows. But if I use a clear to align the rows, I loose the horizontal alignment of the columns.
Many of the examples I've seen for converting tables to divs use fixed or percentage widths, but I want the layout to have the same fluid behavior of the table since the validation messages may or may not appear and the labels/fields will have varying sizes.
Is there a designer out there who can show me how this layout can be achieved without tables?
This is not a problem with display (CSS2), but it requires IE7+. Please see this example fiddle:
Markup:
<form>
<span>
<label for="edit1">First label:</label><input id="edit1" type="text" />
<label for="edit2">Second label:</label><input id="edit2" type="text" />
</span>
<span>
<br /><p>That sounds right!</p>
<br /><p>Problem!</p>
</span>
<span>
<label for="edit3">3:</label><input id="edit3" type="text" />
<label for="edit4">Fourth and last label:</label><input id="edit4" type="text" />
</span>
<span>
<br /><p>No succes. Try again and enter another value.</p>
<br /><p>Wait...</p>
</span>
</form>
Style sheet:
form {
display: table;
}
form span {
display: table-row;
}
form span * {
vertical-align: top;
padding-right: 10px;
display: table-cell;
}
The problem you're facing is you are looking at each cell as its own little block of data. You need to look at the bigger picture. What is that collection of data? Is it actually a list of things? Does it belong in a ul or dl?
Quit trying to make this look like a table with different elements when it's not a table at all. And tables should never be used for layout.
Fluid is very vague term, by fluid do you mean its width can expand and contract like a table does for different size screens, or when the browser window is resized? Or do you want he column widths to all adjust to same width as the cell with the longest/largest content?
The first is done like so
<div style="position: absolute; width: 100%;">
<div style="position: relative; width: 100%; clear: both;">
<div style="position: relative; width: 50%; float: left;"> </div>
<div style="position: relative; width: 50%; float: left;"> </div>
</div>
<div style="position: relative; width: 100%; clear: both;">
<div style="position: relative; width: 50%; float: left;"> </div>
<div style="position: relative; width: 50%; float: left;"> </div>
</div>
</div>
That should simulate two rows with two columns in each row and widths are based on the screen size.
The second with the columns adjusting based on the content size.
<div style="position: absolute; width: 100%;">
<div style="position: relative; float: left;">
<div style="position: relative; min-width: 1%; max-width: 75%;"> </div>
<div style="position: relative; min-width: 1%; max-width: 75%;"> </div>
</div>
<div style="position: relative; float: left;">
<div style="position: relative; min-width: 1%; max-width: 75%;"> </div>
<div style="position: relative; min-width: 1%; max-width: 75%;"> </div>
</div>
</div>
That is two columns with with two rows each. Set the "min-width" and "max-width" to whatever you need. I don't think I have ever had a reason to test anything like this, so if it doesn't work you could try setting "display: inline" for each row.
And If neither of these work try posting your test code. I am certain you can make a tables out of divs. A while back after several years of not using tables I inadvertently made a site, that should have used tables, out of divs. What your looking to do can be done it is just a matter of working the CSS.
Related
I need to have a div (divParent) which needs to have 2 other divs (divContainer, divButtons) where the divButton will display at the very bottom of the DivParent and the divContainer will use the entire remaining space of the divParent up to the divButton, but it cannot overlap the divButtons and if the content of the divContainer is too big, I need the vertical scrollbar to be displayed.
I've got the following more or less working but the divContainer seems to be overlapping the divButtons and it does not display the vertical scrollbar if the content is too big, even when I specify overflow: auto or overflow-y: auto.
<div id="divParent" style="width: 100%; height: 100%; background-color: red; position: relative">
<div id="divContainer" style="overflow-y:auto;">
<table id="fields">
<large content goes here>
</table>
</div>
<div id="divButtons" style="width: 100%; background-color: blue; position: absolute; bottom: 0">
<div style="float:right;">
<table>
<tr>
<td style="padding: 2px">
<div id="submitbutton">test1</div>
</td>
<td style="padding: 2px">
<div id="cancelbutton">test2</div>
</td>
</tr>
</table>
</div>
</div>
</div>
If I specify "max-height:100px" on the divContainer to test, it does exactly what I need where the vertical scrollbar is displayed but it's clearly no longer stretched all the way to the divButton.
Note that the divParent is then used in a third-party popup window.
Any suggestions?
Thanks.
I eventually figured it out, but credit to #Brad for his answer in:
from How do I make a div stretch in a flexbox of dynamic height?
I had to rejig a few things but eventually got there and my divs are defined as follows now:
<div id='divParent' style='display: flex;
flex-direction: column; height: 100%; width: 100%;'>
<div id='divContainer' style='width: 100%; flex-grow:1;
overflow: auto'>
<div id='divButtons' style='width: 100%; height: 40px;
margin-top: 5px'>
That'it!
Install bootstrap and you will have great control of div placements. Bootstrap creates 12 columns for each row:
<div class="row">
<div class="col-md-7" align="right">
<div class="row">
</div>
<div class="row">
</div>
</div>
<div class="col-md-3" align="left">
</div>
<div class="col-md-2" style="padding-right:2%;">
</div>
</div>
How do I align the text as in the picture below?
<div id="contact-info">
<div id="contact-list">
<div id="adresa">
<img src="http://avocat.dac-proiect.ro/wp/wp-content/themes/twentyfourteen/images/ADRESA.png" style="width:22px;height:31px;float:left;">
<p style="text-align:center;">Calea Dorobantilor,nr.74</p>
<p style="text-align:center;">,bl.Y9,SC.2,Ap.25,Cluj-Napoca,400609,Romania</p>
</div
<div id="telefon"></div>
<div id="mail"></div>
</div>
</div>
#contact-info
{
width:300px;
height:300px;
background:url(images/BODY-CONTACT.png);
position:absolute;
right:0;
}
How can I solve this problem?
Fail to fix it as I want
www.avocat.dac-proiect.ro/wp
Generally, you should use div to nesting elements, in order to align them in a decent way. Also pay attention to the display:blockorinline. You could read more in W3C docs. My touch to this problem is as follow:
<div id="adresa">
<div id="addPadding" style="
padding: 2em;">
<img src="http://avocat.dac-proiect.ro/wp/wp-content/themes/twentyfourteen/images/ADRESA.png" style="width:22px;height:31px;float:left;display: inline;">
<div style="
float: right;
display: inline;
width: 80%;
">
<p style="text-align:left;">Calea Dorobantilor,nr.74,</p>
<p style="text-align:left;">bl.Y9,SC.2,Ap.25,<br>Cluj-Napoca,400609,
<br>Romania</p>
</div>
</div>
</div>
I used 2<div>, one wrap the two <p> and the other one wrap the <img>and the new '' (or you can just simply add padding on the <div id="adresa">).
it will get a more similar layout result to your mockup, I wish I could took screen shot for you.
you just need to fix the text-align:left and margin on <p>tag to finish your job.
NOTE: This is NOT the whole solution. I just gave you an idea about what approach should be used.
This is so simple ... For this you need to make <p> and <img> position: absolute;. like below
.centered {
position: absolute;
right: 12px;
top: 110px;
}
and add class to ps and img like
<p class="centered">....</p>
<img class="centered" src="...." />
Try this using different top and right values for each p and `img.
Although the CSS purists would tell you not to, I would just add a table
<table>
<tr>
<td>
<img src="http://avocat.dac-proiect.ro/wp/wp-content/themes/twentyfourteen/images/ADRESA.png" style="width:22px;height:31px;">
</td>
<td valign="top">
<p style="text-align:center;">Calea Dorobantilor,nr.74</p>
<p style="text-align:center;">,bl.Y9,SC.2,Ap.25,Cluj-Napoca,400609,Romania</p>
</td>
</tr>
</table>
here's my code:
<div class="search-option">
<div id="slider-days" class="search-opt-left"></div>
<input type="text" id="amount-days" class="search-opt-right input-small"/>
</div>
I want to make the slider and input field algin to the middle of the line.
How can i do that ? Thanks !
My CSS:
.search-opt-left {
float: left;
width: 250px;
vertical-align: middle;
}
.search-opt-right {
float: left;
margin-left: 20px;
}
This can be buggy.
What I usually do is make a table. And since table rows start with vertically-centered alignments, it's quite simple.
<div class="search-option">
<table cellspacing="0" cellpadding="0">
<tr>
<td>
<div id="slider-days" class="search-opt-left"></div>
</td>
<td>
<input type="text" id="amount-days" class="search-opt-right input-small"/>
</td>
</tr>
</table>
</div>
You may then alter your CSS to get rid of the un-needed stuff.
Good day,
I've got a simple CSS question.
I'm trying to change a table into a CSS box, but aligning the table content is difficult to me right now.
Below there's an example of what's inside of the css box i created. How can I align these three elements (calendar and icon to the left , a text link to the center, and the other date field to the right ?) correctly ?
I've tried several things but the problem is getting it aligned properly. I want to change everything in this application that has been created with tables, to css. And I've been an 80% succesful so far.
I'd like to see some easy to understand code, to see how I can apply it on my code.
Thank you for your kind help. I might be burned out due to stress.
[Calendar (icon) Link Date]
UPDATE #1
This is the code for what I'm saying:
<asp:UpdatePanel runat="server" ID="updHoldingsPanel" UpdateMode="Always">
<ContentTemplate>
<div class="sitenote">
<table valign="absmiddle" border="0" cellpadding="0" cellspacing="0">
<tr style="height: 19px">
<td valign="absmiddle" style="text-align: left; width: 9%;">
<asp:Panel ID="pnlDateZero" runat="server" Width="187px">
<table valign="middle" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="middle">
<asp:Label ID="Label1" runat="server" Text="As of" Width="40px"></asp:Label></td>
<td valign="middle" style="width: 80px">
<asp:TextBox ID="txtDate" runat="server" AutoPostBack="True" Width="80px" Height="15px" OnTextChanged="txtDate_TextChanged" ></asp:TextBox>
<%--<asp:TextBox ID="txtDate" runat="server" AutoPostBack="True" Width="80px" Height="15px" contentEditable="false" OnTextChanged="txtDate_TextChanged" ></asp:TextBox>--%>
</td>
<td valign="absmiddle">
<span style="float:left; vertical-align:top; padding-top:0px; padding-top:1px;">
<asp:ImageButton align="middle" ID="imgCalendar" runat="server" ImageUrl="~/images/calendar5.gif"/>
<%--<cc1:CalendarExtender ID="ceDate" runat="server" PopupButtonID="imgCalendar" Format="MM/dd/yyyy" TargetControlID="txtDate" FirstDayOfWeek="Monday"></cc1:CalendarExtender>--%>
</span>
</td>
</tr>
</table>
</asp:Panel>
<asp:Label ID="lblAsOf" Text="" runat="server" Visible="False"></asp:Label></td>
<td style="text-align:center; width: 27%;">
</td>
<td style="text-align:center; width: 11%;">
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LINK</asp:LinkButton>
</td>
<td style="text-align:left; width: 2%;">
<asp:UpdateProgress AssociatedUpdatePanelID="updHoldingsPanel" id="UpdateProgress1" runat="server" DisplayAfter="100" DynamicLayout="false">
<ProgressTemplate>
<asp:Image ID="Image3" runat="server" ImageUrl="~/images/live_com_loading.gif">
</asp:Image>
</ProgressTemplate>
</asp:UpdateProgress>
</td>
<td valign="absmiddle" style="text-align: right; width: 1%;">
</td>
<td style="text-align: right; valign="absmiddle">
<asp:CheckBox ID="chkInclude" runat="server" AutoPostBack="true"
OnCheckedChanged="chkInclude_CheckedChanged" Text="Include Zero Holdings"
VerticalAlign="Middle" />
</td>
</tr>
</table>
</div>
AND THE CSS OF THE BOX IS :
.sitenote {
display:block;
padding:6px;
border:1px solid #bae2f0;
background:#e3f4f9;
line-height:130%;
font-size:13px;
margin-top: 0;
margin-right: 0;
margin-bottom: 0.5em;
margin-left: 0;
}
You need to combine float:left and float:right elements and text-align css property
Full code at : http://jsbin.com/ilano3/3/edit
<div style="float:left">left</div>
<div style="float:right">right</div>
<div style="text-align:center">center</div>
You can use something like this, I think that it's the simplest approach:
<style>
.wrapper {
width: 600px;
}
.column {
float: left;
width: 200px;
}
.first {
text-align: left;
}
.second{
text-align: center;
}
.third{
text-align: right;
}
</style>
<div class="wrapper">
<div class="column first">
icon
</div>
<div class="column second">
link
</div>
<div class="column third">
date
</div>
</div>
You can add CSS for .first, .second and .third to change their width, text alignment, color...
http://jsfiddle.net/T8JMM/2/
If you really need your data/text in columns, you shouldn't shun tables. Tables are used for tabular data and if that's what your data is then just keep using them. The only bad thing about tables is when you use it for design. It looks to me that what you are trying to display is tabular data so just use 'em :)
To check if your data is bound to be shown in tables, answer the following questions:
Should the icon, link and date be on the same row ALWAYS?
Is the date on the right side bound to the link on the left side?
If your answer is yes to both questions, just use a table.
Otherwise, use lists. Separate the icon, link and date data into lists like this:
<ul class="icon-list column">
<li>Calendar (icon)</li>
<li>Calendar (icon)</li>
<li>Calendar (icon)</li>
</ul>
<ul class="link-list column">
<li>Link</li>
<li>Link</li>
<li>Link</li>
</ul>
<ul class="date-list column">
<li>Date</li>
<li>Date</li>
<li>Date</li>
</ul>
Then using css, just float them and add some width:
.column {
float: left;
width: 33%;
}
Don't forget to clear the floats! :)
Try something like this:
<style type="text/css" media="all">
.row { border: 1px solid black; text-align: center; }
.row > .left { float: left; }
.row > .center { display: inline-block; }
.row > .right { float: right; }
/* after/inline-block (FF, IE5.5-7, S2, O9) */
.row:after { content: ""; display: block; height: 0; clear: both; visibility: hidden; }
.row { display: inline-block; }
.row { display: block; }
</style>
<div class="row">
<div class="left">Calendar (icon)</div>
<div class="center">Link</div>
<div class="right">Date</div>
</div>
The left and right divs are simply positioned with float and are contained within the float by the .row:after… CSS at the end. The center div will accept the row's text-align: center, since it's inline-block.
Note that the center piece is aligned based on the available space between the floats; multiple rows have have mis-aligned centers. Also, as it stands, too narrow of a width will cause the last piece (of left/center/right) to go to a new line; there may be a way to truncate it with overflow, but that depends on what you're going for.
Hope this helps!
Alright, I'm trying to buy into the idea that html tables should not be used, and that divs should be. However, I often have code that resembles the following
<table>
<tr>
<td>First Name:</td>
<td colspan="2"><input id="txtFirstName"/></td>
</tr>
<tr>
<td>Last Name:</td>
<td colspan="2"><input type="text" id="txtLastName"/></td>
</tr>
<tr>
<td>Address:</td>
<td>
<select type="text" id="ddlState">
<option value="NY">NY</option>
<option value="CA">CA</option>
</select>
</td>
<td>
<select type="text" id="ddlCountry">
<option value="NY">USA</option>
<option value="CA">CAN</option>
</select>
</td>
</tr>
</table>
I want the labels to be aligned and I want the controls to be aligned. How would I do this without using tables?
This ought to do the trick.
<style>
div.block{
overflow:hidden;
}
div.block label{
width:160px;
display:block;
float:left;
text-align:left;
}
div.block .input{
margin-left:4px;
float:left;
}
</style>
<div class="block">
<label>First field</label>
<input class="input" type="text" id="txtFirstName"/>
</div>
<div class="block">
<label>Second field</label>
<input class="input" type="text" id="txtLastName"/>
</div>
I hope you get the concept.
Please be aware that although tables are discouraged as a primary means of page layout, they still have their place. Tables can and should be used when and where appropriate and until some of the more popular browsers (ahem, IE, ahem) become more standards compliant, tables are sometimes the best route to a solution.
I looked all over for an easy solution and found this code that worked for me. The right div is a third column which I left in for readability sake.
Here is the HTML:
<div class="container">
<div class="row">
<div class="left">
<p>PHONE & FAX:</p>
</div>
<div class="middle">
<p>+43 99 554 28 53</p>
</div>
<div class="right"> </div>
</div>
<div class="row">
<div class="left">
<p>Cellphone Gert:</p>
</div>
<div class="middle">
<p>+43 99 302 52 32</p>
</div>
<div class="right"> </div>
</div>
<div class="row">
<div class="left">
<p>Cellphone Petra:</p>
</div>
<div class="middle">
<p>+43 99 739 38 84</p>
</div>
<div class="right"> </div>
</div>
</div>
And the CSS:
.container {
display: table;
}
.row {
display: table-row;
}
.left, .right, .middle {
display: table-cell;
padding-right: 25px;
}
.left p, .right p, .middle p {
margin: 1px 1px;
}
You can create simple float-based forms without having to lose your liquid layout. For example:
<style type="text/css">
.row { clear: left; padding: 6px; }
.row label { float: left; width: 10em; }
.row .field { display: block; margin-left: 10em; }
.row .field input, .row .field select {
width: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; -khtml-box-sizing: border-box;
}
</style>
<div class="row">
<label for="f-firstname">First name</label>
<span class="field"><input name="firstname" id="f-firstname" value="Bob" /></span>
</div>
<div class="row">
<label for="f-state">State</label>
<span class="field"><select name="state" id="f-state">
<option value="NY">NY</option>
</select></span>
</div>
This does tend to break down, though, when you have complex form layouts where there's a grid of multiple fixed and flexible width columns. At that point you have to decide whether to stick with divs and abandon liquid layout in favour of just dropping everything into fixed pixel positions, or let tables do it.
For me personally, liquid layout is a more important usability feature than the exact elements used to lay out the form, so I usually go for tables.
Basically it boils down to using a fixed-width page and setting the width for those labels and controls. This is the most common way in which table-less layouts are implemented.
There are many ways to go about setting widths. Blueprint.css is a very popular css framework which can help you set up columns/widths.
there is a very useful online tool for this, just automatically transform the table into divs:
http://www.html-cleaner.com/features/replace-html-table-tags-with-divs/
And the video that explains it: https://www.youtube.com/watch?v=R1ArAee6wEQ
I'm using this on a daily basis. I hope it helps ;)