I created a table just like below:
As you can see the border top is not taking any changes although I did add it in the following code:
<div class="col-6 col-md-3 col-sm-6 font-style" style="font-size:10px;">
<table class="table table-outer-border font-style " >
<tbody class="font-style">
<tr >
<td>Receiver:</td>
</tr>
<tr>
<td>Attention To:</td>
</tr>
<tr>
<td>Contact No:</td>
</tr>
</tbody>
</table>
</div>
<div class="col-6 col-md-3 col-sm-6 font-style" style="font-size:10px;">
<table class="table table-outer-border font-style " >
<tbody class="font-style">
<tr >
<td>Deliver To:</td>
</tr>
<tr>
<td>Attention To:</td>
</tr>
<tr>
<td>Contact No:</td>
</tr>
</tbody>
</table>
</div>
Style:
.table-outer-border {
border-left: black solid;
border-right: black solid;
border-top: black solid ;
border-bottom: black solid;
border-width: thin;
}
Why is it not working and also is there a way to remove the lines between the rows?
There are couple of changes you need to do in your code, I have just made that for you, Kindly validate and let me know.
#MainDiv {
height: 100px;
width: 500px;
margin: 5px;
display: flex;
flex-direction: row;
font-size: 20px;
}
td {
width: 150px;
}
#firstDiv {
border: 1px solid black;
}
#secondDiv {
margin-left: 20px;
border: 1px solid black;
}
<div id="MainDiv">
<div id="firstDiv" class="col-6 col-md-3 col-sm-6 font-style">
<table class="table table-outer-border font-style ">
<tbody>
<tr>
<td>Receiver:</td>
<td></td>
</tr>
<tr>
<td>Attention To:</td>
<td></td>
</tr>
<tr>
<td>Contact No:</td>
<td></td>
</tr>
</tbody>
</table>
</div>
<div id="secondDiv" class="col-6 col-md-3 col-sm-6 font-style">
<table class="table table-outer-border font-style ">
<tbody class="font-style">
<tr>
<td>Deliver To:</td>
<td></td>
</tr>
<tr>
<td>Attention To:</td>
<td></td>
</tr>
<tr>
<td>Contact No:</td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
Related
I created a table with a JS script and this is like a simplified version of the result.
In this table the <thead> is and should be sticky and works fine in Firefox.
The problem is 'foo'. This div should be sticky inside the and works fine in Chrome but not Firefox.
I no longer use flex and overflow but still does not work. I've also read that the display:table should work fine but i use rowSpan and Its not fully supported.
Does anyone know how can i fix my problem and get the same results with both browsers?
Fyi the script creates a sticky custom element without shadowroot which is what is inside the .
thead {
position: -webkit-sticky;
position: sticky;
top: 0;
}
thead th {
background: #d24d57;
}
th,
td {
border: 1px solid gray;
background: silver;
height: 3em;
}
.container {
width: 100%;
height: 100%;
position: relative;
top: 0px;
}
.divSticky {
width: 100%;
position: sticky;
top: 50px;
}
.subcontainer {
position: absolute;
right: 0px;
top: 1px;
width: 40%;
height: 100%;
max-height: 120px;
}
.text {
min-height: 30%;
max-height: 70%;
color: purple;
position: relative;
font-size: 13px;
z-index: 1;
width: 100%;
}
<html>
<body>
<table style="position: absolute; width: 200px; height: 878px; overflow-y: scroll;">
<thead>
<tr>
<th>Firstname</th>
<th>Surname</th>
<th>Age</th>
</tr>
</thead>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr style="height:700px;">
<td>
<div class="container">
<div class="divSticky">
<div class="subcontainer">
<div class="text">
foo
</div>
</div>
</div>
</div>
</td>
<td rowSpan="3">
<div class="container">
<div class="divSticky">
<div class="subcontainer">
<div class="text">
foo
</div>
</div>
</div>
</div>
</td>
<td>
<div class="container">
<div class="divSticky">
<div class="subcontainer">
<div class="text">
foo
</div>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td>Name</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr style="height:700px;">
<td>
<div class="container">
<div class="divSticky">
<div class="subcontainer">
<div class="text">
foo
</div>
</div>
</div>
</div>
</td>
<td>
<div class="container">
<div class="divSticky">
<div class="subcontainer">
<div class="text">
foo
</div>
</div>
</div>
</div>
</td>
<td>
<div class="container">
<div class="divSticky">
<div class="subcontainer">
<div class="text">
foo
</div>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
<tr>
<td>Name</td>
<td>Surname</td>
<td>xx</td>
</tr>
</table>
</body>
</html>
Try
<tr style="height:700px;vertical-align:top">
Hello I need a fluid table with other div in the same line. Here you can see a fiddle where I could do what I want, which works perfectly on Chrome. The problem is when I try it on Firefox. The column width is the minimum, it is not expanding as in Chrome.
<div class="container" style="background-color: lightgray; overflow: auto;">
<div style="height: 200px; overflow-y: auto; margin-right: 300px; float: left;">
<table border="1" style="">
<theader>
<tr>
<th>ID</th>
<th style="width:100%">Name</th>
<th>Time</th>
</tr>
</theader>
<tbody>
<tr>
<td>1</td>
<td style="word-break: break-all">Dan</td>
<td>00:00:00.000</td>
</tr>
<tr>
<td>2</td>
<td style="word-break: break-all">Bob</td>
<td>00:00:00.000</td>
</tr>
<tr>
<td>3</td>
<td style="word-break: break-all">Jenn</td>
<td>00:00:00.000</td>
</tr>
</tbody>
</table>
</div>
<div style="background-color: #65de4f80; width: 300px; height: 200px; margin-left: -300px; float: left;"></div>
</div>
REmove float : left and also the overflow: auto on top you don 't need
<div class="container" style="background-color: lightgray;">
<div style="height: 200px; overflow-y: auto; margin-right: 300px;">
<table border="1" style="">
<theader>
<tr>
<th>ID</th>
<th style="width:100%">Name</th>
<th>Time</th>
</tr>
</theader>
<tbody>
<tr>
<td>1</td>
<td style="word-break: break-all">Dan</td>
<td>00:00:00.000</td>
</tr>
<tr>
<td>2</td>
<td style="word-break: break-all">Bob</td>
<td>00:00:00.000</td>
</tr>
<tr>
<td>3</td>
<td style="word-break: break-all">Jenn</td>
<td>00:00:00.000</td>
</tr>
</tbody>
</table>
</div>
<div style="background-color: #65de4f80; width: 300px; height: 200px; margin-left: -300px; float: left;"></div>
</div>
Regards
I have browsed some of the answers and tried the solutions out but I am still not getting the results.
I want to set a different margin for different breakpoints in bootstrap-for example:
Laptop view fix,
tablet view
It probably has something to do with the set widths as well, but I just didn't know how to make everything work in width as well as have margin look good on all screen sizes.
This is my code :
.container {
max-width: 25rem;
text-align: center;
padding: 0rem;
margin: 1rem 0.4rem;
}
.table.table.table-borderless {
margin-bottom: 0rem;
}
.table.table.table-borderless td,
.table.table-dark.table-borderless td {
padding: 0rem;
}
table.statistic-employment,
table.statistic-requirement {
width: 24.8rem;
}
.statistic-requirement {
height: 10 rem;
}
.col-12.col-md-6.col-xl-4 {
padding: 0rem 0rem;
}
<meta name="viewport" content="width=device-width, initial-scale=1.0 shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div class="main-container">
<div class="row">
<div class="col-12 col-md-6 col-xl-4 ">
<!--Prva kartica-->
<div class="container">
<table class="table table-borderless" style="background-color: thistle;">
<tbody>
<tr>
<td style="font-weight: 400; font-size: 3rem;">UKUPNO</td>
</tr>
</tbody>
</table>
<table class="table table-dark table-borderless">
<tbody>
<tr>
<td>Zaprimljeno zahtjeva</td>
</tr>
<tr>
<td style="font-size: 5.5rem;">4979</td>
</tr>
<tr>
<td>poslano</td>
</tr>
<tr>
<td>17.4.2020. do 12h</td>
</tr>
</tbody>
</table>
<table class="table table-borderless" style="background-color: thistle; font-weight: 700;">
<tr>
<td>Osobna provjera</td>
<td>SMS obavijest</td>
<td>Kontakt centar</td>
</tr>
<tr>
<td style="font-size: 2.5rem;">6243</td>
<td style="font-size: 2.5rem;">2322</td>
<td style="font-size: 2.5rem;">230</td>
</tr>
</table>
<div class="statistic-employment" style="background-color: rgb(228, 224, 224); height: 11rem;">
<h6>Po statusu zaposlenosti</h6>
<table class="statistic-employment">
<tr>
<td>EMPLOYED</td>
<td>6308</td>
</tr>
<tr>
<td>PENSIONER</td>
<td>251</td>
</tr>
<tr>
<td>PENSIONER_EMPLOYED</td>
<td>34</td>
</tr>
<tr>
<td>UNEMPLOYED</td>
<td>952</td>
</tr>
<tr>
<td>UNEMPLOYED_COVID</td>
<td>1090</td>
</tr>
</table>
</div>
<div class="statistic-requirement" style="background-color: rgb(238, 189, 223); height: 9.8rem;">
<h6>Broj upisanih po danu</h6>
<table class="statistic-requirement">
<tr>
<td>datum</td>
<td>dan</td>
<td>zahtjeva</td>
</tr>
<tr>
<td>06.04.</td>
<td>ponedjeljak</td>
<td>2640</td>
</tr>
<tr>
<td>07.04.</td>
<td>utorak</td>
<td>1205</td>
</tr>
<tr>
<td>08.04.</td>
<td>srijeda</td>
<td>700</td>
</tr>
</table>
</div>
</div>
</div>
<div class="col-12 col-md-6 col-xl-4">
<!--Druga kartica-->
<div class="container">
<table class="table table-borderless" style="background-color: thistle;">
<tr>
<td style="font-weight: 400; font-size: 3rem;">UKUPNO</td>
</tr>
</table>
<table class="table table-dark table-borderless">
<tr>
<td>Zaprimljeno zahtjeva</td>
</tr>
<tr>
<td style="font-size: 5.5rem;">4979</td>
</tr>
<tr>
<td>poslano</td>
</tr>
<tr>
<td>17.4.2020. do 12h</td>
</tr>
</table>
<table class="table table-borderless" style="background-color: thistle; font-weight: 700;">
<tr>
<td>Osobna provjera</td>
<td>SMS obavijest</td>
<td>Kontakt centar</td>
</tr>
<tr>
<td style="font-size: 2.5rem;">6243</td>
<td style="font-size: 2.5rem;">2322</td>
<td style="font-size: 2.5rem;">230</td>
</tr>
</table>
<div class="statistic-employment" style="background-color: rgb(228, 224, 224); height: 11rem;">
<h6>Po statusu zaposlenosti</h6>
<table class="statistic-employment">
<tr>
<td>EMPLOYED</td>
<td>6308</td>
</tr>
<tr>
<td>PENSIONER</td>
<td>251</td>
</tr>
<tr>
<td>UNEMPLOYED</td>
<td>952</td>
</tr>
<tr>
<td>UNEMPLOYED_COVID</td>
<td>1090</td>
</tr>
</table>
</div>
<div class="statistic-requirement" style="background-color: rgb(238, 189, 223);">
<h6>Broj upisanih po satu</h6>
<table class="statistic-requirement">
<tr>
<td>datum</td>
<td>sat</td>
<td>zahtjeva</td>
</tr>
<tr>
<td>27.04.</td>
<td>06</td>
<td>1</td>
</tr>
<tr>
<td>27.04.</td>
<td>07</td>
<td>3</td>
</tr>
<tr>
<td>27.04.</td>
<td>08</td>
<td>11</td>
</tr>
<tr>
<td>27.04.</td>
<td>09</td>
<td>26</td>
</tr>
</table>
</div>
</div>
</div>
<div class="col-12 col-md-6 col-xl-4">
<!--Treca kartica-->
<div class="container">
<table class="table table-borderless" style="background-color: thistle;">
<tr>
<td style="font-weight: 400; font-size: 3rem;">UKUPNO</td>
</tr>
</table>
<table class="table table-dark table-borderless">
<tr>
<td style="padding: 0rem">Zaprimljeno zahtjeva</td>
</tr>
<tr>
<td style="font-size: 5.5rem; padding: 0rem ">4979</td>
</tr>
<tr>
<td style="padding: 0rem">poslano</td>
</tr>
<tr>
<td style="padding: 0rem">17.4.2020. do 12h</td>
</tr>
</table>
<table class="table table-borderless" style="background-color: thistle
; font-weight: 700;">
<tr>
<td>Osobna provjera</td>
<td>SMS obavijest</td>
<td>Kontakt centar</td>
</tr>
<tr>
<td style="font-size: 2.5rem;">6243</td>
<td style="font-size: 2.5rem;">2322</td>
<td style="font-size: 2.5rem;">230</td>
</tr>
</table>
<div class="statistic-employment" style="background-color: rgb(228, 224, 224); height: 11rem;">
<h6>Po statusu zaposlenosti</h6>
<table class="statistic-employment">
<tr>
<td>EMPLOYED</td>
<td>83</td>
</tr>
<tr>
<td>PENSIONER</td>
<td>10</td>
</tr>
<tr>
<td>UNEMPLOYED</td>
<td>25</td>
</tr>
<tr>
<td>UNEMPLOYED_COVID</td>
<td>19</td>
</tr>
</table>
</div>
<div class="statistic-requirement" style="background-color: rgb(238, 189, 223);">
<h6>Broj upisanih po satu</h6>
<table class="statistic-requirement">
<tr>
<td>datum</td>
<td>sat</td>
<td>zahtjeva</td>
</tr>
<tr>
<td>28.04.</td>
<td>00</td>
<td>1</td>
</tr>
<tr>
<td>28.04.</td>
<td>05</td>
<td>2</td>
</tr>
<tr>
<td>28.04.</td>
<td>06</td>
<td>1</td>
</tr>
<tr>
<td>28.04.</td>
<td>08</td>
<td>14</td>
</tr>
</table>
</div>
</div>
</div>
<!--Row and Main-container-->
</div>
</div>
Use #media only screen and (min-width: )
For each type of screen size, define the style you want.
For example:
/* Medium devices (landscape tablets, 768px and up) */
#media only screen and (min-width: 768px) {
.container {
margin: 1 rem;
}
}
/* Large devices (laptops/desktops, 992px and up) */
#media only screen and (min-width: 992px) {
.container {
margin: 2 rem;
}
}
Hope this helps
This answer is using margin class of bootstrap
<div class="container my-md-3 mx-md-2 my-lg-5 mx-lg-4">
</div>
my-md-3 means when screen >= 768px, margin top and bottom is 1rem
mx-md-2 means when screen >= 768px, margin right and left is .5rem
my-lg-5 means when screen >= 992px, margin top and bottom is 3rem
my-lg-4 means when screen >= 992px, margin right and left is 1.5rem
For more detail, Please look https://getbootstrap.com/docs/4.4/utilities/spacing/
I have the following html with a responsive table.
When the page is accessed via mobile, the user has to scroll horizontally to view the entire content, is there a bootstrap way to break the columns vertically so a mobile user does not have to scroll horizontally ?
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body>
<div class="container"> <h2>Table</h2> <p>The .table-responsive class creates a responsive table which will scroll horizontally on small devices (under 768px). When viewing on anything larger than 768px wide, there is no difference:</p> <div class="table-responsive"> <table class="table">
<thead>
<tr>
<th>#</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>City</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Anna</td>
<td>Pitt</td>
<td>35</td>
<td>New York</td>
<td>USA</td>
</tr>
</tbody> </table> </div> </div>
</body> </html>
The last example here (No More Tables) shows the desired behavior perfectly https://elvery.net/demo/responsive-tables/
though it is not done with bootstrap.
If anyone can shed light on how to achieve this with bootstrap, ill appreciate.
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body>
<style>
.table-responsive{
width: 100%;
margin-bottom: 15px;
overflow-y: hidden;
-ms-overflow-style: -ms-autohiding-scrollbar;
border: 1px solid #ddd;
}
</style>
<div class="container"> <h2>Table</h2> <p>The .table-responsive class creates a responsive table which will scroll horizontally on small devices (under 768px). When viewing on anything larger than 768px wide, there is no difference:</p> <div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>City</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Anna</td>
<td>Pitt</td>
<td>35</td>
<td>New York</td>
<td>USA</td>
</tr>
</tbody> </table> </div> </div>
</body> </html>
no need to do anything only put css as i putted on style tag that work perfectly
i hope this work good.
thanks.
Use this way
#media screen and (max-width: 640px) {
table#customDataTable caption {
background-image: none;
}
table#customDataTable thead {
display: none;
}
table#customDataTable tbody td {
display: block;
padding: .6rem;
}
table#customDataTable tbody tr td:first-child {
background: #666;
color: #fff;
}
table#customDataTable tbody tr td:first-child a {
color: #fff;
}
table#customDataTable tbody tr td:first-child:before {
color: rgb(225, 181, 71);
}
table#customDataTable tbody td:before {
content: attr(data-th);
font-weight: bold;
display: inline-block;
width: 10rem;
}
table#customDataTable tr th:last-child,
table#customDataTable tr td:last-child {
max-width: 100% !important;
min-width: 100px !important;
width: 100% !important;
}
}
<table class="table" id="customDataTable">
<thead>
<tr>
<th>#</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>City</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<tr>
<td data-th="#">1</td>
<td data-th="Firstname">Anna</td>
<td data-th="Lastname">Pitt</td>
<td data-th="Age">35</td>
<td data-th="City">New York</td>
<td data-th="Country">USA</td>
</tr>
<tr>
<td data-th="#">1</td>
<td data-th="Firstname">Anna</td>
<td data-th="Lastname">Pitt</td>
<td data-th="Age">35</td>
<td data-th="City">New York</td>
<td data-th="Country">USA</td>
</tr>
<tr>
<td data-th="#">1</td>
<td data-th="Firstname">Anna</td>
<td data-th="Lastname">Pitt</td>
<td data-th="Age">35</td>
<td data-th="City">New York</td>
<td data-th="Country">USA</td>
</tr>
<tr>
<td data-th="#">1</td>
<td data-th="Firstname">Anna</td>
<td data-th="Lastname">Pitt</td>
<td data-th="Age">35</td>
<td data-th="City">New York</td>
<td data-th="Country">USA</td>
</tr>
<tr>
<td data-th="#">1</td>
<td data-th="Firstname">Anna</td>
<td data-th="Lastname">Pitt</td>
<td data-th="Age">35</td>
<td data-th="City">New York</td>
<td data-th="Country">USA</td>
</tr>
</tbody>
</table>
https://jsfiddle.net/lalji1051/ztrjqvL8/
An alternative solution is to use Bootstrap.
HTML:
<div class="col-md-1 " style="padding-top:10%">
<div class="row">
<div class="col-md-2 offset-lg-1 col-sm-12 text-center feature-comparison-table-header-cell bold">
Compare Editions
</div>
<div class="col-md-2 col-sm-12 text-center feature-comparison-table-header-cell bold">
Lite
</div>
<div class="col-md-2 col-sm-12 text-center feature-comparison-table-header-cell bold">
Standard
</div>
<div class="col-md-2 col-sm-12 text-center feature-comparison-table-header-cell bold">
Premium
</div>
<div class="col-md-2 col-sm-12 text-center feature-comparison-table-header-cell bold">
Professional
</div>
</div>
<div class="row">
<div class="col-md-2 offset-lg-1 col-sm-12 text-center feature-comparison-table-header-cell">
Monthly Flat Rate
</div>
<div class="col-md-2 col-sm-12 text-center feature-comparison-table-cell">
<p>Free</p>
</div>
<div class="col-md-2 col-sm-12 text-center feature-comparison-table-cell">
<p>
$40/Month
</p>
</div>
<div class="col-md-2 col-sm-12 text-center feature-comparison-table-cell">
<p>
$50/Month
</p>
</div>
<div class="col-md-2 col-sm-12 text-center feature-comparison-table-cell">
<p>
$60/Month
</p>
</div>
</div>
</div>
The key is then just stylizing it to look like a table
#pricing-detailed-section .feature-comparison-table-header-cell {
padding: 15px;
border: 1px solid #DFE1E5;
font-size: 1rem;
padding-top: 40px;
height:100px;
}
#pricing-detailed-section .bold {
font-weight:600;
}
#pricing-detailed-section .feature-comparison-table-cell {
height: 100px;
border: 1px solid
#DFE1E5;
background-color:#FFF;
display: table;
}
#pricing-detailed-section .feature-comparison-table-cell p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
When you switch to mobile, it'll fit everything vertically. Note the code for the paragraph section solves multi-line fields not being centered.
Given a table with a column that contains numbers, I'd like to position them in the center.
But, I'd like to right-align the numbers as well!
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: center;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>45</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>18923538273</td>
</tr>
<tr>
<td>9823</td>
</tr>
</tbody>
</table>
Outputs:
Desired output:
Note: The table cell width should be constant (200px), regardless of the numbers. For example, if all numbers are 0, they all should be in the center of the table:
Also:
You are allowed to modify the content of the <td>s, but there should be one number per <tr>.
CSS only, please.
Updated based on an edit of the question and a few comments
In a comment you wrote "In the desired outcome, the cell width stays the same (200px) as numbers change".
In another comment you wrote "...my numbers are links and I want them to occupy the full cell width".
Given those requirements, the only CSS based solution I can find is, where one use CSS Table instead of <table> elements, an anchor a element displayed as table-row, making the full width clickable without adding an event handler, and for the centering, using pseudo elements to puch the numbers to the middle.
Stack snippet
.table {
display: table;
border: 1px solid black;
}
.tr {
display: table-row;
text-decoration: none;
color: black;
}
.tr span {
display: table-cell;
width: 200px;
}
a.tr {
text-align: right;
}
.tr::before, .tr::after {
content: '';
display: table-cell;
width: 50%;
}
<div class="table">
<div class="thead">
<span class="tr">
<span>Amount</span>
</span>
</div>
<div class="tbody">
<a href="#1" class="tr">
<span>45</span>
</a>
<a href="#2" class="tr">
<span>2</span>
</a>
<a href="#3" class="tr">
<span>18923538273</span>
</a>
<a href="#4" class="tr">
<span>9823</span>
</a>
</div>
</div>
<div class="table">
<div class="thead">
<span class="tr">
<span>Amount</span>
</span>
</div>
<div class="tbody">
<a href="#1" class="tr">
<span>0</span>
</a>
<a href="#2" class="tr">
<span>0</span>
</a>
<a href="#3" class="tr">
<span>0</span>
</a>
<a href="#4" class="tr">
<span>0</span>
</a>
</div>
</div>
_____________________________________________________________________________
This is my first answer, which I will leave, as there might be someone that can make use of it as is.
One simple way to accomplish that is to simply nest a table for the values, center it using auto margin and right align its td's content.
This way you will get pretty much the exact same behavior as with your original markup, but get a better control of the values alignment.
Stack snippet
table {
border: 1px solid black;
}
th {
width: 200px;
}
table table {
border: none;
margin: 0 auto;
}
table table td {
text-align: right;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<table>
<tr>
<td>45</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>18923538273</td>
</tr>
<tr>
<td>9823</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<table>
<tr>
<td>0</td>
</tr>
<tr>
<td>0</td>
</tr>
<tr>
<td>0</td>
</tr>
<tr>
<td>0</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
You can of course use div's instead of a table, displayed as inline block or inline flex column.
Inline block
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: center;
}
td > div {
display: inline-block;
}
td > div > div {
text-align: right;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<div>45</div>
<div>2</div>
<div>18923538273</div>
<div>9823</div>
</div>
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<div>0</div>
<div>0</div>
<div>0</div>
<div>0</div>
</div>
</td>
</tr>
</tbody>
</table>
Inline flex column
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: center;
}
td > div {
display: inline-flex;
flex-direction: column;
}
td > div > div {
text-align: right;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<div>45</div>
<div>2</div>
<div>18923538273</div>
<div>9823</div>
</div>
</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
<div>0</div>
<div>0</div>
<div>0</div>
<div>0</div>
</div>
</td>
</tr>
</tbody>
</table>
TL;DR:
table {
border: 1px solid black;
width: 200px;
}
td {
text-align: right;
min-width: 10px;
}
td:first-child, td:last-child {
width: 50%;
}
... and adding an extra column before and after the existing one. jsFiddle here.
Initial answer:
Considering your markup,
td {
text-align: right;
border-left:7rem solid transparent;
border-right:7rem solid transparent;
}
... should do it.
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: right;
border-left:7rem solid transparent;
border-right:7rem solid transparent;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>45</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>18923538273</td>
</tr>
<tr>
<td>9823</td>
</tr>
</tbody>
</table>
Any other solution involves changing the markup (you need to add inner elements inside <td>s, give them smaller width than the <td>, and right align their text). You can do it by modifying the HTML source or on the fly, using JavaScript.
After a good number of tries, the only reliable solution I found (implying markup modification and no JavaScript), was to add additional columns in the table, relying on the table's ability to line up all the cells in a column.
I updated the snippet below so that the column occupies the minimum necessary width, based on most wide number and right-aligns all cells based on resulting width width. This means that when all values are 0, the entire row of values are centered. Here it is:
table {
border: 1px solid black;
width: 200px;
}
td {
text-align: right;
min-width: 10px;
}
td:first-child, td:last-child {
width: 50%;
}
/* just stacking tables side by side, not part of solution */
table {
float: left;
width: 200px;
margin-right: 7px;
}
body { overflow-y: hidden;}
<table>
<thead>
<tr>
<th colspan="3">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>45</td>
<td></td>
</tr>
<tr>
<td></td><td>2</td><td></td>
</tr>
<tr>
<td></td><td>0</td><td></td>
</tr>
<tr>
<td></td><td>1234</td><td></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="3">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>0</td>
<td></td>
</tr>
<tr>
<td></td><td>2</td><td></td>
</tr>
<tr>
<td></td><td>1</td><td></td>
</tr>
<tr>
<td></td><td>4</td><td></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th colspan="3">Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td>44</td>
<td></td>
</tr>
<tr>
<td></td><td>0</td><td></td>
</tr>
<tr>
<td></td><td>1155</td><td></td>
</tr>
<tr>
<td></td><td>1234548775564</td><td></td>
</tr>
</tbody>
</table>
make text-align:right and padding-right:5emin td css selector
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: right;
padding-right: 4em;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>45</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>18923538273</td>
</tr>
<tr>
<td>9823</td>
</tr>
</tbody>
</table>
<style>
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: center;
float:right; <!--added this-->
margin-right:50px; <!-- and this-->
}
</style>
I added float:right in td
adjust the margin-right value to your desired value;
One option is to change the display property for td elements to block
You can then set a max-width to bring td elements to the center of tr elements.
Once that's done you set the text-align property to right for td elements to make the numbers start from the right hand side.
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
display: block;
max-width: 70%;
text-align: right;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>45</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>18923538273</td>
</tr>
<tr>
<td>9823</td>
</tr>
</tbody>
</table>
Wrap your numbers with element(span) inside the td and add the text align right styles on it.
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: center;
}
td span {
width: 150px;
text-align: right;
background: beige;
display: inline-block;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td><span>45</span></td>
</tr>
<tr>
<td><span>2</span></td>
</tr>
<tr>
<td><span>18923538273</span></td>
</tr>
<tr>
<td><span>9823</span></td>
</tr>
</tbody>
</table>
.table {
display: flex;
align-items: center;
justify-content: center;
width: 300px;
background-color: red;
flex-direction: column;
}
div {
text-align: right;
}
<body>
<div class='table'>
<div>
<div>1</div>
<div>1111111</div>
<div>1111111111111</div>
</div>
</div>
</body>
text-align :right ----> pulls the text into right end
padding-right: 50% or padding-left : 50% ----> add space from the right or left to center
use 45 - 49 percentage in padding to make a crisp center alignment depends on your requirement
table {
border: 1px solid black;
}
th {
width: 200px;
}
td {
text-align: right;
padding-right: 50%;
}
<table>
<thead>
<tr>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>45</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>18923538273</td>
</tr>
<tr>
<td>9823</td>
</tr>
</tbody>
</table>