CSS page layout, keep text in its own div column - html

I have a page layout with two div. They are both float:left and so appear as columns next to each other. I want them to stay that way. However, if the text in the right-most div is too long then the whole text moves down below the left most column. This also happens if the text is short, but I make the browser window smaller. What I want is for the long text to take up more lines, but only within its own right column.
CSS:
.left{float:left}
.right{float:left}
HTML:
<div id="container">
<div class = "left">
<span>some text</span>
</div>
<div class = "right">
<span>some long text....</span>
</div>
</div>
Edit: Also, the left column should stay fixed.

Sounds like you want to do something like this:
http://jsfiddle.net/VV4Hc/8/
The outer container specifies the width those two divs should occupy. As you can see, they also have percentage widths so you can change the container's property without having to go back and change the divs.
Word of warning, remember to clear your floats as well, so other elements don't get "caught" in the float. To do that, just define an element with the property clear:both like this:
.container {
width: 1000px;
margin: 0px auto; /* This will center the container on the page. */
}
.left, .right {
float: left;
width: 50%;
}
.clearfix {clear:both;}
<div class="container">
<div class="left">...</div>
<div class="right">...</div>
<div class="clearfix"></div>
<p>I won't get caught.</p>
</div>
If you're looking to do this for layout, you may want to consider a CSS framework, which has these sorts of things perfectly measured based on a specified number of columns. See:
Bootstrap/Kickstrap
960.gs
Blueprint

You can set the positions for those divs like
.left{width: 50%; position:relative; top:0%; left:0%;}
.right{width: 50%; position:relative; top:0%; left:50%;}

I was able to solve it with a table. When the window is made smaller, at first only the right column is made smaller and the words squeeze down on more lines until the column has disappeared. If the browser is made smaller than the width of the left column, then the left column starts disappearing, cutting off words. Works in my Chrome and IE although I don't know about older IE.
.td_right{vertical-align:top; max-width:300px;}
.td_left{vertical-align:top; min-width:300px; width:300}
<table >
<tr>
<td class = "td_left">
stuff here
</td>
<td class = "td_right">
stuff here
</td>
</tr>
</table>

Related

how to left align many rows of inline divs, that together are centered in the screen (regardless of screen size)

I have several inline divs, inside a table cell, that are all left aligned, and will wrap to the next row when there is no more space. For the most part, they look pretty good, and rearrange to properly fill the screen.
The problem is when the width of the screen is JUST SHY OF the size necessary to fit another element on the right. There is a big gap on the right side of the screen, compared to the left. It's a stylistic choice, but I am trying to figure out a way to balance the margins on both sides, no matter what the screen size is. Here is my site as it looks currently: www.bradthedesigner.com
I am trying to get something that allows each individually to be left aligned, in a group that is centered. Instead I can either get everything left aligned (but the extra space at right looks bad, especially on small screens), or everything centered (which looks bad when there is only one item on the bottom row).
HTML
<center>
<div class="container">
<div class="outerelement">
<div class="element"></div>
</div>
<div class="outerelement">
<div class="element"></div>
</div>
<div class="outerelement">
<div class="element"></div>
</div>
<div class="outerelement">
<div class="element"></div>
</div>
<div class="outerelement">
<div class="element"></div>
</div>
<div class="outerelement">
<div class="element"></div>
</div>
<div class="outerelement">
<div class="element"></div>
</div>
<div class="outerelement">
<div class="element"></div>
</div>
</div>
</center>
CSS
.container
{
text-align:center;
border:2px solid black;
}
.outerelement
{
width:216px;
height:216px;
display: inline-block;
}
.element
{
background-color:#999999;
width: 200px;
height: 200px;
border-radius:16px;
}
Looking at the code I've got online, I see that I switched to tables, because I was more familiar with their behavior and borders for debugging. Here is me recreating my results with divs though... I've tried using <center> outside, align=center inside, and all sorts of CSS styles that came up on other searches through stack overflow.
I've been beating my head against a wall, and can't seem to find any-one doing something similar. It doesn't seem too crazy does it? Basically it boils down to, if the contents of a div are too big to fit, of course it should wrap... but why then does the div stay the width of the screen if the contents that fit on any row don't reach that far? A table with rows and columns would work, but that isn't able to adapt to different screen resolutions or window re-sizing, is it?
Well from the image you are posting I can tell the problem ...
You want to whole block that contain the small blocks to be horizontally aligned (equal spaces on each side) and the inline divs to be aligned to the left ...
Your first attempt is the default behavior (the inline blocks are wrapping to the next line but no proper alignment for the container)
Your second attempt was to set text-align: center to the container which is perfect but it won't align the last line as you desire ...
To achieve what you want, you need to wrap all the inline divs in a single div with predefined width ... Something like
.wrapper-div {
width: 500px;
margin: 0 auto;
}
The margin: 0 auto declaration will force the div to be horizontally aligned as you want.
When you render out the blocks, render out empty placeholders that aren't visible but take up the space of the missing elements on the last row. This will push the existing elements on the last row to the left.

Put same DIV twice on same row with space between them

I want to put the same div twice on the same row and cover all of its width and I need to put some space between them both.
The problem is when I use margin it will affect them both since they have same class so the second div will go below the other because the total width will become bigger than the container width.
I tried to use overflow:hidden or overflow-x:hidden with margin or change their size but nothing changed.(also I've tried to use borders with overflow hidden)
I am forced to use many div from the same class and I need them to cover all the width of the row.
Edit: the code is big so I will post a small example to explain my question
<div class="container">
<div class="block">content...</div>
<div class="block">content...</div>
</div>
<style>
.container{width:1000px; margin:0px auto;}
.block{width:480px;height:500px;float:left;}
</style>
I want to put first block + 40px space + second block
If you want the two .block divs on the same row what I would do is not do it in pixels but with %'s.
For example what I would do is this:
Give your div that you want on the right an id of right and the one that you want on the left an id of left:
<div class="container">
<div class="block" id="right">content...</div>
<div class="block" id="left">content...</div>
</div>
Then I would style it with
<style>
.container{width:1000px; margin:0px auto;}
.block{width:48%;height:500px;display:inline;}
#left{float:left;}
#right{float:right;}
</style>
You can play around with the exact width percent to get it to your standards.
You might want to make give them different class names or ids if you want to manipulate the two of them different.
<div class="container">
<div class="block1">content...</div>
<div class="block1">content...</div>
</div>
You can use inline display to make them appear in the same row.
.block1, block2 {
display: inline;
}
From there you can style them how you want by selecting either of those classes.
Could this be something like you're after?
http://jsfiddle.net/justin_thomas/9S46N/
The CSS:
.myRow {
width:48em;
}
.myclass {
padding: 1em;
margin-left:1em;
margin-right:1em;
float: left;
display:inline;
width: 20em;
}
The HTML:
<div class="myRow">
<div class="myClass">Blah... blah...</div>
<div class="myClass">Blah... blah...</div>
</div>
In there, i've used floats to get the desired effect. Unfortunately this means you have a hard time if you can't specify the actual width of the row's container in physical units (or one of its parents) and you also need to know whe number of columns there will be in this row to use as the width amount in the class with the divs.
I don't really like this as as solution as you need to make sure that the sum of each (div width + left-margin + right-margin) is never larger than the width of the row container.

expand two divs similar to the divs in wordpress dashboard

I have two divs on a page with the same height position. I'm trying to make them expandable, allot like what goes on in the WordPress dashboard area:
Now i've got the left div to expand but only with the right div staying at the same width. I need both to expand on zooming in and out.
any ideas how this is done?
I've been looking it up for the past hour but i cant find anything.
A link to a tutorial would be cool (good luck finding one).
EDIT:
Here guys, i found something similar: http://jsfiddle.net/Khez/2zLPF/embedded/result/
do you see how the two divs side by side expand? the green and blue ones...
If you want your divs to dynamically change depending on the width of their container, set the widths using percentages:
HTML:
<div class="column">
<div class="wrapper">
<p>Text</p>
</div>
</div>
<div class="column">
<div class="wrapper">
<p>Text</p>
</div>
</div>
CSS:
.column {
float: left;
width: 50%; }
.column div { margin: 0 20px; /* Set the spacing between the cells */ }
Preview: http://jsfiddle.net/Wexcode/F7h2C/
NOTE: Because you are setting the combined widths of the columns to 100%, you cannot add padding to .column if you want them to be on the same line. The inner div wrapper will allow you to add spacing between your two columns. You should apply all background attributes to .wrapper.

Adding side by side divs confusion

I always seems to get this simple HTML stuff wrong. I am currently trying to add side by side divs in the middle of a page on this test page I made:
http://www.comehike.com/hiking_dev.php
The code I added was something like this:
<div style="width: 460px; float: left; ">
<strong>Test hello</strong>
</div>
<div style="width: 300px; float: right; ">
<strong>Test hello 2</strong>
</div>
I added <strong> tags so you can spot it on the page better.
But do you see there is text that appears there that reads like this "When considering the injury risk of any" - but that text is in the <p> tag below. Why is it appearing there?
Is it better practice to wrap my 2 divs that I am trying to align, within another div?
After your two floating divs, add another empty div...
<div style="clear:both;"></div>
This will cause the two floating divs to push all subsequent content below them. As you have it now, there is 200 pixels of empty space after the first div allowing the other content to simply wrap around it.
Increasing the width of the floating divs may not be desirable for your layout so clear:both; is most typical for this situation.
Surround those two divs in a parent div with overflow set to hidden.
<div style="overflow:hidden;">
<div style="width: 460px; float: left; ">
<strong>Test hello</strong>
</div>
<div style="width: 300px; float: right; ">
<strong>Test hello 2</strong>
</div>
</div>
An alternative (as others have pointed out) is to use a third element:
<div style="clear:both;"></div>
It's debatable as to which is better. Usually, either is just fine. Here's a post about the topic:
Floated Child Elements: overflow:hidden or clear:both?
You'll either need to add a div below your two divs with clear:both; as others have suggested, or you could add clear:both; to the following <p> element.
Because you have an entire page width of 960px. You're combined div widths are 760px (400+300). If you add 200px to the second div you should be fine.
Edit: Because of padding, you can increase either of the divs by 150px and be fine.

Simple CSS MasterPage layout

I'm helpless, tried my best understanding CSS but it's just not for me.
I would like to make a really simple MasterPage:
at the top a div of full width and height 40px (1)
at the bottom also a div of full width and height 40px (2)
in the middle:
on the left: a div of width 200 px (3)
on the right side of the left div: a div with contentPlaceHolder (4)
What I would like to get is: if i make some site that uses my master page and place a panel in the contentPlaceHolder that has width 800px, I would like my site to adjust to it - top, middle and bottom divs to have their width of 1000px (200 + 800). I also wouldn't like (and I have a huge problem with that) the (4) to move down if I resize (shrink) the browser window - I would like all the divs to be blocked.
This is my master page html:
<div>
<div class="header">
</div>
<div>
<div class="links">
</div>
<div class="content">
</div>
</div>
<div class="footer">
</div>
</div>
What kind of CSS do I have to write to make this finally work?
Not sure if you have checked into this or not, but we use the YUI-Grids CSS Framework for our layouts. It keeps us from having to spend a lot of time on CSS, which we are not great at being developers.
There is even a grid builder which will let you graphically layout a page, and then copy and paste the required HTML to make it happen :)
To prevent floated divs from being "squeezed" out of the alignment you want, you usually use either width or min-width.
For example, in this code the div containing the links and content will never be smaller than 1000 pixels. If the screen is smaller than 1000 pixels, a scrollbar is displayed.
<div style="min-width: 1000px">
<div class="links"></div>
<div class="content"></div>
</div>
You could also use width instead of min-width:
<div style="width: 1000px">
<div class="links"></div>
<div class="content"></div>
</div>
The difference between the two is simple: if you specify min-width, the div CAN grow to be larger if it needs to. If you specify width, the div will be exactly the size you specified.
Be aware that min-width is not supported by IE6.
Here's a quick stab at specific CSS/Markup for this problem.
Markup:
<!-- Header, etc. -->
<div class="contentView">
<div class="links">
</div>
<div class="content">
</div>
</div>
<!-- Footer, etc. -->
CSS:
.contentView {
/* Causes absolutely positioned children to be positioned relative to this object */
position: relative;
}
.links {
position: absolute;
top: 0;
left: 0;
width: 200px;
}
.content {
padding-left: 200px;
}
You might want your footer to be "sticky." Check here for information on that: http://ryanfait.com/resources/footer-stick-to-bottom-of-page/
How appropriate this is depends on precisely what the design calls for. This makes the links section more of a floating box on the left than a column for example.
This ends up looking like this (.content is green, .links is red):