I have these HTML elements:
<div id="div1">
<h2>Search what you want</h2>
<input id="textSearch" type="text" class="form-control input-lg" placeholder="Search" />
</div>
<div id="div2">
<select id="droplist" class="form-control">
<option>Option</option>
</select>
</div>
I am trying to display them inline but I can't find the way to do it. I have searched on SO and applied some of the solutions, like involving both of the elements in a div (div1 and div2) and add in the CSS code something like this:
#div1, #div2
{
display: inline;
}
I have also tried with inline-block and so many other solutions but none of them have worked. Is there a possible way to do it?
You need to make the h2 in div1 inline too:
#div1, #div2, #div1 h2
{
display: inline;
}
You can try:
#div1, #div2 {
display: inline-block;
}
or:
#div1, #div2 {
width: 50%;
float: left;
}
As a side note, please, consider using class names for styling instead of id-s.
Please try this:
HTML:
<div id="disptable">
<div id="disptabcell">
<span>Search what you want:</span>
<input id="textSearch" type="text" class="form-control input-lg" placeholder="Search" />
</div>
<div id="disptabcell">
<select id="droplist" class="form-control">
<option>Option</option>
</select>
</div>
</div>
CSS:
#disptable{display:table}
#disptabcell{display:table-cell}
span{font-size:18px;font-weight:bold}
Related
In my project, I have a dialog div with four labels, four text input, one submit button and one shutdow button.
I want it appears like this:
AAAAAAAA:input text
BBBBBBBB:input text
CCCCCCCC:input text
DDDDDDDD:input text
submit shutdown
But acturally, it does not appear format, it likes this:
AAAAAAAA:input text
BBBBBBBB:
input text
CCCCCCCC:input text
DDDDDDDD:
submit input text
shutdown
It is ugly, I don't want it like that.
Here is my css and html code:
<style>
.addDiv{width:25%;height:20%;position:absolute;top:35%;left:35%;border:2px solid #ffffff;color:white;display:none;border-radius:15px;background:rgba(0,0,0,0.7);}
.firDiv{height:80%;width:100%}
.firDiv label{float:left;margin-left:10%;margin-top:2%;width:20%}
.firDiv input{float:right;border:1px solid #99ccff;background:rgba(0,0,0,0.35);margin-right:10%;
margin-top:2%;color:white;width:45%}
.secDiv{height:20%;text-align:center;width:100%;margin-top:5%}
</style>
<div id="addCnt" class="addDiv">
<form action="mng.php?act=add&table=IDC" method="POST">
<div class="firDiv">
<label>AAAAAAA:</label><input type="text" name="prdSty"><br />
<label>BBBBBBB:</label><input type="text" name="diffLvl"><br />
<label>CCCCCCC:</label><input type="text" name="repTm"><br />
<label>DDDDDDD:</label><input type="text" name="fixTm"><br />
</div>
<div class="secDiv">
<input type="submit" value="add" /><input id="sdnBt" type="button" value="shutdown" >
</div>
</form>
</div>
Who can help me?
may be something like this?
* {
box-sizing: border-box;
}
.firDiv label {
padding: 5px;
text-align: right;
float: left;
width: 17%;
}
.firDiv .control {
float: left;
width: 83%;
}
.form-group {
clear: both
}
.secDiv {
margin-left: 16%;
padding-top:10px;
}
<form action="mng.php?act=add&table=IDC" method="POST">
<div class="firDiv">
<div class='form-group'>
<label>AAAAAAA:</label>
<div class='control'><input type="text" name="prdSty"></div>
</div>
<div class='form-group'>
<label>BBBBBBB:</label>
<div class='control'><input type="text" name="prdSty"></div>
</div>
<div class='form-group'>
<label>CCCCCCCC:</label>
<div class='control'><input type="text" name="prdSty"></div>
</div>
<div class='form-group'>
<label>DDDDDDDDD:</label>
<div class='control'><input type="text" name="prdSty"></div>
</div>
</div>
<div class="secDiv">
<input type="submit" value="add" />
<input id="sdnBt" type="button" value="shutdown">
</div>
</form>
Two things you need to do here. First you have to clear the floats after the first Div completed. Second is apply float left to your input fields.
.firDiv input{float:left;border:1px solid #99ccff;background:rgba(0,0,0,0.35);margin-right:10%;
margin-top:2%;color:white;width:45%;}
.secDiv{height:20%;text-align:center;width:100%;margin-top:10%; clear:both;}
I have applied clear:both in secDiv and float:left in firDiv.
DEMO
add this to your css fiddle
.secDiv input {
width: 50%;
}
Rather try adding each input and label to their own div, something like this:
https://jsfiddle.net/ba0cL61b/5/
What this does is adds a div .row inside your firDiv div and inside the .row adds a div for your label and one for your input. So it would look like this:
<div class="row">
<div class="label">
<label>AAAAAAA:</label>
</div>
<div class="input">
<input type="text" name="prdSty">
</div>
</div>
This is a bit easier to understand and style.
in html/css i have simply two form and i want to set that to single row as inline widthout using float. you can see this ONLINE DEMO
in this below code i want to set display:inline to have single row as two div. like with this screen shot:
HTML:
<div class='single-page'>
<div id="verification-panel">
<form>
<fieldset>
<legend>verification code</legend>
<label>Mobile Number:</label>
<input type="text" class="inputs" placeholder="EX:">
<span class="help-block"></span>
<button type="button" class="btn">Submit</button>
</fieldset>
</form>
</div>
<div id="activation-panel">
<form>
<fieldset>
<legend>Active Acount</legend>
<label>Verfication code:</label>
<input type="text" class="inputs" placeholder="Verficcation code">
<span class="help-block"></span>
<button type="button" class="btn">Submit</button>
</fieldset>
</form>
</div>
</div>
use display:inline-block;
#verification-panel,#activation-panel{
display:inline-block;
}
Copy this code to your CSS section:
.single-page {
display: inline-block;
width: 769px;
}
.verification-panel,
.activation-panel { display: inline-block; }
To get the forms inline, apart from declaring the display: inline-block to both classes, you have to define a width for the parent container big enough to let them sit besides each other.
Here is a demo
.single-page{
display: inline;
width: 100%;
}
#verification-panel,#activation-panel{
display: inline-block;
width: 49%; // because of margin or etc.
}
#verification-panel,#activation-panel{
display:inline-block; width:250px;
}
I do not want to use hardcoded widths and table tags to generate the layout I want to achieve, but my CSS skills aren't developed quite yet. My layout is as follows:
<div id="container">
<div id="DataType">
Type of Data Request <input type="text" />
</div>
<div id="Arguments" >
Arguments <textarea name="arguments" rows="4" cols="45" ></textarea>
</div>
<div id="TargetEnvironment" >
Target Environment <input type="text" />
</div>
<div id="SubmitButton">
<button id="btnSubmit">Submit Request</button>
</div>
</div>
and I would like to have it behave a certain way with CSS. I can achieve it with tables and hard coded margins, but not with proper CSS. Here is what it currently looks like, and what I want it to look like
Here's a solution using float.
#container {
width: 500px;
}
label {
margin-top: 1em;
display: block;
vertical-align: top;
clear: right;
overflow: auto;
}
input, textarea {
width: 300px;
float: right;
padding: 2px;
}
button {
float: right;
}
<div id="container">
<label id="DataType">
Type of Data Request <input type="text" />
</label>
<label id="Arguments" >
Arguments <textarea name="arguments" rows="4" cols="45" ></textarea>
</label>
<label id="TargetEnvironment" >
Target Environment <input type="text" />
</label>
<label id="SubmitButton">
<button id="btnSubmit">Submit Request</button>
</label>
</div>
You can set the width and use float:right on your inputs.
I don't see any problem in using margins or paddings to separate the lines.
Another tip: use the label tag for the labels of your form.
This code
HTML
<input type="text" name="category" id="category" value=""> <input type="text" name="category_1" id="category_1" value="" tabindex="1"></div>
Though with repeated efforts starting from adding tabindex, nbsp to css cannot have these two inputs appear on single line.
CSS
<style type="text/css">
input.category {
vertical-align:top;
}
</style>
UPDATE
I think there is a plugin css which is over-ridding this behaviour. I have applied all what you guys said nothing works here is the css. I'm using plugin mcdropdown. Here is the code at start is just the copy of style followed with is is the css copy paste of mcdropdown.css file.
Please let me know how this can be done.
add class="category" for input fields and css:
.category {
float: left;
display: block;
}
#category_1 {
margin-left: 20px; /* or space you want..*/
}
and remove those spaces ( ) not really good way to code :)
Benefit of changing element display to block is that you can set vertical margins and paddings to it when needed.
Example usage with labels could be:
html:
<div class="col1">
<label for="field1">Field1 title</label>
<input type="text" name="field1" id="field1" />
</div>
<div class="col2">
<label for="field2">Field2 title</label>
<input type="text" name="field2" id="field2" />
</div>
<div class="clearfix"></div>
CSS:
.col1 {
float: left;
width: 200px;
}
.col2 {
float: left;
width: 200px;
}
.clearfix:after {
content: " ";
visibility: hidden;
display: block;
height: 0;
clear: both;
}
To get them to display on a single line use the css display attribute to change their display to inline here is how I do it:
<html>
<head>
<style>
#category, #category_1{
display: inline;
}
</style>
</head>
<body>
<input type="text" name="category" id="category" value="">
<input type="text" name="category_1" id="category_1" value="" tabindex="1">
</body>
That should solve your problem and it's really simple to! Have a great day!
They are both inline elements and should appear on the same line by default. Close your input tags appropriately (<input... />) and remove the closing </div> tag:
change
<input type="text" name="category" id="category" value=""> <input type="text" name="category_1" id="category_1" value="" tabindex="1"></div>
to
<input type="text" name="category" id="category" value="" /> <input type="text" name="category_1" id="category_1" value="" tabindex="1" />
You can do that by removing float, and add display:inline-block.
Here: http://jsfiddle.net/xW3tt/2/
Can you try this, By default your elements aligned and dispalyed in single line. If you want to apply any css styling or css properties then you may use as like in below. Added class in input elements class="category"
CSS:
<style type="text/css">
input.category {
float:left;
width:100px;
}
</style>
HTML:
<div style='width:500px;'>
<input type="text" name="category" id="category" value="" class="category">
<input type="text" name="category_1" id="category_1" value="" class="category" tabindex="1">
</div>
This answer is a wild guess operation, Try
Try applying below CSS to over-ride:
input.category {
float:left !important;
margin: 0 !important;
padding:0 !important;
clear:none !important;
}
and apply .category class to both your input (*SEE FIDDLE)
FIDDLE DEMO
Let's say I have an html snippet like this:
<div style="width:300px;">
<label for="MyInput">label text</label>
<input type="text" id="MyInput" />
</div>
This isn't my exact code, but the important thing is there's a label and a text input on the same line in a fixed-width container. How can I style the input to fill the remaining width of the container without wrapping and without knowing the size of the label?
Here is a simple and clean solution without using JavaScript or table layout hacks. It is similar to this answer: Input text auto width filling 100% with other elements floating
It is important to wrap the input field with a span which is display:block. Next thing is that the button has to come first and the the input field second.
Then you can float the button to the right and the input field fills the remaining space.
form {
width: 500px;
overflow: hidden;
background-color: yellow;
}
input {
width: 100%;
}
span {
display: block;
overflow: hidden;
padding-right:10px;
}
button {
float: right;
}
<form method="post">
<button>Search</button>
<span><input type="text" title="Search" /></span>
</form>
A simple fiddle: http://jsfiddle.net/v7YTT/90/
Update 1: If your website is targeted towards modern browsers only, I suggest using flexible boxes. Here you can see the current support.
Update 2: This even works with multiple buttons or other elements that share the full with with the input field. Here is an example.
as much as everyone hates tables for layout, they do help with stuff like this, either using explicit table tags or using display:table-cell
<div style="width:300px; display:table">
<label for="MyInput" style="display:table-cell; width:1px">label text</label>
<input type="text" id="MyInput" style="display:table-cell; width:100%" />
</div>
I suggest using Flexbox:
Be sure to add the proper vendor prefixes though!
form {
width: 400px;
border: 1px solid black;
display: flex;
}
input {
flex: 2;
}
input, label {
margin: 5px;
}
<form method="post">
<label for="myInput">Sample label</label>
<input type="text" id="myInput" placeholder="Sample Input"/>
</form>
Please use flexbox for this. You have a container that is going to flex its children into a row. The first child takes its space as needed. The second one flexes to take all the remaining space:
<div style="display:flex;flex-direction:row">
<label for="MyInput">label text</label>
<input type="text" id="MyInput" style="flex:1" />
</div>
Easiest way to achieve this would be :
CSS :
label{ float: left; }
span
{
display: block;
overflow: hidden;
padding-right: 5px;
padding-left: 10px;
}
span > input{ width: 100%; }
HTML :
<fieldset>
<label>label</label><span><input type="text" /></span>
<label>longer label</label><span><input type="text" /></span>
</fieldset>
Looks like : http://jsfiddle.net/JwfRX/
Very easy trick is using a CSS calc formula. All modern browsers, IE9, wide range of mobile browsers should support this.
<div style='white-space:nowrap'>
<span style='display:inline-block;width:80px;font-weight:bold'>
<label for='field1'>Field1</label>
</span>
<input id='field1' name='field1' type='text' value='Some text' size='30' style='width:calc(100% - 80px)' />
</div>
you can try this :
div#panel {
border:solid;
width:500px;
height:300px;
}
div#content {
height:90%;
background-color:#1ea8d1; /*light blue*/
}
div#panel input {
width:100%;
height:10%;
/*make input doesnt overflow inside div*/
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
/*make input doesnt overflow inside div*/
}
<div id="panel">
<div id="content"></div>
<input type="text" placeholder="write here..."/>
</div>
The answers given here are a bit outdated. So, here I'm with the easiest solution using modern flexbox.
.input-container{
display:flex;
}
input{
flex-grow: 1;
margin-left: 5px;
}
<div style="width:300px;">
<div class="input-container">
<label for="MyInput">label text: </label>
<input type="text" id="MyInput"/>
</div>
<div class="input-container">
<label for="MyInput2">Long label text: </label>
<input type="text" id="MyInput2" />
</div>
</div>
If you're using Bootstrap 4:
<form class="d-flex">
<label for="myInput" class="align-items-center">Sample label</label>
<input type="text" id="myInput" placeholder="Sample Input" class="flex-grow-1"/>
</form>
Better yet, use what's built into Bootstrap:
<form>
<div class="input-group">
<div class="input-group-prepend">
<label for="myInput" class="input-group-text">Default</label>
</div>
<input type="text" class="form-control" id="myInput">
</div>
</form>
https://jsfiddle.net/nap1ykbr/