I'm always using this method to display relevant css for the user, but I'd like to know if there was a better way to achieve this.
<?php
//MySQL request
$data = $request->fetch();
?>
<div
class="if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
if($_SESSION['id'] == $data['id']) {
echo "divLoggedInUser";
}
else {
echo "divLoggedInUser";
}
} else{
echo "divNotLoggedIn";
}"
id="<?php echo $data['id']; ?>">
<!--class is used for relavant css, id for javascript element selector -->
</div>
This is not such a big problem in this example, but it becomes a mess when you have a lot of code.
I'd like to know if there's a equivalent way to do this?
Something in css like this would be so greatfull:
.div::loggedIn
.div::notLoggedIn
There isn't, either way you have to generate some code which will allow you to determine in CSS whether you style an element for logged in user or not.
If you haven't done that already, put your code in function for a better read:
<?php
function getCssUserClass() {
if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
if($_SESSION['id'] == $data['id']) {
return "loggedIn";
} else {
return "notLoggedIn";
}
} else {
return "loggedIn";
}
}
?>
<div class="<?= getCssUserClass() ?>"><span class="username">Frank</span></div>
Then you can style your div and all elements inside it accordingly:
.loggedIn {
}
.loggedIn span.username {
color: #FF0000;
}
.notLoggedIn {
}
notLoggedIn span.username {
color: #999;
}
Why not present a different CSS file based on if the user is logged in and then assign generic classes to the divs that should be hidden or not.
Create 2 CSS files, loggedinusers.css and notloggedinusers.css. For your loggedinusers.css
div.showToLoggedInUser {
display:block;
}
and in your notoggedinusers.css
div.showToLoggedInUser {
display:none;
}
Then user your logged in logic to add the appropriate CSS file
<?php
$css_file = "notloggedinuser.css";
if(isset($_SESSION['id']) && !empty($_SESSION['id'])){
if($_SESSION['id'] == $data['id']) {
$css_file = "loggedinuser.css";
}
?>
<link rel="stylesheet" href="<?PHP echo $css_file; ?> />
You could set the loggedIn/notLoggedIn class on the body element, then add CSS rules that need to reference that setting like this:
div.anyOtherClass {
/* default styling */
}
body.loggedIn div.anyOtherClass{
/* logged-in styling */
}
body.loggedIn div.anyOtherClass{
/* logged-out styling */
}
Related
I've been searching for this for like 5 or 6 days now and nothing seems to be working...
I am trying to use the code below to hide the div "" only on woocommerce variable products... but I can't seem to get it to work.
add_action( 'show_hide_product_variable_price', 8 );
function show_hide_product_variable_price() {
global $product;
if( $product->has_child() ) {
?>
<style>
.summary-price-box {
display: none;
}
</style>
<?php
}
}
I have also tried using this line in the above code and still nothing...
if( $product->is_type('variable') ) {
So I actually solved this earlier today.
You can either achieve this using CSS like this:
.product-type-variable .summary-price-box {
display: none !important;
}
or you can achieve this using Javascript like this:
add_filter('woocommerce_get_price_html', 'lw_hide_variation_price', 10, 2);
function lw_hide_variation_price( $v_price, $v_product ) {
$v_product_types = array( 'variable');
if ( in_array ( $v_product->product_type, $v_product_types ) && !(is_shop()) ) {
return '';
}
// return regular price
return $v_price;
}
the css with hide the full "summary-price-box" and the javascript will hide only the "price range" or "From: $X.XX"
How can I hide the symbol "#" and colon ":" of order time in the WooCommerce Admin Add New order page? I've inspected the elements in Chrome to find a class or ID but not found. I know I can hide it with this code.
add_action('admin_head', 'my_custom_style');
function my_custom_style() {
echo '<style>
div {display: none;}
</style>';
}
enter image description here
I don't see a purpose of doing this, but if you really wish to do this, you can use input name css selectors to do it.
I have used CSS and JS both to get rid of # and :.
add_action('admin_footer', 'bks_my_custom_style');
function bks_my_custom_style() {
echo '
<script>
jQuery(function() {
var text = jQuery(".order_data_column_container .form-field").contents().filter(function() {
return this.nodeType == Node.TEXT_NODE;
}).remove();
});
</script>
<style>
input[name="order_date_minute"],
input[name="order_date_hour"]
{
display: none;
}
</style>
';
}
The code goes inside functions.php file.
Tested and WORKS.
Screenshot :
Is there a selector that specifies CSS to only applied when matching a specific URL or part of URL?
For example, here is my CSS stylesheet:
p {
color: green;
}
url("home.html") {
color: blue;
}
url("about.html") {
color: yellow;
}
path("/path/index*") {
color: indigo;
}
When the user visits home.html I want the home.html selector to be applied. When I'm on the about.html URL I want the about.html to be applied.
CSS media queries allow you to switch to a different set of styles when the width of the view changes. It also lets you specify a different set of styles when the user is going to view on the screen or send it to a printer.
My question again is, "Is it possible to specify a different set of styles depending on the URL or values in the URL." So it's not a question of how to do what I'm asking but if it's possible to.
I am using a CMS and it has a theme that allows you to add your own CSS. There is one stylesheet. That's it. Not two but one.
And I have one page that has specific CSS to that page and only that page. That is the origin of this question. There may be a thousand workarounds but my question is not about the workarounds.
However since this has been answered I do not mind workaround answers related to the question.
It looks like the #document rule was proposed for just this case but it was removed from CSS3 spec and planned for CSS4. From my tests it does not appear to be supported and it's not listed on caniuse at the time of this posting.
The syntax is as follows:
#document url("http://www.example.com/widgets/") {
body {
color: white;
background: tomato;
}
}
/* The above applies styles only to the page at the given URL */
#document url-prefix("http://www.example.com/widgets/") {
/*
Styles written here are applied to all URLs that
begin with 'http://www.example.com/widgets/'
*/
}
#document regexp("https:.*") {
/* Styles written here are applied to all URLs that begin with 'https:' */
}
Test code using #media query for comparison:
var styleTag = document.createElement ("style");
document.head.appendChild (styleTag);
var sheet = styleTag.sheet;
sheet.insertRule ("#media (min-width:600px) { html {color:red}}", 0);
console.log(document.styleSheets.length);
Results:
// no errors, stylesheet is added
Test code testing #document rule:
var styleTag = document.createElement ("style");
document.head.appendChild (styleTag);
var sheet = styleTag.sheet;
sheet.insertRule ("#document url('http://www.google.com') { html {color:red}}", 0);
Results:
/*
Exception: SyntaxError: An invalid or illegal string was specified
#Scratchpad/3:4:0
*/
TIL about #document thanks to #BoltClock
More info
You could attach a data-url custom attribute to an element's parent tag, either in the HTML or using JavaScript, and then query that data's value in CSS. Here's a working example:
const urlHolder = document.getElementById("url-holder");
document.getElementById("changer").onclick = () => {
urlHolder.dataset.url = "https://mywebsite.com/about"
};
#url-holder[data-url*="blog"] .url-based{
background-color: red;
}
#url-holder[data-url*="blog"] .url-based::after{
content: "You're on the blog page!"
}
.url-based::after{
content: "You're not on the blog page."
}
.box{
width: 200px;
height: 200px;
border: 2px solid black;
margin: 5px;
}
<button id="changer" type="button">Change data-url to "https://mywebsite.com/about"</button>
<div id="url-holder" data-url="https://mywebsite.com/blog">
<div class="box url-based"></div>
<div class="box not-url-based"></div>
</div>
No idea how performant such a solution would be though.
To be sad there is no pseudo classes to select element's based on URL.The only way you can do it is by adding class to the body tag or specific element and then override the CSS.
if just for only HTML, use jQuery
<script>
var currentLocation = window.location.pathname;
if (currentLocation == 'home.html'){
$('head').append('<link href="home-style.css" rel="stylesheet">');
} else if (currentLocation == 'about.html'){
$('head').append('<link href="about-style.css" rel="stylesheet">');
} else {
$('head').append('<link href="index-style.css" rel="stylesheet">');
}
</script>
If use you WordPress:
Add your theme function.php
function site_stil_script() {
if ($_SERVER['REQUEST_URI']=='/YOURPAGE/') {
wp_enqueue_style( 'theme_css', get_template_directory_uri() . '/assets/css/theme-difrent-style.css', array(), '20120208', 'all' );
} else {
wp_enqueue_style( 'theme_css', get_template_directory_uri() . '/assets/css/theme-style.css', array(), '20120208', 'all' );
}
//other lines....
}
add_action( 'wp_enqueue_scripts', 'site_stil_script' );
I am trying to create a sass map to apply an icon class to a nested element.
My elements are a div (which will have the class applied) and a span inside that "holds" the icon:
<div class="pboi-tour_adventure">
<span></span>
</div>
In actuality, it's part of an AngularJS ng-for, if that matters:
<div class="icon-sample" ng-repeat="icon in maps.icons">
<div class="{{icon}}">
<span></span>
</div>
</div>
So, following the guidance in this article, I am using a sass map and an each loop:
$poi-icons: (
tour_adventure: "\e96c",
tour_beach: "\e96d",
tour_tourist_attractions: "\e96e",
tour_tourist_infomation: "\e96f"
);
#each $name, $icon in $poi-icons {
.pboi-#{$name} {
span::before {
content: $icon;
}
}
}
My issue is that when I inspect the html, there is clearly no "before" pseudo element in the inspector, and the compiled css is
.pboi-tour_adventure span::before { content: ""; }
When I expect it to be:
.pboi-tour_adventure span::before { content: "\e96c"; }
Of course, the icon is not working because of this.
NOTE: Even if I flatten it out to match the example in the article:
#each $name, $icon in $poi-icons {
.pboi-#{$name}::before {
content: $icon;
}
}
The same thing happens.
Simplified case:
I put this into Sassmeister:
$foo: 'bar';
$icons: (
busi_ATM: 'foo',
busi_bank: $foo,
busi_business: '\e902'
);
#each $name, $icon in $icons {
.pboi-#{$name}:before {
content: $icon;
}
}
and I get
.pboi-busi_ATM:before {
content: "foo";
}
.pboi-busi_bank:before {
content: "bar";
}
.pboi-busi_business:before {
content: "";
}
Why does Sass pass through the first 2 strings but not '\e902'?
Also, and more confusing, is when I use the loop the :before shows up in the compiled CSS file, but not in the Web Inspector in the browser. This I don't get at all.
Sass converts the characters internally to the exact ligature/symbol/icon. That's the reason you are seeing a ""; in the inspector. It doesn't mean Sass is not able to parse the character. It means that those characters are not readable in the developer tools.
If for some reason you are seeing the same box in the output, then you are not setting the font properly, or your font is still trying to load. You can check the computed glyph in dev tools to verify what font is loaded.
If you still want to see the actual Unicode, then you can do a simple test and verify it. you can pass only 'e902' and append the '\' in front of it. I have attached the snippet for it
#charset 'UTF-8';
$poi-icons: (
tour_adventure: "e96c",
tour_beach: "e96d",
tour_tourist_attractions: "e96e",
tour_tourist_infomation: "e96f"
);
#each $name, $icon in $poi-icons {
.pboi-#{$name} {
span::before {
content: unquote("\"")+unquote(str-insert($icon,"\\", 1))+unquote("\"");
}
}
}
reference sass issue:
https://github.com/sass/sass/issues/1395
I have some tags.
<a class="aclass" href="~/product/explain?id=1">item1</a>
<a class="aclass" href="~/product/explain?id=2">item2</a>
<a class="aclass" href="~/product/explain?id=3">item3</a>
I want that when a user clicks on any tag, its color will change to red.
I wrote below the code. But when page posts, tag will lose its changed color(red).
//$('.aclass').click( function (e) {
$('.aclass').live("click", function (e) {
$('.aclass').css("color", "#47b2ed");
$(this).css("color", "red");
});
style.css:
.aclass {
color: #47b2ed;
text-decoration: none;
}
<script type="text/javascript">
//$('.aclass').click( function (e) {
$('.aclass').aclass.live("click", function (e) {
$('.aclass').aclass.css("color", "#47b2ed");
$(this).aclass.css("color", "red");
});
</script>0;}
How abt this one :-
a.aclass:link {color:#FF0000;} /* unvisited link */
a.aclass:visited {color:#00FF00;} /* visited link */
a.aclass:hover {color:#FF00FF;} /* mouse over link */
a.aclass:active {color:#0000FF;} /* selected link */
DEMO
Yes of course it does not so. On the server side there is no action taken to present the links in different colors. It is only done on the client (your browser).
You are sending the information, which link is clicked to the server (via the href). On the server side you need to use a programming framework such as PHP to send different HTML code back to the client in regard of the clicked links information
Example with PHP
<?php
$id = intval($_GET['id']);
?>
<a class="aclass<?php print $id == 1 ? ' active' : ''?>" href="~/product/explain?id=1"> item1</a>
<a class="aclass<?php print $id == 2 ? ' active' : ''?>" href="~/product/explain?id=2"> item2</a>
<a class="aclass<?php print $id == 3 ? ' active' : ''?>" href="~/product/explain?id=3"> item3</a>
And add the color red to the CSS
aclass.active {
color:red;
}