Left input takes all available space - css

I know this questions has already been answered before and I've read the topics :
Make div take all space left after another div
Expand div to take remaining width
Expand div to max width when floatleft is set
The magic of overflow hidden (external)
However I can't manage to implement them in my case or they simply don't seem to work as I try to have a fix width on the right and a flexible width on the left (unlike the above examples).
Here is my problem (which is fairly simple) : I have a form with a search field (left) and a span element (right). The span element has a fixed width and height. I want the input to fit the remaining left space.
form :
<div id="container">
<form>
<input type="search" />
<span class="submit"></span>
</form>
</div>
style.css :
#container {
width: 300px;
}
[type="search"] {
/* Positionning
* ------------ */
display: block;
height: 40px;
padding: 0px 10px;
vertical-align: top;
}
.submit {
/* Positionning
* ------------ */
display: block;
height: 40px;
width: 50px;
/* Styling
* ------- */
background-color: #CF0526;
}
From what I've read, I thought that a width: 100%; overflow: hidden on the input and a float: right on the span who be enough, sadly not. Here is a jsfiddle of my problem, hopefully it may help you.
EDIT: I changed the title from "left div" to "left input" as it may matter, especially since this solution does not work while it looks accurate for divs positionning.

You can try with the property calc like this:
input[type="search"] {
width: calc(100% - 40px);
margin:0;
padding:0;
height:30px;
float:left;
}
.submit {
float:left;
width: 40px;
height: 30px;
background-color: red;
}
The demo http://jsfiddle.net/SL3FB/10/ ... Maybe a problem the compatibility
Another solution using box.sizing who has more compatibility: http://jsfiddle.net/SL3FB/18/

You can substract the width from the span to the width from the textfield which is 100%.
Here is the fiddle http://jsfiddle.net/SL3FB/15/.
Code is like this:
width:calc(100% - 50px);
float:left;
Make 'em both float:left; to have a better result!
Hope this works for you.

Use CSS Tables
1) Set display: table-cell for both the input and the span
2) Set a fixed width on the span and (the trick:) width:100% on the input
FIDDLE
#container {
width: 300px;
border: 1px solid black;
}
form
{
display:table;
width: 100%;
}
.submit {
display:table-cell;
width: 40px;
height: 30px;
background-color: red;
}
input
{
width: 100%;
display:table-cell;
}

Does this work for you?
http://jsfiddle.net/SL3FB/4/
I've given your search a width of 85% so it fits up agains the red box.
.search {
width:85%;
}

Related

How do i wrap a parent div to the width of a child div (dialog)?

Jsfiddle to demonstrate my issue.
I have a SPA (Single Page Application).
In the appliation several dialogs can popup on the screen.
Every popup has it own width and height.
The title and content of the dialogs are added by angularJs
The problem i have here is the size of the dialog.
Currently all popups are made and added seperatly. I want to change this into one popup with variable content. The problem that comes with this is that the popup must wrap the contents width.
Example (as shown in the Jsfiddle)
<div class="dialog">
<div class="titlebar"></div>
<div class="content">
The content that is added has css that tells it has a width of 400px
This means the dialog needs to wrap to this 400px
</div>
</div>
How do i solve this by only using CSS?
Some examples of the variation of popups (although the width of both look the same, this is not the case)
Use display:table for the dialog.
Here is your Updated Fiddle.
For young browser you may use :
1) display:flex; property (includes centering) DEMO
.backdrop {
position: fixed;
top:0;
}
.backdrop {
width: 100%;
height: 100%;
z-index: 100;
background-color: rgba(0, 0, 0, 0.2);
display: flex;
align-items: center;
justify-content: center;
}
.dialog {
margin:auto;
position:relative;
}
2) max-content as value for width and not set any width to inner
content . (exept some padding to keep room for the close button) :
DEMO
Info on W3C about those new keywords value, soon avalaible i hope.
CSS updated
.dialog {
width: max-content;
z-index: 101;
margin: auto;
/* basic way t o center */
top:50%;
left:50%;
margin:-80px -150px;
}
.titlebar {
height: 50px;
line-height: 50px;
background-color: #000000;
border-radius: 10px 10px 0px 0px;
}
.title{
color:#FFFFFF;
font-size: x-large;
padding:0 50px 0 10px;
}
.close_button {
position: absolute;
right: 0px;
top: 0px;
line-height:30px;
padding: 5px;
margin: 5px;
border-radius: 10px;
background-color: #ffd549;
color: #000000;
}
.content {
background-color: #FFFFFF;
}
.content-width {
background-color:#FFF000;
}
or as already said , use the display: table, inline-table
Using display: inline-block; text-align: center;
Works in ie >= 8.
Fiddle.
I don't understand the problem.
If you want to center the content-width div element, simply add margin: auto;.
If you want the container to fit the WIDTH of its content, you must change the display property from block to something else, like inline-block or table (as suggested by #jacelysh).
What is it exactly that you are trying to do?
A div without a set width will take up the width of the parent.
try this.
.content {
background-color: #FFFFFF;
min-width: 100%;
}
.content-width {
width: 100%;
background-color:#FFF000;
}
http://jsfiddle.net/VQA4k/6/
Checking again now. You can just remove the width from those two classes and it will work.
This is what you want I think.
http://jsfiddle.net/VQA4k/16/

How to minus the padding width from 100% width?

When I set the padding size for input field it automatically changed the size. It becomes bigger.
.container{
width: 150px;
}
.item label{
display: block;
width: 100%;
margin-bottom: 10px;
}
.from-item{
width: 100%;
}
input[type="text"]{
width: 100%;
padding: 5px;
}
JSFfiddle
You should think about putting this in your CSS:
* { box-sizing: border-box }
This alters the box model such that padding will not add to the size that an element occupies on the screen. It is, to my mind and the mind of many others, a much better model to work with:
http://www.paulirish.com/2012/box-sizing-border-box-ftw/
General info on the box model: http://css-tricks.com/the-css-box-model/
You can use box-sizing:border-box to solve your problem, but it is a css3 property. Thus incompatible with old browsers.
Another way to achive this is put a wrapper div around input & give padding to it.
<div class="ibox"><input type="text" class="from-item"></div>
.ibox{
padding: 5px;
}
input[type="text"]{
width: 100%;
}
Here is Demo link http://jsfiddle.net/aq8mP/1/

Fill remaining width between elements and cut text

I have a footer to posts on a blog, the posts are of dynamic with.
There are some elements in the footer that are left-aligned, one that's right-aligned and one between them that should fill the remaining space.
I thought I could use
text-overflow:ellipsis
which works if I set it to a fix width, but at the moment, the space-filling element just gets too large so the last element breaks to a new line.
Adding
white-space:nowrap;
to the outer container didn't help.
Also it'd be a nice bonus if the space-filling element would always fill the remaining space, even if it's content is not large enough.
Here is my fiddle http://jsfiddle.net/MFxk5/ , the space-filling element is the
<a href="c" class="c">...
Thanks to everyone for helping out! Maybe some will mark this as duplicate, but I think the combination with text-overflow:ellipsis makes this unique - I really searched for a solution.
Sounds like you want a fixed-fluid-fixed layout here is how you do it in pure css. If its not what you mean let me know. a Fiddle to view: http://jsfiddle.net/rE2NC/ just move the viewport left and right and you will see how the middle expands contracts as the width does.
HTML
<div class="FooterContainer">
<div class="Footer">
<div class="Left">Left</div>
<div class="Middle">Middle</div>
<div class="Right">Right</div>
</div>
</div>
CSS
.FooterContainer {
width:100%;
}
.Footer {
padding-left:200px; //or whatever width you want the .Left to be
padding-right:200px; //or whatever width you want the .Right to be
}
.Left {
width:200px; //Should match the padding-left of .Footer
margin-left:-200px; //Should be the negative of the width
float:left;
}
.Right {
width:200px; //Should match the padding-right of .Footer
margin-right:-200px; //Should be the negative of the width
float:left;
}
.Middle {
width:100%; //This fills the rest
float:left;
overflow:hidden; //use this to make sure text dont flow out
}
jQuery Solution
I started with a jQuery assisted solution.
The CSS looks like:
div {
width: 100%;
overflow: auto;
}
a {
border: 1px solid gray;
margin: 3px;
height: 50px;
float: left;
}
.c {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.d {
float: right;
}
and the jQuery function:
$("div").each(function(){
var innerWidth = 0;
$(this).find("a").not(".flex").each(function(){
innerWidth += $(this).outerWidth(true);
});
var flexWidth = $(this).width() - innerWidth - 9; /* padding + border + 1 */
$(this).find(".flex").width(flexWidth);
});
There is a hard coded constant that represents the left/right padding and border on the flexible with div (a.c in your example), and for some reason, there is a 1px adjustment to keep the floats on a single line. (Not quite sure the origin...).
Fiddle: http://jsfiddle.net/audetwebdesign/HmvsN/
Mix of Fixed Widths with Floats
I made a slight adjustment to the HTML as follows (move a.d in front of a.c):
<div class="ex2">
First column
Second column
Last column
Very long text...
</div>
and use the following CSS:
.ex2 a {
border: 1px solid gray;
margin: 3px;
height: 50px;
}
.ex2 .a {
width: 90px;
float: left;
}
.ex2 .b {
width: 90px;
float: left;
}
.ex2 .c {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
display: block;
margin: 3px 100px 3px 199px;
}
.ex2 .d {
width: 90px;
float: right;
}
Essentially, float the left two elements and the right one such that it wraps around the wider one. The width of the wider element has left/right margins to accommodate the floated elements.
Overall, both approaches have merit, but it seems to be a lot of work for what we are getting...

Floating divs don't expand body width

I'm trying to develop a horizontal web page, with fixed height and variable width.
In order to get it, I need a row of floating <div>s to expand the <body> width.
|------------- body --------------| /* variable width */
|-div-| |-div-| |-div-| |-div-| /* fixed width */
The following code doesn't seem to work:
body{
height: 40px;
}
div{
width: 2000px;
height: 20px;
background: red;
margin: 10px;
float: left;
}
http://jsfiddle.net/7cS2R/12/
Is is possible to do so without using javascript?
Block elements expand to the full width of their parent-element's width. To make them respect their childrens with you can either declare:
display: inline-block;
or
position:absolute;
on your body-element.
EDIT: after you clarified your question - simply add the white-space declaration to your body:
white-space:nowrap;
Demo
Try this:
body{
height: 40px;
display: inline-block;
}
http://jsfiddle.net/7cS2R/6/

Two columns and one footer divided by Border (Individual sides) using css

I want to create a robust css style that works whith almost all browser (included IE7, firefox 3)
that show me two columns and one footer divided by dotted border.
I was trying to implement the following code,
but I have one problem:
when I apply border-right-style:dotted; to left class
A and B are not at the same horizontal level.
please halp me to fix the css style.
Click here for the current example.
HTML
<div class="container">
<div clas="left">A</div>
<div class="right">B</div>
<div class="footer">C</div>
</div>
CSS
div.container {
background:#eee;
margin: 0 auto;
width: 750px;
}
.left{
background:#ddd;
float: left;
width: 50%;
border-right-style:dotted;
}
.right {
background:#eee;
float: right;
width: 50%;
}
.footer {
background: none repeat scroll 0 0 #eef;
clear: both;
border-top-style:dotted;
}
The problem that you're experiencing is that the border of the element is not contained within the defined width of that element; so the element is 50% of its parents width, but with an additional width added by the border.
If you reduce the width of the elements to, for example, 48%, then it seems to work as you'd like:
div.container {
background:#eee;
margin: 0 auto;
width: 750px;
}
.left{
background:#ddd;
float: left;
width: 48%;
border-right-style:dotted;
}
.right {
background:#eee;
float: right;
width: 48%;
}
JS Fiddle demo.
Edited with update,
You could, for Firefox and Chromium (FF5.x and Chromium 12.x on Ubuntu 11.04) use:
div {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-o-box-sizing: border-box; /* Left this in, but it doesn't seem to work... */
}
JS Fiddle demo.
Which incorporates the border width into the width of the element; with this approach you could retain the width: 50%; on the elements and borders would sort themselves out. Unfortunately it doesn't work on Opera or, presumably, IE.
Fixed
http://jsfiddle.net/euYTQ/19/
What you've got to remember is that a border counts + of the % assigned.
So say you have a box thats 100px's wide (100%), and you put one side with a 1px border (1%), thats actually 101%. So in your case, it was breaking to the next line of space, hence giving you your error.
In my fix i simply set the right container to 49%. Which would be great for fluid solutions, or if you have a fixed layout, set it to a fixed value.
Remember, padding is the same too... it will count + of the assigned size or percent.
Hope this helps!
The reason A and B are on different levels is because they don't fit into one width. You have them each declared with width: 50% but one of the also has a border. Border width is added to the width of the div - thus the two divs plus the border don't fit into horizontal spacing.
For example, try putting width: 49% on each of them - and you'll see the difference. This is not ideal, as you don't always know the width of the viewport. If you can work with exact pixel widths, it would be easier. Try this CSS for a change:
div.container {
background:#eee;
margin: 0 auto;
width: 750px;
}
.left{
background:#ddd;
float: left;
width: 374px;
border-right:dotted 2px black;
}
.right {
background:#eee;
float: right;
width: 374px;
}
.footer {
background: none repeat scroll 0 0 #eef;
clear: both;
border-top-style:dotted;
}
This is because 50% + 50% + 1px(the border) is higher than 100%.
If your .container isn't going to change width's you could give them both a fixed pixel value.
However if your .container is going to change width's you could try adding another element that contains the border alone like so:
.border {
height:100%;
width:0;
border-left:3px dotted #000;
position:absolute;
left:50%;
top:0;
}
Don't forget to give .container a position:relative;.
#Antojs; padding & border add width to the element if the element in percentage it's create problem. So; can give width to one like this:
div.container {
background:#eee;
margin: 0 auto;
width: 750px;
}
.left{
background:#ddd;
float: left;
width: 50%;
border-right-style:dotted;
}
.right {
background:#eee;
overflow:hidden;
}
Now in .right if you give border & padding it's not effect anything & you can also use css3 box-sizing: border-box; but it's not supported by IE7
check this fiddle http://jsfiddle.net/euYTQ/30/
The problem is that the border adds width to the div with .left. As the container div appears to be of fixed width, you could simply give the .left and .right elements fixed width values too (or reduce their percentage widths), and make .left slightly narrower:
.left{
background:#ddd;
float: left;
width: 372px;
border-right-style:dotted;
}
.right {
background:#eee;
float: right;
width: 375px;
}
Here's an updated fiddle. I would also suggest reading up on the box model to get an idea of how borders, padding etc. add on to width.
http://jsfiddle.net/euYTQ/18/
50% and 50% = 100% so no space for the border.
Put your div right in the div left
<div class="left">section left
<div class="right">section right</div>
</div>
and change a little the css
.left{
background:#ddd;
float: left;
width: 50%;
}
.right {
background:#eee;
float: right;
border-left-style:dotted;
}
Example : http://jsfiddle.net/euYTQ/28/

Resources