Floating divs left/right without separating them on large window - html

The following example shows floating div on the left and right with two different container widths.
For large container (or large window), the divs floating left stick to the left border, and the div floating right stick to the right.
Is there a way to force the divs to stick together (horizontally centered in the container) when the container is large? Note that fixing the width of the container or that of any div is not an option.
Ideally, I would like to avoid javascript.
.container {
display: inline-block;
border: 1px solid black;
}
.inner1 {
float: left;
background-color: #aff;
}
.inner2 {
float: right;
background-color: #ffa;
}
.inner3 {
float: left;
background-color: #faf;
}
First example with wide container<br />
<div class="container" style="width:400px">
<div class="inner1"><---------- First</div>
<div class="inner2">Second
<br />--------
<br />------</div>
<div class="inner3">Third ----------</div>
</div>
<br />
Second example with small container<br />
<div class="container" style="width:100px">
<div class="inner1"><---------- First</div>
<div class="inner2">Second
<br />--------
<br />------</div>
<div class="inner3">Third ----------</div>
</div>
EDIT :
The reason why I'm using floats is to prevent the default ordering of divs. When non-floating, they are ordered either left-to-right or top-to-bottom, or both. This ordering prevents, sometime, useful space to be filled. For example, imagine div1 wide but not tall, and div2 thin and tall. If the screen is large enough, div2 will be to the right of div1. There will be a lot of space left below div1 and to the left of div2. I want to put div3 in that space. But because of the ordering, div3 will go to the right or below div2.
To resolve this problem, I used floating divs, but it created the other problem I stated initially: floating left/right makes divs separate on large screens.
If the problem can be solved without floats, it would be best.

i cant comment yet, since im pretty new.
so i will just talk
if i understand the question right, You
might be looking for the
vertical-align: middle;
I hope thats what you where looking for.

Do you mean something like this? Just remove float from .inner3.
.container {
display: inline-block;
border: 1px solid black;
}
.inner1 {
float: left;
background-color: #aff;
}
.inner2 {
float: right;
background-color: #ffa;
}
.inner3 {
background-color: #faf;
}
First example with wide container<br />
<div class="container" style="width:400px">
<div class="inner1"><---------- First</div>
<div class="inner2">Second
<br />--------
<br />------</div>
<div class="inner3">Third ----------</div>
</div>
<br />
Second example with small container<br />
<div class="container" style="width:100px">
<div class="inner1"><---------- First</div>
<div class="inner2">Second
<br />--------
<br />------</div>
<div class="inner3">Third ----------</div>
</div>

You can use CSS3 #media query. Just float the divs on screen with 400px width and above.
.container {
border: 1px solid black;
}
.inner1 {
float: left;
background-color: #aff;
}
.inner2 {
float: right;
background-color: #ffa;
}
.inner3 {
float: left;
background-color: #faf;
}
#media (min-width: 400px) {
.container {
margin: 0 auto;
}
.inner1 {
float: left;
}
.inner2 {
float: left;
}
.inner3 {
float: left;
}
}
The container will centered on the screen width >= 400px. And the inner divs will float to the left. By default, the inner1, inner2, and inner3 will be floated to the left, right, and left respectively.
Example on JSFiddle

I think, in this case remove display: inline-block; from .container and add overflow: hidden; here and into .inner3 :
.container {
border: 1px solid black;
overflow: hidden;
}
.inner1 {
float: left;
background-color: #aff;
}
.inner2 {
float: right;
background-color: #ffa;
}
.inner3 {
overflow: hidden;
background-color: #faf;
float: left;
}
EDIT jsfiddle:
jsfiddle-link

Related

Can't get divs to align properly with css

My setup is this:
.cprp_data {
width: 100%;
overflow: hidden;
}
.cprp_title {
width: 80%;
float: left;
}
.gelijkeniscore {
float: right;
}
.cprp_percentage {
float: right;
}
.cprp_excerpt {
width: 80%;
float: left;
}
.cprp-custom-container {
float: right;
}
<div class="cprp_data">
<div class="cprp_title">TITLE</div>
<div class="gelijkeniscore">SOME CONTENT</div>
<div class="cprp_percentage">SOME CONTENT</div>
<div class="cprp_excerpt">SOME CONTENT</div>
<div class="cprp-custom-container">SOME CONTENT IN SEVERAL DIVS
</div>
</div>
cprp_data should function as the wrapper, and this is how i want the other divs inside it to to align:
<cprp_title> <gelijkeniscore>
<cprp_excerpt> <cprp_percentage>
<cprp-custom-container>
To be clear, divs cprp_excerpt and cprp_title should align left on top of each other using a width of 80%, and the other 3 divs are supposed to align right also on top of each other using the remaining 20%, looking kinda like a sidebar.
I can't get it to work. Been trying for hours using several CSS setups. The above CSS is as close as i have gotten so far. But somehow the cprp_excerpt div keeps taking up 100% of the width pushing the cprp-custom-container way down. Any help would be much appreciated!
shift the cprp_percentage div after the cprp_excerpt div
As inferred from the problem statement:
all the internal divs are now stacked upon each other in the desired hierarchy
left container occupies 80% screen width
right container occupies 20% screen width
The container wrapper remains intact, i have just introduced two sub-wrappers to distinguish contents of left and right side of screen
Hope this code helps!
.cprp_data {
width: 100%;
overflow: hidden;
/* display flex to control the scenario */
display: flex;
}
/*.cprp_title {
width: 80%;
float: left;
}
.gelijkeniscore {
float: right;
}
.cprp_percentage {
float: right;
}
.cprp_excerpt {
width: 80%;
float: left;
}
.cprp-custom-container {
float: right;
}
*/
/* as the left section should be 80% in width */
.left{
width: 80%;
border: 1px solid #000000;
}
/* as the right section should be 20% */
.right{
width: 20%;
border: 1px solid #000000;
}
<div class="cprp_data">
<div class="left">
<div class="cprp_title">TITLE</div>
<div class="cprp_excerpt">SOME CONTENT</div>
</div>
<div class="right">
<div class="gelijkeniscore">SOME CONTENT</div>
<div class="cprp_percentage">SOME CONTENT</div>
<div class="cprp-custom-container">SOME CONTENT IN SEVERAL DIVS
</div>
</div>
</div>

HTML paragraph new line if no place in inline-block div

I have in div two divs with floats (left and right). In right div there are paragraphs. All that two divs have inline-block display. If paragraphs in right div too long, then right div jump over the left, and set to display block.
I'm want to paraghraps do new line if it too long.
Code:
.left {
margin: 30px;
float: left;
display: inline-block;
}
.right {
float: left;
margin-top: 30px;
}
.right p {
margin: 10px;
font-weight: 900;
}
<div class="box_container">
<div class="left">
<img src="{url}">
</div>
<div class="right">
<p>p1</p>
<p>p2</p>
<p>p3</p>
<p>p4</p>
<p>p5</p>
</div>
</div>
When text in paragraph too long:
.left {
margin: 30px;
float: left;
display: inline-block;
}
.right {
float: left;
margin-top: 30px;
}
.right p {
margin: 10px;
font-weight: 900;
}
.left img {
border: 5px solid white;
}
<div class="box_container">
<div class="left">
<img src="http://monitorgame.com/m/games/001.jpg">
</div>
<div class="right">
<p>p1</p>
<p>p2</p>
<p>p3</p>
<p>p4</p>
<p>p5 text text text text text text lalalalalalalalalalalallalalallalalalala</p>
</div>
</div>
You should allocate space for them. I like using floats in these instances, so for example you could add float:left width: 50% to each one, something like that.
.left {
margin: 30px;
float: left;
width: 50%;
}
.right {
float: left;
width: 50%
margin-top: 30px;
}
You already had the float, you just needed to specify the width. They could be static too not % if you want, but if the static sizes don't fit in the screen they will break like your example.
see working here : https://jsfiddle.net/3LtLuxbc/3/
Just a note on the fiddle - I changed your img size to with 100% and removed the border so it would scale , you can change that to suit your design.
Add a width to the right div. This will force the text to wrap. Without a specified width, div will increase in size until reaching max size of wrapper div or page

Div Repositioning on Window Resize

I've been trying to achieve this for hours and I'm not quite getting it to work, so here it goes nothing:
I have this site:Site HomePage
composed by this HTML elements:
<div id="headerwrap">
<div id="header">
</div>
</div>
<div id="navigationwrap">
<div id="navigation">
</div>
</div>
<div id="midcontentwrap">
<div id="leftwrap">
<div id="left">
</div>
</div>
<div id="midwrap">
<div id="midleft">
</div>
<div id="midright">
</div>
</div>
<div id="rightwrap">
<div id="right">
</div>
</div>
</div>
</div>
What I need is:
- When the browser window is resized, either left and right columns stay where they are and the MID COLUMN RIGHT SIDE needs to go below MID COLUMN LEFT SIDE.
My CSS file is pretty simple by now and this is the only major thing I need to do as the window size changes.
Thanks in advance for any help.
Yep, you're going to want to use media queries. Here's a JSFiddle of it in action.
Resize the display iFrame of the Fiddle back and forth past 500px width to view the results. I spruced up your HTML a little, too, to make it more modern (sorry):
HTML:
<section class='contentWrap'>
<aside>
This element corresponds to the element on the far left of the image you linked to.
</aside>
<div class='mainContent'>
<article class='left'>
This element corresponds to the mid-left element in the image you linked to.
</article>
<article class='right'>
This element corresponds to the mid-right element in the image you linked to.
</article>
</div>
<nav>
This element corresponds to the element on the far right side of the image you linked to.
</nav>
</section>
CSS:
.contentWrap {
width: 100%;
}
.contentWrap aside {
display: inline-block;
width: 25%;
height: 200px;
border: 1px solid purple;
}
.mainContent {
display: inline-block;
width: 45%; /* only because the borders are upsetting the percantages */
height: 200px;
border: 1px solid gray;
vertical-align: top;
}
.mainContent article {
border: 1px solid #00cae9;
margin-bottom: 2px;
}
.contentWrap nav {
display: inline-block;
width: 25%;
height: 200px;
border: 1px solid orangered;
vertical-align: top;
}
#media screen and (min-width: 500px) {
.contentWrap {
width: 500px;
margin: 0 auto;
}
.mainContent article {
display: inline-block;
width: 47%;
vertical-align: top;
}
}
NB: if you're viewing it on a super small screen, it won't work; that's JSFiddle's problem.
Oh fun, an excuse to have a play with CSS Media Queries!
DEMO: http://jsfiddle.net/Vn2QY/1/
CSS
#midcontentwrap {
min-width: 500px;
}
#leftwrap, #midwrap, #rightwrap {
float: left;
min-height: 400px;
}
#leftwrap, #rightwrap {
min-width: 100px;
width: 25%;
background-color: #15a;
}
#midwrap {
width: 50%;
background-color: #45a
}
#midleft, #midright {
width: 50%;
float: left;
}
#midleft {
background-color: #a45;
}
#midright {
background-color: #4a5;
}
#media all and (max-width: 500px) {
#midleft, #midright {
width: 100%;
}
}
The key piece here is the final part of the CSS. It basically states that "for all media (screen, printing, etc) when the browser width is less than 500 pixels in width, change the styling for #midleft and #midright and make them 100% of the available width."
By increasing their widths their existing float styling will force them on to new lines.
Try this DEMO
I'm guessing your want to get a fluid/responsive design. This should work for you.
Use float:left and min-width
To solve this problem....use % value for all div id width

Make div fill up rest of the parent

I have a header/ container with no specified width (therefore it's as long as the parent). Inside that, I have two smaller divs. Now, the first one should only contain a picture (with a set size), and the other should be as big as there's space left. I can't use a set width, because I don't know the width of the header.
How do I do this with pure CSS?
What I want ultimately is a picture, then some text aligned to the right top, and then some text aligned with the bottom of the picture on the left.
Do you know of any better way to do this?
Here's a picture so it's easier to understand:
Thanks, Aleksander
EDIT 1:
HTML:
<div class="header">
<div class="header_left">
<div class="pic"><img width="35px" src="http://upload.wikimedia.org/wikipedia/commons/1/1a/Volkswagen_Logo.png" /></div>
</div>
<div class="header_right">
<div class="time">18m ago</div>
<div class="name">Volkswagen</div>
</div>
</div>
CSS:
.header {
}
.header_left {
display: inline-block;
}
.pic {
margin: 5px;
}
.header_right {
display: inline-block;
}
.time {
margin: 5px;
float: right;
}
.name {
margin: 5px;
float:left;
}
It's kinda' messy right now, because what I've just been trying a lot of stuff, and this is the last thing.
It would be easier if you displayed your html. Here's an example based on your description. You can see this working in the fiddle here
http://jsfiddle.net/Z68ds/18/
.header {
overflow:hidden;
padding: 4px;
background: #ddd;
}
.caption {
float: right;
font-size: 0.9em;
}
.avatar {
float: left;
}
.title {
margin: 14px 0 0 38px;
}
<div class="header">
<div class="caption">
texty text2
</div>
<img class="avatar" src="http://i.stack.imgur.com/5dv0i.jpg?s=32&g=1" />
<div class="title">texty text1</div>
</div>
You have to use overflow in the element you don't want to set a width without floating it.
#left {
float: left;
width: 100px;
}
#right {
overflow: hidden;
}
This will force the #right element to cover the rest of its parent. No extra markup needed.
Is this what you want to achive?
<div id="header">
<img id="logo" src="http://blog.grio.com/wp-content/uploads/2012/09/stackoverflow.png" />
<p id="textRight">texty text2</p>
<p id="textLeft">texty text1</p>
<div class="clearer"></div>
</div>
/* CSS */
#logo {
float: left;
}
#textRight {
float: right;
}
#textLeft {
clear: right;
float: left;
}
.clearer {
clear: both;
}
Here is a fiddle:
http://jsfiddle.net/T26cD/

CSS: Having 3 div on the same line with the middle one taking the remaining space

I'm building a toolbar, I'd like the yellow part in the following example to take the whole space left (in white):
http://jsfiddle.net/MWjGH/1/
<div class="left"> Some content </div>
<span class="middle"> This should fill the space left </span>
<div class="right"> Some other content </div>
with css:
.left {
float: left;
background-color: #ddd;
display: inline-block;
}
.middle {
background-color: yellow;
display: inline-block;
}
.right {
float: right;
background-color: #ddd;
display: inline-block;
}
Edit: the content of left and right is dynamic, it can change, so I don't want to set width on them
I don't know if that suits you because of a slight HTML change:
<div class="left"> Some content </div>
<div class="right"> Some other content </div>
<span class="middle"> This should fill the space </span>
But I believe it is what you want,
CSS:
.left {
float: left;
background-color: #ddd;
}
.middle {
background-color: yellow;
display: block;
overflow:hidden;
}
.right {
float: right;
background-color: #ddd;
}
DEMO :http://jsfiddle.net/pavloschris/MWjGH/12/
Put the middle div after the floated divs:
<div class="left"> Some content </div>
<div class="right"> Some other content </div>
<div class="middle"> This should fill the space left </div>
Then, don't change any of the display properties so they stay on block (the default for div)
.left {
float: left;
background-color: #ddd;
}
.middle {
background-color: yellow;
}
.right {
float: right;
background-color: #ddd;
}
Fiddle: http://jsfiddle.net/hLmj7/
If you do't have a fixed width for the two side columns, you can always display:table-cell.
.left {
background-color: #ddd;
display: table-cell;
}
.middle {
background-color: yellow;
display: table-cell;
width:100%;
}
.right {
background-color: #ddd;
display: table-cell;
}
JSFiddle example.
With this you're then able to add min-width to the outer columns without having to keep changing the width of the middle element.
JSFiddle example with min-width applied.
I would wrap your divs in a wrapper, and assign the background-color to the wrapper div
Then, you don't need to specify width at all.
jsfiddle
Html:
<div class="toolbar">
<div class="left"> Some content </div>
<div class="middle"> This should fill the space left </div>
<div class="right"> Some other content </div>
</div>
CSS:
.toolbar {
background-color: yellow;
}
.left {
float: left;
background-color: #ddd;
display: inline-block;
}
.middle {
display: inline-block;
}
.right {
float: right;
background-color: #ddd;
display: inline-block;
}
Consider adding clearfix to the wrapper div as the divs inside are floating :)
try this:
don't right align your last div
make all your containers float:left
and give percentage width to each of your containers, so that their total sum should be 100%;
working fiddle
if you don't want to enforce a static width do this:
give each of your containers a width:auto, but be notified, that,
if the total sum of the width of each of the containers turns out to be more than that of parent( body in your case) contaienr, then line-break would occur,
a div will slide down to the next row.
see this fiddle