Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Positioning PHP blocks in CSS

Featured Replies

Hi guys, I'm having difficulty aligning a PHP block which contains 3 images, randomly alternating on refresh, to stay within my CSS wrapper. It aligns fine from the left but once the browser is resized, the image seems to inherit the right hand margin, leaving no gap. Where in the PHP code are you able to apply CSS rules so that I can stop this from happening? As you can tell I am new to PHP. I'll will paste my code example maybe someone could tell me, there is a possibility I can resolve it with just a simple line of PHP ;) Cheers!

 

<!-- start of header -->
<?php
     srand( microtime() * 1000000 );
     $num = rand( 1,3 );

     switch( $num )

{
case 1 : $bannerimg="bannerblue.png"; $url="index.php"; break;
case 2 : $bannerimg="bannergreen.png"; $url="index.php"; break;
case 3 : $bannerimg="bannergrey.png"; $url="index.php"; break;
}

$banner = "<center><a href=\"$url\"> ";
$banner.= "<img src=\"$bannerimg\" ";
$banner.= "width=\"auto\" height=\"auto\" border=\"0\" >";
$banner.= "</a></center>";
echo( $banner );
?>

OK, quick and nasty straight to the point reply ;o)

 

Just create a div for each of them with maybe a surrounding div and use float left.

 

Something like this

 


<div id-"bannerhold">
<div class="case1">case 1 : $bannerimg="bannerblue.png"; $url="index.php";</div>
<div class="case2">case 2 : $bannerimg="bannergreen.png"; $url="index.php";</div>
<div class="case3">case 3 : $bannerimg="bannergrey.png"; $url="index.php";</div>
</div>

 

CSS


#bannerhold {
display: inline;
margin: 0 auto;
}

.case1, .case2, .case3 {
float:left;
padding: 0 6px 0 0;
}

 

The above just aligns the divs with a padding right of 6px. bannerhold with margin: 0 auto; means it's centered.

 

Or of course you could do the very same thing but by using a list ...

 

Plenty of ways around this one ...

 

Hope that helps

you don't need 3 seperate divs for the images, as your using classes and they are all the same, just make it one div e.g.

<div id-"bannerhold">
<div class="case">case 1 : $bannerimg="bannerblue.png"; $url="index.php";</div>
<div class="case">case 2 : $bannerimg="bannergreen.png"; $url="index.php";</div>
<div class="case">case 3 : $bannerimg="bannergrey.png"; $url="index.php";</div>
</div>

#bannerhold {
display: inline;
margin: 0 auto;
}
.case {
float:left;
padding: 0 6px 0 0;
}

 

 

It's good to streamline your code a little

  • Author

I do understand the simplicity in what you are saying, and I have tried these methods. Unfortunately for some reason the browser renders syntax errors whenever I insert <div> tags in PHP blocks which I found very strange. So really its back to the drawing board. I guess my next question is what is the technique used and the correct syntax for styling these PHP blocks in CSS. Thanks for your responses!

  • Author

If its any use to anyone, the syntax error is as follows

 

Parse error: syntax error, unexpected '<', expecting T_CASE or T_DEFAULT or '}' in /var/www/whitefire/index.php on line 59

you don't need 3 seperate divs for the images, as your using classes and they are all the same, just make it one div e.g.

<div id-"bannerhold">
<div class="case">case 1 : $bannerimg="bannerblue.png"; $url="index.php";</div>
<div class="case">case 2 : $bannerimg="bannergreen.png"; $url="index.php";</div>
<div class="case">case 3 : $bannerimg="bannergrey.png"; $url="index.php";</div>
</div>

#bannerhold {
display: inline;
margin: 0 auto;
}
.case {
float:left;
padding: 0 6px 0 0;
}

 

 

It's good to streamline your code a little

 

No you don't, but I generally like to have full control over my elements ... In case I want to mess around with them in the future.... Supposing I wanted to have a bigger margin from 1-2 than from 2-3 or offset the centre image for whatever reason and so forth ... This way I can do that with a swift change.

If its any use to anyone, the syntax error is as follows

 

Parse error: syntax error, unexpected '<', expecting T_CASE or T_DEFAULT or '}' in /var/www/whitefire/index.php on line 59

 

You will need to post your code, but it looks like you haven't echo'd the div e.g.

 

<?php echo '<div class="div_name">' ?>

  • Author

Ok, the code now looks like this

<?php
     srand( microtime() * 1000000 );
     $num = rand( 1,3 );

     switch( $num )
{
<div id-"bannerhold">
 <div class="case">case 1 : $bannerimg="bannerblue.png"; $url="index.php";</div>
 <div class="case">case 2 : $bannerimg="bannergreen.png"; $url="index.php";</div>
 <div class="case">case 3 : $bannerimg="bannergrey.png"; $url="index.php";</div>
</div>
}

$banner = "<center><a href=\"$url\"> ";
$banner.= "<img src=\"$bannerimg\" ";
$banner.= "width=\"auto\" height=\"auto\" border=\"0\" >";
$banner.= "</a></center>";
echo( $banner );
?>

 

I will experiment with the PHP's echo function to see if I can get anywhere

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.