Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-22-2009, 06:12 PM   PM User | #1
jolly_nikki
New Coder

 
Join Date: Aug 2009
Posts: 94
Thanks: 0
Thanked 16 Times in 16 Posts
jolly_nikki is on a distinguished road
Arranging div tags

I am looping through an array to display content in div tag. I want 2 div tags in one row. Could someone post code showing how this can be done.
jolly_nikki is offline   Reply With Quote
Old 10-22-2009, 08:21 PM   PM User | #2
met
Regular Coder

 
Join Date: Oct 2009
Location: United Kingdom
Posts: 728
Thanks: 4
Thanked 119 Times in 119 Posts
met has a little shameless behaviour in the past
PHP Code:
echo '<div id="tag1"><div id="tag2"></div></div>';
/* ? _ ? */ 
post some code and elaborate
met is offline   Reply With Quote
Old 10-22-2009, 08:57 PM   PM User | #3
whizard
Senior Coder

 
whizard's Avatar
 
Join Date: Jan 2005
Location: Philadelphia, PA, USA
Posts: 1,457
Thanks: 10
Thanked 37 Times in 37 Posts
whizard will become famous soon enoughwhizard will become famous soon enough
DIV tags are block elements and therefore display on a fresh line. You can either use <span> tags instead, or use CSS to redefine the div as an inline element:

.mydiv
{
display:inline;
}

Dan
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
whizard is offline   Reply With Quote
Old 10-22-2009, 09:38 PM   PM User | #4
jolly_nikki
New Coder

 
Join Date: Aug 2009
Posts: 94
Thanks: 0
Thanked 16 Times in 16 Posts
jolly_nikki is on a distinguished road
Below is the code

Code:
<html>
	<head>
		<style>
		    .mainContainer{
						width: 98%;
						margin: auto;
					}

					.mainContainer p{
						font-weight: bold;
						font-size: 12px;
						color: white;
						background-color: blue;
						width: 350px;
					    margin: 0px;
					}

					.mainContainer span{
						font-size: 10px;
						color: #333333;
						padding: 4px;
						width: 350px;
					}

					.mainContainer div{
						border: 1px solid blue;
					    width: 350px;
					    margin-bottom: 8px;
					}
			
		</style>
	</head>

	<body>
		<div class = "mainContainer">
			<div>
			    <p>Div 1</p>
			       <span>Some Data</span> <br/> 
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
			  </div>
			
			  <div>
			    <p>Div 2</p>
			       <span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
			  </div>
			
			  <div>
			    <p>Div 3</p>
			       <span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
			  </div>
			  
			  <div>
			    <p>Div 4</p>
			       <span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
					<span>Some Data</span> <br/>  
				</div>
                               ----
			</div>
		</body>
</html>
I would like to display Div1 and Div2 in one row and Div3 and Div4 in second row and so on. This is a popup window with 800px as width and 600px as height and is scrollable.
jolly_nikki is offline   Reply With Quote
Old 10-22-2009, 09:49 PM   PM User | #5
Scriptet
Regular Coder

 
Join Date: Apr 2008
Posts: 685
Thanks: 15
Thanked 105 Times in 104 Posts
Scriptet is on a distinguished road
Add float:left; to maincontainer div and then make sure the width of the div is exactly half of the window size.
Scriptet is offline   Reply With Quote
Old 10-22-2009, 10:22 PM   PM User | #6
jolly_nikki
New Coder

 
Join Date: Aug 2009
Posts: 94
Thanks: 0
Thanked 16 Times in 16 Posts
jolly_nikki is on a distinguished road
Quote:
Originally Posted by Scriptet View Post
Add float:left; to maincontainer div and then make sure the width of the div is exactly half of the window size.

Adding float: left; to main container moved the divs to left of the page but not 2 divs in same line. The width of the page is 800px and i have set the width of the div to 350px.
jolly_nikki is offline   Reply With Quote
Old 10-22-2009, 10:27 PM   PM User | #7
Scriptet
Regular Coder

 
Join Date: Apr 2008
Posts: 685
Thanks: 15
Thanked 105 Times in 104 Posts
Scriptet is on a distinguished road
Sorry I meant add float: left; to the .maincontainer div selector like:

Code:
<style>
.mainContainer{
  width: 98%;
  margin: auto;
  overflow:auto;
}
.mainContainer p{
  font-weight: bold;
  font-size: 12px;
  color: white;
  background-color: blue;
  width: 350px;
  margin: 0px;
}
.mainContainer span{
  font-size: 10px;
  color: #333333;
  padding: 4px;
  display:block;
  width: 350px;
}
.mainContainer div{
  border: 1px solid blue;
  width: 350px;
  margin-bottom: 8px;
  float:left;
}			
</style>
The changes are highlighted in bold. You need overflow:auto; so that the container wraps around the DIV's which are now floated side by side.
The third change adding display:block is required if you want to set a width on a SPAN, otherwise it will just ignore the width you set.
Scriptet is offline   Reply With Quote
Old 10-22-2009, 10:56 PM   PM User | #8
jolly_nikki
New Coder

 
Join Date: Aug 2009
Posts: 94
Thanks: 0
Thanked 16 Times in 16 Posts
jolly_nikki is on a distinguished road
That did help to some extent.
Div 5 should be below Div3 but its below Div4.
Attached Thumbnails
Click image for larger version

Name:	divs.gif
Views:	60
Size:	8.4 KB
ID:	7868  
jolly_nikki is offline   Reply With Quote
Old 10-23-2009, 04:23 PM   PM User | #9
Scriptet
Regular Coder

 
Join Date: Apr 2008
Posts: 685
Thanks: 15
Thanked 105 Times in 104 Posts
Scriptet is on a distinguished road
One way I can think of would be to give every other div (div numbers 3,5,7,9 etc...) a seperate class, and then you add clear:left; to that class.

The other way would be to wrap every 2 DIV's in another DIV so the next one forces below the largest DIV e.g.

Code:
<div id="container">
<div>Some Text</div>
<div>SomeText</div>
</div>
<div id="container">
<div>Some Text</div>
<div>SomeText</div>
</div>
First method requires an extra class, second method requires a bit of extra markup..
Scriptet is offline   Reply With Quote
Old 10-23-2009, 06:50 PM   PM User | #10
jolly_nikki
New Coder

 
Join Date: Aug 2009
Posts: 94
Thanks: 0
Thanked 16 Times in 16 Posts
jolly_nikki is on a distinguished road
The first idea worked very well for me. Thanks a lot. I really appreciate it.
jolly_nikki is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:02 PM.


Advertisement
Log in to turn off these ads.