Positioning divs left and right within anohter div - html

I am quite new to css and html, and I am having trouble floating divs within a another div,
I've done quite a bit of research online but have not been able to come up with a solution.
these are the sites I have read and where of no use:
barelyfitz /screencast/html-training/css/positioning/
stackoverflow /questions/580195/css-layout-2-column-fixed-fluid
mirificampress /show.php?id=106
How to get Floating DIVs inside fixed-width DIV to continue horizontally?
My code can be found on jsFiddle here

I hope this will help.
CSS:
#left, #right {
width: 100px; //change this to whatever required
float: left;
}
HTML :
<div id="wrapper">
<div id="left">
<p class="t0">lorum itsum left</p>
<div>
<div id="right">
<p class="t0">lorum itsum right</p>
<div>
<div>

Like this?
http://jsfiddle.net/Ev474/
<div id="wrapper">
<div id="inner">
<div id="left">
Left Content
</div>
<div id="right">
Right Content
</div>
</div>
</div>
div {
height: 50px;
}
#wrapper {
width: 200px;
overflow-x: auto;
overflow-y: hidden;
background-color: #ccc;
}
#inner {
width: 400px;
}
#left {
width: 150px;
float: left;
background-color: #f00;
}
#right {
width: 150px;
float: left;
background-color: #0f0;
}​

Since you are a beginner. I will make it straight forward. Below is extraction of your code. I used internal style sheet. Your example you are using external style sheet.
Using float attribute you can set it to left and right. Here is used float:left to alight one div to left and float:right to alight other one to the right.
Each opened tag has to be closed tag.
<head>
</head>
<!--Internal style sheet-->
<style>
.left{
float:left;
}
.right{
float:right;
}
</style>
<body>
<div id="wrapper" >
<div class="left">
<p class="t0">lorum itsum left</p>
</div>
<div class="right">
<p class="t0">lorum itsum right</p>
</div>
</div>
</body>
</html>
Additional note: If you want to adjust the size of left and right div then use width in style sheet. Refer the updated style sheet below. I made left div width to 80% of the screen width and right width to 20%.(total should be 100%). Adjust accordingly.Background color used to set the background color of the div.
.left{
float:left;
background-color:powderblue;
width:80%;
}
.right{
float:right;
width:20%;
background-color:yellow;
}

Related

Make Div take up the remaining width

Basically I have 2 DIVs. One has a fixed width. Now I want the other one take up the remaining width.
I can only acheive this by putting the float on the fixed-width-div - which for me is the one to the right. But it only works when I swap out the DIVs in my HTML (right one comes first). I don't like having it this way.
So is there any way to get this exact setup, but without having to swap out the DIVs in my HTML?
Here's a JSFiddle for clarification
https://jsfiddle.net/MHeqG/1862/
HTML (When I put the left DIV first the float breaks)
<div id="wrap">
<div id="right">
right
</div>
<div id="left">
left
</div>
</div>
CSS
#wrap {
width: 300px;
}
#left {
width: 100%;
background-color:#ff0000;
}
#right {
float: right;
width: 50px;
background-color:#00FF00;
}
You can try and use display:flex;, I think it gets you what you want.
Fiddle
You can make use of CSS calc() function
#wrap {
width: 300px;
}
#left {
width: calc(100% - 50px);
background-color:#ff0000;
}
#right {
float: right;
width: 50px;
background-color:#00FF00;
}
<div id="wrap">
<div id="right">
right
</div>
<div id="left">
left
</div>
</div>
Would this work?
https://jsfiddle.net/MHeqG/1864/
CSS:
#wrap {
position:relative;
width: 300px;
}
#left {
margin-right:55px;
background-color:#ff0000;
}
#right {
position:absolute;
top:0;
right:0;
width: 50px;
background-color:#00FF00;
}
HTML:
<div id="wrap">
<div id="left">
left
</div>
<div id="right">
right
</div>
</div>

aligning nested div's left and right inside container

I am trying to create a div which is as wide as the page and has two div's inside it, one which is aligned left and one aligned right. Its' turning out to be a lot more difficult than I expected. With the code below, both div's align left. I have made a jsFidle to demonstrate the problem. Thanks for reading.
<style>
#container{
border:1px solid;
}
#left{
text-align:left;
border:1px solid red;
display:table-cell;
}
#right{
text-align:right;
border:1px solid blue;
display:table-cell;
}
</style>
<body>
<div id = "container">
<div id = "left">far left</div>
<div id = "right">far right</div>
</div>
</body>
There's no need to use floating elements or absolute positioning in general for something like this.
It's an approach that should've stopped along with using tables for general layouts.
Sample Jsfiddle
CSS:
#container {
display: table;
width: 100%;
}
#container > div {
display: table-cell;
}
.right {
text-align: right;
}
HTML:
<div id="container">
<div>
<p>Left</p>
</div>
<div class="right">
<p>Right</p>
</div>
</div>
You need to use
float: left
and
float: right
on the divs.
A cooler way of aligning things is to use
display:flex
on the container, but you'll have to check the browser compatibility for that I'm afraid.

table. vertical-align. on columns with stickyfooter, and header

I am using http://www.cssstickyfooter.com/ for a fluid header/footer/content page.
I am trying to do a two column layout for my site. Left div navigation; the right div content.
The left column has content vertically aligned in the center. The content is both vertically-aligned in the center as well as horizontally aligned in the center.
I'm stuck at laying out the navigation.
I would think I should be able to make a div for the nav container {float:left; width:300px;display:table;} then make the nav_content div something like {height:300px; display:table-cell; vertical-align:middle;}.
I thought at first the issue was that the container needs to span 100% height of whatever was left over after the footer and then the content would be able to vertically align the height. (The only thing I can find is 'background-hacks' to achieve this and Jscript to calculate and dynamically update absolute height. It doesn't seem right to me that those are the only options.) But when I set the parent div to a set height, then try and vertically-align the content, it still does not work. I really do not understand as all the resources I have read states that the parent contains table display and table-cell can use the vertical-align middle. (does using float mess this up?)
Here is a crudely drawn layout I am trying to accomplish.
http://i.imgur.com/VefhxU7.png
Here is the idea with the code.
<div id="wrap">
<div id="header">
</div>
<div id="main">
<div id="container">
<div id="content">
</div>
</div>
<div id="side">
<div id="nav">
</div>
</div>
</div>
</div>
<div id="footer">
</div>
#side
{
background: none repeat scroll 0 0 #B5E3FF;
float: left;
position: relative;
width: 250px;
display:table;
}
#nav
{
display:inline-block;
vertical-align:middle;
height: 350px;
width:200px;
background-color: blue;
}
What am I doing wrong? Thanks for anyone who tries to help. :)
try this :
<style>
#footer {
background-color: #999;
}
#header {
background-color: #0C6;
}
#side
{
background: none repeat scroll 0 0 #B5E3FF;
float: right;
position: relative;
width: 70%;
display:table;
height: 350px;
}
#nav
{
display:inline-block;
vertical-align:middle;
height: 350px;
width:30%;
background-color: blue;
float: left;
}
</style>
<body>
<div id="mainContainer">
<div id="header">This is header</div>
<div id="nav">This is Nav</div>
<div id="side">This is side</div>
<div id="footer">This is footer</div>
</div>
</body>

3 column layout w/ center a vertically centered div

here is the structure of my html
<style>
.left {
float:left;
width: 450px;
}
.center {
float:left;
width: 150px;
}
.right {
float:right;
width:450px;
}
</style>
<div>
<div class="left">
</div>
<div class="center">
Add
</div>
<div class="right">
</div>
</div>
The content of the left div is a table loaded via ajax. What I would like to do is have the link vertically centered (or darn close) dynamically so that when teh table is loaded and that div changes in height my link then moves appropriately. I've created a grid view control on this table and based on the selected value I want to use the link to populate the right column. Any help would be appreciated. The center div has a fixed width but height would probably fill the containing div to allow for vertically centering.
<style>Update
.left {
float:left;
width: 450px;
}
.center {
width: 150px;
margin-left:auto;
margin-right:auto;
}
.right {
float:right;
width:450px;
}
</style>
<div>
<div class="left">
</div>
<div class="center">
Add
</div>
<div class="right">
</div>
</div>
P.D: Don't forget to tick , thanks :D

CSS: resize div width according to parents width

Ok, I am basically building a fluid layout.
My HTML is like this:
<div id="container">
<div class="box" id="left">Left</div>
<div class="box" id="center">This text is long and can get longer</div>
<div class="box" id="right">Right</div>
<div class="clear"></div>
</div>
Here is the css:
#container{
width: 100%;
}
.box{
float: left;
}
#left, #right{
width: 100px;
}
#center{
width: auto; /* ? */
overflow: hidden;
}
.clear{
clear:both;
}
What I need to know is how do I get the #center to re-size when #container is re-sized without the elements moving underneath each other.
Try these corrections (just simple floating elements, no need to set absolute elems or paddings)
just added a new fiddle
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>fluid layout</title>
<style>
/*class to set the width of the columns */
.floatbox{
width:100px;
}
#container{
width: 100%;
float:left;
}
#left{
float:left;
}
#right{
float:right;
}
#center{
overflow: hidden;
}
.clear{
clear:both;
}
</style>
</head>
<body>
<div id="container">
<!-- floating column to the right, it must be placed BEFORE the left one -->
<div class="floatbox" id="right">Right</div>
<div class="floatbox" id="left">Left</div>
<!-- central column, it takes automatically the remaining width, no need to declare further css rules -->
<div id="center">This text is long and can get longer</div>
<!-- footer, beneath everything, css is ok -->
<div class="clear"></div>
</div>
</body>
</html>
#container also must be floated (or with overflow: auto/hidden) to achieve it.
I strongly suggest you use some of the more well known fluid solutions: http://www.noupe.com/css/9-timeless-3-column-layout-techniques.html
The easiest way to do this and completely avoid the float issues that arise would be to
use padding on the container and absolute position the left/right elements in the padding area. (demo at http://www.jsfiddle.net/gaby/8gKWq/1)
Html
<div id="container">
<div class="box" id="left">Left</div>
<div class="box" id="right">Right</div>
<div class="box" id="centre">This text is long and can get longer</div>
</div>
The order of the divs does not matter anymore..
Css
#container{
padding:0 100px;
position:relative;
}
.box{
/*style the boxes here*/
}
#left, #right{
width: 100px;
position:absolute;
}
#left{left:0;top:0;}
#right{right:0;top:0;}
#center{
/*anything specific to the center box should go here.*/
}