My php page will generate a dynamic html.
The following is the output;
<label class="required">suject *</label>
Unfortunately i can't edit the html
Now I need to show '*' in red color.
How I can write the css for that?
There are several ways to do this:
If you have access to the PHP source code, then you can modify the output accordingly, perhaps wrap the * with <span> tags and style the accordingly.
Alternative, you can use JS to do the same:
$(document).ready(function() {
// Loop through all labels with required class
$('label.required').each(function() {
$(this).html($(this).html().replace(/\*/g, '<span class="asterisk">*</span>'));
});
});
And in your CSS, you can style it in any way you want:
.required .asterisk {
color: red;
}
See fiddle here - http://jsfiddle.net/teddyrised/JTesR/
Even better: Using CSS
This is written in light of trying to be as semantically correct as possible - you can remove the * character with JS (or changing the PHP code), but use a pseudo-class to add it back to the label element:
label.required:after {
content: '*';
color: red;
}
CSS allows you to select elements (and a limited number of pseudo-elements such as :first-line).
There is no way to select "last character", "characters matching *" or anything else that would make what you want achievable.
You have to modify the DOM. The easiest way to do that would be to modify the HTML it is generated from. The hacky approach would be to modify it with JavaScript after it has loaded.
This is based on Terry's answer: You can simply use :after to simulate effect, and place that content over generated HTML.
label.required:after {
content: '*';
color: red;
background:white;
position: relative;
left: -8px;
}
http://jsfiddle.net/x2KN7/
Its not possible through CSS, but you can do something with the javascript. If this content is generated on page load then you can hook page load event, get reference to this element and make changes.
<script type='text/javascript'>
document.onload = function (e) {
var col = document.getElementsByClassName("required");
for (var i=0;i<col.length;i++) {
if (col[i].innerHTML == "subject *") {
col[i].innerHTML = "subject <span style='color:red'>*</span>";
}
}
}
</script>
Try the above, hopefully it will solve your problem. I have used simple javascript, but if you know a javascript library (YUI, dojo, jquery) then you can do this in 1 or 2 line.
Related
The problem is simple:
Using tinyMCE, I am storing html data into the database. Then at the home page, I am showing 5 latest posts I've inserted into the database. I want to crop each of these posts to the specified length if they exceed it and put a continue reading link (can be a soft crop here, doesn't have to be rigid).
It would be easy to crop the string with php's wordwrap function but since the string is composed of html I don't wanna ruin the html code by cropping it from a wrong place.
So the question is: Is there an easy way to crop this, (can be a css, javascript solution as well) or do I have a write a long function with lots of checks to implement such a basic feature? I was thinking to use the DOM class but before creating function I just wanted to ask you guys.
EDIT
Styling is essential to me. So there is no possibility to strip the tags.
Thanks in advance.
One possible solution could be to strip tags, if the styling of this HTML is not vital, so you can cut wherever you want:
http://php.net/manual/es/function.strip-tags.php
Another solution for CSS3 browsers, would be to use text-overflow, read more about this:
http://davidwalsh.name/css-ellipsis
assuming you use jQuery
the javascript
$(function () {
$('p').filter(function () {
return $(this).height() > 50;
}).addClass('collapsed').css('position', 'relative').append('read more');
$('.expand').on('click', function(){
if($(this).parents('.collapsed').length)
$(this).parents('.collapsed:first').removeClass('collapsed')
else
$(this).parents(':first').addClass('collapsed')
})
})
the css
.collapsed{overflow: hidden; height: 20px; margin-bottom: 10px; position: relative}
.expand{position: absolute; right: 0; bottom: 0; background: #fff; padding: 0 0 0 15px;}
Just a proof of concept. Needs JS optimization
I am building the website www.verbum.xtrweb.com/verbum.php
I use the request function in php to get the definition of spanish words from the site: www.rae.es . The output i get is in an , but unfortunately, i cannot change the style of the elements it outputs. For example, if you look up the word "hola" you will get the meaning for "hola" but with www.rae.es 's stylesheet. What i want is to apply to this output my own style (color, font, font size, etc.)
I now there is something called $important! but according to the way i have previously tested, it did not work.
I evoke the magic aid of the stackoverflow community with urgence.
The content you want to change is located in an iframe. I would suggest that you just inject the stylesheet into the iframe direct, but unfortunately the content you want is on a different domain. Because of the cross-domain security policy, you will not be able to modify the content of the iframe directly.
There are two ways to solve this. You can use a PHP application to get the content you need, then you can filter it and change it in any way you would like. This would replace the iframe, and instead you would just display the content you need on the page. The other option is to use a more simple PHP application that gets the content, but doesn't modify it at all. Then you point your iframe to this PHP app, and it would display in the same way as it does now, BUT it will be from the same domain. Then you can inject a stylesheet with Javascript and make it look any way you want.
The "important!" CSS override might be needed if you go with the second method I mentioned, but it is not the root cause of your problem. If you want to read more about how to use important!, I recommend this article.
Below I have implemented an example of the first method I described. This will download the requested page, then inject a custom set of styles.
<?php
$url = 'http://lema.rae.es/drae/srv/search?id=IwxflJmT9DXX2DMkYs8Z';
$css = <<<EOT
<style type="text/css">
body
{
background: #eeeeee;
}
.a
{
color: green;
}
.f
{
font-size: 200%;
}
.o
{
font-size: 80%;
}
img
{
opacity: .5;
}
img:hover
{
opacity: 1;
}
</style>
EOT;
$data = file_get_contents($url);
$data = str_replace('</head>', $css.'</head>', $data);
echo $data;
?>
I know that it is usually considered bad practice to mix CSS with HTML and that CSS selectors (ids, classes, etc) should generally be used to style elements in external stylesheets. But this can be quite inconvenient when writing a dynamic page in PHP. It is much easier to output the element's style directly using PHP by writing to the element's style attribute. Would this be considered bad practice, even if using classes would make it even more difficult to manage from the developer's end?
For example, I have an element's color style store in a database and this element has a different color depending on the parameters provided in the dynamic PHP page. In this scenario, I could write to the css stylesheet with php some class with the color variable I get from the database
echo '.custom-color {';
echo 'color: ' . $colorFromDatabase . ';';
echo '}';
and then append the class to the element:
<div class="custom-color"></div>
or I could just echo the style directly to the element.
echo '<div style="color:' . $colorFromDatabase . ';" >';
Which is the better way to go?
Writing the style inline is usually considered bad, whether it's with PHP or by hand. It is simpler but harder to maintain and you don't get to cache your CSS content.
If you want the CSS to be more dynamic , you should experiment with http://sass-lang.com/
Note that custom-color is not a very good class name, it should be something like main-header, blog-post, last-modified since they tell you about the content not about its style.
SASS would let you define a variable for a color and reuse that within your CSS files, you could then just change the variable names and regenerate your CSS files
The advantage of what you're doing is that you don't have to learn new things. PHP will get it done. You need to write something into your build if you want to use SASS with content from a database and come up with a scheme for which CSS to use.
Note that there are a few cases where it would make sense to write the attribute directly, but it's hard to explain when exactly. It's usually when it's only going to be used once.
Example from their page
// Variable Definitions
$page-width: 800px;
$sidebar-width: 200px;
$primary-color: #eeeeee;
// Global Attributes
body {
font: {
family: sans-serif;
size: 30em;
weight: bold;
}
}
// Scoped Styles
#contents {
width: $page-width;
#sidebar {
float: right;
width: $sidebar-width;
}
#main {
width: $page-width - $sidebar-width;
background: $primary-color;
h2 { color: blue; }
}
}
#footer {
height: 200px;
}
Why not both? Separated CSS from HTML and PHP in your CSS file?
You might also check out CSS-preprocessors. LESS is the most common one, but there's also SASS, Stylus and whatnot.
For the best semantic use, I would use this:
div.colored {
color: attr(data-color)
}
Then use PHP like:
<div class="colored" data-color="<?= $color ?>"></div>
And you could always back this up with some simple Javascript.
i am making a website in php
i make left menu like this
these menu coming from database in one string.
i am printing it with echo.
i use image as a background to each menu.
now i want like this
i have a arrow image.
i know i can do it with z-index. but i cant do it with only css.
so i need a help to do it with javascript.
i want to change the html using javascript or jquery
For an all CSS solution try to building your menu like this...
<style type="text/css">
.menu li {
background:url(path/to/gradient.png) top left repeat-x;
}
.menu li a {
display:block;
padding:2px 5px;
backround:url(path/to/arrow.png) bottom right no-repeat;
}
</style>
<ul class="menu">
<li>Fan Club</li>
<li>Blog</li>
<li>Poll</li>
</ul>
If you must do your solution in JavaScript (which I suggest you avoid) you can access the z-index property of any element (that supports it) like so:
// DOM Scripting Example (Single Element)
myElement.style.zIndex = 1000;
// jQuery Example (All elements with the "arrow" class)
$('.arrow').css('z-index', 1000);
Using jquery:
$(function() {
$('.myselectorclass').css('z-index','1000');
});
Replace '1000' with your desired z-index value, of course
I generate a div for my javascript automated validation script. It is generated with below code:
var alertbox = document.createElement("div");
Now how do i center that div programatically both horizontally and vertically?
Note: Currently I am showing an alert box but once i know how to center a div, i will
replace the alertbox with this dynamically generated div which will also provide some
lightbox-like effect.
Thanks
Note: For reference, you can download latest version of this script here.
Please note that i dont want to use external css for this because this is validation script and it should be able to do it programatically. For example:
using something like this can be helpful:
alertbox.style.position: whatever
....
It's a matter of applying the same CSS rules as you would have with static content. Horizontal centering can be achieved as described in this article - basically you give the div a fixed with and set left and right margins to auto. Vertical centering is a bit trickier - a few approaches are discussed here and detailed in this this blog post.
The tidiest way is probably to define a CSS class that takes care of the centering and then apply that class to the dynamically generated element like this:
alertbox.className = "myCssClass";
Update:
Since you are already using JavaScript for creating the div, you could of course use it for the centering as well (in combination with CSS absolute positioning) - that would actually probably be a cleaner solution (due to the hackishness of the CSS vertical centering). Exactly how you do this depends a bit on what tools you are using - it's probably much easier to achieve with a JS framework such as Prototype or jQuery than with "raw" JavaScript since browsers handle window/browser heights a bit differently.
If you are using jQuery, why not use the validation plugin?
You should be able to combine it with a modal window (like SimpleModal).
But if you don't want to change what you have already done, try something like this:
I would just apply some CSS rules to the div to position it (I've included an overlay which covers up the page and puts the alertbox on top):
Note: The reason the div is positioned to the far left is because you need to get the dimensions of the div with the contents inside. A hidden div will have a height and width of zero. Once the size is determined, it calculates the center of the page and positions the div.
CSS
#overlay {
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
background: #000;
opacity: 0.8;
filter: alpha(opacity=80);
z-index: 100;
}
#alertbox {
background: #444;
padding: 10px;
position: absolute;
left: -99999px;
top: 0;
z-index: 101;
}
Script
function alertBox(alertMsg){
// Add overlay
$('<div id="overlay"></div>')
.hide()
.appendTo('body')
.fadeIn('slow');
// Add alert
$('<div id="alertbox"></div>')
.html(alertMsg)
.appendTo('body');
// calculate & position alertbox in center of viewport
var abx = $('#alertbox');
var abxTop = ($(window).height() - abx.height())/2;
var abxLft = ($(window).width() - abx.width())/2;
abx
.hide()
.css({ top: abxTop, left: abxLft })
.fadeIn('slow');
// add click to hide alertbox & overlay
$('#overlay, #alertbox').click(function(){
$('#overlay, #alertbox').fadeOut('slow',function(){
$('#alertbox').remove();
$('#overlay').remove();
});
})
}
For whatever reason, I have not been able to center a DIV using margin-right and margin-left. What I have found works better is to encapsulate them within a table (it's a bit globby code, but it works for me). And you can use the DOM style object to modify the margin as follows:
<table style="margin-left:auto;margin-right:auto;"><tr><td><div id="yourdiv"></div></td></tr></table>