Want HTML element to take up as much width as possible - html

So I'm trying to create an input prompt similar to the python interpreter
It's supposed to be a single line with 3 parts:
the prompt '>>' which is pushed all the way to the left
the input text area, just a place to type input into
the submission button which is pushed all the way to the right
My problem is that I want the 2nd element to automatically use all of the remaining width that the other two elements are not using.
Here is the closest I've gotten, it's almost right, except ideally the input text area would extend all the way to the button. I'd also like to be able to do it without hard-coding widths
#top-container {
width: 600px;
}
#input-prompt {
/* nothing */
}
#input-area {
display: inline;
background-color: #DDDDDD;
}
#input-button{
float: right;
}
<div id="top-container">
<!-- Part 1, the prompt '>>' -->
<label id="input-prompt">
>>
</label>
<!-- Part 2, the text input area -->
<div id="input-area" contenteditable>
(this should be wider)
</div>
<!-- Part 3, the submission button -->
<button id="input-button">Submit</button>
</div>

Here is an example code that does the trick:
#top-container {
width: 600px;
}
#input-prompt {
float: left;
}
#input-area {
overflow: hidden;
background-color: #DDDDDD;
}
#input-button{
float: right;
}
<div id="top-container">
<!-- Part 1, the prompt '>>' -->
<label id="input-prompt">
>>
</label>
<!-- Part 3, the submission button -->
<button id="input-button">Submit</button>
<!-- Part 2, the text input area -->
<div id="input-area" contenteditable>
(is it OK now?)
</div>
</div>
Block with overflow:hidden establishes new block formatting context and thus can't overlap floats, so nearly all browsers place it next to floats and make it use all available space.
Alternatively, you can achieve the same layout with Flexboxes, but their browser support is still not ideal (especially IE9-).

You can use display:table for the container ,please see below
CSS:
#top-container {
width: 600px;
display:table;
}
#input-prompt {
background-color: red;
display: table-cell;
}
#input-area {
width:100%;
display: table-cell;
background-color: #DDDDDD;
}
#input-button{
background-color:yellow;
display: table-cell;
}
#top-container {
width: 600px;
display:table;
}
#input-prompt {
background-color: red;
display: table-cell;
}
#input-area {
width:100%;
display: table-cell;
background-color: #DDDDDD;
}
#input-button{
background-color:yellow;
display: table-cell;
}
<div id="top-container">
<!-- Part 1, the prompt '>>' -->
<label id="input-prompt">
>>
</label>
<!-- Part 2, the text input area -->
<div id="input-area" contenteditable>
(this should be wider)
</div>
<!-- Part 3, the submission button -->
<button id="input-button">Submit</button>
</div>

You could try this:
#top-container {
width: 600px;
}
#input-area {
display: inline-block;
background-color: #DDDDDD;
width:89%;
}
#input-area:before{
content:'>>'
}
#input-button{
display:inline-block
}

This reserves fixed areas for the prompt and button and makes the input box cover the remaining area:
#top-container {
position: relative;
width: 600px;
}
#input-prompt {
display: inline-block;
}
#input-area {
position: absolute; top: 0; left: 1.5em; right: 4.5em;
background-color: #DDDDDD;
}
#input-button{
position: absolute; top: 0; right: 0;
}

Related

Getting an element to vertically align with the rest of its line

In the example below I'm trying to add some inline input range elements that align with the line but also align with the right edge of the outer div. In the actual site the html is code that's been run through prettify so it's nothing but <span> elements inside a <pre> element so whitespace and in particular, linefeeds, is relevant.
In any case I insert these <input type="range"> elements wrapped by 2 divs and I'm trying to get them to align to the content in the same line but on the right of the outer container.
I haven't actually found a solution. Originally I had a normal font size and if I added some extranous content inside the .holders things would appear to align but in reality once I increased the font size it showed they were not actually aligning.
Is there some way to fix this?
I get that normally you might do something like this with table (separate column for the sliders) or a flexbox but that would require parsing all the code to find the linebreaks and then generating a bunch of new html.
.outer {
position: relative;
background: pink;
padding: 1em;
width: 200px;
}
.holder {
display: inline-block;
}
.holder>div {
position: absolute;
display: inline-block;
right: 0;
}
.holder input {
position: absolute;
display: inline-block;
right: 1em;
}
<h1>test</h1>
<div class="outer">
<div>
<span>foo: </span>
<div class="holder">
<div>
<input type="range">
</div>
</div>
</div>
<div>
<span>bar: </span>
<div class="holder">
<div>
<input type="range">
</div>
</div>
</div>
</div>
Note: the code above shows the issue but the real code is whitespace sensative pre and spans which are hard to read. Here it is.
body {
font-size: 35pt;
}
.outer {
position: relative;
background: pink;
padding: 0.2em;
width: 300px;
}
.holder {
display: inline-block;
}
.holder>div {
position: absolute;
display: inline-block;
right: 0;
}
.holder input {
position: absolute;
display: inline-block;
right: 1em;
}
<pre class="outer"><span>foo: </span><div class="holder"> <div><input type="range"></div></div></div>
<span>bar: </span><div class="holder"><div><input type="range"></div></div>
</pre>
As I mentioned above the <pre> and <span> are generated, after which I insert the range inputs.
Yet another wrikle is that on the actual site the <pre> section gets scrollbars if a line is too long (just like S.O. code area) but in that case I still want the input areas at the right of the visible area (if you haven't scrolled).
body {
font-size: 35pt;
}
.outer {
position: relative;
background: pink;
padding: 0.2em;
width: 300px;
overflow: auto;
}
.holder {
display: inline-block;
}
.holder>div {
position: absolute;
display: inline-block;
right: 0;
}
.holder input {
position: absolute;
display: inline-block;
right: 1em;
}
<pre class="outer"><span>longlongline: </span><div class="holder"> <div><input type="range"></div></div></div>
<span>bar: </span><div class="holder"><div><input type="range"></div></div>
</pre>
A screenshot of the final result will probably help.
Normally the sliders are inline (no CSS apart from display: inline-block) but if the window is not wide enough the sliders would get pushed off. So, instead I make them transparent and set them to use absolute positioning so they don't get pushed off. This way you can still both read the code and interact with the sliders.
They appear to align ATM but if you go to the page and make sure your window is less than 450px wide and set the prettyprint.pre font size to say 15pt then you'll see the alignment is just luck and that they aren't actully being "aligned" it's just certain sizes of various things happen to make them appear aligned.
I fully understand they might not actually be a solution except to parse the code and separate each line into its own container but if possible I'd like to use the correct CSS (assuming it exists) to make them align.
.outer {
background: pink;
padding: 1em;
width: 200px;
}
.holder {
display: inline-block;
vertical-align: middle;
}
.holder>div {
display: inline-block;
}
.holder input {
display: inline-block;
}
you can try to add "vertical-align:middle"
.outer {
position: relative;
background: pink;
padding: 1em;
width: 200px;
}
.outer>div{
position: relative;
display: inline-block;
}
span{
vertical-align:middle;
}
.holder {
vertical-align:middle;
display: inline-block;
}
.holder>div {
position: relative;
display: inline-block;
right: 0;
}
.holder input {
position: relative;
display: inline-block;
}
<h1>test</h1>
<div class="outer">
<div>
<span>
foo:
</span>
<div class="holder">
<div>
<input type="range" />
</div>
</div>
</div>
<div>
<span>
bar:
</span>
<div class="holder">
<div>
<input type="range" />
</div>
</div>
</div>
</div>

Reposition div left of another div rather than below

I am attempting to tile a webpage with div elements of various sizes. However, I am running into an issue with once x number of div elements have filled the width of the screen the following div is placed below the previous 'row', rather than being floated to fit into space between elements in the previous 'row'. The code below better demonstrates what I mean; I'd like the 'game' div to be floated to fit into the space above where it is currently positioned.
h1 {
color: white;
}
.center {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
.container {
display: inline-block;
}
.default {
margin: 1em;
float: left;
}
/* For hover text */
.hover_img {
width: 100%;
height: 100%;
position: relative;
float: left;
}
.hover_img h4 {
color: white;
}
.hover_img:hover img {
opacity: .2;
}
.hover_img:hover .center_text {
display: block;
}
.center_text {
position: absolute;
top: 50%;
left: 0;
display: none;
font-weight: bold;
text-align: center;
}
img {
margin: 0;
}
.rectangle-tile-horizontal {
height: 15em;
width: 35em;
}
.red {
background-color: rgba(255, 63, 63, 0.8);
}
#game, #game img {
width: 30em;
height: 30em;
}
#app, #app img {
width: 40em;
height: 35em;
}
<div class="container">
<div class="rectangle-tile-horizontal red center default">
<h1><b>Projects</b></h1>
</div>
<div class="rectangle-tile-horizontal hover_img default" id="app">
<img src="http://cohenwoodworking.com/wp-content/uploads/2016/09/image-placeholder-500x500.jpg">
<div class="center_text"><h4>Web App</h4></div>
</div>
<div class="hover_img default" id="game">
<img src="http://cohenwoodworking.com/wp-content/uploads/2016/09/image-placeholder-500x500.jpg">
<div class="center_text"><h4>Breakout</h4> </div>
</div>
I'm afraid what you want to do is actually re-order your divs to create a space-filling layout. To the best of my knowledge, using only CSS for this is difficult, if not outright impossible.
I suggest you take a look at this SO post, or perhaps even the Bulma framework is what you want.
If, however, you move away from re-ordering the containers automagically and instead look towards a solution that elastically adapts the width of each container to fill the available space while maintaining its "order" (first, second, third), I am sure CSS will be a viable solution. If you require assistance, please use the search or ask anew.
Create a style for your div class or id like
.className
{display:inline;}
and use it in your each div
Hope this will help you
An example of this
http://jsfiddle.net/JDERf/

Put two html elements horizontally by rule

I want put textbox with button side by side.
But with next rules:
1) TextBox must fill all free width space of screen (leftpanel div)
2) Button have fixed width and height and must always stick to the right edge of browser. (rightpanel div)
My CSS style:
<style type="text/css">
div.centerpanel {
display: inline-block;
width: 100%;
height: 100%;
}
.leftpanel {
background: red;
display: inline-block;
width: 90%;
float: left
}
.rightpanel {
background: blue;
display: inline-block;
width: 10%;
float: left
}
</style>
in full screen it works well. But if i make browser window shortly by dragging edge/corners button partially trimmed.
sample (near full screen):
sample (after dragging small screen):
what i want:
There are two ways to achieve your layout,
First flexbox
.flexwrap {
display: flex;
width: 100%:
}
.flexwrap input {
flex: 1 1;
margin-right:10px;
}
<div class=flexwrap>
<input>
<button>+</button>
</div>
Second table
.tablewrap {
display: table;
width: 100%;
}
.tablewrap .t-cell {
display: table-cell;
}
.tablewrap .t-cell input {
width: 100%;
}
.tablewrap .t-cell:first-child{
padding-right:20px;
}
.tablewrap .t-cell:last-child {
width:20px;
}
<div class=tablewrap>
<span class=t-cell>
<input>
</span>
<span class=t-cell>
<button>+</button>
</span>
</div>

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;

Why can't I select inside these input boxes?

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