Trying to delete certain css elements from style - css

I am trying to delete the some elements from the html style="" attribute.
<div style="color: red; background: #000; position: fixed; top:0; left: 0;">Hey</div>
I want to cut this HTML so it will delete
position element, and background element.
Example:
<div style="color: red; top:0; left: 0;">Hey</div>
How can I do that on Regex?

A simple regex could be:
/background:.*?;|position:.*?;/
If you'd like to check only inside the style attribute in a tag, try the following PHP test code:
<?php
$str = '<div style="color: red; background: #000; position: fixed; top:0; left: 0;">Hey</div>';
echo preg_replace('/(<.*?style=.*?)background:.*?;|position:.*?;(.*?")/','$1$2',$str);
?>
You also asked to match the case when the attribute doesn't have a semicolon (unique attribute, last attribute). To match the case without semicolon, we should assume that is either the only attribute or is the last one. In both cases we'll have the following regexes working:
position:[^;]*?("|')
background:[^;]*?("|')
Basically, I'm asking to match the keyword position: or background: followed by any char except the semicolon, repeated zero or more times until a quote (single or double) is found.
This covers the case we called "without semicolon".
The following code should be working for all cases, it's not optimized and is only for clarity and example. It consist of a chain of calls:
$str = preg_replace('/(<.*?style.*?)(position:[^;]*?)("|\')/','$1$3',$str);
$str = preg_replace('/(<.*?style.*?)(background:[^;]*?)("|\')/','$1$3',$str);
$str = preg_replace('/(<.*?style.*?)(position:.*?;)(.*?")/','$1$3',$str);
$str = preg_replace('/(<.*?style.*?)(background:.*?;)(.*?")/','$1$3',$str);
echo $str;

try something like (untested though, so not quite sure wether the expressions are correct, though it should give you enough leadway to solve the rest of the problem)
<?php
$pattern = array();
$replacement = array();
$pattern[0] = '/position: [a-zA-Z]+;/';
$pattern[1] = '/background: #[a-zA-Z0-9]+;/';
$replacement[0] = '';
$replacement[1] = '';
$input_html = '<div style="color: red; background: #000; position: fixed; top:0; left: 0;">Hey</div>';
echo preg_replace($pattern, $replacement, $input_html);
?>

something like this should work:
$tag = '<div style="color: red; background: #000; position: fixed; top:0; left: 0;">Hey</div>';
$tag = preg_replace('/background:\s?#[0-9a-zA-Z]{3,6};\s?/', '', $tag);
that would only be for the background attribute, you would then do another for the color attribute.
$tag = preg_replace('/position:\s?[a-zA-Z];\s?/', '', $tag);

For the future, you could try to use Chirpy - VS Add In For Handling Js, Css, DotLess, and T4 Files or similar apps, try googling CSS combine/compress, they regroup all your repeated selectors and stuff.
Also I recommend to use a linked css file, that way you can organize your classes much better

Related

How to display specific tags on top of a page?

Hello to you dear developper community !
I am working on my blog, trying to sort things out. I am trying to find a way to display only some tags in labels, on top of specifics pages, so that my visitors can easily see what they are interested in.
I already use a widget in my sidebar for all the tags. The idea would be to display maybe 4 to 5 of them on top of my pages, according to the topic.
For instance, on "my travels", for the "France" section, I would like to display "Discovery", "Hiking", "Food", "Vidéos", "Photos".
What I would like to do, see in black labels
Thanks a lot for your help !!
Lily
.topLabel {
background: black;
color: white;
font-family: Arial;
padding: 4px;
float: left;
margin-right: 4px;
cursor: pointer;
}
.topLabel:hover {
background: grey;
}
<a class="topLabel">Hiking</a>
<a class="topLabel">Food</a>
<a class="topLabel">Photos</a>
<a class="topLabel">Videos</a>
<a class="topLabel">Discovery</a>
maybe this helps you.`
Access the post tags using get_the_tags()
$post_tags = get_the_tags();
if ($post_tags) {
foreach($post_tags as $tag) {
echo $tag->name . ' ';
}
}

getting title to automatically reduce font-size based on container size to fit on 1 line

Hiall,
I’m using wordpress and are displaying a main title header for feature article.
<span class="mytitle">
<?php the_title(); ?>
</span>
Now if the heading title is 30 characters in length it fits onto 1 line perfect, but if the length of the title is 32 characters then it spills onto 2 lines and I have an ugly looking headline because there is all this unwanted space.
Is there anyway to tell the title header in css to stay on 1 line and automatically reduce its own font size to fit in that one line etc???
I know I can use
css
white-space: nowrap;
to stop the line from wrapping to the next line, but what about the font-size, anyway for it to automatically reduce its size based on the container it is in or?
Any help would be great
In my opinion the best option is to set 2 css classes and a simple if statement for the title length:
$string = the_title();
$length = strlen( utf8_decode( $string ) );
if ($length > 30) {
echo '<span class="larger">' . the_title() . '</span>';
} else {
echo '<span class="smaller">' . the_title() . '</span>';
}
You can use a media query to change the size of the text based on screen size.
example css:
#media screen and (max-width:1024px) {
.mytitle{
font-size:28px;
}
}
you can put whatever pixel amount you want in the max-width: <-- what ever screen size you want to trigger this style at.
you can copy paste the same code and use it at a mobile width of like 480px and change the font size to 24px, so then it works are a bunch of different screen sizes.
You can achieve this with JavaScript in a number of ways. Here's a simple plug and play solution. Note that it requires your container to have a height:
function adjustHeights(elem) {
var fontstep = 2;
if ($(elem).height()>$(elem).parent().height() || $(elem).width()>$(elem).parent().width()) {
$(elem).css('font-size',(($(elem).css('font-size').substr(0,2)-fontstep)) + 'px').css('line-height',(($(elem).css('font-size').substr(0,2))) + 'px');
adjustHeights(elem);
}
}
adjustHeights('h1');
#container {
width: 300px;
height: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<h1>Some text that normally flows more than one line</h1>
</div>
Source: Meta Toad
function adjustHeights(elem) {
var fontstep = 2;
if ($(elem).height()>$(elem).parent().height() || $(elem).width()>$(elem).parent().width()) {
$(elem).css('font-size',(($(elem).css('font-size').substr(0,2)-fontstep)) + 'px').css('line-height',(($(elem).css('font-size').substr(0,2))) + 'px');
adjustHeights(elem);
}
}
adjustHeights('h1');
#container {
width: 300px;
height: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<h1>Some text that normally flows more than one line</h1>
</div>
List item

Text is going to outside of box (CSS Issue)

I'm getting link address from db and it's showing on a div box with html h3 tag which class name is .images. This css class images width is 210px. But unfortunately this link address is going to outside the box.. It's should be within the box. Could you tell me what is the wrong in my css code ?
Css code:
.images{
max-width:210px;
float:left;
position:relative;
margin:15px 30px 15px 0;
border:0px #000 solid;
}
.images h3 a{width:210px !important; height:auto;}
.images img{
margin:0;
padding:5px;
border:1px #ccc solid;
}
pho code:
while($res = mysql_fetch_array($iamges)){
echo "<div class='images'>";
$image = $res['image'];
$directory = "galary_images/";
$link = inputvalid($res['link']);
echo "<h3><a href='$link' target='_new'>$link</a></h3>";
if(empty($link))
{
echo "<img src='$directory/$image'>";
}
else
{
echo "<a href='$link' target='_new'><img src='$directory/$image'></a>";
}
echo "</div>";
}
Since the link has no spaces in between, they cannot be broken into seperate lines. you can use code like this :
$link = "long link";
<a href = "long link">echo (strlen()<=10)?$link:substr($link, 0 ,7)."...";
</a>
Say link is http://stackoverflow.com/questions/20198005/text-is-going-to-outside-of-box-css-issue,
it will display something like this : http://stackoverflow.com on the name of the link but will send you to the same place.
Have you tried setting an
overflow:hidden
in your css for .images?

css: how to display prev and next links outside slider

Just having a couple of css issues which havn't been able to figure out so I left it back to the default css.
Below is the application showing image sliders: Application
My question is how to get the Prev and Next links to be displayed on either side of the image outside the image slider rather than in the slider as it is overlapping over the images?
My other question is below the sliders it contains the slider numbers 1 2 3 so we know which image is first second and third in sliders and select them. My question is how to separate the numbers so they are not too close together?
Below is CSS for Prev and Next links:
ul.bjqs-controls{list-style:none;margin:0;padding:0;z-index:9999;}
ul.bjqs-controls.v-centered li a{position:absolute;}
ul.bjqs-controls.v-centered li.bjqs-next a{right:0;}
ul.bjqs-controls.v-centered li.bjqs-prev a{left:0;}
Below is css for the numbers bottom of images:
ol.bjqs-markers{list-style: none; padding: 0; margin: 0; width:100%;}
ol.bjqs-markers.h-centered{text-align: center;}
ol.bjqs-markers li{display:inline;}
ol.bjqs-markers li a{display:inline-block;}
HTML and jquery:
<div id="banner-image_<?php echo $key; ?>">
<ul class="bjqs">
<?php foreach ($arrImageFile[$key] as $i) { ?>
<li><img alt="<?php echo $i; ?>" height="200" width="200" src="<?php echo 'ImageFiles/'.$i; ?>"></li>
<?php } ?>
</ul>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#banner-image_<?php echo $key; ?>').bjqs({
animtype : 'slide',
height : 200,
width : 200,
responsive : true,
randomstart : false,
automatic : false
});
});
</script>
The answer to the second part of the question is to add padding to
ol.bjqs-markers li{display:inline;}
As shown here: http://jsfiddle.net/uYXkA/
I will answer the first part after the OP posts the HTML.
EDIT:
All right, it looks like jquery creates it's own css for your prev and next. We can get around this- just add this to your css:
ul.bjqs-controls.v-centered li.bjqs-prev a {
left: -40px;
}
And:
ul.bjqs-controls.v-centered li.bjqs-next a {
right: -40px;
}
Next you will likely want to shift the images as a whole. To do this just change the css for div.lt-container. If you add
margin: 0 auto;
width: 50%;
it will become centered.
It won't look very pretty, but it will work.

Spacing between divs in text?

Here is a little css problem
The code:
printf( '<div id="replyto">Reply to</div>
<a class="replytolink" href="%1$s">%2$s %3$s</a>',
$parent_link, $parent->comment_author, $parent->comment_date );
The css:
#replyto {
float: left;
}
.replytolink {
float: left;
}
is output
Reply to"Comment author Date"
How can I output it
Reply to "Comment author Date"
with correct spacing between text and link?
From a semantic point of view, using a floating div followed by an anchor isn't the best choice possible of tags in this case.
Personally, I would replace that div with a span, then you wouldn't need to float them, and so word spacing would work as usual..
Try this:
.replytolink {
padding-left:5px;
float: left;
}
Example: http://jsfiddle.net/RTFb7/
Add a padding-right to #replyto:
#replyto {
float: left;
padding-right:5px;
}
Add a space?
printf( '<div id="replyto">Reply to </div><a class="replytolink" href="%1$s">%2$s %3$s</a>', $parent_link, $parent->comment_author, $parent->comment_date );
Or add padding-right: 2em on #replyto.
Solution by Lucius:
printf( '<span>reply to %2$s %3$s</span>', $parent_link, $parent->comment_author, $parent->comment_date );
Neat. No CSS is required. Give the <span> tag an ID if needed. The extra blank space after "reply to " is now output.

Resources