I've got 3 elements in a div with an overflow. First two are divs, third is a table. I need to place them in a single row.
My table's width is determined by cell width. Setting a table width would make this easy, but I can't do that because cell number varies. I want it to overflow a parent div and make it scrollable and not shrink based on parent width. This was achieved by setting a table's width to 100% and setting it table-layout to fixed. But because it has width set to 100%, it puts it in a second row.
I can't set position absolute on table because it's contained within a moveable div. I need it to be relative to parent div. I've found it's doable by setting a negative margin-top on table, but I would prefer to not have this hardcoded in case height of my first two elements changes.
Here is a JSFiddle of my situation: https://jsfiddle.net/9mvjftag/
HTML:
<div class="container">
<div class="first-row">
test
</div>
<div class="second-row">
test
</div>
<table>
<tr>
<td>test</td>
<td>test2</td>
</tr>
<tr>
<td>test</td>
<td>test2</td>
</tr>
</table>
</div>
CSS:
.container {
width:300px;
background-color:red;
overflow: auto;
}
.container > * {
float: left;
}
table {
table-layout: fixed;
width: 100%;
}
table td {
background-color: white;
width: 200px;
}
How do I achieve this? Changing table's css is fine as well if there's another way to achieve my current situation (it's width determined by cell width, overflowing the parent div)
Here is an article that may help you it involves using Flexbox.
so in your case, this may work
HTML
<ul class='row'>
<li class='item'></li>
<li class='item'></li>
</ul>
CSS
.row{
background-color: #fff;
min-width: 100%;
min-height: 200px;
display: flex;
justify-content: center;
align-items: center;
overflow-x: auto;
}
.row::-webkit-scrollbar {
display: none;
}
.item{
background-color: #e74c3c;
min-width: 200px;
margin: 5px;
}
all the class names or HTML selectors can be interchangeable and I just used %ul and %li and examples
Related
I'm trying to set rows within a table to be 20% of page height.
Currently index.html looks something like this:
div#machines {
display: flex;
height: 100%;
justify-content: center;
align-items: center;
padding-left: 10%;
padding-right: 10%;
width: 80%;
}
html,
body {
height: 100%;
min-height: 100%;
}
td {
width: 20%;
height: 100%;
}
tr {
width: 100%;
height: 20%;
}
table {
width: 100%;
height: 100%;
}
<body>
<div id="machines">
<table>
<tr>
<td id="stone">0</td>
<td id="copper">0</td>
<td id="iron">0</td>
<td id="coal">0</td>
</tr>
<!-- ... (trs continue but truncated for length of code) -->
</table>
</div>
</body>
So therefore (at least to my understanding) a table is taking up 100% of the machines div then I create a row in that table that is 100% of the width and 20% of the height (but incorrectly the row becomes 100% of the height of the table.) The td should then be 20% of the width of the row and 100% the height of the row.
How can I fix this so that a tr is only 20% of the page height? Thank you.
You can use Viewport units (the browser window size). 100vh is the height of the screen and 100vw is the width of the screen.
If the table higher you can use the min-height property and set it to 100vh.
If i understood the question, i think this css would do what you need:
td {width: 20vw;}
tr {height: 20vh;}
table {width: 100%; min-height: 100vh;}
Hope it helps.
Read more here: https://developer.mozilla.org/en-US/docs/Web/CSS/length
It's the conflict between the style of your tr and td. Try removing one each and see the difference. It's not possible to set different heights of the two since td is embedded in tr.Hope this helps.
I want to make a 900px wide centered table with one row and two cells.
I want the right cell to always be 200px wide and the left cell should fill out the rest, but when I make the browser window smaller the right cell jumps down under the left cell.
(when both cells have percentages it works fine but the right cell/block needs to be 200px)?
My HTML:
<table class="sitecontent">
<tr>
<td class="boxed"> contents </td>
<td class="infobar"> contents </td>
</tr>
</table>
My CSS:
.sitecontent {
max-width:900px;
margin: 0 auto;
display: table;
height: 100%;
}
.boxed {
max-width:75%;
float: left;
}
.infobar {
float: left;
width: 200px;
border: 1px solid rgb(240,240,240);
overflow: auto;
max-height: 100%;
}
Shouldn't the max-width make the width of "boxed" less than 75% so that the content fits when the table width goes below 900px?
I've tried using divs instead of a table, but the result is the same?
If you want the other cell to fill out the rest of the space you can use a function called calc()
This allows you to determine sizes using basic addition and subtraction using percentages and fixed units.
calc() is widely supported see http://caniuse.com/#feat=calc
One good reference is http://css-tricks.com/a-couple-of-use-cases-for-calc/
In the following example I have
.boxed {
max-width:calc(100% - 200px);
}
http://jsfiddle.net/AuroraArcade/42g5mw80/2/
In order to make the infobar 200px wide you either have to use box-sizing:border-box on the element or you can adjust the inner width or you can remove the borders and spacing.
One example
.infobar {
width: 200px;
box-sizing: border-box;
border: 1px solid rgb(240,240,240);
overflow: auto;
max-height: 100%;
}
This question has been asked several time on stackoverflow, however I was wondering if someone -perhaps you- doesn't have a unique solution to my problem.
I currently have an parent div that is of varying height and width whose contents are also of varying width and height. To vertically align the child div I have styled it's parent as display: table; and it as display: table-cell; and nested yet another div, as seen below:
<!-- css styling -->
<style type="text/css">
.div-table {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.div-cell {
display: table-cell;
padding: 10px;
vertical-align: middle;
}
.div-alignedcontents {
margin-left: auto;
margin-right: auto;
text-align: center;
}
</style>
<!-- html -->
<div class="div-table">
<div class="div-cell">
<div class="div-alignedcontents">
<p>Some content that has a varying height and width!</p>
</div>
</div>
</div>
The problem is that the overflow:hidden property doesn't seem to work on tables, and table-layout: fixed property doesn't constrain vertical proportions/height. One solution would be to nest all the above html in yet another div and style that div with overflow:hidden, however I thought I might pick your brains for any suggestions first.
Thank you in advance for any help.
One solution could be defining padding for .div-cell in percentage and subtracting the same padding from the width of your div-table. Something like this:
.div-table {
width: 95%
height: 95%
}
.div-cell {
padding: 2.5%
}
Fiddle
This is probably the most unusual CSS behavior I have ever seen:
I have an extremely simple table that consists of two cells - one with plain text and another with a link:
<div class="content">
<table>
<tr>
<td>
Hello, world!
</td>
<td>
Hello, world!
</td>
</tr>
</table>
</div>
I have also applied the following CSS to the table:
div.content {
background-color: green;
height: 100px;
}
table td {
background-color: red;
height: 50px;
}
table td a {
background-color: orange;
box-sizing: border-box;
display: block;
height: 100%;
padding: 8px;
width: 100%;
}
When rendered in Chrome 28, I see the following:
Why is there a large amount of red above and below the link? I have specified height: 100%; for the link, so it should be taking up the full height of the <td>, which has an explicit height.
It's definitely an issue with the box-sizing:border-box attribute. My guess is that putting that inside a table cell (which is treated differently then a div) is confusing the browser. Often, new techniques + old techniques don't mix.
I would suggest doing the following:
table td a {
background-color: orange;
display: block;
height: 100%;
padding: 8px;
}
The width:100% was unneeded since the table cell already expanded to the text width + padding width. For some reason, it doesn't seem to add the padding to the height 100% with the table cell (go figure, weirdness with tables! lol). If you need it to expand to a larger table cell width, I would suggest then putting the width:100% back but then ditch the horizontal padding (i.e. put padding:8px 0px;).
As far as I think its the box-sizing attribute causing this, change your css to:
table td a {
background-color: black;
height: 100%;
display:block;
margin: 0;
padding: 0px;
width: 100%;
padding-top: 12px;
}
Hope that helps;
Add This Code to table td:
display:inline-block;
because There is some difference between tables and divisions in box modeling.
you must set display-block on any none-block element for apply box-model style.
Try setting height in px for a as
table td a {
background-color: orange;
box-sizing: border-box;
display: block;
height: 50px;
padding: 8px;
width: 100%;
}
here's an example of a jury-rig: http://jsfiddle.net/rTAwd/
We're using a line height to adjust the cell's height, so we don't need to mess with vertical alignment, and relying on a wrapper div to provide our background and padding.
<div class="content">
<table>
<tr>
<td>Hello, world!</td>
<td>
<div> Hello, world!</div>
</td>
</tr>
</table>
</div>
css
div.content {
background-color: green;
height: 100px;
}
table td {
background-color: red;
}
table td div a {
line-height: 2em;
}
table td div {
background-color: orange;
padding: 8px;
}
I think its a bug, i had the same issue a while ago, if you want the text to vertically align in the middle, instead of using display:block on the <a> tag use display:table and use border-spacing instead of padding, like this:
table td a {
background-color: orange;
display: table;
height: 100%;
border-spacing: 8px 13px;
}
I removed the width:100% too since it will do it by default, you can see an example here.
border-spacing is the CSS property for cellpadding.
I've got HTML structure like this :
<div class="wrapper">
<table>
<tbody>
<tr>
<td>some content</td>
</tr>
</tbody>
</table>
</div>
My wrapper style is like this :
.wrapper{
color: #444444;
height: 165px;
line-height: 135%;
margin-top: 10px;
padding-bottom: 10px;
width: 270px;
}
and my table has only width defined along with some inherited styles :
.wrapper> table {
width: 100%;
}
table {
background-color: transparent;
border-collapse: collapse;
border-spacing: 0;
max-width: 100%;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
What I want to do is that my table consumes maximum height of parent element that is .wrapper whose height value is 165px but for some reason table height is not decreasing.
My end goal was to set a y-overflow on wrapper so if table gets bigger that .wrapper that user can scroll, what am I missing here any hints?
When I change table height to be > then 190px, the table reacts it grows bigger, but if I change to smaller size i.e. 165px or any below 190px it doesn't seem to respond
Addig height: 100% to the table style does make it inherit the height from the parent element:
.wrapper> table {
width: 100%;
height: 100%;
}
Demo: http://jsfiddle.net/WDFa2/
Note: The height that the table inherits is the specificed height of the parent, i.e. 165px, not the height including padding, i.e. 175px.
If you want it 100% high, give it height: 100%. Currently you don't specify the height at all.
Remember that tables tend to not obey all height rules. If the content doesn't fit, they may be a little stubborn. ;)