CSS alignment difficulty [duplicate] - html

This question already has answers here:
text in table cells not centered as in headers
(2 answers)
Closed 8 years ago.
CSS:
.one {
width: 13%;
}
.two {
width: 30%;
}
.three {
width: 30%;
}
HTML:
<table>
<tr>
<th class= "one">Quantity</th>
<th class= "two">Info</th>
<th class= "three">Price</th>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity1" value=""/>
<td class = "two">Cheap Monday Jeans 30/34 </td>
<td class = "three">$39.99 </td>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity2" value=""/></td>
<td class = "two">Herschel Bag (free) </td>
<td class = "three">$129.99 </td>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity3" value=""/></td>
<td class = "two">Diesel t-shirt (s) </td>
<td class = "three">$59.99 </td>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity4" value=""/></td>
<td class = "two">Superdry Patrol Lite Jacket (m) </td>
<td class = "three">$129.99 </td>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity5" value=""/></td>
<td class = "two">I love Canada t-shirt (s) </td>
<td class = "three">$19.99 </td>
</tr>
</table>
I want the table rows (clothes information & prices) to be aligned right below the table headers. Meaning, I want it to be centered. I don't know why table rows are skewed to the left and cannot be aligned right below to the headers.
Even tried
td { text-align: center; }, but doesn't work.
Help would be appreciated, thank you.

Left align the th header elements. You could also center align the td elements but it doesn't look as pretty.
th.one {
text-align:left;
}
th.two {
text-align:left;
}
th.three {
text-align:left;
}
.one {
width: 13%;
}
.two {
width: 30%;
}
.three {
width: 30%;
}

Hey see this demo hope it works for you
link
These are the only changes you have to do in your CSS part :
.one {
width: 1%;
text-align:center;
}
.two {
width: 10%;
text-align:center;
}
.three {
width: 10%;
text-align:center;
}
The output that i got was like this :
I hope this is what you want .

you should make the <th> elements align left:
th {
text-align:left;
}
Also, you should consider using the <colgroup> tag, with nested <col> tags to style your table columns, as such:
<table>
<colgroup>
<col class="one" />
<col class="two" />
<col class="three" />
</colgroup>
<tr>
<th>Quantity</th>
<th>Info</th>
<th>Price</th>
</tr>
<tr>
<td><input type="text" name="quantity1" value=""/>
<td>Cheap Monday Jeans 30/34 </td>
<td>$39.99 </td>
</tr>
...rest of the code
This way, you don't have to add the "class" attribute to ALLLLL the <td>s.

Is that what you want? DEMO http://jsfiddle.net/yeyene/u7WzM/2/
Right align, and center.
th.one, td.one {
text-align:center;
}
th.two {
text-align:center;
}
td.two {
text-align:right;
padding-right:5%;
}
th.three, td.three {
text-align:center;
}
.one {
width: 13%;
}
.two {
width: 30%;
}
.three {
width: 30%;
}

Add text-align:center; to the css classes.
.one {
width: 13%;
text-align:center;
}
.two {
width: 30%;
text-align:center;
}
.three {
width: 30%;
text-align:center;
}
Or add
td
{
text-align:center;
}

The problem is that you want something left aligned to line up beneath something center aligned. Your best bet is probably to left align the headings and using some padding to provide space between the columns.
HTML:
<table>
<tr>
<th class= "one">Quantity</th>
<th class= "two">Info</th>
<th class= "three">Prices</th>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity1" value=""/></td>
<td class = "two">Cheap Monday Jeans 30/34 </td>
<td class = "three">$39.99 </td>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity2" value=""/></td>
<td class = "two">Herschel Bag (free) </td>
<td class = "three">$129.99 </td>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity3" value=""/></td>
<td class = "two">Diesel t-shirt (s) </td>
<td class = "three">$59.99 </td>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity4" value=""/></td>
<td class = "two">Superdry Patrol Lite Jacket (m) </td>
<td class = "three">$129.99 </td>
</tr>
<tr>
<td class = "one"><input type="text" name="quantity5" value=""/></td>
<td class = "two">I love Canada t-shirt (s) </td>
<td class = "three">$19.99 </td>
</tr>
</table>
And CSS:
.one {
width: 13%;
}
.two {
width: 30%;
}
.three {
width: 30%;
}
th {
text-align: left;
}
.two, .three {
padding-left: 10%;
}
For example: http://jsfiddle.net/cpmmk/

Related

how to prevent the tbody from overlapping with tfoot

I would like to create a html template that when print in chrome browser, the header and footer will fixed on the top and bottom of the page. The tbody will be in between of header and footer. It's height is the total height after minus header and footer. If the tbody exceed the height, it will break into next page. Now, I am having issue that when printing the html template, the tbody will overlap the tfoot and thead on the next page, as shown in these pictures. Please kindly advice. My html template is on the below github link.
.
The HTML is in my github repository. Please help to check on this link.
MyTemplate
Below is my code and my reference for dealing page break for large html table.
Reference
This is a short snippet of my code structure
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
<style type="text/css">
html,
body,
* {
margin: 0;
padding: 0;
}
table {
page-break-inside: auto;
border-collapse: collapse;
}
tr {
page-break-inside: avoid;
page-break-after: auto
}
td {
page-break-inside: avoid;
page-break-after: auto
}
div {
page-break-inside: avoid;
}
thead {
display: table-header-group;
}
tfoot {
display: table-footer-group
}
/* tbody {
display: table;
width: 100%;
table-layout: fixed;
} */
#media screen {
div.divFooter {
display: none;
}
div.divHeader {
display: none;
}
}
#media print {
div.divFooter {
position: fixed;
height: 250px;
bottom: 0;
left: 0;
text-align: left;
display: block;
}
div.divHeader {
position: fixed;
height: 100px;
top: 0;
left: 0;
display: block;
}
div.divBody {
height: calc(100% - 350px);
position: relative;
text-align: left;
margin-top: 20rem;
margin-bottom: -20rem;
page-break-after: always;
}
}
</style>
</head>
<body>
<table style="width: 100%;">
<thead>
<tr>
<th>
<div class="divHeader" style="display: inline-block; width: 100%;">
<table style="width: 100%; text-align: left;">
<tr>
<td><img style="width: 120px; height: 120px;"
src="https://bp.doctor2u.my/assets/img/bplogo.png">
</td>
<td style="vertical-align: top; ">
<table>
<tr style="margin-left: 1 rem;">
<td colspan="2">
<a
style="font-family:Helvetica;font-weight: bold; font-size: 20pt;">
PATHOLOGY
REPORT
</a>
</td>
</tr>
<tr>
<td colspan="2">
<span>B. P. CLINICAL LAB SDN. BHD. (152314-H)</span>
</td>
</tr>
<tr>
<td>
<span>[BranchAddress] ,</span>
</td>
<td>
<span> [BranchEmail]</span>
</td>
</tr>
<tr>
<td>
<label>Tel : </label><span>[BranchTel]</span>
</td>
<td>
<label>Fax : </label><span>[BranchFax]</span>
</td>
</tr>
<tr>
<td colspan="2">
<label>GST Reg No. : </label><span>[GST]</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
<table style="width: 100%; text-align: left;">
<tr>
<td>
<label>Client PatientID :</label><span>[ClientId]</span>
</td>
<td colspan="2">
<label>Barcode :</label><span>[LabNo]</span>
</td>
</tr>
<tr>
<td colspan="2">
<label>Patient Name :</label><span>[PatientName]</span>
</td>
<td>
<label>Age/Gender :</label><span>[Age] / [Gender]</span>
</td>
</tr>
<tr class="spaceUnder">
<td>
<label>NRIC No :</label><span>[IC]</span>
</td>
<td>
<label>Refer No :</label><span>[ReferNo]</span>
</td>
<td>
<label>Branch :</label><span>[BranchName]</span>
</td>
</tr>
<tr>
<td colspan="2">
<label>Client Name :</label><span>[ClinicName]</span>
</td>
<td>
<label>Tel :</label><span>[Tel]</span>
</td>
</tr>
<tr class="spaceUnder2">
<td colspan="2">
<label>Doctor’s Name :</label><span>[DoctorName]</span>
</td>
<td>
<label>Fax:</label><span>[Fax]</span>
</td>
</tr>
<tr>
<td colspan="3">
<label>Specimen :</label><span>[Specimen]</span><span>-sampled at
</span><span>[SampleDate]</span>
</td>
</tr>
<tr>
<td colspan="3">
<label>Received :</label><span>[LabNo] by [ReceivedBranch]
[ReceiveDate]</span>
</td>
</tr>
</table>
<label for="" style="float: right;">Page 2 of 3</label>
<table
style="border-top: 5px solid black; width: 100%; border-spacing: 1rem; text-align: center;"
class="spaceUnderTable">
<tr class="spaceUnder">
<th>
<label for="">TEST NAME</label>
</th>
<th>
<label for="">RESULT</label>
</th>
<th>
<label for="">UNIT</label>
</th>
<th>
<label for="">REFERENCE NOTE</label>
</th>
<th>
<label for="">RESULT</label>
</th>
<th>
<label for="">UNIT</label>
</th>
<th>
<label for="">REFERENCE NOTE</label>
</th>
</tr>
</table>
</div>
</th>
</tr>
</thead>
<tfoot>
<tr>
<th>
<div class="divFooter" style="display: inline-block; width: 100%; left:0px;
bottom:0px; text-align: left;">
<table style="width: 100%; margin-top: 1rem;">
<tr>
<td rowspan="7" style="width: 20%;">
<!-- [Image] -->
</td>
</tr>
<tr>
<td>
Results validated and authorised through the Laboratory information
System.
</td>
</tr>
<tr>
<td>
Results checked and authorised for release
</td>
</tr>
<tr>
<td>
Test done at B.P. Clinical Lab Sdn. Bhd. (Ipoh) 273-B Jalan Raja
Permaisuri
Bainun,
30250
Ipoh, Perak.
</td>
</tr>
<tr>
<td>
Tel: 05-2559090 Fax:05-2419226
</td>
</tr>
<tr>
<td>
Ipoh lab is a MS ISO 15189 accredited lab
</td>
</tr>
<tr>
<td>
* Not SAMM Accredited
</td>
</tr>
<tr>
<td colspan="4">
<p style=" margin-top: 1rem; text-align: center;">
For further confirmation, please
repeat test with another
fresh
specimen, if desired.
Should
you
have further enquiries, please
contact your nearest BP branch or mail us at
corpcare#bphealthcare.com
<br>
[BranchAddress], [BranchEmail]
Tel: <span>[Tel]</span>,
Fax: [Fax]
</p>
</td>
</tr>
</table>
</div>
</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>
<div class="divBody">
<table style="width: 100%; border-spacing: 1rem; text-align: center;"
class="spaceUnderTable">
<!-- LIPIDS STUDIES -->
<tr>
<td style="text-align: left;">
<label for=""><strong>Full Blood Count</strong></label>
</td>
</tr>
<tr>
<td style="text-align: left;">
<label for="">Total RBC</label>
</td>
<td>
<label for="">[TotalRBCResult]</label>
</td>
<td>
<label for="">[TotalRBCUnit]</label>
</td>
<td>
<label for="">[TotalRBCRange]</label>
</td>
<td>
<label for="">[TotalRBCResult2]</label>
</td>
<td>
<label for="">[TotalRBCUnit2]</label>
</td>
<td>
<label for="">[TotalRBCRange2]</label>
</td>
</tr>
<!-- More row -->
<br>
<br>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>

How can I get the information table and image aligned on the same line

I am trying to make a web-page where I have a table of information and an image side-by-side as in the example below
Example:
bar fOo
Instead of:
bar
fOo
I have placed the image within a div and the information table in another div element, and I have been playing with the CSS properties to try to get them to be side-by-side, however they refuse to work as expected.
The CSS is below, #book is the image itself, while book_information is the information inside the table.
#book
{
float: right;
flex: 33.33%;
padding: 5px;
margin-left: 0;
width: 50%;
}
#book_information
{
width: 50%;
float: left;
padding-right: 0;
margin-right: 0;
}
table
{
border-collapse: collapse;
float:top;
border: 1px solid black;
table-layout: fixed;
width: 50%;
}
What should I use to allow this to work as expected? and what improvements could work I work on to get it responsive?
Below is the html structure of the page at the moment that I am using along with this:
<main>
<div>
<div class= "left" id="book"><img src="Book.jpg" alt="></div>
<div class="left" id="book_information">
<table id="information">
<tr>
<td class="1">Price:</td>
<td class="1">€18.90</td>
</tr>
<tr>
<td>Author</td>
<td></td>
</tr>
<tr>
<td class="1">About</td>
<td class="1"><p></p></td>
</tr>
<tr>
<td>Where to get it:</td>
<td>Amazon ,
Casa Del Libro ,
FNAC ,
Libelista
</td>
</tr>
</table>
</div>
</div>
</main>
There are different ways of doing this.
Here's a solution that involves using float:left style for your table:
table {
float: left;
}
img {
width: 100px;
height: 100px;
}
<table>
<tr>
<td>Price</td>
<td>$5</td>
</tr>
<tr>
<td>Author</td>
<td>Bob</td>
</tr>
</table>
<div>
<img
src="https://via.placeholder.com/120.png?text=Book+Image"
alt="Book Image Here">
</div>
This one involves putting your table and image in yet another table:
img {
width: 100px;
height: 100px;
}
<table>
<tr>
<td>
<table>
<tr>
<td>Price</td>
<td>$5</td>
</tr>
<tr>
<td>Author</td>
<td>Bob</td>
</tr>
</table>
</td>
<td>
<div>
<img
src="https://via.placeholder.com/120.png?text=Book+Image"
alt="Book Image Here">
</div>
</td>
</tr>
</table>

How to remove html table data padding

I read applied the suggested CSS here answers to this problem. But it doesn't help me.
How can I remove this padding?
Also how to add space?
HTML and CSS gives me this output:-
#page {
border: 0px;
border-collapse: collapse;
border-spacing: 0px;
}
#page td {
padding: 0;
margin: 0;
}
Hello world <br/>
<table id="page">
<tr>
<td align="left"><input type='button' value='button1'></td>
<td align="left"><input type='button' value='button2'></td>
<td align="left"><input type='button' value='button3'></td>
<td align="left"><input type='button' value='button4'></td>
</tr>
<tr>
<td align="left"><input type='button' value='Sort Name A-Z'></td>
<td align="left"><input type='button' value='Save sorting'></td>
</tr>
</table>
To make the buttons the same size, force them to occupy a specific width (in my example, 98% of the available width)
to create "padding" make them smaller then the full available width and center them (please note the css vertical-align is the recommended way to align a td over the deprecated align attribute
#page {
border: 0px;
border-collapse: collapse;
border-spacing: 0px;
}
#page td {
padding: 0;
margin: 0;
vertical-align: center
}
input[type="button"] {
width: 98%;
}
Hello world <br/>
<table id="page">
<tr>
<td align="left"><input type='button' value='button1'></td>
<td align="left"><input type='button' value='button2'></td>
<td align="left"><input type='button' value='button3'></td>
<td align="left"><input type='button' value='button4'></td>
</tr>
<tr>
<td align="left"><input type='button' value='Sort Name A-Z'></td>
<td align="left"><input type='button' value='Save sorting'></td>
</tr>
</table>
There are so many ways to do that. Here is an example. First I decide the size of the table, then I make the inputs width:100% and finally I add a margin-left.
#page {
border:0px;
border-spacing:0px;
}
#page td {
padding: 0;
margin: 0;
}
/*Makes all the inputs 100% width and add a margin*/
input {
width: calc(100% - 20px);
margin-right: 20px;
}
/*Define a size for the table*/
table {
width: 600px;
}
<html>
<header><title>This is title</title></header>
<body>
Hello world <br/>
<table id="page" >
<tr>
<td align="left"><input type='button' value='button1' ></td>
<td align="left"><input type='button' value='button2' ></td>
<td align="left"><input type='button' value='button3' ></td>
<td align="left"><input type='button' value='button4' ></td>
</tr>
<tr>
<td align="left"><input type='button' value='Sort Name A-Z'></td>
<td align="left"><input type='button' value='Save sorting' ></td>
</tr>
</table>
</body>

Place two cells under a header with the colspan of 1

I am struggling to put the cells containing { Lazy, Dog, Then, It } under the same header (with a colspan of 1)
I've tried creating div tags within my cell, creating 2 cells, and all the possible widths and colspan combinations I can think of.
Using div tags and CSS I can get Lazy and Dog under the header, but they are not individual cells.
<html>
<head>
<style>
table,td,tr,th{
border: 1px solid black;
}
.lazy {
float: left;
width: 50%;
}
.dog {
float: right;
width: 50%;
}
</style>
</head>
<body>
<table>
<tr>
<th>Quick</th>
<th>brown fox</th>
<th>jumps</th>
</tr>
<tr>
<td rowspan=3>over the</td>
<td><div class="lazy">lazy</div> <div class="dog">dog</div></td>
<td>and</td>
</tr>
<tr>
<td>then</td>
<td>it</td>
<td>fall</td>
</tr>
<tr>
<td colspan=2> prey to a lion </td>
</tr>
</table>
</body>
</html>
Thank you for any advice.
add colspan="2" to <th>brown fox</th>
you don't have to put div inside <td> to separate data
then edit your <td colspan=2> prey to a lion </td>
to <td colspan="3"> prey to a lion </td>
here is the working fiddle, hope it helped you
https://jsfiddle.net/LLa90017/1/
Easy as pie as follows:
table, td, tr, th {
border: 1px solid black;
}
.lazy {
float: left;
width: 50%;
}
.dog {
float: right;
width: 50%;
}
<table>
<tr>
<th>Quick</th>
<th colspan="2">brown fox</th>
<th>jumps</th>
</tr>
<tr>
<td rowspan=3>over the</td>
<td>lazy</td>
<td>dog</td>
<td>and</td>
</tr>
<tr>
<td>then</td>
<td>it</td>
<td>fall</td>
</tr>
<tr>
<td colspan=3> prey to a lion </td>
</tr>
</table>

How to do the horizontal alignment of HTML components

Have a look on target code.Here I am trying to split the main div into 2 equal halves and place the 2 tables at center of each of them.
Please help me to do the same
<div id="title">
Split Screen into 2 equaly halves
</div>
<div >
<table id="myTable">
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
<tr >
<td class="row_top"></td>
<td class="row"></td>
<td class="row_top"></td>
</tr>
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
</table>
<table id="myTable">
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
<tr >
<td class="row_top"></td>
<td class="row"></td>
<td class="row_top"></td>
</tr>
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
</table>
</div>
There is no such thing as splitting one element into two halves. Instead make 2 elements and position them properly.
Make divs 50% of the width of the parent element (body in this case). Make them float to the left.
Align the tables inside using
margin: 50px auto;
Solution: https://jsfiddle.net/hnmgnLat/2/
Place the two tables in different divs.set the width of the two divs to 45% each. Then set the float of the first div to left. I hope this works. The 45% width should give room for margin and padding in case it exist. Or better still set all padding and margin to 0px and set the width to 50%
try like this
<div class="parent">
<div class="child"></div>
<div class="child"></div>
</div>
add this style in css
.parent {
width: 100%;
margin: 0 auto;
}
.child {
min-width:50%;
display:inline-block;
}
you can use an ID only one TIME in your CODE and then you do an left and right float
or you can use flexbox
.row
{
border: 2px solid black;
width: 100px;
height: 100px;
}
.row_top
{
width: 100px;
height: 100px;
border-top:2px solid black;
border-bottom:2px solid black;
}
.row_bottom
{
width: 100px;
height: 100px;
border-left:2px solid black;
border-right:2px solid black;
}
.test{
display:flex;
flex-direction:row;
justify-content: space-around;
}
#title
{
text-align: center;
border-bottom: 1px solid black;
width: 250px;
margin: auto;
padding: 10px;
}
<div id="title">
Split Screen into 2 equaly halves
</div>
<div class="test">
<table id="myTableleft">
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
<tr >
<td class="row_top"></td>
<td class="row"></td>
<td class="row_top"></td>
</tr>
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
</table>
<table id="myTableright">
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
<tr >
<td class="row_top"></td>
<td class="row"></td>
<td class="row_top"></td>
</tr>
<tr >
<td ></td>
<td class="row_bottom"></td>
<td ></td>
</tr>
</table>
</div>