I'm trying to place some content within a table cell but it needs to meet the following specifications.
The content I'm adding is:
x *
Where x is any number (i.e a variable length) and * is a picture of a star, which will be a fixed length of let's say ypx. I want this content to appear centred in the table cell, with x always being z pixels to the left of *. For example's sake, let's say the * is 16px wide, and z is 2px (i.e a 2px spacing between x and the star picture.
I've tried a simple 2 column layout where I float the x to the left, float the * to the right, which works but does not center the two columns, instead x sticks to the left of the table cell, and * sticks to the right of it.
Any help appreciated :) . I can use JS if need be, but I'd prefer a HTML/CSS solution of course.
here just in case the jsBin expires i will put the code in an answer. once again excuse my sloppy code, im literally about to fall asleep
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<style>
td { width:300px; height:300px;border:1px solid;position:relative;}
td img {width:100%;max-width:16px;padding-left:2px}
.num { float:left}
.wrapper { margin:0 auto;}
</style>
<script>
$(function(){
width = $('.num').width() + 18;
$('.wrapper').css('width',width.toString() + 'px');
});
</script>
<table>
<tr>
<td>
<div class="wrapper">
<div class='num'>1234</div>
<img src="http://www.wpclipart.com/signs_symbol/icons_oversized/male_user_icon.png">
</div>
</td>
</tr>
</table>
http://jsbin.com/gesajeyiya/1/watch
If you know you will only be displaying numbers of 0 to 9999 then you do not need a script
http://jsbin.com/vagulejaqi/1/watch?html,css,output
<table id="your-table">
<tbody>
<tr>
<td>
<div class='cell-inner-wrapper'>
<div class='my-numbers'>
9999
</div>
<img src="https://www.novell.com/documentation/groupwisemobility2/gwmob2_guide_admin/graphics/icon_user_n.png">
</div>
</td>
<td>
<div class='cell-inner-wrapper'>
<div class='my-numbers'>
1
</div>
<img src="https://www.novell.com/documentation/groupwisemobility2/gwmob2_guide_admin/graphics/icon_user_n.png">
</div>
</td>
</tr>
</tbody>
</table>
#your-table {
font-family:monospace;
font-size:12px;
width:100%;
border: 1px solid #000;
}
#your-table td {
border: 1px solid #000;
width:50%;
}
.cell-inner-wrapper {
//12px * 4 + 16px + 2px
max-width: 66px;
text-align:center;
}
#your-table td img{
padding-left: 2px;
display:inline;
vertical-align:middle;
}
.my-numbers {
max-width:48px;
display:inline;
line-height:4.5em;
}
Related
I have a big table, I'm going to try to make a game. I need all the table parts to be square, but the edges are more rectangular. I can't find anything to help fix this.
I've tried setting the max width and height of tr and th to 6.667vw(approx. 1/15 of the screen width) but the edge sizes are still too big to be the small round error. I've erased the width and height of tr and th to see if it was something of that, but that didn't work either. I have a lot of table elements, a 15x15, I am giving the smallest amount possible for my given problem; I am also adding the css.
Css:
* {
margin: 0;
padding: 0;
}
table {
width: 100%;
height: 100vw;
border: 3px solid red;
border-collapse: collapse;
}
tr th {
border: 2px solid black;
}
HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Scrabble</title>
<link rel="stylesheet" href="style.css"
</head>
<body>
<div class="game">
<div class="board">
<table>
<tr class="row1">
// 15 <tr> elements with
// incrementing 'col#' classes
</tr>
<tr class="row2"> // class 'row#' incremented by 1
// 15 <tr> elements
</tr>
// 13 more <tr> elements with
// incrementing 'row#' classes
</table>
</div>
<div class="letterSelect"></div>
</div>
</body>
</html>
Sorry for the html with no elements, I wanted to keep it concise while giving you enough detail to replicate. The class names have no functionality, but I included them because they are in my code. I hope this is enough.
First thing to note is you shouldn't really use a <table> to make your game board. A <table> should be used for displaying tabular data. More information about tables on MDN.
I would advise using display:grid to make your board instead. More information about grid layout on MDN.
I have created a codepen to use as a starting point. The code is also shown below.
HTML
<div class="game">
<div class="board">
<div class="square"></div>
<!-- 224 more .square elements to make your 15 x 15 board -->
</div>
</div>
CSS
.game {
padding: 20px; // just to add some breathing space to the demo
display: grid;
place-content: center;
}
.board {
display: grid;
grid-template-columns: repeat(15, 5vmin); // substitute 5vmin for whatever size you like, just make sure to use the same value for grid-template-rows
grid-template-rows: repeat(15, 5vmin);
border: 1px solid black;
}
.square {
border: 1px solid black;
}
I put together this fiddle
What I have is 2 divs and then one of them has 2 more divs in it. What I am trying to do it properly position label and span within second div
<div id="someotherdiv">
</div>
<div class="reportuserinfo">
<div class="leftdivinfo">
<label>Teacher:</label><span>Teacher Name</span><br/>
<label>District:</label><span>District Name</span><br/>
<label>School:</label><span>School Name</span>
</div>
<div class="rightdivinfo">
<label>Class:</label><span>Class Name</span><br/>
<label>Content:</label><span id="currcontent">Content</span><br/>
<label>Unit:</label><span id="currunit"></span>
</div>
</div>
If you look in fiddle right now label is floating to the right and so is span but they look strange, what I am attempting to accomplish is something like this on both sides of second div:
Teacher: Teacher Name
District: District Name
School: School Name
The way they look right now is
Teacher: Teacher Name
District: District Name
School: School Name
Thanks for your help.
I just added width:30%; in your css class ".reportuserinfo label" and it fixed the issue (fiddle). You need to define width in order to fix the text alignment issue. You can adjust space margins as per your requirements.
Here is how your code looks like to me now :-
.reportuserinfo label {
float: left;;
margin-right: 30px;
font-weight: bold;
text-align: right;
width:30%;
}
You should define a min-width style in your css apart from width:40% to ensure that the text you are trying to write does not scroll if browser width is decreased
.leftdivinfo {
float:left;
min-width: 200px;
width:40%;
background: yellow;
padding-left: 30px;
padding-right: 30px;
}
Have you tried using tables? You could enclose this information in a table, then right-align the text in the left column:
table {
border: none;
}
td:first-child {
text-align: right;
}
td {
padding: 5px;
}
<table>
<tr>
<td>Class Teacher:</td>
<td>John Smith</td>
</tr>
<tr>
<td>District:</td>
<td>Smithsville</td>
</tr>
<tr>
<td>School:</td>
<td>Smith's High School</td>
</tr>
</table>
Try setting width to auto and adding display:inline-block.
.leftdivinfo {
float:left;
width: auto;
background: yellow;
padding-left: 10px;
padding-right: 10px;
display:inline-block
}
Tables might be a better way to make this.
EDIT:
I see you want the colons aligned, add min-width:70px; to the label class.
I'm an experienced .net developer, but all of us have areas where we can benefit from improvements. I feel that I'm severely lacking in HTML/CSS skills and I'm trying to teach myself in this area.
I'm trying to create an HTML layout for an application that monitors bandwidth consumption on the local network. I envisage the layout as consisting of 4 elements:
+--------------------------+
+ Controls +
+--------+-----------------+
+ Legend + Graph +
+ + +
+ + +
+ + +
+ + +
+--------+-----------------+
+ Log +
+--------------------------+
Controls - this is a narrow area that will allow clear, pause and otherwise control the graph. It will also allow to show and hide log area. This means that the layout need to accommodate the log area being hidden.
Legend - is a table that will list all series on the graph. It will show series color and also some numeric data associated with the series. If there are more lines in the legend that fit the screen it should be possible to vertical scroll legend area. Horizontal scroll is never required for the area it's assumed that it is always narrow enough. The height of Legend (and Graph) should take up all the remaining space that is not used by Control area and Log area. The width of the legend will be equal to the natural table width.
Graph - there will be a Graph here painted over HTML canvas. This should take up all available space both horizontally and vertically.
Log - here will be two or three lines of log displayed. If there are more than 3 lines to display there should be a vertical scroll bar on this area. This area need to be able to be hid-able. The height of this area can be made fixed. (in the vicinity of 100px - 200px).
The layout should adapt to window re-sizing, and keep looking descent when windows is being made small (to a point of course). Unless the window is too small, it should not have outer horizontal and vertical scrollbars.
Unfortunately, I cannot get it right in several places.
This is my code:
<html>
<head>
<style>
* {margin:0;padding:0;height:100%;}
html, button, input, select, textarea {
font-family: sans-serif;
font-weight: 100;
letter-spacing: 0.01em;
}
.container {
min-height:100%;
position:relative;
}
.control {
background:green;
width:100%;
height:auto;
margin-top: 0;
}
.content {
width:100%;
margin:0;
margin-top:0;
margin-bottom:0;
}
.legend {
position:relative;
background:blue;
float:left
}
.graph {
background:red;
}
.log {
background:yellow;
width:100%;
height:auto;
position:absolute;
margin-top: 0;
margin-bottom: 0;
bottom:0;
left:0;
}
.table {
border-collapse: collapse;
border-spacing: 0;
empty-cells: show;
border: 1px solid #cbcbcb;
}
.table td,
.table th {
border-left: 1px solid #cbcbcb;
border-width: 0 0 0 1px;
font-size: inherit;
margin: 0;
overflow: visible;
padding: 0.5em 1em;
}
.table thead {
background-color: #e0e0e0;
color: #000;
text-align: left;
vertical-align: bottom;
}
.table td {
background-color: transparent;
}
.table-odd td {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<div class="container">
<div class="control">header1<br/>header2</div>
<div class="content">
<div class="legend">
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Make</th>
<th>Model</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr class="table-odd">
<td style="background-color: #FFB300">1</td>
<td>Honda</td>
<td>Accord</td>
<td>2009</td>
</tr>
<tr>
<td style="background-color: #803E75">2</td>
<td>Toyota</td>
<td>Camry</td>
<td>2012</td>
</tr>
<tr class="table-odd">
<td style="background-color: #FF6800">3</td>
<td>Hyundai</td>
<td>Elantra</td>
<td>2010</td>
</tr>
<tr>
<td style="background-color: #A6BDD7">4</td>
<td>Ford</td>
<td>Focus</td>
<td>2008</td>
</tr>
<tr class="table-odd">
<td style="background-color: #C10020">5</td>
<td>Nissan</td>
<td>Sentra</td>
<td>2011</td>
</tr>
<tr>
<td style="background-color: #CEA262">6</td>
<td>BMW</td>
<td>M3</td>
<td>2009</td>
</tr>
<tr class="table-odd">
<td style="background-color: #817066">7</td>
<td>Honda</td>
<td>Civic</td>
<td>2010</td>
</tr>
<tr>
<td style="background-color: #007D34">8</td>
<td>Kia</td>
<td>Soul</td>
<td>2010</td>
</tr>
</tbody>
</table>
</div>
<div class="graph"><canvas></canvas></div>
</div>
<div class="log">log1<br/>log2</div>
</div>
<script>
function resize() {
var canvas = document.querySelector('canvas');
canvas.style.width = '100%';
canvas.style.height = '100%';
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
ctx = canvas.getContext('2d');
ctx.strokeStyle='yellow';
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(canvas.width,canvas.height);
ctx.stroke();
}
resize();
window.addEventListener('resize', resize, false);
</script>
</body>
</html>
This is the corresponding JSFiddle
Particular problems that I'm facing:
Why canvas is being rendered outside of the enclosing div? This is very surprising to me and I cannot figure out why.
How do I make the table be spaced out naturally? In particular:
Why first line is so tall?
How do I make the table do not take the whole height? It's enclosing dive that has height:100%, not the table, so why is it so tall?
How do I make it scroll-able if it's does not fit in the height?
The Legend/Graph area seems to extend underneath the Log area. Why? How do I prevent that?
Finally, how can I make the Log area of fixed height and scroll-able?
My apologies, I know that this question is a tall order, I'll gratefully accept any help and/or pointers. I do realize that I lack basic understanding, but that's what I'm trying to work against. I spent most of the evening today researching this topic and looking for source that allowed me to put together at least this non-working example. I'm comfortable with JavaScript, it's HTML/CSS that I mainly need help with. I studied the documentation on what properties of different DOM objects do, but it's difficult to figure out what properties to use and how.
You are complicating your CSS a lot for what you want,
you can use CSS Flexbox along with CSS calc() for this
body {
margin: 0
}
section {
background: red;
height: 50px;
}
article {
display: flex;
height: calc(100vh - 100px)
}
aside,
div {
background: lightblue;
}
aside {
overflow-y: auto;
max-width: 45%
}
aside ~ div {
flex: 1
}
canvas {
width: 100%;
height: 100%;
background: green
}
.table {
display: table;
table-layout:fixed;
width:100%
}
.row {
display: table-row;
background-color: #fff;
}
.column {
display: table-cell;
vertical-align: top;
border-left: 1px solid #cbcbcb;
border-width: 0 0 0 1px;
font-size: inherit;
margin: 0;
padding: 0.5em 1em;
background-color: inherit;
}
.cell-header {
font-weight: bold;
}
.row-odd {
background-color: #f2f2f2;
}
<main>
<section>Controls</section>
<article>
<aside>
<div class="table">
<div class="row row-odd">
<div class="column cell-header">#</div>
<div class="column cell-header">Make</div>
<div class="column cell-header">Model</div>
<div class="column cell-header">Year</div>
</div>
<div class="row">
<div class="column" style="background-color: #FFB300">1</div>
<div class="column">Honda</div>
<div class="column">Accord</div>
<div class="column">2009</div>
</div>
<div class="row row-odd">
<div class="column" style="background-color: #803E75">2</div>
<div class="column">Toyota</div>
<div class="column">Camry</div>
<div class="column">2012</div>
</div>
<div class="row">
<div class="column" style="background-color: #FF6800">3</div>
<div class="column">Hyundai</div>
<div class="column">Elantra</div>
<div class="column">2010</div>
</div>
<div class="row row-odd">
<div class="column" style="background-color: #A6BDD7">4</div>
<div class="column">Ford</div>
<div class="column">Focus</div>
<div class="column">2008</div>
</div>
<div class="row">
<div class="column" style="background-color: #C10020">5</div>
<div class="column">Nissan</div>
<div class="column">Sentra</div>
<div class="column">2011</div>
</div>
<div class="row row-odd">
<div class="column" style="background-color: #CEA262">6</div>
<div class="column">BMW</div>
<div class="column">M3</div>
<div class="column">2009</div>
</div>
<div class="row">
<div class="column" style="background-color: #817066">7</div>
<div class="column">Honda</div>
<div class="column">Civic</div>
<div class="column">2010</div>
</div>
<div class="row row-odd">
<div class="column" style="background-color: #007D34">8</div>
<div class="column">Kia</div>
<div class="column">Soul</div>
<div class="column">2010</div>
</div>
</div>
</aside>
<div>
<canvas width="985" height="223"></canvas>
</div>
</article>
<section>Log</section>
</main>
Well your code is a mess. Here is my attempt to clean it a bit and achieve what you described: https://jsfiddle.net/dckex2g7/
I assumed that top and bottom bars have fixed height which makes this a bit simpler. If they don't, you should use display: flex; flex-direction: column on body element and flex-grow: 1 on .content.
Notice how I haven't used any JS for layout. To make it responsive you should use things like min-width and media queries. You almost never need JS for layout unless in a very few very specific cases which are not covered by flexbox somehow.
There's really a lot to describe about this solution so if you have any specific question ask away.
My HTML:
<table style="width:100%;">
<tbody>
<tr style="cursor:pointer; border-bottom:1px solid #ACACAC; height:60px;">
<td style="text-align:right; vertical-align:middle; padding:10px 10px 10px 0px;">
<span style="color:#F87E20;">Copy</span>
<div style="display:inline; color:#ACACAC;"> | </div>
<span style="color:#F87E20;">Export</span>
<div style="display:inline; color:#ACACAC;"> | </div>
<span style="color:#F87E20;">Delete</span>
</td>
</tr>
</tbody>
</table>
The result:
This is all fine, and is working wonderfully. I want to make some QOL changes, though, and while looking into some of the changes I wanted to make, ran into something that is confusing me quite a bit.
The entire row is clickable, as well as the Copy, Export and Delete spans. This becomes a problem when I try to click on Export, but miss by 2 or 3 pixels, and instead navigate away from this area. I wanted to make the clickable area for the spans bigger, so I gave the a style property like so: padding:10px 0px 10px 0px;
The padding works as intended, enlarging the clickable area around the spans, making it easier to click on them. However, I was expecting the padding to also make the entire row taller, but instead it's as if the spans' padding is just flowing over the padding on the parent.
Here are some images to help explain the situation:
Parent:
And Child:
I don't understand why the child's padding is flowing outside it's container, and I don't want to go on in this direction without understanding what's going on. I was wondering if anyone could please help me understand what's happening here?
Your spans are inline elements. Top and bottom padding is ignored in case of inline elements.
By default, spans are inline, and divs are block. However, you can always override these with display: block; or display: inline;. Block elements (also inline-blocks) have full padding support.
See:
table {
width: 100%;
border-bottom: 1px solid #ACACAC;
}
tr {
cursor: pointer;
height: 60px;
}
td {
text-align: right;
vertical-align: middle;
padding: 10px 10px 10px 0px;
background-color: #e0c000;
}
span {
display: inline-block;
color: #F87E20;
background-color: #f0e000;
}
.padded {
padding: 10px 0 10px;
}
div {
display: inline;
color: #ACACAC;
}
<table>
<tbody>
<tr>
<td>
<span>Copy</span>
<div> | </div>
<span class="padded">Export</span>
<div> | </div>
<span>Delete</span>
</td>
</tr>
</tbody>
</table>
See also this article for more on this.
I have the following content:
It is a table, with one row and three cells, two blue cells, and the middle cell, and in the middle cell I have a div, for now it looks good.
But if I put the zoom property in the div (zoom: 0.8) I get an extra space in IE11, as if the div was still the same size, like this:
In chrome, the table just adjusts to the div size, but not in IE, is there anyway I can achieve this?
This is the fiddle of the example:
http://jsfiddle.net/Z3wbN/3/
HTML:
<table class="container">
<tr>
<td class="border">
</td>
<td>
<div class="content">
This is a test
</div>
</td>
<td class="border">
</td>
</tr>
CSS:
.container {
background-color: #ddd;
}
.border {
background-color: blue;
width:10px;
}
.content {
margin: auto;
width: 500px;
border: 2px solid yellow;
zoom: 0.8;
}
One possible solution, although I don't know if you'll like it, could be this one: http://jsfiddle.net/Z3wbN/14/
On that solution:
A couple of classes/ids are added to the tags;
The width is assigned to the middle cell instead of to the div inside that cell;
if it's an IE browser, the div width is adjusted to 125% (100% / 0.8 that is the zoom).
The way of detecting the browser is JavaScript but you could try any that you want (I got it from Detect IE version (prior to v9) in JavaScript):
// if it's an IE browser then update the class to "container ie"
if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
document.getElementById("container").className = "container ie";
}
Then the CSS is adjusted as specified in the list above:
td.middle {
width:500px;
}
.content {
margin: auto;
border: 2px solid yellow;
zoom: 0.8;
}
.ie .content {
width:125%;
}
This solution displays a "similar" result on IE and Chrome/Firefox.
You need to use display:table-cell; to class .content
Here is the updated fiddle:
.container {
background-color: #ddd;
}
.border {
background-color: blue;
width: 10px;
}
.content {
margin: auto;
width: 500px;
border: 2px solid yellow;
zoom: 0.8;
display: table-cell;
}
zoom: 0.5;
<table class="container">
<tr>
<td class="border">
</td>
<td align="center">
<div class="content">
This is a test
</div>
</td>
<td class="border">
</td>
</tr>
</table>
All you need to do is to apply zoom to .container too :
.container {
zoom: 0.8
}
Here's the JSFiddle: http://jsfiddle.net/Z3wbN/12/