I have a table cell with various height.
I want to have 1 or more spans in the last cell with some requirements:
height must be equal of the full row height, most imortant
width must be equal to the spans. So exactly fit to the right and no whitespace on the left.
I can not find the solution.
The CSS:
* {
box-sizing:border-box;
}
table {
width:100%;
}
td { border:1px solid blue; }
input {
height:50px;
}
.buttons {
width:1px;
white-space:nowrap;
}
.button {
padding:5px 20px;
background:red;
display:inline-block;
}
And the HTML:
<table>
<tr>
<td><input value="I have height of 50px" /></td>
<td class="buttons">
<span class="button" title="I have width of 25px">+</span>
<span class="button" title="I have width of 25px">+</span>
</td>
</tr>
</table>
I tried position relative/absolute on the last td and a container for the buttons, but then it will fall outside the table
Ofcourse i tried height:100%, no difference
http://jsfiddle.net/70a6q5kz/3/
--edit
Solution
I found this solution:
Css:
table { height:100%; }
.buttons { height:100% }
Full solution:
http://jsfiddle.net/70a6q5kz/11/
In the css you could remove the padding from td, create the .button:last-of-type and insert a margin-right:-1px on it
* {
box-sizing:border-box;
}
table {
width:100%;
}
td {
border:1px solid blue;
padding: 0; /********************************/
}
input {
height:50px;
}
.buttons {
width:1px;
white-space:nowrap;
padding: 0;
}
.button:last-of-type{ /*************************/
margin-right:-1px; /*************************/
} /*************************/
.button {
padding:15px 20px;
background:blue;
display:inline-block;
height:50px;
}
The html can stay the same
Possible Solution
I found this as an possible solution. In simple words:
Css:
table { height:100%; }
.buttons { height:100% }
Full solution:
http://jsfiddle.net/70a6q5kz/11/
Works in IE 11, FF and chrome
You could do something with flex. You also need to remove the initial padding of the tds.
Check snippet:
* {
box-sizing: border-box;
}
table {
width: 100%;
}
td {
border: 1px solid blue;
padding: 0; /* remove initial padding */
}
input {
height: 50px;
}
.buttons {
display: flex;
white-space: nowrap;
}
.button {
flex: auto;
width: 25px;
background: red;
text-align: center;
}
<table>
<tr>
<td><input value="I have height of 50px" /></td>
<td>
<div class="buttons">
<span class="button" title="I have width of 25px">+</span>
<span class="button" title="I have width of 25px">+</span>
</div>
</td>
</tr>
</table>
Related
I'm trying to align some text, and input field and an image which is approx 25x25 px
What I get is the text and Image at the top of the div and the input slightly lower down.
How do I align them so they are all vertically level with each other.
This is what I have so far:
div.block {overflow:hidden; border:1px solid #000 }
div.block label {width:150px; display:block; float:left; text-align:left; vertical-align:middle; }
div.block.input {margin-left:4px; float:left; vertical-align:middle; }
https://jsfiddle.net/a3cmfpzL/
Thanks
This is for Horizontal:
.block{
display:flex;
}
label{
border:1px solid red;
}
img{
height:25px;
width:25px;
}
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890'/>
<img src='http://placekitten.com/301/301'/>
</div>
If you're looking for Vertical direction then:
*{
border:0;
padding:0;
}
.block{
display:flex;
flex-direction:column;
}
label{
border:1px solid red;
width:100px;
}
img{
height:25px;
width:25px;
}
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890'/>
<img src='http://placekitten.com/301/301'/>
</div>
I would strongly advise you to use flexbox.
Can be a bit confusing at the beginning, but once you get the hang of it, you'll love it.
Here you can find a perfect guide for using it.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Something like this (just written out of my mind, so untested)
Try removing your classes first.
Your CSS
.flex {
display: flex;
flex-direction: column;
justify-content: space-between; // only use this if you don't justify-self the elements
}
.border {
border:1px solid #000;
}
img {
width: 25px;
height: 25px;
justify-self: top; // if you don't use flex-direction on .flex
}
input {
justify-self: top; // if you don't use flex-direction on .flex
}
Your HTML
<div style="flex border">
<img src='image.png'/>
<label>Test Label</label>
<input type='text' value='1234567890'/>
</div>
Few Issues
This div.block.input isn't selecting anything so all those styles aren't being used
You made the label a block level element so it won't respond to the vertical-align property.
The <input> and the <img> are inline elements so they will share the same line box in which they live, Their vertical alignment will be set by the vertical-align property.
The default value for vertical-align property is baseline:
Align the baseline of the box with the baseline of the parent box. If the box does not have a baseline, align the bottom margin edge with the parent's baseline.
In this case we have an <img> which doesn't have a baseline, So it will be aligned based on it's bottom margin, in our case the img have no margin so it's basically the bottom edge.
The <input> However have a baseline, So it's baseline will be aligned with the bottom edge of the img next to it.
DEMO
div.block {
overflow: hidden;
border: 1px solid #000
}
div.block label {
width: 150px;
display: block;
float: left;
text-align: left;
vertical-align: middle;
}
div.block.input {
margin-left: 4px;
float: left;
vertical-align: middle;
}
div {
position: relative;
}
div:after {
content: '';
position: absolute;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(red, red) 0 24.5px/ 100% 1px no-repeat
}
<p>red line is the baseline</p>
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890' />
<img src='https://i.picsum.photos/id/527/25/25.jpg' />
</div>
Solutions
I cleaned the code a bit and remove unnecessary styles.
Floats aren't necessary
If you to apply a width to the label use display:inline-block; instead
1) vertical-align: middle;
* {
padding: 0;
margin: 0;
}
div.block {
overflow: hidden;
border: 1px solid #000
}
label {
width: 150px;
display: inline-block;
vertical-align: middle;
background: brown;
}
input {
vertical-align: middle;
border: 0;
background: red;
}
img {
vertical-align: middle;
}
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890' />
<img src='https://i.picsum.photos/id/527/25/25.jpg' />
</div>
Items will be aligned relative to middle of the tallest item which is the img.
2) If you want elements to share the same height
Knowing the <img> is the tallest and it's height, we can simple apply that same height to the other elements, We still need to apply vertical-align:top to align the elements based on their top edge instead of the baseline
* {
padding: 0;
margin: 0;
/* because we defined a height on the element,
we need to ensure that any extra space should be
calculated within that height */
box-sizing: border-box;
}
div.block {
overflow: hidden;
border: 1px solid #000;
font-size:0;
}
label {
display: inline-block;
width: 150px;
height: 25px;
font-size:16px;
vertical-align: top;
line-height:25px;
background:brown;
}
input {
height: 25px;
vertical-align: top;
border:0;
background:red;
}
<div class='block'>
<label>Test Label</label>
<input type='text' value='1234567890' />
<img src='https://i.picsum.photos/id/527/25/25.jpg' />
</div>
3) Flexbox
Which is the simplest and i won't be covering because it have been covered in other answers.
Try adding position:absolute and display:flex inside your div.block.
That should do the trick if I understand your question correctly.
Given this table:
<table>
<tr>
<td rowspan="2"><a>Hello</a></td>
<td><a>World</a></td>
</tr>
<tr>
<td><a>!</a></td>
</tr>
</table>
I want to style the a element in every td to be 100 percent height:
table, td {
border: 1px solid black;
}
td {
height:400px;
}
a {
background-color: red;
height: 100%;
display:block;
}
This (JsFiddle) works fine on IE and Chrome. However Firefox seems to have a problem with the colspan.
So I tried to change the given fixed height on tr instead of td:
table, td {
border: 1px solid black;
height: 100%
}
tr {
height:400px;
}
a {
background-color: red;
height: 100%;
display:block;
}
This (JsFiddle) works fine on Firefox and Chrome, but not on IE.
On the right side is, what I want:
How can I make this work in all Browsers?
if you want more control over the TDs, i would highly suggest to use the display option of table and table-cell:
.table-wrapper{
position:relative;
display:table;
width:100%;
height:400px;
border:1px solid #000;
}
.table-wrapper a{
position:relative;
display:table-cell;
height:100%;
text-align:center;
vertical-align:middle;
}
you can easily choose the width, and control the content of "table-cell". This is supported by IE9.
link of demo:https://jsfiddle.net/keinchy/mq3nafje/6/
I'm building a dummy form in CodePen and in the middle row I wanted to have two input boxes next to each other taking up 50% each in the row. I was able to do this, however, I cannot click inside either input box to start typing. The only way I can start typing in each input is to start at the first box and press 'tab'. Any suggestions?
HTML:
<div class="wrapper">
<h1>Application for Philadelphia Eagles</h1>
<h2><strong>Position:</strong> Wide Receiver</h2>
<p>An attempt at Input label floats</p>
<form class="form-container">
<div class="form-tr">
<div class="tc-100"><input type="text"></div>
</div>
<div class="form-tr">
<div class="tc-50 flt-l"><input type="text"></div>
<div class="tc-50 flt-r"><input type="text"></div>
</div>
<div class="form-tr">
<div class="tc-100"><input type="text"></div>
</div>
</form>
</div>
CSS:
#import url('https://fonts.googleapis.com/css?family=Josefin+Slab:400,300,700');
body {
font-family: Josefin Slab, sans-serif;
}
h1, h2 {margin: 0.465em}
.wrapper {
margin: 0 auto;
text-align: center;
width: 75%;
}
.flt-l { float:left; }
.flt-r { float:right; }
.form-container {
position:relative;
border: 1px solid #000;
width:40em;
margin: 0 auto;
}
.form-tr {
display: block;
position:relative;
margin-bottom: 0px;
width:100%;
}
.tc-50 {
width:50%;
}
input[type=text] {
width: 100%;
}
input[type=text] {
font-size:1em;
padding:1em;
box-sizing: border-box;
}
My CodePen here: http://codepen.io/mjdeangelis/pen/avpBex?editors=110
Another fix is removing the position:relative property from the form-tr on the CSS file since the outer form container already has it and in this case its making the 2 inputs in the middle overlap.
Before:
.form-tr {
display: block;
position:relative;
margin-bottom: 0px;
width:100%;
}
After:
.form-tr {
display: block;
margin-bottom: 0px;
width:100%;
}
For extra references you can always check out the following links:
http://www.w3schools.com/cssref/playit.asp?filename=playcss_position
http://www.w3schools.com/cssref/pr_class_position.asp
Change your classes like:
.flt-l {
float:left;
}
.flt-r {
display: inline-block;
}
The third row is overlapping with second row, hence not clickable. Provide a height to the second row, so that third row is pushed below it.
Updated code is available here
http://codepen.io/anon/pen/rOjjYE?editors=110
.split-row{
height:50px;
}
Set form-tr height to match the height of your full-width rows.
.form-tr {
display: block;
position: relative;
margin-bottom: 0px;
width: 100%;
height: 55px;
}
If you can remove "position: relative" in class, it also works.
.form-tr {
display: block;
margin-bottom: 0px;
width:100%;
}
I have a html <table> that have dynamic width (changes with window size), and a fixed width <span> (500px).
I want to display both next to each other so that both would fill the whole width of the parent container
I want to do so only using CSS (not js)
I have been playing around with CSS but it seems to be ruining the table's width
HTML
<div class='container'>
<table class='table'>....</table>
<span class='span'>....</span>
</div>
CSS
.container {
......
}
.table {
.....
}
.span {
width: 500px;
display: inline-block; //or block if neccessary
}
You may give a try with the table-layout propertie to .container and span.
Browsers should create themselves the element missing to produce the first table-cell.
DEMO
span {
display:table-cell;
width:500px;
border:solid;
}
table {
border:solid;
margin:0;
width:100%;
}
.container {
display:table;
width:100%;
table-layout:fixed;
}
This should work
because of table-layout:fixed
and because browser should create themselves the missing element (like a tbody is always produced in a <table> when missing or when in a document is missing either tags like html or body ).
Demo
css
body, html {
margin:0;
padding:0;
}
.container {
}
table {
width: calc(100% - 500px);
border-collapse: collapse;
float: left;
}
/* Zebra striping */
tr:nth-of-type(odd) {
background: #eee;
}
th {
background: #333;
color: white;
font-weight: bold;
}
td, th {
padding: 6px;
border: 1px solid #ccc;
text-align: left;
}
.span {
display: inline-block;
width: 500px;
height: 100px;
background: red;
}
html
<div class='container'>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>etc</th>
</tr>
</thead>
<tbody>
<tr>
<td>Gaurav</td>
<td>Singh</td>
<td>etc</td>
</tr>
<tr>
<td>Gaurav</td>
<td>Singh</td>
<td>etc</td>
</tr>
</tbody>
</table> <span class='span'>....</span>
</div>
This is what i'm doing right now. I'm using LESS CSS for my design. I need to put 2 spans between a specified input. all the elements should be 100% width. spans should always 20px width input width can be change according to the screen width. Can anyone help me?
span width: 20px;
<div class="wrapper">
<span class="span-one">span</span>
<input type="text" class="input">
<span class="span-two">span</span>
</div>
You can achieve this with absolute positioning. It will take the spans out of the flow and put them on top of the input. You should also put the input in a div to do this as it doesn't naturally get 100% width when display:block set on it.
HTML
<div class="wrapper">
<span class="span-one">span</span>
<div class="input"><input type="text" class="input"></div>
<span class="span-two">span</span>
</div>
CSS
.wrapper { position: relative; }
div.input { margin: 0 20px; }
input {
width: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box; }
span.span-one {
position: absolute;
width:20px; height:20px;
left:0; top:0;
background-color: red; }
span.span-two {
position: absolute;
width:20px; height:20px;
right:0; top:0;
background-color: red; }
Here's the fiddle: http://jsfiddle.net/ywUeu/1/
Of course the word 'span' in the spans is longer than 20px so it comes out of the span.
Might be best to add 'box-sizing' to input as I've done too.
Positioning is not ideal but you already approved the answer...this is an FYI....
Instead of positioning use: display: table-cell;
http://jsfiddle.net/Riskbreaker/CBC5A/1/
With your HTML:
.wrapper, .text {
width: 100%;
}
.wrapper > span {
display: inline-block;
width: 20px;
}
.wrapper.input {
width: calc(100% - 40px);
}
Better way:
<div class="wrapper">
<div class="input"><input type="text" class="text-input"></div>
</div>
CSS:
/*
.wrapper, .input{ width is 100% by default }
*/
.text-input{
margin: 0 20px;
width: calc(100% - 40px);
}