How to expand input to take remaining room in a div - html

I would like to create a dropdown that drops down a custom panel (div) instead of a list of options. That panel is irrelevant because it is not tied to the layout I am asking about. For the basic drop-down look I have the following:
<style>
.folder-selection {
width: 100%;
}
.dropdown-button {
float: right;
}
</style>
<div id=container>
<input type="text" class="folder-selection" />
<button type="button" class="dropdown-button">...</button>
</div>
Now I know the float and width 100% are not right, but I have a container div, with an input on the left and a button on the right. The button must remain fixed to the right of the input. If the container is narrow, the input must be narrow, and vice versa, but I want to achieve this without knowing at design time the width of the container.
The container should fit into any width and the input's width should adjust accordingly. Just like a normal select element, where the text portion always fills all the space not taken by the dropdown icon/button at its right.

Below example will help you. Let me know, if you don't want fixed width icon in right so I'll update this code accordingly.
#container {
position: relative;
border: 1px solid #ccc;
padding: 5px 40px 5px 5px;
margin: 0 0 10px;
}
.folder-selection {
width: 100%;
padding: 5px;
border: none;
box-sizing: border-box;
height: 30px;
}
.dropdown-button {
position: absolute;
top: 5px;
right: 5px;
height: 30px;
}
<div id=container>
<input type="text" class="folder-selection" />
<button type="button" class="dropdown-button">...</button>
</div>

<style>
.dropdown-button {
width: 16px; /*Set width of button*/
}
.folder-selection {
width: calc(100% - 16px); /*div's width minus button's width*/
}
</style>
<div id=container>
<input type="text" class="folder-selection" /><!-- this comment is to remove white space between the two elements
--><button type="button" class="dropdown-button">...</button>
</div>
You don't really have to set the width of the button as long as you know its width on runtime.

This code will help you to position your elements as you expected.
* {
box-sizing: border-box;
}
.dropdown-container {
width: 500px;
display: table;
background: orange;
}
.dropdown-container .input-container {
display: table-cell;
}
.dropdown-container .input-container input {
width: 100%;
padding-right: 10px;
}
.dropdown-container .button-container {
display: table-cell;
width: 150px;
}
.dropdown-container .button-container button {
width: 100%;
}
<div class="dropdown-container">
<div class="input-container">
<input type="text" class="folder-selection" />
</div>
<div class="button-container">
<button type="button" class="dropdown-button">Drop</button>
</div>
</div>

Related

Fill the width of div with button and input

I need to fill a div element with a button and input. The button'll have the exact width and fthe rest will be filled with the input. In my code I can't even set the width of the button and the input is always wider than the parent.
Then I'll set overlay: hidden to the .bar so that the input with the button look like I want. But for that to behave like I want, I need to set the mentioned widths correctly. So what could I do to fill this parent div with this button of exact width and input for the rest of its width but no more than that? I tried lots of properties, nothing helped. Only condition is, that I want display: flex remain in the code, everything else can be changed. Thanks for suggestions.
.container {
width:205px;
}
.bar {
border-radius: 25px;
box-sizing: border-box;
display: flex;
//overflow: hidden;
}
.bar_input {
font-size: 15px;
height: 20px;
box-sizing: border-box;
width: 100%;
}
.bar_button {
padding: 8px 8px 14px 1px;
width: 37px;
height: 35px;
left: 3px;
right: auto;
}
<div class="container u-l-fr" style="background-color: darkred">
<div class="container-bar" style="background-color: darkblue">
<div class="bar" style="background-color: gold">
<button aria-label="Search" class="bar_button" role="button" type="button" style="background-color: black;"></button>
<input class="bar_input" id="search_input" placeholder="Search">
</div>
</div>
</div>
Its because your container width is less than the default width that is assigned to a <input> element. So you can either increase width of your container or use this code.
.bar_input {
font-size: 15px;
height: 20px;
box-sizing: border-box;
width: calc(100% - 37px);
}
Using bootstrap you can add class to the button .btn-block
Otherwise you can add style width: 100%; to the button

Vertically aligning a div and an input

After trying to get it right for hours I've finally found a way to align two divs perfectly on one line, but now I'm trying to get a button and an input box aligned and they're just slightly off. Note: I'm using Bootstrap with this!
Here's my code:
html:
<input id="col1" type="text" class="form-control" placeholder="Enter activity" /><button id="col2" class="btn btn-outline-success my-2 my-sm-0" type = "button">Add</button>
css:
body {
margin: 10px;
}
#col1 {
display: inline-block;
width: 70%;
height: 100px;
}
#col2 {
display: inline-block;
width: 30%;
height: 100px;
}
button, input {
height: 100%;
width: 100%;
line-height: 0;
border: 0;
}
http://jsfiddle.net/x1hphsvb/335/
Any help is appreciated!
Just add vertical-align: baseline; to the button's CSS. (The default vertical alignment for buttons in bootstrap is "middle"):
http://jsfiddle.net/q3aarre2/1/
You have a few more properties to zero out:
button, input {
...
border: 0;
line-height: 0;
vertical-align: top; /* or 'middle'; for Bootstrap */
}
Demo
My dear friend, you just do it
input, button {
vertical-align:middle;
}

DIV with max-height not expanding

I've made a drop-down list which attaches to a text input, and the list which appears beneath has a header and footer row, and scrolling content in between. JS fiddle here:
https://jsfiddle.net/tpgjjh81/3/
It works great, except I'd like the drop-down to have a flexible height, depending on its content, up to a specified max-height. However, if I change:
DIV.dropdown {
...
height: 100px;
...
}
to:
DIV.dropdown {
...
max-height: 100px;
...
}
...then the "content" part of the list doesn't show at all when the drop-down appears, only the header and footer rows. The DIV's within have height: 100% so I would have thought these would push the outer DIV to its max-height but it doesn't appear to be working?
edit: I've also tried adding height: auto alongside the max-height but it doesn't have any effect.
See this fiddle here: https://jsfiddle.net/tpgjjh81/22/
The main issue was to do with the DIV.scroll_inner having absolute positioning with 0 edges (to fill the scroll_outer container).
Let the outer container control its own content, and limit the inner content's height to max-height: 100px.
Essentially, what you are looking for is something like this:
DIV.wrapper {
display: inline-block;
position: relative;
}
DIV.dropdown {
display: none;
position: absolute;
clear: left;
left: 0;
right: 0;
border: 1px solid black;
}
DIV.list_container {
display: table;
width: 100%;
height: 100%;
}
DIV.header,DIV.footer {
display: table-row;
background-color: lightgray;
}
DIV.scroll_outer {
display: table-row;
height: auto;
background-color: white;
}
DIV.scroll_inner {
overflow: auto;
max-height: 100px;
}
EDIT I've also removed some now unnecessary properties from the CSS, and updated this above, and in the fiddle. This should at least get you off the ground.
Best of luck in your project! :)
I cleaned up your css. Not sure why you were using display table and setting up the height etc. Just hide the parent overflow and set child overflow to scroll and that should do it.
Also I moved the border to outer container so it is not cut off.
is_visible = false;
function Toggle() {
is_visible = !is_visible;
document.getElementById("dropdown").style.display = (is_visible ? "block" : "none");
}
DIV.wrapper {
display: inline-block;
position: relative;
}
DIV.dropdown {
display: none;
position: absolute;
clear: left;
left: 0;
right: 0;
height: 100px;
border-bottom: 1px solid black;
overflow: scroll;
}
DIV.list_container {
display: block;
border: 1px solid black;
border-bottom: 0px;
}
DIV.header,DIV.footer {
display: block;
background-color: lightgray;
}
DIV.scroll_outer {
display: block;
position: relative;
background-color: white;
}
DIV.scroll_inner {
overflow: hidden;
}
<div class="wrapper">
<input type="text" size="50" value="Click me" onclick="Toggle()" />
<div class="dropdown" id="dropdown">
<div class="list_container">
<div class="header">Header</div>
<div class="scroll_outer">
<div class="scroll_inner">
Item 1<br />
Item 2<br />
Item 3<br />
Item 4<br />
Item 5<br />
Item 6<br />
Item 7
</div>
</div>
<div class="footer">Footer</div>
</div>
</div>
</div>
height: auto;
max-height: 100px;
overflow: auto;

Responsive Design: how to have text input and button stay 100% width of parent

Very simple question... I'm hacking it right now with floated percentages (but I know that there has to be a better solution) please see my photo as an example to go by. I want to have the parent stay 100% in width and the search box be an automatic width that always stays next to the search button, and I want the search button to be able to grow as wide as it wants to (depending on the text inside of it/padding).
UPDATE (The Flexbox Way!)
The proper way to achieve this now is with Flexbox!
CSS "Flexbox" Way (https://jsfiddle.net/1jxkyLdv/)
/* CSS
**************************************************************************/
/* Reset */
* { box-sizing: border-box; margin: 0; padding: 0; }
body { margin: 1rem; }
h2 { margin: 2rem 0 0; }
/* Flexbox Example */
.flexbox { display: flex; }
.flexbox .stretch { flex: 1; }
.flexbox .normal { flex: 0; margin: 0 0 0 1rem; }
.flexbox div input { padding: .5em 1em; width: 100%; }
.flexbox div button { padding: .5em 1em; white-space: nowrap; }
<!-- HTML ------------------------------------------------------------------->
<h1>Flexbox Way!</h1>
<h2>Short Word</h2>
<section class="flexbox">
<div class="stretch">
<input type="text" placeholder="Search..." />
</div>
<div class="normal">
<button>Search</button>
</div>
</section>
<h2>Long Word</h2>
<section class="flexbox">
<div class="stretch">
<input type="text" placeholder="Search..." />
</div>
<div class="normal">
<button>Super Long Word For Search Button With Flexbox!</button>
</div>
</section>
THE OLD WAY
I despise using tables or using css to make divs act like tables), But here's the other way.
CSS "Table-Cell" Way (http://jsfiddle.net/eUhTM/3/)
* { box-sizing: border-box; }
section { width: 100%; display: table; padding: 1em 0 0; }
div { display: table-cell; width: 100%; }
input { width: 100%; padding: .5em 1em; }
button { color: black; padding: .5em 1em; white-space: nowrap; margin: 0 0 0 1em; }
<h1>Short Word</h1>
<section>
<div>
<input type="text" placeholder="Search..." />
</div>
<div>
<button>Search</button>
</div>
</section>
SOLUTION
The main trick is to make the section a "display: table;" and the divs inside "display: table-cell;", you're input "width: 100%" and you're button "white-space: nowrap".
I'm still interested in solutions though!
Thank you everyone for your great answers.
Correct answer from MrRioku in the comments
http://jsfiddle.net/eUhTM/3/
My original answer
http://jsfiddle.net/eUhTM/
This will probably be downvoted to oblivion for obvious reasons but what about doing this:
<table style="width:100%;">
<tr>
<td style="width:100%;">
<input type="text" placeholder="Search" style="width:100%;">
</td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>
I used inline CSS for simplified viewing :)
This is indeed a bit tricky, especially if you do not know the width of the button in advance. You could off course go for a js solution, which should be fairly straightforward, but I prefer sticking to css as much as possible.
I did come up with a solution that works in your layout:
<div class='searchBox'>
<input type='text' placeholder='search...'/>
<button>Search</button>
</div>
.searchBox {
position: relative;
padding: 10px;
}
input {
box-sizing: border-box;
width: 100%;
border: 1px solid #999;
background: #fff;
padding: 10px;
}
button {
height: 40px;
background-color: #555;
padding: 0 10px;
border: none;
color: #fff;
position: absolute;
top: 10px;
right: 9px;
}
button:before {
content: '';
position: absolute;
display: block;
height: 40px;
top: 0px;
left: -10px;
width: 10px;
background: #fff;
border-left: 1px solid #999;
}
and a fiddle: http://jsfiddle.net/VhZS5/
Not the cleanest solution ever, but it should be cross (modern) browser (the border-box may require some prefixing), is semantically correct, and it doesn't use tables.
Note that I positioned the button absolute on top of the input field. Then I used a :before on the button to cover up the input box slightly and give the impression of some spacing between the input and the button.
Let me know if you want me to explain further.

Span, Input, Span 100% width CSS issue

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);
}