Two DIV aside with no break on small screen and flexible width - html

I want 2 divs aside and they have to stay aside. If the screen is too small I don't want the two divs under each other. The first DIV has a fixed width of 400px, the second DIV can be between 150px and infinite. The height is both fixed at 300px.
How can I do that? I already tried with float, but that causes the DIVs to break if the screen is too small.
Making a wrapper-div around with a large width would work, but looks ugly and buggy.

I'd do something like the below;
HTML
<div id="containerdiv">
<div id="fixeddiv"> </div>
<div id="elasticdiv">
<div id="div2000"> </div>
</div>
</div>
CSS
#containerdiv{
min-width:550px;
}
#fixeddiv{
height:300px;
width:400px;
background:red;
float:left;
}
#elasticdiv{
height:300px;
overflow:hidden;
min-width:150px;
background:blue;
}
#div2000{
width:2000px;
background:yellow;
float:left;
}
How Divs will appear

Create a wrapper div and give it display: table-row; min-width: 550px;, and then make the divs inside it have display: table-cell;. Something like this:
.container
{
display: table-row;
min-width: 550px;
}
.container > div
{
display: table-cell;
height: 300px
}
.container > div:first-child
{
width: 400px;
}
.container > div:last-child
{
min-width: 150px;
}
And then this for the HTML:
<div class="container>
<div>I have 400px width.</div>
<div>I have at least 150px width, and am next to the other guy.</div>
</div>
Here, have a Fiddle

Related

HTML CSS make divs side by side, out of screen width

My goal is to put div with width=100vw, after that div there should be second div with width for example 300px (so that second div should be out of screen). I tried many things with float, display inline and so on, now I don't have any more ideas.
<div id="div1"></div>
<div id="div2"></div>
Here is fiddle with example code
https://jsfiddle.net/kg5ea4sc/5/
You can use white-space: nowrap on parent element and display: inline-block on two inner elements. Also maybe you want to add vertical-align: top so it will look like this Fiddle
.element {
white-space: nowrap;
}
#div1{
background: green;
display: inline-block;
width:100vw;
height: 80px;
}
#div2{
background: red;
display: inline-block;
width:300px;
height: 100px;
}
<div class="element">
<div id="div1"></div>
<div id="div2"></div>
</div>
https://jsfiddle.net/guanzo/kg5ea4sc/18/
The second div is outside of the screen. You'll have to manipulate either it's position or the overflow:hidden property on the container if you want to see it though.
HTML
<div id="container">
<div id="div1"></div>
<div id="div2"></div>
</div>
CSS
#div1{
background: green;
width:100vw;
height: 80px;
}
#div2{
background: red;
width:300px;
height: 100px;
}
div{
display:inline-block;
}
#container{
width:100vw;
white-space:nowrap;
overflow:hidden;
}
Here is my fork of your fiddle: https://jsfiddle.net/nyzvbvo7/1/
You can scoll to the right to see the second div
What I changed:
I added
body {
width: calc(100vw + 300px);
margin: 0;
}
#div1, #div2 {
display: inline-block;
vertical-align: top;
}
So I made the body wide enough to hold both containers and set the container's display to inline-block. vertical-align: top; can be left out, the the containers will be algned at their baseline (which can vary depending on the content)

dynamic and fixed width with css

I have two elements aligned horizontal.
I want the right one to have a dynamic width and the left one to take up as much space as is left. How Do I do that?
Se JSFiddle
or code
<div class="wrapper">
<div style="background:red;" class="one">hello</div>
<div style="background:blue" class="two">dude</div>
</div>
.wrapper > div {
border: 1px yellow solid;
display: table-cell;
height:80px;
}
.one {
width: 100%;
}
.two {
width: 100px;
}
.wrapper {
width:100%;
height:200px;
border:2px solid blue;
}
.right {
height:200px;
width:60%;
background:red;
float:right;
}
.left {
width:auto;
height:200px;
background:green;
}
}
<div class="wrapper">
<div class="right">hello</div>
<div class="left">dude</div>
</div>
You can align two element like div horizontal to each other having right element can be dynamic and left element set his width automatically. To take width automatically you can use width:auto; property for first div. And second div having some width in percent or pixel so first div can take remaining width and set it right using float right property. I have created it with example.
If you change width of right element then width of left element will take remaining width automatically.
you can also take reference
Help with div - make div fit the remaining width
try this..
<div class="wrapper">
<div style="background:red;" class="one">hello</div>
<div style="background:blue" class="two">dude</div>
</div>
.wrapper > div {
border: 1px yellow solid;
display: table-cell;
height:80px;
}
.one {
width: 100%;
}
.two {
width: auto;
}

Fluid Fixed Content

What's missing to make this fluid-fixed layout work?
HTML:
<div class="header">
<div class="title">Title</div>
<div class="options">Opt</div>
</div>
CSS:
.header{margin:0;padding:0;}
.title{margin-right:50px;}
.options{float:right;width:50px;position:relative;top:0;left:auto;}
DEMO: http://jsfiddle.net/7Sdq6/
The link below works but I can't figure out what is missing for the above example to work.
http://jsfiddle.net/andresilich/6vPqA/13/show/
EDIT: I can position .options absolutely but I have a dropdown within that and I do not want the dropdown's position to be positioned relatively to .options
Demo
just add
display:inline-block;
Instead of margin-right: 50px use width: calc(100% - 50px)
Demo
css
.header {
width:400px;
}
.title {
width: calc(100% - 50px); /* takes the width of the parent and we substract the width of the right floated div from it instead of using margin-right */
background: red;
display: inline-block;
}
.options {
float:right;
width:50px;
background: blue;
}
As I understand you need to achieve a fixed title with fluid options, so you need to use this CSS
CSS
.title {
margin:0 50px 0 0;
float:left;
width: 400px /*your width*/
}
.options {overflow:hidden}
I figured it out..
The fixed content needs to be before the fluid content in the HTML:
<div class="header">
<div class="options">Opt</div>
<div class="title">Title</div>
</div>
http://jsfiddle.net/7Sdq6/5/

floats and wrong parent width

I have a parent div with few child divs inside. Both have float left. Problem is when a child divs breaks to next line, parent div get a wrong width.
Here is my HTML
<div class='parent'>
<div class='child'></div>
<div class='child'></div>
<div class='child'></div>
</div>
And CSS:
.parent{
float:left;
border:1px solid black;
}
.child{
margin:1px;
float:left;
width:300px;
height:50px;
border:1px solid black;
background:#65AEF1;
}
Here is fiddle:
http://jsfiddle.net/FDGqR/1/
As you can see, parent div got extra width. How can I force parent div to take exactly the width that childs really take?
This can be achieved using media queries..
Just add the following code to your css:
#media (max-width: 840px) {
.parent {
width: 548px;
}
}
#media (max-width: 550px) {
.parent {
width: 275px;
}
}
You may need to set the above fixed widths for better design.
Even I am new to this.. so let me know if something goes wrong :)
Working Fiddle
You could also set percentages % for width
.child{
margin:1px;
width:33%;
height:50px;
border:1px solid black;
background:#65AEF1;
}
Check this jsFiddle, if this is what you wanted.
You could do something like that:
<div class='parent'>
<div class='child'></div>
<div class='child'></div>
<div style="clear:both;"></div>
<div class='child'></div>
</div>
With the use of clear the parent div will take only the width of the two child divs
Couldn't you also just set the .parent width to 610px (double the child width plus a little room for the margin) and then you would have two columns of .child divs?
.parent{width:610px;}

Prevent float left div from going to a new line

I have 4 divs that are set to float left but the end div keeps wrapping two a new line on a smaller screen which is really annoying me...i want them to scale with the screen size so they always stay on the same line regardless of screen size... and im trying not to use a table (which is very tempting giving they v.reliable for this!!!)
I'm wondering how to fix this annoying issue so they always stay in position regardless of screen size??
I have this as my CSS:
.wrapper{
margin:0 auto;
width: 80%;
display: table-cell;
}
.gridf{
float:left;
margin-right:3px;
width:200px;
min-height:200px;
border:1px solid white;
}
.grid{
float:left;
margin-left: 3px;
margin-right:3px;
width:200px;
min-height:200px;
border:1px solid white;
}
.gridl{
float:left;
margin-left: 3px;
width:200px;
min-height:200px;
border:1px solid white;
}
My HTML:
<div style="overflow: hidden;">
<div class="wrapper">
<div class="gridf"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="gridl"></div>
</div>
</div>
Please help :D
Your wrapper is a percentage width container with 4 fixed-width child elements floated.
The width of the wrapper is dependent on the width of the viewport. If the viewport is narrowed to the point that the wrapper's width is less than that of the 4 child element widths together, then naturally they won't all fit and therefore will wrap.
The fix is to make sure your wrapper doesn't get smaller than the combination of the children.
So, add up with widths, borders and margins of the child elements and then give the wrapper a min-width attribute equal to that.
Hi i think you should this check to this demo
.wrapper {
margin: 0 auto;
width: 80%;
border: solid 1px red;
overflow: hidden;
}
.gridf,
.grid,
.gridl {
Background: green;
width: 24%;
min-height: 100px;
float: left;
margin: 2px 0;
}
.gridf {} .grid {
margin: 2px 1%;
}
.gridl {
background: yellow;
}
<div class="wrapper">
<div class="gridf">One</div>
<div class="grid">Two</div>
<div class="grid">Three</div>
<div class="gridl">Four</div>
</div>
Although this is an old post, I think that the problem, which I also run into, is the fact that you want all these cells to be of a fixed size, and not %, right? The solution you chose changed initial format where you specified width:200px;
Well, I would suggest to look here: http://jsfiddle.net/gn2bg/
The ONLY one thing I did is to add inner wrapper around your cells:
.inwrapper{
float:left;
overflow: hidden;
min-width: 830px;
}
and new html as this:
<div class="wrapper">
<div class="inwrapper">
<div class="gridf"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="gridl"></div>
</div>
</div>
Notice that your wrapper requires 80% of space.
The inwrapper, however, tells that its size is fixed - 830px (total of all internal div sizes plus room for padding.)
This way inwrapper uses 'elbows' to stretch the width, and override these 80% of 'wrapper'
I understand that you already made decision as to what is your best solution. I am leaving this response to anyone else in the future who needs exact answer to your exact question.
You can try removing the table-cell display rule from the wrapper and setting percentages (or min-widths) on the child divs like this jsFiddle example.
That should do the trick :
<div class="wrapper">
<div style="width:850px">
<div class="gridf"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="gridl"></div>
</div>
</div>
And that will be supported on any browser.
http://jsfiddle.net/5GrKU/3/
HTML
<div style="overflow: hidden;">
<div class="wrapper">
<div class="gridf"></div>
<div class="grid"></div>
<div class="grid"></div>
<div class="gridl"></div>
</div>
</div>
CSS
.wrapper{
margin:0 auto;
width: 80%;
display: inline;
}
.gridf{
float:left;
margin-right:3px;
width:20%;
min-height:200px;
border:1px solid red;
}
.grid{
float:left;
margin-left: 3px;
margin-right:3px;
width:20%;
min-height:200px;
border:1px solid red;
}
.gridl{
float:left;
margin-left: 3px;
width:20%;
min-height:200px;
border:1px solid red;
}
for you reference i have also added the URL of the demo. http://jsfiddle.net/sg8FE/
UPDATE
just change display:inline in wrapper class to display:block rest all is right and the div's are centered.
by giving a fixed width in your inner divs you are forcing them to have that width no matter what is the size of the view port. And giving the outer div a width of 80% you are shrinking its size with the width of your view port. You need to do either giving fixed width to all those divs or giving a relative width to all.