Defining CSS colour through php - php

I need a way of defining a css highlight colour as a php value and then using that as a class in my stylesheets.
I know that someone people do this as inline styles but I just have far too many elements using the class to put it inline. Ideally I need a way to echo the variable in the stylesheet (SASS?)
I can't find this anywhere, which seems odd!

At the moment you have 2 viable options.
Using Javascript
The first option is using some javascript inside your HTML file, in a script tag.
Define a variable with PHP and use it to target the desired elements.
var myVariable = "dodgerblue";
$(".el").css("background-color", myVariable);
body {
counter-reset: example;
display: flex;
justify-content: space-around;
height: 100vh;
margin: 0;
}
div {
flex: 0 0 10%;
counter-increment: example;
}
div::after {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
content: counter(example);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="el"></div>
<div class="el"></div>
<div class="el"></div>
<div class="el"></div>
<div class="el"></div>
Another option is like #cale_b suggests.
Using an internal stylesheet.
In your output (ideally in the section of your page), at some
point after your stylesheet is referenced, cause PHP to do something
like this: echo <style>.highlight_color {color: #ffff00;}</style>.
Using CSS is not possible at the moment.
Sadly there is not a CSS solution for this, yet.
Hopefully some day browsers will support the use of attr() CSS function in all property values, not just content.
div {
background-color: attr(data-myvariable);
}
div::after {
content: attr(data-myvariable);
}
<div data-myvariable="red">
Example
</div>

Related

Adjust position of DIV beneath fixed header PHP

I have a header that uses position:fixed to stay at the top of the page, it starts as 50px high, however, if a larger/smaller logo is added it's height changes meaning it can then overlap the DIV container beneath it. Is there a way to move the DIV container based on the height of the header. This is the CSS I am using;
<style>
#header {
position:fixed;
top:0px;
height:50px;
width:100%;
}
.container {
margin-top: 50px;
width: 100%;
height: 250px;
}
</style>
This is the HTML I am using, its really quite basic.
<div id="header">
... Menu ...
</div>
<div class="container">
... Content ...
</div>
I have looked into using javascript but I understand this is client side so would not help. Is there a way around this?
You can use jquery to read the height of the container and then use this to set the value for margin-top of container .
Try this code:
$(document).ready(function(){
var h=$("#header").css("height");
$(".container").css("margin-top",h);
});

Can :hover apply to generic element tags specific to a parent div?

I am using a bunch of divs (created with PHP) to generate a block of clickable elements. What I need to do is apply some styles to these generic elements, rather than to specific ones, yet using the code below seems to be invalid.
#Container {
height: 80%;
width: 60%;
background-color: green;
}
#Container div:hover {
background-color: blue;
}
<div id="Container">
<div style="background-color: red; width: 100px; height: 100px;">
</div>
http://jsfiddle.net/XD2eZ/
So I am not sure if it is an issue that a generic div element cannot be styled as a sub-element AND have a :hover attribute that operates properly. I know that classes or id's can be specified to handle this, but have thousands of unique divs. I also cannot use
#Container:hover div{ background-color: blue;}
As it ALSO seems to be invalid, but I need to select the one element from a block, and not all at once.
Any ideas here? Thanks in advance.
This will work if you remove the background color from the HTML, and apply it using css:
#Container {
height: 80%;
width: 60%;
background-color: green;
}
#Container div {background-color: red;}
#Container div:hover {
background-color: blue;
}
Example: http://jsfiddle.net/XD2eZ/1/
The reasone is CSS Specificity - a style attribute rule is much more specific (stronger) than an ID + element rule.

How to layout these elements via HTML/CSS circumventing DOMPDF's lack of float?

The image below explains what I am trying to achieve.
I need to show a user's car picture with the name under it. Each image/name pair should be in a DIV so as a user adds more cars, they move to the next line or page. Ideally the DIVs should be centered, more as a matter of aesthetics.
Using DOMPDF, I am generating a PDF from an HTML layout.
Unfortunately, DOMPDF's support for float is bad, even in the new 0.6.2 beta. I wonder if this layout I am proposing could be done without float. DOMPDF also does not support unordered lists.
I have tried some solutions using tables, but this also isn't good since DOMPDF does not allow cells to spill over to the next page.
I am using PHP 5/ codeigniter, and DOMPDF 0.5.2 (stable).
Any suggestions on how to get this layout are greatly appreciated!
Here is the code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
img {width: 150px; height: 150px;}
h1 {font-size: 3em; text-align: center;}
h2 {text-transform: uppercase; width: 150px; text-align: center;}
div {margin: 10px; width: 150px;}
</style>
</head>
<h1>My Cars</h1>
<?php foreach ($cars as $row): ?>
<div>
<img src="<?php echo $row->cars_picture; ?>" />
<h2><?php echo $row->cars_name; ?></h2>
</div>
<?php endforeach; ?>
</html>
Thanks to #rkw and #manyxcxi for helping out.
At the end the only way of doing this without hacks was to use mpdf instead of DOMPDF.
I have the impression mpdf is a much better library, with better documentation. It has partial support for float, but it works very nicely and does exactly what I needed above.
If the boxes are all fixed width and you know the width of your PDF, then you can calculate the boxes per row and use a spacer div on the left of the bottom row to give you the offset you're looking for.
Without using float, you would have to use instead of : http://jsfiddle.net/AxZam/40/
relevant css:
body {
width:800px;
}
#content {
margin: 0px auto;
width: 600px;
text-align: center;
}
img {
width: 150px;
height: 150px;
}
h1 {
font-size: 3em;
}
.cars {
text-transform: uppercase;
width:150px;
display:block;
position:absolute;
top:0px; left:0px; }
span {
margin: 10px;
position: relative;
}
relevant html section:
<div id='content'>
<h1>My Cars</h1>
<span>
<img />
<span class='cars'>car</span>
</span>
...
</div>

A div 'infront' of a div?

I'm trying to code a design I've just made: http://www.richardhedges.co.uk/brlan/design.jpg
I'm pretty much done coding but the only thing I don't know how to do is the footer overlapping the main content. What I'd like it to do is scroll the content. (Like it is on Facebook messages)
The footer is simply a div with nothing in it:
<div class="footer"></div>
And here's the stylesheet:
div.footer {
width: 980px;
height: 114px;
float: left;
background-image: url(../images/footer.png);
background-repeat: no-repeat;
margin-bottom: 20px;
}
I need to create a new div which I'll include the content in (as shown in the design.JPG) however it must be 'behind' the PNG image in the footer. I've absolutely no idea how I'd do this - My apologies for ignorance.
div#footer {
position: absolute;
bottom: 0px;
z-index: 9001;
}
div#content {
position: relative;
}
Use absolute positioning and a higher z-index on the footer div than on the ones it'll be in front of.

change css style using javascript

hi
in my php application i want to change the style using javascript
<div id="middle-cnt" class="ad-image-wrapper"><div class="ad-image" style="width: 1000px; height: 450px; top: 0px; left: 131px;"><img height="450" width="1000" src="images/Gallery2/4.jpg"><p class="ad-image-description" style="width: 1000px; bottom: 0px;"><table width="100%"><tbody><tr><td class="image-desc">Stevie III – 36" long x 24" wide - Oil on Canvas<div style="color: rgb(187, 167, 0); padding-left: 10px; position: absolute; top: 0pt; left: 450px; font-size: 35px; letter-spacing: 6px;">SOLD</div></td></tr></tbody></table></p></div></div>
In the baove code i want to change the div style left:0px ie <div id="middle-cnt" class="ad-image-wrapper" ><div class="ad-image" style="width: 1000px; height: 450px; top: 0px; left: 0px;"><img ..
I am using the script below.
function fnshowgallery(){
var elm=document.getElementById('middle-cnt').childNodes;
var divElm=elm[0];
divElm.style.Left='0px';
}
But it's not get the desired result.
Does any one know this?
But i don't know how.
document.getElementById("middle-cnt").childNodes[0].style.left = "0px";
This should work, if it doesn't, please double-check your DOM structure.
The element needs a position-property too, if you want the left-property to have any effect.
javascript is a case sensitive language. and it's camel case. So it's not Left but **left. It has to be:
divElm.style.left='0px';
Rather than manipulate the CSS directly with JavaScript it's usually a better approach to only use JavaScript to set the CSS class name and leave all your CSS in your .css files. For example:
/* CSS */
.foo {
height:450px;
left:0;
position:relative; /* or maybe absolute? */
top:0;
width:1000px;
}
Then change your JavaScript to something like:
var elm = document.getElementById('middle-cnt').childNodes[0];
if (elm) {
var cssClass = (elm.className) ? ' foo' : 'foo';
elm.className += cssClass;
}
Then, if you need to make any more changes to your CSS you shouldn't need to touch the Javascript.
But for the left property to work in your CSS you'll need to set the position property to either relative or absolute.
If I evaluate that javascript in my console with that HTML snippet, it works; the "left" styling is changed to 0px.
But, I think you need to have the parent (#middle-cnt) to have position:relative, and your div (.ad-image) with position:absolute. Maybe you have this in your stylesheet...
Might help if you say what browser(s) you have tested with, as well.

Categories