elements of variable sizes in the same space - html

I have two elements that have variable width, and must share the same space in a div
I created an example in this link:
http://jsfiddle.net/zWVVN/
#test-1: Should look like.
#test-2: Problem situation.

Hi you define hr float left that conditions you must define width of all hr those give to float as like this
Css
#teste1, #teste2 {
width: 300px; }
hr {
border: 1px solid green;
background: red;
height: 25px;
float: left;
width:200px;
}
h2 {
float: right;}
#teste1 hr{
width: 230px;}
HTML
<div id="teste1">
<hr>
<h2>conteudo</h2>
</div>
<div id="teste2">
<hr>
<h2>conteudo</h2>
</div>
​
Live demo http://jsfiddle.net/rohitazad/3e6bd/2/

If you want both divs to take up the whole space and do not want to wrap text down, then why have u set the width for both the divs to 300px. Remove the width assigned to both the divs and make both HR to float left.
#teste1, #teste2 {
//this class not needed
}
hr {
border: 0;
background: red;
height: 5px;
float: left;
}
h2 {
float: left;
}
Demo : http://jsfiddle.net/3e6bd/3/

Related

Float div to left, other div to right

I am trying to adjust float part of my menu to the left, and the other part to the right. However, I cannot get it to work.
I have been trying to accomplish it using floats, but I can't quite get it to work.
How can I do this in a proper way?
HTML
<div class="topbar">
<div class="title">Title</div>
<div class="topbar-boxes">
<div class="topbar-boxes-left">
<div class="topbar-box">Box1</div>
<div class="topbar-box">Box2</div>
<div class="topbar-box">Box3</div>
</div>
<div class="topbar-boxes-right">
<div class="topbar-box">Box1</div>
<div class="topbar-box">Box2</div>
<div class="topbar-box">Box3</div>
</div>
</div>
<div style="clear:both"></div>
</div>
CSS
.topbar {
width: 100%;
padding: 14px;
font-size: 18pt;
color: white;
background-color: rgba(42, 42, 42, 0.95);
}
.title {
float: left;
padding-right: 50px;
}
.topbar-boxes {
float: left;
margin: -14px;
}
.topbar-boxes-left {
float: left;
}
.topbar-boxes-right {
float: right;
}
.topbar-box {
float: left;
padding: 20px;
border-left: 1px solid black;
}
http://jsfiddle.net/8eqSN/
Your problem is because you're floating the .topbar-boxes div. When you float, the length of the element is made as thin as possible (rather than filling the width of the page as you'd expect normally), so your float is working, you just can't see it because the parent div is only as wide as the child elements.
To fix, remove this float:
.topbar-boxes {
/*float: left; <----- remove this.*/
margin: -14px;
}
That's because float elements depends on the width of the parent to be positioned since the parent of the boxes topbar-boxes is floated too it width isn't 100%.
Based on your structure and since the tittle is floated too I suggest you remove that container and all works fine taking the margin on the others:
.topbar-boxes-left {
margin: -14px;
float: left;
}
.topbar-boxes-right {
margin: -14px;
float: right;
}
Check this Demo Fiddle
The container div (.topbar-boxes) has a width:auto, by default in every element. That means that its width is going to be as much as its content.
Try to set width:100% in the .topbar-boxes.

Fluid layout with optional sideboxes

I want a layout with three boxes (two optional) like this:
[side box 1] [ main content
[side box 2] . main content ]
or
[ main content spans 100% if side boxes aren't provided ]
I want the main content box to span the entire height and width available in #load (minus margins) except if the side boxes are there, then I want it to only span up until those boxes (and their right margin).
My CSS:
#load {
margin: 10px;
height: 100%;
min-width: 1080px;
}
#primary,#secondaryOne,#secondaryTwo {
border-radius: 8px;
background: #f5f5f5;
border: 1px solid #ccc;
}
#primary {
float: right;
height: inherit;
width: 75%;
height:500px;
background:red;
}
#secondaryOne,#secondaryTwo {
min-width: 250px;
max-width: 300px;
height: 220px;
margin-right: 10px;
width: 20%;
clear:left;
float:left;
}
#secondaryTwo {
margin-top: 10px;
}
Simple HTML
<div id='load'>
<div id='primary'></div>
<div id='secondaryOne'></div>
<div id='secondaryTwo'></div>
</div>
JSFiddle
Problems
*SOLVED*Making #primary span the entire width if the sideboxes are missing.
*SOLVED*Is there a way to line the two sideboxes (#secondaryOne,#secondaryTwo) on the left side of #primary without nesting them in a separate div? If I use float: left on them, they line side by side, if I don't float them, the #primary generates below them, not beside them.
Solutions
Problem #1 was solved by joeytje50 using the secondary + primary tags and placing the secondary side boxes before the primary in HTML.
Problem #2 was solved in more than one way. The way I chose so that the secondary tags were placed together and before the primary was by NoobEditor using clear: left and a negative margin-top.
The solution can be found at: http://jsfiddle.net/v4cvv/67/
The main part of the solution is:
#primary {
width: 100%;
}
#secondaryOne + #primary, #secondaryTwo + #primary {
margin-top: -221px;
width: 75%;
}
Alternate Solution
One problem I found with the above solution, is it requires the two boxes and them to be the same height. A solution around this is by grouping the boxes in their own div. This solution is:
HTML
<div id='load'>
<div id="sideboxes">
<div id="boxOne" class="box"></div>
<div id="boxTwo" class="box"></div>
<div id="boxThree" class="box"></div>
</div>
<div id="primary" class="box"></div>
</div>
CSS
.box {
border-radius: 8px;
background: #f5f5f5;
border: 1px solid #fff;
}
#primary {
float: right;
display:block;
height: 97%;
width: 100%;
}
#sideboxes + #primary {
width: 75%;
}
#sideboxes {
float: left;
height: 97%;
width: 23%;
margin-right: 10px;
}
#sideboxes .box {
float: left;
height: 220px;
margin-bottom: 10px;
width: 100%;
}
The alternate solution no longer requires clear and can be extended for other uses. You may now also have 1, 2, 3, or however many boxes you want in the sideboxes div.
Thanks all for their help.
To answer your question about the primary box being 100% width when the secondary boxes are not there, you could do the following:
#primary {width:100%;}
.secondary + #primary {width:75%;}
If you put that CSS code at the bottom of your stylesheet, and then put the primary div tag after your first secondary div tag, then it'll by default be 100% wide, unless there's an element that has class="secondary". This won't change anything about the position the div is rendered, but it will fix your problem.
Alternatively, if your secondary divs are possibly hidden instead of not being there, you could do this:
#primary, .secondary.hidden + #primary {width:100%;}
.secondary + #primary {width:75%;}
That is, assuming you hide the secondary tabs via a class such as .hidden.
Here is a working version that becomes 100% width when the secondaries are removed, but still is 75% width when there is a .secondary element before it.
Keeping your HTML markup smae, here is the solution for your problem : demo
CSS
html.body {
height:100%;
}
#load {
margin: 10px;
height: 100%;
width:100%;
}
#primary, #secondaryOne, #secondaryTwo {
border-radius: 8px;
background: #f5f5f5;
border: 1px solid #ccc;
}
#primary {
float: right;
display:block;
height: 100%;
max-width:100%;;
width: 68%;
margin-top:-70%; /* this is the key */
}
#secondaryOne, #secondaryTwo {
width:30%;
height: 220px;
margin-right: 10px;
}
#secondaryTwo {
margin-top: 10px;
}
Problem #1
To get your #primary div to adapt it's width, you can use jquery to verify the presence of the .secondary divs and to set the an other width to the #primary div.
With .secondary demo
without .secondary demo
JQUERY:
if ($('.secondary').length){
$('#primary').css('width', '75%');
}
Problem #2
You can use clear:left; and by changing the order of the divs in your html markup you will have your 2 divs stacked on the left and your content div on the right.
FIDDLE
HTML:
<div id='load'>
<div id='primary'></div>
<div id='secondaryOne' class="secondary"></div>
<div id='secondaryTwo' class="secondary"></div>
</div>
CSS :
.secondary{
clear:left;
float:left;
}
Try
#primary {
min-width:75%;
max-width:100%;
}
http://jsfiddle.net/Paramasivan/v4cvv/65/

Positioning elements in one line

i have problem with displaing divs inline on mobiles. So code works fine in PC's but when i visit my website in mobile i just dont display divs in right way.
Image div is above the div with text (should be inline). This is not case of width because even if i change width of image to just 5px, this small image is still above not right next to.
Example: http://www.filmypodobnedo.pl/matrix/
Whole thing is about listing of similar movies to chosen one (in example matrix), listed movies (IMAGE + TEXT should be displayed inline, that works on PC but not on mobile).
HTML:
<div class="podobny_film">
<div id="zdjecie_podobne">
<img class="featured-project-image" width="100" height="150" alt="Filmy podobne do Equilibrium" src="http://www.filmypodobnedo.pl/photos/Equilibrium.jpg">
</div>
<div id="tekst_podobne">
</div>
CSS:
.podobny_film {
float: left;
width: 80%;
color: #555555;
border-style: dashed;
border-width: 1px;
border-color: black;
padding: 10px;
}
#zdjecie_podobne {
width: 120px;
float: left;
}
#tekst_podobne {
width: 75%;
float: left;
font-size: 16px;
}
add display:inline-block to #zdjecie_podobne and #tekst_podobne
The div "podobny_film" contains div "zdjecie_podobne" with fixed pixel width and div "tekst_podobne" with percentage width. Try to use a percentage width for the div "zdjecie_podobne" and for the image as well, then it should not break.
#zdjecie_podobne {
width:24%; /*together with tekst_podobne that's 99% so be aware of any margins or paddings that might sum up > 100%!*/
float: left;
}
.featured-project-image { width:100%; }
This should work :) :
#zdjecie_podobne {
width: 120px;
float: left;
display:inline-block;
}
#tekst_podobne {
width: 75%;
float: left;
font-size: 16px;
display:inline-block;
}

How can I put two divs on the same line?

I have 2 divs and I want to align them in the same line. The html code is:
<div class="box">
<div class="boxleft"> </div>
<div class="boxright"> </div>
</div>
I tried 'float: left;' and 'float: right;' but the background is going crazy, it apears just on ~30px of the height. I tried to put a height('till then I didn't use height in CSS). It didnt' work. I tried 'display: inline-block' too, but without succes.
Thanks.
CSS:
.box {
width: 956px;
margin-left: auto;
margin-right: auto;
background: #584231;}
.boxleft {
width: 706px;
margin-right: auto;
border-right: 2px solid black;}
.boxright {
width: 250px;
margin-left: auto;
float: right;}
Float: left should do the trick depending on the width of the parent boxand the width of boxleft and boxright. If the parent box has width: 500px; and boxleft and boxrightboth have width: 250px; float:left;. You should be fine.
Have a look at the css properties float:left and clear:both.
http://www.w3schools.com/css/css_float.asp
I put some colors on each background to make it clear, you're maybe lacking a width and height for each element..
.boxleft , .boxright {
float : left;
width : 200px;
height : 100px;
margin : 10px;
}
.boxleft {
background : yellow;
}
.boxright {
background : blue;
}
http://jsfiddle.net/n9mHX/
On most modern browsers nowadays display: table-cell is the better alternative to floating.
You may use
display:inline-block;
or
float
or as per latest browser out you may use
display: table-cell
or you may use
clear: both
If you're not a "CSS guy", look at http://twitter.github.io/bootstrap/. With bootstrap, put two div on the same line is done this way :
<div class="row-fluid box">
<div class="span6 boxleft"></div>
<div class="span6 boxright"></div>
</div>
You need to clear the floats via clearfix on the parent container.
.box {
width: 956px;
background: #584231;
}
/* clearfix */
.box:after {
content: '';
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.boxleft {
width: 704px;
border-right: 2px solid black;
float: left;
}
.boxright {
width: 250px;
float: right;
}
The border is adding 2px to your divs width. That's why I specified it with 704px.
Using inline-block as display for the left and right box should work too.

limit middle div width to child div width

I have nested divs like so:
<div class="first">
<div class="second">
<div class="third"></div>
</div>
</div>​
The third div contains dynamic content - so I don't know it's dimensions.
What I want is the second div to take the width of the third div and not of the first div which is a lot bigger.
So in this demo, I want the border to enclose the green square.
Is this possible with css only? if so, how?
Thanks.
Put a float: left; in the second class. That should do the trick.
.second {
float: left;
}
or
.second {
display: inline-block; //not working on ie7
}
Actually div is a block level element so you can give the display:inline-block to second div and than it will take the third div width & height vic-versa...
CSS
.first
{
width: 500px;
height: 500px;
background: yellow;
}
.second
{
border: 5px solid blue;
display:inline-block;
}
.third
{
min-width: 100px;
min-height: 100px;
background: green;
}
DEMO