I'm creating a horizontally scrolling web site. There's a container div which I want to retain a fixed height but expand as required horizontally to fit the content inside it. At the moment the div only expands horizontally as far as the page width. There are actually 9 images to display but only the first 4 are shown. See code and image below. How do I make the container div expand horizontally to show all images please?
css:
body
{
background-color:#dbdbdb;
}
div.infinite-container
{
background-color:#db0080;
height:180px;
}
img.infinite-item
{
width="320";
height="180";
margin-right:8px;
margin-bottom:8px;
display:inline-block;
}
.infinite-more-link
{
visibility:hidden;
}
PHP:
<div class="infinite-container">');
if ($num_results > 0)
{
$array = array();
while ($row = mysql_fetch_assoc($result))
{
$array[] = $row;
}
for ($i = 0; $i < $numImagesPerPage; $i++)
{
$filePath = "animations/".$array[$i]['animationid'].".gif";
echo('<img class="infinite-item" src="'.$filePath.'"/>');
}
}
echo('</div>');
This screenshot is after the changes below suggested by Andrei. The pink area is the container div. The images appear to break out below it.
From the code you posted, doing something like this should work:
body
{
background-color:#dbdbdb;
overflow:auto;
}
div.infinite-container
{
background-color:#db0080;
height:180px;
display:inline-block;
white-space: nowrap;
}
img.infinite-item
{
width: 320px;
height: 180px;
margin-right:8px;
margin-bottom:8px;
display:inline-block;
}
jsFiddle example:
http://jsfiddle.net/S6Abd/
What this does is:
set the display mode to inline-block on the container. This way the container will be as large as needed to contain all items.
set overflow:auto on body to show scroll-bars.
correct the width and height of each item.
add white-space: nowrap; to the container to force the items to stay on one line.
Add this CSS style :)
div.infinite-container
{
width:2952px; /* (320 + 8) * 9 = 2952 */
}
But on the serious note - DIV shows (kind of) all your images, only images 5-9 are in next line and because container have fixed height, then they are hidden.
Related
Writing an extension for a Plugin I have the possibility to change all attributes of an HTML element using PHP.
$attributes["style"] .= 'padding-left:10px;';
array_push($attributes["class"], "long-container");
array_push($attributes["class"], "super smooth");
$attributes["data-whatever"] = "great";
Now I want to give a user the possibility to enter the width / height ratio of a div dynamically (the solution of how to do this is described in the answer by #Web_Designer here: Maintain the aspect ratio of a div with CSS).
Within the function where I can change the output of the third-party Plugin I wrote the following code for calculating the width height ratio according to the input. As the height of my boxes is :
if( !empty( $args['stretchy-desktop'] ) ) {
$sd = array_map('trim',explode(":",$args['stretchy-desktop']));
if(count($sd)==2) {
$sd[0] = floatval(str_replace(",",".",$sd[0]));
$sd[1] = floatval(str_replace(",",".",$sd[1]));
if($sd[0]>0 && $sd[1]>0) {
$padding = ($sd[1] / $sd[0])*100;
array_push($attributes['class'], 'stretchy-desktop');
$attributes['style'] .= 'padding-bottom:'.$padding.'%;';
}
}
}
Great right? However now the user wants a possibility to enter a different weight height ratio for mobile devices as well as a different dynamic min-height for mobile devices and this is there I am stuck.
1) It is not possible to give inline #media queries right now otherwise my solution would be like this (Is it possible to put CSS #media rules inline?):
$attributes['style'] .= '#media (min-width:540px) {padding-bottom:'.$padding.'%;}#media (max-width:539px) {padding-bottom:'.$padding_mobile.';}';
2) It is not possible to use HTML attribute values in CSS right now (CSS values using HTML5 data attribute) otherwise my solution would be like this:
$attributes['data-desktoppadding'] = $padding;
$attributes['data-mobilepadding'] = $padding_mobile;
In CSS:
#media (min-width:540px) {
.long-container {
padding-bottom: attr(data-desktoppadding);
}
}
#media (max-width:539px) {
.long-container {
padding-bottom: attr(data-mobilepadding);
}
}
3) As the values are dynamic numbers I can not define a CSS class for every possible existing float.
Of course I could use JavaScript but we all know the significant drawbacks (including ugly page load).
Can you think of any CSS solution for this dilemma?
Here is a solution I came up with. It involves creating a wrapper div around the target element. Basically, the way that this works is that the outer div is assigned the inline styles for the mobile mode, and the inner div is assigned the inline styles for desktop mode. When the browser window is resized to be below the threshold for desktop view, it resets the inner div's (desktop) inline styles to defaults so the outer div's (mobile) inline styles take over. When the browser window is resized to be above the threshold, it resets the outer div's (mobile) inline styles to defaults so the inner div's (desktop) inline styles take over. The way that it overrides the inline styles is by using the !important keyword in the rulesets in the CSS media queries.
I think it goes without saying that the inline styles in the snippet below would be replaced with your $attributes['style']. But since you will have separate mobile and desktop styles, I guess it would be $attributes['style-mobile'] and $attributes['style-desktop'].
#media (min-width:540px) {
.padding-mobile {
padding-bottom:0 !important;
width: auto !important;
}
}
#media (max-width:539px) {
.padding-desktop {
padding-bottom:0 !important;
width: auto !important;
}
}
<div class="padding-mobile" style="width:100%;background-color:red;padding-bottom:100%;">
<div class="padding-desktop" style="width:50%;background-color:red;padding-bottom:25%;">
</div>
</div>
An elegant approach that works in most major browsers is the usage of custom properties. They are basically variables in CSS. As of writing this (2017-03-27), only IE and Edge do not support this, although they are working on support for Edge.
You would add the variables to the $attributes['style'] and actually apply them in the stylesheet inside a media query. They are then used dynamically by the browser.
I have implemented a demo as a snippet, but because it is easier to change the viewport size on JSFiddle, also a copy of the demo there. Note that the responsive breakpoint is defined in CSS here, and the variables are defined in inline styles of the element.
.container {
width: 200px;
}
.block {
position: relative;
display: block;
box-sizing: border-box;
width: 100%;
height: 0;
padding: 10px;
padding-bottom: var(--desktop-ratio);
background: #bada55;
color: #444;
}
#media (min-width: 540px) {
.mobile {
display: none;
}
}
#media (max-width: 539px) {
.desktop {
display: none;
}
.block {
padding-bottom: var(--mobile-ratio);
}
}
<div class="container">
<div class="block" style="--desktop-ratio: 56.25%; --mobile-ratio: 200%;";>
My aspect ratio is set via custom properties. It is <span class="mobile">1:2</span><span class="desktop">16:9</span>.
</div>
</div>
It is (at least at the moment) apparently not possible to set a breakpoint using a variable. That is,
#media (min-width: var(--breakpoint)) { ... }
is apparently not understood by at least Firefox and Chrome at the moment.
Alternatively, building on Kodos Johnson's answer: if you have only a single breakpoint, you can make use of the padding-bottom and padding-top. One of the two defines the aspect ratio on small screens, and the other defines it on big screens. This removes the need to add a wrapper element. Here is an example, based on the one from Kodos' answer.
#media (min-width:540px) {
.block {
padding-top: 0 !important;
}
}
#media (max-width:539px) {
.block {
padding-bottom: 0 !important;
}
}
<div class="block" style="width: 50%;
padding-bottom: 25%;
padding-top: 100%;
background-color: red;">
</div>
i have a big page on html gen from php
and in want to create print view with html and css
i use print.css style to create this page
then have some question :
how to create A4 size page
how to create border for all pages and for per page
how to create fix size view in any browser ?
have php class for generate this ?
thanks for your answers :)
this may be help u out
<div id="printpage">
//blah blah
</div>
Print
function printdiv()
{
//your print div data
//alert(document.getElementById("printpage").innerHTML);
var newstr=document.getElementById("printpage").innerHTML;
var header='<header><div align="center"><h3 style="color:#EB5005"> Your HEader </h3></div><br></header><hr><br>'
var footer ="Your Footer";
//You can set height width over here
var popupWin = window.open('', '_blank', 'width=1100,height=600');
popupWin.document.open();
popupWin.document.write('<html> <body onload="window.print()">'+ newstr + '</html>' + footer);
popupWin.document.close();
return false;
}
The basis:
on your CSS print styles add a media query for printing and use the #page rule
#media print{
#page {
size: auto; /* auto is the initial value */
size: A4 portrait;
margin: 0; /* this affects the margin in the printer settings */
border: 1px solid red; /* set a border for all printed pages */
}
}
have php class for generate this ?
I dont think you really need extra libraries to do that. But, probably, you'll need some browser compatiblity... isn't?
EDIT
To support IE10+ you may want to add also this rule:
/* IE10+ CSS print styles */
#media all and (-ms-high-contrast: none), (-ms-high-contrast: active) {
/* IE10+ CSS print styles go here */
#page {
size: auto; /* auto is the initial value */
size: A4 portrait;
margin: 0; /* this affects the margin in the printer settings */
border: 1px solid red; /* set a border for all printed pages */
}
}
You should be able to manage yourself with only CSS to print a website content.
Basically I am createing a unknown size of checkboxs that is dependent on the row that is chosen from a table in my database. The reason I dont know the size is that the user chooses which row they will use with some rows containing what will become 10 checkboxs adn others containing as many as 75. So the problem is that if the user selects a row with a large amount of options it goes through the border of my div and then forces me to scroll the page down what I am looking for is a way to say >
if(number of checkboxs is >25 )
create a new column on my page
I dont know whether the right way to go about this is to use php or javascript or possibly do it using css I am new to all of these languages so any help no matter how trivial will greatly appreciated.
<div id="major1">
<?php
$courses=mysql_query("SELECT * FROM MAJORS_CHECKLIST WHERE MAJOR='$major'");
$courses_row=mysql_fetch_row($courses);
$count = 0;
echo "$courses_row[0] <br/>";
$checkit = 0;
$sidebyside = 0;
foreach($courses_row as $i=>$courses_row){
if($courses_row['$count'] == NULL)
{
break;//if we run out of courses stop printing them
}
if($courses_row[$count] == $courses_row[0] && $checkit == 0 )
{
$checkit = $checkit + 1;
}
else
{
echo "<input type='checkbox' value='$courses_row' name='majorCourses[]' /> ";//answer-$i
echo "$courses_row<br /> ";
}
$count = $count + 1;
/*$sidebyside++;
if($sidebyside == 2)//tried using this to put 2 checkboxes side by side that ened up just messing everything up
{
echo "<br/>";
$sidebyside = 0;
}*/
}
?>
here is my css:
#major1{
color: white;
/*border: 1px solid black;*/
padding: 5px;
float: left;
height:500px;
width:150px;
}
Producing a bunch of checkboxes in the div can be controlled using CSS. Should set the parent div of the checkboxes to the following rules: width:auto; height:auto; padding:10px 10px; position:relative; This is all assuming that the parent div of the checkboxes is a child of another div to control the preferred dimensions
edit: if you do not want to want to use css with the methods above, you can control the "X" amount of checkboxes per row. You can create a counter to count how many are being displayed and do a if($counter % X == 0) echo "</div><div>"; This is all assuming you have a starting div at the beginning of your code and an ending div at the end of the code.
I have this code which I am using to display random images. But the images show up at the top left corner of the site. I want to be able to position the image as I wish. What are the changes I have to do in the code order to the above mentioned.
Here's the code--
/*
* Name your images 1.jpg, 2.jpg etc.
*
* Add this line to your page where you want the images to * appear: <?php include"randomimage.php"; ?>
*/
// Change this to the total number of images in the folder
$total = "2";
// Change to the type of files to use eg. .jpg or .gif
$file_type = ".jpg";
// Change to the location of the folder containing the images
$image_folder = "sample.url.com";
// You do not need to edit below this line
$start = "1";
$random = mt_rand($start, $total);
$image_name = $random . $file_type;
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
?>
Thanks in advance
This would be a CSS solution, PHP can't position images. With CSS you can position things in many different ways:
Using margins (e.g., margin: top right bottom left;)
Using paddings (e.g., padding: top right bottom left;)
Using floats (e.g., float: right or left;)
Using positions (e.g., position: absolute or relative; and then using top/left and bottom/right to position).
For example, you can center your image to the middle of the page using margins. Add this to the top of your page:
<style type="text/css"> /*Initializing CSS code*/
img { margin: 0 auto; }
</style>
Or you can float the image to the far right of your page using a float, assuming the parent object has a width of 100%:
<style type="text/css"> /*Initializing CSS code*/
img { float: right; }
</style>
Or using an absolute position to position it at the bottom right:
<style type="text/css"> /*Initializing CSS code*/
img {
position: absolute;
right: 0px;
bottom: 0px;
}
</style>
You may want to read a CSS tutorial to learn the differences between all the positioning techniques and when to use them and where + little hacks, annoyances and incidents that come when you use each of them.
http://www.google.com/search?q=css+tutorial
You need to modify your html code.
In your case you need to change value of this string:
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
Like this:
echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\ style=\"Your css style goes here\"/>";
Please learn some of the basics before asking on Stack Overflow.
http://www.w3.org/Style/CSS/learning.en.html css guides.
While the CSS answers are good, not every project will need or want CSS. This alternative solution, given your code, works just fine within the PHP tags:
(I had to put a space after the first < or it wouldn't display properly on this page. Just remove that space from each line to make it work)
Align Right:
< right>
< img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
< /right>
Align Center:
< center>
< img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
< /center>
Align Left:
< left>
< img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";
< /left>
This is technically an HTML solution, but since it works within PHP tags you can use this to effectively position anything you like.
I'm trying to create an unordered list of <a>text1 text2 text3</a> elements, with a while loop. This list is then styled using #sidebar li a in my CSS.
My problem is that the text1, text2, text3 that is passed into each <a> element in my while loop can take on different lengths and I would like for them to be spaced equally like a table. However, I CANNOT use a table, because to format like a table, requires me to do this....
<li><a><tr><td>text1</td> <td>text2</td> <td>text3</td></tr></a></li>...
and because of that, my CSS "background" image will repeat for EACH <td>, when I only want the background image ONCE for each <tr>...(using different style tags than shown below)
Is there a way to change my while loop to space my text1,text2,text3 evenly like a table (without using a table) and maintain my CSS background image ONCE per each <li>? Any help would be INCREDIBLY appreciated!
My PHP file
while($row = mysql_fetch_assoc($result))
{
echo "<ul id=\"sidebar\">";
echo "<li>" . $row['column1'] . " ". $row['column2']. " ". $row['column 3']."</li></ul>";
}
My CSS file
#sidebar li a {
background: url(../images/sidebar.gif) no-repeat 0px 0px;
}
while($row = mysql_fetch_assoc($result))
{
echo "<ul id=\"sidebar\">;
echo "<li><span class="psuedo-col">" . $row['column1'] . "</span> <span class="psuedo-col">". $row['column2']. "</span> <span class="psuedo-col">". $row['column 3']."</span></li></ul>";
}
Add <span>s around the content from the $row['...'], in order that the css has something to serve as a hook, and set an explicit width on those spans. Bearing in mind that if the content of the spans is too large it will either require an overflow rule (hidden, visible or auto) or your content will start to look odd.
As an example
span.psuedo-col {
display: inline-block;
width: 10em;
overflow: hidden;
}
Or you could use
`display: block;
/* other stuff */
float: left; /* or right, depending on your alignment requirements */
The floats, obviously, will take the contents of the spans out of the flow of the document, perhaps causing the <li> itself to collapse, sine it'll have no content.
Asfar as I understand your question, you want your LI elements to have a fixed width like TD in a table:
#sidebar li {
float:left;
width:33%; /* three columns with equal width */
}