Giving text input the remaining width - css

I have a label, input and button one line. The width of the label and button are dynamic, they take the width of the text they contain. The label is aligned to the left, and the button to the right.
I want the button to be aligned to the far right, and the input in the middle take up all the remaining space.
I put a float: right on the button, but if I put the width of the input to 100% it goes to the next line and acts like a block element.
What would be the best / easiest way to realize what I'm trying to do here? This is a little bit tricky for me. I'm not sure what to do.
label {
margin-right: 10px;
}
input[type=text] {
width: 355px; /*100% doesnt work*/
height: 20px;
padding: 5px 10px;
}
input[type=text]:focus {
outline: 0;
}
button {
margin-left: -1px;
float: right;
vertical-align: bottom;
}
<fieldset class="last">
<label for="place">Any text here:</label>
<input type="text" id="place" value="Test value">
<button>Click</button>
</fieldset>
JSFiddle Demo

Here's a bit of a hacky way to do it:
table {
width: 100%;
}
.fixedText {
width: 1px;
white-space: nowrap;
}
.relativeText {
width: 100%;
}
#place {
width: 100%;
}
<table>
<tr>
<td class="fixedText">
<label for="place">Any text here:</label>
</td>
<td class="relativeText">
<input type="text" id="place" value="Test value">
</td>
<td class="fixedText">
<button>Click</button>
</td>
</tr>
</table>

Just to show you that you can also do it without tables (although I would probably go for the display: table-cell).
First float the label and the button to the left and right. Add a wrapper around the input. The wrapper gets overflow: hidden which creates a new block formatting context, see http://www.stubbornella.org/content/2009/07/23/overflow-a-secret-benefit/ for background information. In short this allow the wrapper to take up the remaining space.
Afterwards we apply some fixes to the padding, box-sizing, height and line-height to make it all look good again.
label {
height: 40px;
line-height: 40px;
margin-right: 10px;
float: left;
}
.wrapper {
overflow: hidden;
}
input[type=text] {
width: 100%;
height: 40px;
padding: 5px 10px;
box-sizing: border-box;
}
input[type=text]:focus {
outline: 0;
}
button {
margin-left: -1px;
float: right;
}
<fieldset class="last">
<label for="place">Any text here:</label>
<button>Click</button>
<div class="wrapper"><input type="text" id="place" value="Test value"></div>
</fieldset>

Related

How to separate label text respective to div?

I have a label defined where one half needs to be at left side and the other half of text needs to be on right side. How can I solve this so that the other half is pulled right?
I have added margin-right to get the text to pull at right but it's not consistent for other divs.
<div class="radio">
<input class="radio-test" type="radio" value="3" checked="checked" name="test[id]" id="test_id">
<label class="radio-label" for="test_id_1">
Test of $12.0
<span class="test-cost">Free</span>
</label>
<hr class="test-hr">
<p class="test-message"></p><p>- First test message</p><p></p>
</div>
Expected Result:
Current Result:
How can I make text in above image i.e. 'Free' to appear on the right most side as shown on expected result? Also make it always consistent on other div such that the space from div is same across.
Here is a complete working example with JsBin: https://jsbin.com/yafesuyola/1/edit?html,css,output
It uses flexbox with justify-content: space-between. I also added a div around the label and the input to keep them on the same line with 100% width.
<div class="radio">
<div class="radio-and-label">
<input class="radio-test" type="radio" value="3" checked="checked" name="test[id]" id="test_id">
<label class="radio-label" for="test_id_1">
Test of $12.0
<span class="test-cost">Free</span>
</label>
</div>
<hr class="test-hr">
<p class="test-message"></p><p>- First test message</p><p></p>
</div>
.radio {
border: 2px solid #33c;
border-radius: 5px;
padding: 10px;
background: #e0eeff;
color: #33c;
font-weight: bold;
}
.radio-and-label {
display: flex;
}
.radio-label {
width: 100%;
display: flex;
justify-content: space-between;
}
.test-cost {
text-align: right;
}
.test-hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
Hope that helps!
Clip the element to always be pinned to top right of the element?
.radio {
position: relative;
}
.test-cost {
position: absolute;
top: 2px;
right: 2px; // or whatever px/rem/etc value that fits your need
}

Only first input element inside div gets effected but not others

Here is my example from jdfiddle:
http://jsfiddle.net/7RT7a/
Here is html code:
<div id="product-pack">
<div name="productRow">
<div class="product-title-input">
<p class="titles">Product</p>
<input name="product" type="text" >
</div>
<div class="unit-title-input">
<p class="titles">Unit</p>
<input name="unit" type="text" >
</div>
<div class="quality-title-input">
<p class="titles">Qty.</p>
<input name="qty" type="text" >
</div>
</div>
Here is CSS:
.product-pack {
float: left;
width : 96%;
}
#product-pack input {
width: 95%;
height: 1em;
font-size:1em;
font-weight:bold;
border-style: solid;
background-color:#CCC;
border-color:#999;
border-width:thin;
padding: 1% 0 1% 0;
margin-top: 0.5%;
}
#product-pack .product-title-input, .unit-title-input, .quality-title-input {
float: left;
text-align: left;
}
#product-pack .product-title-input {
width: 82.5%;
padding-top: 2%;
}
#product-pack .unit-title-input {
width: 5%;
padding: 2% 1.5% 0 0;
}
#product-pack .quality-title-input {
width: 5%;
padding-top: 2%;
}
In jsfiddle result, you can see that effect of #product-pack input CSS selector only applies to first input element inside #product-pack and no matter what I try, the other two input elements get no effect.
Am I missing something with CSS or HTML? Any help would be appreciated.
Padding percentages are calculated from the width of the containing block (per MDN). Use px or em instead to specify the padding.
If you're trying to get all the input's to be the same height you have padding differences between the input's parents. specifically here,
#product-pack .unit-title-input {
width: 5%;
padding: 2% 1.5% 0 0;
}
You forgot to add </input> after <input>
Jsfiddle - as you can see here, all of the <input> boxes now are affected by the css styling (they turn yellow)

Input 100% width of remaining width

I'm working with Zurb's Foundation to create a responsive website, however I've run in to an issue when trying to make my search form responsive.
I have an input field, then to the right of it a <button> to submit the form. The button has an image within it, which is 70px wide. So what I need to do is have the input field take up 100% of whatever is remaining.
I've tried looking at the overflow: hidden; method without much luck. I've even tried using percentages, but when you go down to the mobile size the button image is really small due to resizing (a part of foundation).
Here's what I have:
HTML:
<div class="form-search">
<input id="search" type="text" class="input-text" />
<button type="submit" class="button expand postfix">
<img src="images/search-submit.png" />
</button>
</div>
CSS:
div.form-search { overflow: hidden; }
div.form-search input { float:left; overflow: hidden; background: #ebebe7; border: none; padding: 1em; outline: none; }
div.form-search button { display: block; background: none; border: none; padding: 0; margin: 0; }
Fixed it by using:
HTML:
<div class="form-search">
<button type="submit" class="button expand postfix">
<img src="images/search-submit.png" />
</button>
<div>
<input id="search" type="text" name="search" class="input-text" />
</div>
</div>
CSS:
div.form-search div { overflow: hidden; }
div.form-search div input { width: 100%; background: #ebebe7; border: none; padding: 1em; outline: none; }
div.form-search button { float: right; width: emCalc(70); background: none; border: none; padding: 0; margin: 0; }
This floats the submit button to the right of the div and because of it's set width and overflow: hidden; on the input's parent div it fills the remaining space with a 100% width.

How to center a set of html radio buttons in a fieldset with div

I have a set of radio buttons and labels. The radio buttons precede the labels. I would like to center the set of them within a field set. I tried putting them in a div with display set to inline-block. Almost works, but one label gets bumped down to the next line.
My understanding was that giving a div display: inline-block would make it shrink-to-fit, but I'm getting the unexpected behavior you can see here (code below):
http://jsfiddle.net/abalter/TedVe/13/
Is my only hope to manually set margins and stuff? Is there a way to understand why the div is shrinking just a bit too much??
Update...
If I remove the right margin from the label (which is there to add space before the next radio button) then it fits. If, instead, I add margin-left to the button, I still have the problem.
<form>
<fieldset>
<legend>Test</legend>
<div>
<input class="radio-input" type="radio" name="test" value="yes" />
<label class="radio-label">Yes</label>
<input class="radio-input" type="radio" name="test" value="yes" />
<label class="radio-label">No</label>
<input class="radio-input" type="radio" name="test" value="yes" />
<label class="radio-label">Maybe</label>
</div>
</fieldset>
.radio-label {
float: left;
margin-right: 3%;
}
.radio-input {
float: left;
}
fieldset {
text-align: center;
}
div {
display: inline-block;
margin: auto;
border: 1px solid black;
}
Try this remove float left on your .radio-label and .radio-input and now define
display inline-block
As like this
.radio-label {
display: inline-block;
vertical-align: top;
margin-right: 3%;
}
.radio-input {
display: inline-block;
vertical-align: top;
}
Demo
Just remove all styles but text-align:center and you got it. No need to display: inline-block.
here's an updated fiddle:
http://jsfiddle.net/TedVe/8/
.radio-label {}
.radio-input {}
fieldset {
text-align: center;
}
div {}
use this following css.
.radio-label {
margin-right: 3%;
}
fieldset {
text-align: center;
}
div {
display:inline;
border: 1px solid black;
}
add width for div then your problem may fixed
.radio-label {
float: left;
margin-right: 3%;
}
.radio-input {
float: left;
}
fieldset {
text-align: center;
}
div {
display: inline-block;
margin: auto;
border: 1px solid black;
width:50%
}

Floating floats inside float?

I know the title of the question sounds absolutely weird but I had no idea what else to call it.
First off I have a grid layout where I want my .search-wrapper to be 50% wide and floated right. In my demo on jsfiddle the entire .search-wrapper has a green background color. It's important that this element stays the way it is because it should fit into my grid.
Inside this .search-wrapper I have a searchbox and a button both floated side by side. This is just how I want it to be. So the #search-button should be floated left and the input should be aligned right to it.
However the thing I can't achieve is how to float both - the #search-button and the ´inputto the right inside the outer container.search-wrapper`.
The current status …
The way I'd like it to be … 
Here is a demo of my problem: http://jsfiddle.net/mQSBR/2/
Any ideas on that? Your help is very much appreciated. Thank you!
See this, if this is the effect you want: http://jsfiddle.net/mQSBR/9/ [EDIT]
div.search { width: 180px; float: right; } /* fix to 180px wide, float to right */
also add:
.search-wrapper {
min-width: 180px;
}
so the wrapper won't go past the .search div when resizing.
Here is the solution in the updated jsfiddle
<div class="wrapper search-wrapper">
<div class="search">
<form action="/" method="get">
<fieldset>
<input type="text" name="s" id="search-box" value="" class="" placeholder="Search this platform …">
<button type="submit" id="search-button">Search</button>
</fieldset>
</form>
</div>
​
.search-wrapper {
width: 50%;
float: right;
background:green;
margin-top:1em;
}
#search-box {
border: none;
background-color: transparent;
color: transparent;
height: 20px;
float: right;
}
#search-button {
border: none;
background-color: transparent;
color: transparent;
float: right;
background-color:red;
background-position: center -36px;
background-repeat: no-repeat;
}
​
Right align of search box/button
Also, my proposal is to use placeholder attribute of an element instead of overflowing label
You could do something like this
.search {
float: right;
width: 80%;
}
You can do this:
<fieldset style="margin-left: 20%">
<div class="input-inside-label">
http://jsfiddle.net/userdude/mQSBR/3/
Made a couple of edits to compensate for the overflow-x scroll bar:
#search-button {
...
margin-left: -20px;
...
}
.input-inside-label {
...
margin-left: 0;
width: 70%;
...
}
http://jsfiddle.net/userdude/mQSBR/7/

Resources