Request:
I would like to control the placement of my dynamic divs. Each div should refer to the parent div.
Problem:
The code below does not allow the div positions to be altered based on the parent div. For example, #dldescription top:10%; should use div id="dlitem" as a guide. I tried several tactics but can't seem to make it work as planned.
<?php
include '/scripts/connect_to_mysql.php';
$result = mysql_query('SELECT * FROM table');
echo '<div id="dlcontainer" style="border-style:groove;">
<div id="dlitem">';
$i= 0;
while ($row = mysql_fetch_array($result))
{
//each style will control the placement of the div relative to the parent div id=dlitem
echo'<style>
#dldescription_'.$i.'{
vertical-align:middle;
position: absolute;
text-align:center;
top:10%;
left:45px;
}
#dlimage_'.$i.'{
vertical-align:middle;
position: absolute;
text-align:center;
top:10%;
left:45px;
}
#dltitle_'.$i.'{
vertical-align:middle;
position: absolute;
text-align:center;
top:10%;
left:45px;
}
#dllink_'.$i.'{
vertical-align:middle;
position: absolute;
text-align:center;
top:10%;
left:45px;
display: inline-block;
}
</style>
<div id="dltitle_'.$i.'" >'.$row['col1'].'</div>
<div id="dlimage_'.$i.'" align="center"><img src =./'.$row['col2'].' width="75" height="150"></div>
<div id="dldescription_'.$i.'" align="center" ><img src="/img/facebookShare.png"> '.$row['col3'].' off</div>
<div class="dllink_'.$i.'" align="center">Limit '.$row['col4'].'</div>
';
$i++;
}
echo '</div></div>';
?>
To make the div's position relatively to the parent div, you need to change their position from absolute to relative, like:
position:relative;
Here's a good guide: http://learnlayout.com/position.html
Related
I am trying to create a div (trending-panel) that is only 30% of the total page, in that div contains 10 generated divs (trending-div). The multiple generated divs contain an image, text ($title), and the number of views that article has ($Counter). For some odd reason, when I apply a border to the entire trending-panel, the border does not get applied to the last image and text. Even though the last image and text are printed out, the trending-panel is not counting it for some reason and only prints the border towards the first 9. Here's my code:
HTML CODE:
<div class="trending-panel">
<?php
$resultSet = $db->query("SELECT * FROM Articles ORDER BY Counter DESC, dateStamp DESC LIMIT 10");
if ($resultSet->num_rows != 0)
{
while ($rows = $resultSet->fetch_assoc())
{
$image = $rows["image"];
$title = $rows["title"];
$link = $rows["link"];
$count = $rows["Counter"];
echo "<div class='trending-div'><img src='{$image}'/><p>{$title}</p><p id='count'>{$count}</p></div>";
}
}
?>
</div> <!--End of trending panel div-->
CSS:
.trending-panel{
width: 30%;
border: 1px solid lightgray;
}
.trending-div{
position: relative;
clear: both;
border: 1px solid lightgray;
}
.trending-div img{
width: 45%;
height: 120px;
float: left;
}
.trending-panel p{
width: 54%;
float: right;
font-family: "Oswald";
font-size: 16px;
color: black;
}
#count{
position: absolute;
bottom: 0px;
right: 0px;
font-family: "Oswald";
font-size: 14px;
color: black;
}
Generated HTML:
<div class="trending-panel">
<div class='trending-div'><img src='/media/thumbnails/bus-falls-in-alligator-pond.jpg'/><p>School Bus Falls Into Alligator Pond</p><p id='count'>202254</p></div><div class='trending-div'><img src='/media/thumbnails/foods-that-can-kill-you.jpg'/><p>Top Ten Foods That Can Kill You</p><p id='count'>150136</p></div><div class='trending-div'><img src='/media/thumbnails/most-shocking-facts-about-disney.jpg'/><p>The Top Ten Most Shocking Facts About Disney</p><p id='count'>102508</p></div><div class='trending-div'><img src='/media/thumbnails/extremely-weird-religions.jpg'/><p>10 Extremely Weird Religions</p><p id='count'>98069</p></div><div class='trending-div'><img src='/media/entertainment/trump.jpg'/><p>This Is What Would Happen If You Stopped Sleeping</p><p id='count'>45646</p></div><div class='trending-div'><img src='/media/thumbnails/signs-you-grew-up-in-the-90s.jpg'/><p>10 Signs You Grew Up In The 90s</p><p id='count'>26183</p></div><div class='trending-div'><img src='/media/thumbnails/no-spin-earth.jpg'/><p>This Is What Would Happen If The Earth Stopped Spinning</p><p id='count'>7144</p></div><div class='trending-div'><img src='/media/thumbnails/bubble-gum-under-shoe.jpg'/><p>10 Unwritten Rules Everyone Should Follow</p><p id='count'>3593</p></div> </div> <!--End of trending panel div-->
It could be because you image tag doesn't have quotes around the src attribute value. Also add a closer for the image change:
echo "<div class='trending-div'><img src=$image><p>$title</p><p id='count'><$count</p></div>";
to:
echo "<div class='trending-div'><img src='{$image}'/><p>{$title}</p><p id='count'>{$count}</p></div>";
Try this, you have an '<' before $count, which isn't valid, if you want to use < or > then you have to use escape codes because these mark the start and end of tags.
I am trying to create a page where the header is centered 70% of the window and a body 85% of the window. The header goes on top of the body, but I can't seem to accomplish this. When I add relative positioning to my #body div; the div seems to be positioned relative to the window (body) of the page instead of the header. How do I make it so the #body div is positioned relatively below the #header div so the body is going in the flow of the page? (I want all the elements inside #header to stay exactly where I positioned them).
HTML/PHP
<?php
$resultSet = $db->query("SELECT * FROM table");
if ($resultSet->num_rows != 0)
{
while ($rows = $resultSet->fetch_assoc())
{
$id = $rows["id"];
$images = $rows["image"];
$title = $rows["title"];
if ($id <= 3)
{
//Front page Div that contains all the header elements
echo "<div id=header>";
if ($id == 1)
{
//The front page div that contains the large image and the title
echo "<div id=largeDiv>";
echo "<img src=$images>";
//Div inside of largeImg div, that creates semi-transparent block for title
echo "<div id=largeTitle>";
//Main title of header
echo "<h2>$title</h2>";
echo "</div>"; //End of largeTitle div
echo "</div>"; //End of largeDiv
}
//Secondary div inside of #header that goes on the right side of #largeDiv
echo "<div id=smallDiv>";
if ($id == 2)
{
//Left image
echo "<img id=leftImg src=$images>";
}
if ($id == 3)
{
$text = $rows["text"];
//Right image
echo "<img id=rightImg src=$images>";
//Secondary div's title
echo "<h2>$title</h2>";
//Secondary div's text
echo "<p>$text</p>";
}
echo "</div>"; //End of smallDiv
echo "</div>"; //End of header
}
else
{
echo "<div id=body>";
if ($id <= 13)
{
echo "<div id=left>";
echo "<img src=$images>";
echo "</div>";
}
echo "</div>";
}
}
}
?>
CSS
/*BODY OF ENTIRE PAGE*/
body{
position: relative;
}
/*HEADER OF PAGE. CONTAINS ALL HEADER ELEMENTS*/
#header{
position: relative;
width: 70%;
height: auto;
margin: 1.5% auto;
}
/*MAIN DIV OF HEADER*/
#largeDiv{
position: absolute;
width: 65%;
}
/*IMAGE OF THE MAIN DIV OF HEADER*/
#largeDiv img{
width: 100%;
}
/*THE DIV INSIDE OF #LARGEDIV THAT IS A SEMI-TRANSPARENT BLOCK*/
#largeTitle{
position: absolute;
width: 100%;
height: 25%;
bottom: 1.5%;
background-color: rgba(0, 0, 100, 0.4);
}
/*THE H2 TAG FOR THE MAIN TITLE OF THE HEADER, THIS IS INSIDE #LARGETITLE*/
#largeTitle h2{
color: white;
text-align: center;
font-family: sans-serif;
font-size: 22px;
}
/*SECONDARY DIV OF HEADER, NEXT TO THE #LARGEDIV*/
#smallDiv{
position: absolute;
right: 0%;
top: 0%;
width: 32%;
height: auto;
}
/*LEFT SMALL IMAGE OF HEADER*/
#leftImg{
width: 45%;
float: left;
}
/*RIGHT SMALL IMAGE OF HEADER*/
#rightImg {
width: 45%;
float: right;
}
/*THE TITLE OF THE SECONDARY DIV OF HEADER*/
#smallDiv h2{
clear: both;
font-size: 24px;
margin-top: 48%;
color: cornflowerblue;
font-family: sans-serif;
}
/*THE TEXT OF THE SECONDARY DIV OF HEADER*/
#smallDiv p{
font-size: 100%;
font-family: sans-serif;
margin-top: -5%;
}
#body{
position: relative;
width: 85%;
}
#left{
width: 28.33%;
}
#left img{
display: block;
width: 100%;
}
Sample dataset:
<div id=header><div id=largeDiv><img `src=http://i2.cdn.turner.com/cnnnext/dam/assets/150429103538-bernie-sanders-gallery-photo-5-super-169.jpg><div id=largeTitle><h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Val hala mo sema serum.</h2></div></div><div id=smallDiv></div></div><div id=header><div id=smallDiv><img id=leftImg src=http://a4.files.biography.com/image/upload/c_fill,cs_srgb,dpr_1.0,g_face,h_300,q_80,w_300/MTE4MDAzNDEwMDU4NTc3NDIy.jpg></div></div><div id=header><div id=smallDiv><img id=rightImg` src=https://pbs.twimg.com/profile_images/602821590582038528/oobPhTxZ_400x400.png><h2>Prima disputando per ne, mea amet</h2><p>Ut usu soleat torquatos, vix iudico singulis constituto ut. In vim consul assueverit, ius no veniam voluptatum. Veritus vivendum</p></div></div><div id=body><div id=left><img src=http://www.planwallpaper.com/static/images/Winter-Tiger-Wild-Cat-Images.jpg></div></div><div id=body><div id=left><img src=http://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg></div></div><div id=body><div id=left><img src=http://newflowerwallpaper.com/download/flower-images-to-color-and-wallpapers/flower-images-to-color-and-wallpapers-16.jpg></div></div><div id=body><div id=left><img src=http://eskipaper.com/images/images-4.jpg></div></div>
body is a page construct and can't be repositioned, so the position style has no acutal effect.
What would be better is if your header content #header was inside another element such as a HTML5 style header, and perhaps #body inside a main container.
So, for example;
<body>
<header>
<div id="header">
Header Content Here
</div>
</header>
<main>
<div id="body">
<div id="left">
Left Content
</div>
</div>
</main>
</body>
CSS
header {
width: 100%;
}
#header{
position: relative;
width: 70%;
height: auto;
margin: 1.5% auto;
}
main {
width: 100%;
}
#body {
position: relative;
width: 85%;
margin: 0 auto;
}
#left {
width: 28.33%;
}
The above is an incomplete solution, but it's an example of how you look to structure your page.
An altnernative is to have a container (like #container) around all the content with the max width of the inner content.
this is my fiddle with html and css: http://jsfiddle.net/v2r5we0r/
I'm trying to show a DIV "station_info"(shape of a square) when either clicked/hovered on those DIVs "boxes" right next to it (it will have information from my database table that I will put later on).
I have my PHP posted since I'm looping through my DB to create those boxes.
Is it possible using only CSS and HTML? If so, how can i? I only find examples with Jquery, I don't want to use JQuery or Javascript.
Scroll down the Result in fiddle to see the smalss boxes
Thank you in advance.
HTML:
<body>
<div id="map_size" align="center">
<div class='desk_box' style='position:absolute;left:20px;top:1230px;'>id:84
<div class='station_info'>Hello</div></div>
</div> <!-- end div map_Size -->
</body>
CSS:
/*body*/
body{
margin:0px auto;
width:80%;
height:80%;
}
/*map size*/
#map_size{
width:1190px;
height:1300px;
background:#0099FF;
border-style: solid;
border-color: black;
position: relative;
}
/* desk boxes*/
.desk_box{
width: 23px;
height: 10px;
border: 4px solid black;
padding:10px;
}
.desk_box > .station_info{
display:none;
}
.desk_box > .station_info:first-child{
display:block;
}
.desk_box > div:hover + div {
display: block;
}
PHP:
<?php
include 'db_conn.php';
//query to get X,Y coordinates from DB
$coord_sql = "SELECT coordinate_id, x_coord, y_coord FROM coordinates";
$coord_result = mysqli_query($conn,$coord_sql);
//see if query is good
if($coord_result === false) {
die(mysqli_error());
}
?>
<div id="map_size" align="center">
<?php
//get number of rows for X,Y coords in the table
while($row = mysqli_fetch_assoc($coord_result)){
//naming X,Y values
$x_id = $row['coordinate_id'];
$x_pos = $row['x_coord'];
$y_pos = $row['y_coord'];
//draw a box with a DIV at its X,Y coord
echo "<div class='desk_box' style='position:absolute;left:".$x_pos."px;top:".$y_pos."px;'>id:".$x_id."<div class='station_info'>Hello</div></div>";
} //end while coord_result loop
?>
</div>
CSS can only work descending or going further in the same level in the DOM tree, so you'll have to put the div you wanna hide/show on hover AFTER all the DIVs you're going to hover. Then you can use the general sibling operator: ~ which looks for elements AFTER a given element.
Say you have
.toto ~ .titi {}
then those <div class="titi"> will match
<div class="toto"><div>
<div class="titi"></div>
Or
<div class="toto"></div>
<div class="whatever"></div>
<div class="titi"></div>
But this one won't
<div class="titi"></div>
<div class="toto"></div>
Here's a snippet to see if this is what you want.
/*body*/
body{
margin:0px auto;
width:80%;
height:80%;
}
/*map size*/
#map_size{
width:1190px;
height:1300px;
background:#0099FF;
border-style: solid;
border-color: black;
position: relative;
}
/* desk boxes*/
.desk_box{
width: 23px;
height: 10px;
border: 4px solid black;
padding:10px;
}
.desk_box > .station_info{
display:none;
}
.desk_box > .station_info:first-child{
display:block;
}
.desk_box > div:hover + div {
display: block
}
.div-to-show {
display: none;
}
.desk_box:hover ~ .div-to-show {
display: block;
}
<div id="map_size" align="center">
<div class='desk_box' style='position:absolute;left:20px;top:1230px;'>id:84<div class='station_info'>Hello</div></div>
<div class='desk_box' style='position:absolute;left:20px;top:1165px;'>id:85<div class='station_info'>Hello</div></div>
<div class='desk_box' style='position:absolute;left:20px;top:1100px;'>id:86<div class='station_info'>Hello</div></div>
<div class='desk_box' style='position:absolute;left:20px;top:1035px;'>id:87<div class='station_info'>Hello</div></div>
<div class='desk_box' style='position:absolute;left:73px;top:1230px;'>id:92<div class='station_info'>Hello</div></div>
<div class="div-to-show" style="position: absolute; left:150px; bottom: 100px;">Oh hi !</div>
</div> <!-- end div map_Size -->
I am retrieving a list of products from a database and want to display them all in a rows of 3 columns not using a table though. So I want 3 divs to be displayed side by side. then below.
<div class="productindividualdisplay">
<div class="productphoto">
<img src="http://3.bp.blogspot.com/-_xP-UUa4D0c/UfAo1eYxURI/AAAAAAAAAT4/xsibNtxZceQ/s320/Books.jpg" alt="Smiley face" width="250" height="250"></p>
</div>
<div class="producttitle">
<?php echo $row['title'] ?>
</div>
<div class="productprice">
<?php echo "<div id='productrrp'> €" . $row['rrp'] . "</div>";
if(is_null($offeringprice)) {
echo "Not Available";
} else {
echo "€" . $offeringprice['price'];
}
?>
</div>
That is my code but it is just displaying the divs below each other. Is it possible so it fills up the row before starting another one?
Try using display: inline-block; on the divs's css.
A <div> is a block-level element. Block-level elements, like <h1>, <p>, <table> etc. will (by default) span the entire width of their parent elements, so they can't be positioned next to eachother.
You can change this behavior, however, using the following CSS rule:
div.column {
display: inline-block;
}
This will render the <div>s as inline blocks.
Now you can give it a certain width so that three divs fit into a row. Do note that, when you leave whitespace between two <div> elements, there will be some visual whitespace. If you give all div's a width of 33.333333333%, the extra whitespace will cause their combined width to exceed 100%, so the third div will move to the next line.
You can simply prevent this by making sure there is no whitespace between the HTML elements:
<div class="column">
<p>Some contents here</p>
</div><div class="column">
<p>As you can see, no whitespace between the two div elements.</p>
</div>
Of course you can then use margins to control whitespace manually:
div.column {
display: inline-block;
width: 30%;
margin-right: 3.33333333%;
margin-bottom: 10px;
}
You might wanna take a look at this article: Using inline-block to Display a Product Grid View (it uses <li>s instead of <div>s, but the idea is essentially the same)
Here's a FIDDLE
<div class="product-wrapper">
<div class="productindividualdisplay">
<div class="productphoto">
<img src="http://3.bp.blogspot.com/-_xP-UUa4D0c/UfAo1eYxURI/AAAAAAAAAT4/xsibNtxZceQ/s320/Books.jpg" alt="Smiley face" width="250" height="250">
</div>
<div class="producttitle">
Product Title
</div>
<div class="productprice">
<span>$100</span>
</div>
</div>
...more products...
</div>
.product-wrapper {
width: 960px;
padding: 10px;
}
.productindividualdisplay {
background: #fff;
display: inline-block;
width: 260px;
margin: 5px 5px 15px 5px;
padding: 10px;
text-align: center;
border: 1px solid #999;
box-shadow: 0 5px 6px -3px #333;
}
.productphoto {
width: 95%;
margin: 10px auto;
border-bottom: 1px solid #999;
}
.producttitle a {
font-size: 18px;
text-decoration: none;
}
.productprice {
font-size: 18px;
font-weight: 600;
}
Here am throwing the images from database to a div in php while, here I want to delete the image by clicking on the delete icon which has to be in the top right corner of the every image pulled from db. How could I get a delete icon in the top right corner of the images? This is how I am displaying the images in a dialog box.
<?php
$query = "SELECT * FROM files_images";
$res = mysql_query($query);
while ($row = mysql_fetch_array($res)) {
echo "<div id='popimages'>";
echo "<img title='$row[img]' id='$row[id]' style='width:100px;float:left; height:100px;margin-bottom:5px; margin-left:5px;border:2px solid #b06c1c;border-radius:10px;' src='http://localhost/PhpProject2/user_data/" . $row['filename'] . "'/>";
echo "</div>";
}
?>
If I understand correctly, try outputting something like this :
HTML
<div id='popimages'>
<div class="image">
<img class="btn-delete" onclick="alert('Do something!');" src="http://cdn1.iconfinder.com/data/icons/diagona/icon/16/101.png"/>
<img title='$row[img]' id='$row[id]' style='width:100px;float:left; height:100px;margin-bottom:5px; margin-left:5px;border:2px solid #b06c1c;border-radius:10px;' src='http://i.i.com.com/cnwk.1d/i/tim/2011/03/16/Chrome-logo-2011-03-16.jpg'/>
</div>
</div>
CSS:
.image {
width: 100px;
height: 100px;
position: relative;
}
.btn-delete {
position: absolute;
cursor: pointer;
right: 2px;
top: 2px;
/* This was edited out because it was stupid. See fernholz's answer.
left: 100%;
margin-left: -10px;
margin-top: 2px; */
}
JSFiddle: http://jsfiddle.net/TK7zB/
You need to wrap your image with a <div> and place your image and delete link inside that <div>.
I've created this fiddle for you, that might be what you're looking for.
Drewman is technically correct, I would change .btn-delete's style to be
.btn-delete {
cursor: pointer;
position: absolute;
right: 2px;
top: 2px;
}
The top and right can be changed to whatever distance in from the corner you want the delete icon.
Adrenaxus, was pretty close, but needed a height and width on the image div and position relative, not absolute on it.
HTML:
<div class="img-pos">
<span class="close">×</span>
<img src="">
</div>
CSS:
.img-pos{
position: relative;
}
.img-pos.close {
position: absolute;
top: 2px;
right: 2px;
z-index: 100;
}