I want to create an HTML table where each cell is clickable, and clicking on a cell adds a border to the single div within the cell. I want that div's border to exist entirely within the existing confines of the td that contains it, without resizing the table or its cells at all. I can't seem to make this happen correctly.
This previous question seems to address the same issue and points to some articles about the box-sizing CSS options. I have a fiddle where I tried to implement this without success: http://jsfiddle.net/YsAGh/3/.
* {
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
<table>
<tr>
<td><div>1</div></td>
<td><div>2</div></td>
<td><div>3</div></td>
</tr>
....
</table>
Here's what currently happens. The border causes the containing td to grow to accommodate the div's border.
How can I add the border to the div without it affecting the containing table?
Look at my JSFiddle.
You need to provide a width/height to your cells:
td {
// ...
width:33.3%;
height:33.3%;
}
How about using an inset box-shadow?
.selected {
box-shadow: inset 0px 0px 0px 2px red;
}
OK, since I've seen a some support for my response in the comments, here it as an answer :)
Presize your cell by adding a yellow 'hidden' border to the .unselected state:
CSS
.unselected {
background-color: yellow;
border: 2px solid yellow; // Presize with this yellow border
}
div {
..
line-height: 1; // Add line-height to regulate size (optional)
}
Codepen example.
Using table-layout to fix the width of cells and small padding in selected to prevent increasing height.
table {
table-layout: fixed;
}
.selected {
padding: 1px;
}
See JSFiddle
Related
What I'm trying to do
I'm trying to implement a table header with a border and drop shadow, and have it include padding on the table head.
What I've tried
I gave the div that wraps the table a padding of .75em and when I added a drop-shadow and border to the thead, it did not go around the padding (expected). It did produce the effect I was going for, just there is still padding around the thead that I would like to be included with this effect.
Next I tried moving the .75em padding to the thead and tbody, but it is not working as intended. Inspecting says padding has no effect on internal table elements except cells.
Next I tried to wrap the content inside the thead in a div and give that a padding of .75em, but that did not work.
Next I tried to wrap the content outside the thead in a div and give that a padding of .75em, but that did not work either.
My DOM looks like this
<div class='spreadsheet'>
<table class='data'>
<thead>
</thead>
<tbody>
</tbody>
</table>
</div>
What I'm trying to achieve:
Not sure to understand what you want, but is it working for you?
.spreadsheet{
background:lightblue;
padding:0.75em;
}
table {
border-radius: 5px;
box-shadow: 2px 2px 5px lightgray;
border-spacing: 0;
background:white;
}
th {
padding: 20px;
}
thead {
border-radius: 5px;
box-shadow: 2px 2px 5px lightgray;
}
tbody {
margin-top: 50px;
padding-top: 20px;
}
As a temporary solution I've added blank rows and columns to each side and gave them a height and width respectively of the padding size. This causes problems with hovering and makes things more complicated than they probably need to be.
Before flagging this as already asked, pls read the whole scenario. Thanks
SHORT VERSION :
Just to clearly state what i am trying to achieve, here's the page https://www.facebook.com/MercedesBenzPolska/ and I want to add border to the target element (on which i am hovering), whether it be <div> or <img> or <p>, without the shaking
DETAILED VERSION
Webpage in question: Any of Facebook's page.
Requirement: Moving a cursor over an element should add border to the target element [only on hover therefore temporary border not permanent]. Permanent border will be added ONLY if I click on that element. [Simply, if I hover over an element it will be highlighted with, say, pink border and only when i click on it, a green border would be added]
Initial problem: adding border on elements on hover would make the whole page's structure shaky, since I am constantly adding and removing the border. For that what I did was add a transparent 1 px border to all the elements of the page, and on hover just change the color of the border from transparent to pink; thus no shaky.
Present problem: The above solution was working for all the pages till I encountered Facebook's page. It turns out adding the initial 1 px border totally disrupts the structure i.e. the look and feel of the page. DIVs move from somewhere to somewhere else.
How do I now solve my original problem? Is there a way of, maybe like, applying a negative margin or border, so that adding the extra 1 px border does not dirupt the page's structure? I don't know I am just suggesting. Pls help
[SCREENSHOTS]
1. this is when the page loads [without applying the border]
2. Now when I hover over the div containing image ie adding 1 px border on hover, the divs move here and there
css I am using
* { border: 1px solid transparent !important;} //when page loads
.hover-selected{ border: 1px solid #e42a78 !important;} //on hover border
.option-selected:hover { border: 3px solid #529c56 !important;cursor: default;} //when option is selected
The images and the css both reflect towards the same problem, the default 1px transparent border disrupts the page's css and If I don't do that, the on hover border application becomes shaky and the page's css anyway gets disrupt
box-shadow: 0px 0px 0px 1px #000;
Use box shadow instead border. Box-shadow don't take up space.
div {
width:300px;
height:300px;
background: red;
}
div:hover {
box-shadow: 0px 0px 0px 1px #000 inset;
}
<div> Test </div>
outline is perfect for this. It works in a very similar way to border but does not effect layout at all.
div:hover {
outline: 1px solid orange;
}
<div>
Lorem ipsum sit amet.
</div>
<div>
<img src="https://placehold.it/200x100">
</div>
<div>
Lorem ipsum sit amet.
</div>
you can use box-sizing property in css. Try below code with and without box-sizing property
<div class="item">
</div>
.item {
box-sizing: border-box;
height: 50px;
width:50px;
background:red;
}
.item:hover{
border:1px solid black;
}
I would start from something like this and move from there:
*:hover:last-child:before {
display:block;
content:"";
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
border:2px solid red !important;
}
Using a pseudo-element instead of putting a border on the actual object might not create as many issues with the initial layout. Still not exactly what you asked for, but I believe it's at least a bit closer. :-)
EDIT
I believe that the only way to achieve this as good as possible would be to be less greedy when selecting elements in the CSS, and specify a list like so:
a:hover:before,
img:hover:before{
display:block !important;
content:"" !important;
position:absolute !important;
top:0 !important;
bottom:0 !important;
left:0 !important;
right:0 !important;
border:2px solid red !important;
}
I'm struggling to set an EXACT height on a html table-row.
The left column are two divs of height 44px each, and the right column is the table. The height of each row is set to 44px but rows are displayed as 44.44px in height on Google Chrome web inspection tool.
SASS:
table {
width: 100%;
border: none;
tr {
height: 44px;
cursor: pointer;
td {
padding: 0 16px;
border-bottom: 1px solid $bg-light;
vertical-align: middle;
}
}
}
I'm also using Eric Meyers css-reset, although the problem remains with or without it.
Any clues on where the extra .44px comes from?
EDIT
Problem seems to be specific to Google Chrome. Safari renders the correct height.
Possible duplicate of Eliminate gap between tbody tags
but, this is a border-collapse, border-spacing issue with your table next to your div. By default tables have that annoying extra space there. Also make sure that your td's and divs are display inline-block/block or flex if you want to align items in the center.
here's some code and a link for how to resolve it.
table {
border-spacing: 0;
border-collapse: collapse;
}
https://codepen.io/calebswank11/pen/rJvBWK
I've put a new image in my table cells and it's making the table a bit bulky. I've reduced all padding and spacing to 0, but there are still gaps of about 4 pixels around the edge of the images (32 pixel rectangular flags). It is possible to remove all white space from a table cell?
My CSS for the cells is :
.flag {
margin: 0px !important;
padding: 0px !important;
border-spacing:0 !important;
height:28px !important;
width:32px !important;
line-height:32px !important;
}
Note that they need '!important' because they're over-riding another CSS. The exact height of the flags appears to be 28px. If I reduce height below that, the flag starts to get shaved off at the bottom of the cell.
More info so people can try for themselves.
td { border: 1px solid black; }
<link
rel="stylesheet"
type="text/css"
href="//cloud.github.com/downloads/lafeber/world-flags-sprite/flags32.css"
/>
<table class = 'f32'><tr><td class = 'flag us'></td></tr></table>
Make sure your table has visible borders and you'll see that there's a fair bit of spacing above and below the flag image. I want to get rid of that, or preferably all except 1 pixel above and below.
It depends if the white space is all around the images or only below them.
Try to add vertical-align:baseline; to your CSS rule for the cells - this should at least eliminate unwanted space below the image.
If there is whitespace within the image itself, you might try negative margin values on the images, like
.flag img {
margin-top: -4px;
}
The whitespace is within the image itself, and it's using a "sprite", wherein there are multiple images and a background-position is set such that the appropriate icon is displayed.
I've replicated what I think you want below. Note that I've overridden the styles without the use of !important. You just need the selector to be more specific than the one you're overriding.
.f32 td.flag.us {
border: 1px solid black;
height: 23px;
overflow: hidden;
background-position: 0 -7332px;
width: 30px;
}
<link rel="stylesheet" type="text/css" href="//cloud.github.com/downloads/lafeber/world-flags-sprite/flags32.css" />
<table class='f32'>
<tr>
<td class='flag us'></td>
</tr>
</table>
I'm trying to get a table to show on my WP site and I'm just learning CSS, but when I create what I think should be right doesn't come out right. Only the outer right, left, and top border show.
.front {
border: 2px solid red;
}
I put that in the table class. I also tried adding another bottom-border, even by itself, nothing. Here's my table code, very simple. Just trying to learn to use CSS.
<table class="front">
<tbody>
<tr>
<td>test.</td>
<td>test.</td>
</tr>
<tr>
<td colspan="2">test.</td>
</tr>
</tbody>
</table>
I searched on here and tried this, but doesn't work (there were others things I found on here, but this one I remember):
table {
border-collapse: separate;
}
I should note that I use a CSS plugin to make it easier for me to add it to CSS. Haven't used it much, a simple hide which works fine. Not sure if this could have something to do with it, just some extra info that may help.
The problem is probably the height of the table or the container in which is placed.
Look at this fiddle and try setting the height of the table to 100% like this:
.front {
border: 2px solid red;
height: 100%;
}
Just apply the following CSS accordingly; this will fix your issue:
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
The problem is with the "box-size" being "100%", so the borders are pushed further. If you apply the CSS properties above to such elements, you won't have to face such issues.
Edit:
This will work on elements that span across the full width (-or full height) inside another.
So, it will work like this with either "width" or "height", but here's both:
CSS:
.spanBox
{
width:100%;
height:100%;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
}
.redBorder
{
border: 2px solid red;
}
HTML:
<table class="spanBox redBorder"><tr><td>...</td></tr></table>