Set a class on an image within a loop - css

I am not experienced with coding at all. I just copy/paste template examples that I find on the internet and that has worked fine so far. However, there is a new problem that I am unable to solve.
The short question is, how can I set the css class of an image outside the <"img">?
For example, this is the code that I have, and does not work:
$class_key= "top".(string)$count;
$returnarray[$level_key]='<img class=$class_key src=http://somepic.jpg width=100px height=100px/>';
If you want more details, I explain below:
I am layering images, and the position of the top images is randomly determined. I am using a css code to name and define the layers:
<style type="text/css">
.bottom
{position:absolute;
top: 0px;
left: 0px;
z-index: 1;
}
.top1
{
position:absolute;
top: 100px;
left: 100px;
z-index: 2;
}
.top2
{
position:absolute;
top: 100px;
left: 100px;
z-index: 2;
}
And the variable $count is randomly determined. If I explicitly write class="top1" or class="top2", the code works.
Please help me. Thanks

Assuming that you are using PHP...
You are using single quotes on $returnarray[$level_key]='<img class=$class_key.../>'; then $class_key is not interpreted.
You can use the following:
Single quotes concatenating the variable:
`$returnarray[$level_key]='<img class="' . $class_key . '".../>';`
or double quotes
`$returnarray[$level_key]="<img class=\"{$class_key}\".../>";`
or another way using double quotes
`$returnarray[$level_key]="<img class=\"$class_key\".../>";`
See similar: PHP: Using a variable inside a double quotes
To better manage your CSS:
Add a generic class to your $class_key
$class_key= "img-top top".(string)$count;
Then the CSS rules will work for any image that contains img-top class:
.img-top{
position:absolute;
top: 100px;
left: 100px;
z-index: 2;
}
Also you can wrap your loop content inside a div
<div class="img-top">
[loop logic]
</div>
And define the CSS for all images inside it:
.img-top img{
position:absolute;
top: 100px;
left: 100px;
z-index: 2;
}

Related

Cannot override a rule, even when using !important

I must be overseeing something or have some stupid error in my code, but I can't override Class CSS with the ID CSS AND ( blush ) even with adding the loathed !important hack ...
NB: This is a old(ish) WordPress template, still using clearing div for floats, but I have to work with it. Not my choice.
HTML:
<body id="page-front-page" class="page-template-homepage">
<ul class="menu">
<li>Home</li>
<li>About</li>
<li>Gardens</li>
</ul>
<div class="sidebar">
blah
</div>
CSS
#page-front-page > .sidebar {
padding: 0;
position: absolute;
top: 0;
right: 0 !important;
width: 45%;
}
few lines further is
.sidebar {
width: 280px;
padding: 50px 40px;
position: absolute;
top: 0;
left: 0;
font-size: 0.71em;
font-family: 'BrandonGrotesque-Light';
}
I've even tried swapping places of respective .sidebar CSS , but nothing helps. Simply cannot get right:0; to override left:0;.
Here is the inspector screenshot:
What am I missing, not seeing, being dumb about?
Thanks a lot !!!
You would use right 0 !important to override another right declaration. You're using it in an attempt to override another property. That's not how it works. There's no connection because they're two different properties.
If you want the opposite of left: 0, then try left: 100%... or, as #BoltClock mentioned in the comments, left: auto.
This has to do with "specifity": The overriding rule's selector has to be at least as specific as the other one, so you have to use this selector on the second rule:
#page-front-page > .sidebar {
(google for "CSS specifity" to learn about the exact importance of the different parts of a selector concerning specifity )
Addition: Yes, and as Michael_B and BoltClock wrote, you can't override a right parameter with a left parameter, but have to reset the first one to auto
Try:
#page-front-page > .sidebar {
padding: 0;
position: absolute;
top: 0;
right: 0 !important;
width: 45%;
left:inherit!important;
}

CSS: Using data attribute as content URL

So I have a div that allows me to display a QR code of the current page URL:
.page-qr:before {
content: url(https://chart.googleapis.com/chart?cht=qr&chs=100x100&chl=<?php echo current_page(); ?>?choe=UTF-8);
height: 100px;
width: 100px;
}
And used like:
<div class="page-qr"> </div>
Obviously, to get the current URL on-the-fly, I have to put this CSS styling in the <head> of my page. I am trying to move it to the stylesheet.
I had the idea to use a data attribute to specify the URL:
<div class="page-qr" data-url="https://chart.googleapis.com/chart?cht=qr&chs=100x100&chl=<?php echo current_page(); ?>?choe=UTF-8"> </div>
So, my question is, is it possible to double up the usage of content:url() and attr(data-url) in the stylesheet?
.page-qr:before {
content: url(attr(data-url)); /* Doesn't work, but you get the idea */
height: 100px;
width: 100px;
}
This is a proposed feature for attr() in css-values-3. The CSS would look like this:
.page-qr:before {
content: attr(data-url url);
height: 100px;
width: 100px;
}
Unfortunately there are no known implementations, so this still isn't possible.
I just stumbled upon the same problem, but found a solution using custom properties. So we can pass any type that is allowed as a custom property value to it. Then you can absolute position it if that is what you look for.
You can use it like this:
HTML
<div style="--img-url: url('${imgUrl}')"></div>
CSS
div {
position: relative;
}
div::before {
position: absolute;
top:0;
left:0;
width:100px;
height:100px;
content: var(--img-url);
}
See this post for further info

last-child and last-of-type not working in SASS

How would you write this to be SASS compliant?
.fader { display: inline-block; }
.fader img:last-child {
position: absolute;
top: 0;
left: 0;
display: none;
}​
Basically I'm just replicating this example of fading in one image over another (found here.)
His JFiddle example: http://jsfiddle.net/Xm2Be/3/
However his example is straight CSS, I'm working on a project in SASS and am not sure about how to correctly translate it.
My Code
Note in my example below, the img hover isn't working correctly (both images are showing up and no rollover fadein action happens)
My CodePen:
http://codepen.io/leongaban/pen/xnjso
I tried
.try-me img:last-child & .tryme img:last-of-type
But the : throws SASS compile errors, the code below works
.try-me img last-of-type {
position: absolute;
top: 0;
left: 0;
display: none;
}
However it spits out CSS which doesn't help me:
.container .home-content .try-me img last-of-type {
position: absolute;
top: 0;
left: 0;
display: none;
}
UPDATE: Working Codepen:
http://codepen.io/leongaban/pen/xnjso
Nesting is not a requirement with Sass. Don't feel obligated to do so if there's no need to break up the selectors.
.try-me img:last-of-type {
position: absolute;
top: 0;
left: 0;
display: none;
}
If you are applying styles to the image and then specific styles to the last-of-type, then this what it would look like when you nest it:
.try-me img {
// styles
&:last-of-type {
position: absolute;
top: 0;
left: 0;
display: none;
}
}
Neither of the above worked for me, so.
last-of-type only plays nice with elements, you can select things with classes all you like but this gets handled by the elements. So say you have the following tree:
<div class="top-level">
<div class="middle"></div>
<div class="middle"></div>
<div class="middle"></div>
<div class="somethingelse"></div>
</div>
To get to the last div with the class of middle, doesn't work using last-of-type.
My workaround was to simply change the type of element that somethingelse was
Hope it helps someone out, took me a while to figure that out.
Hey why don't you use only CSS? You could remove all the JS, I mean hover is support right back to ie6. I guessed that you know there is no hover event just active on tablets..
I mean you will need to set an area for the image.. But I find it use full, especially if you want an href.
http://codepen.io/Ne-Ne/pen/xlbck
Just my thoughts..

How to get `:after` to work in extjs iconCls?

My css for a tree node icon is the following:
.icon {
background: url(http://dummyimage.com/100x100/ccc/fff);
width: 100px;
height: 100px;
position: relative;
}
.icon:after {
background: url(http://dummyimage.com/32x32/f0f/fff);
width: 32px;
height: 32px;
display: block;
content: ' ';
position: absolute;
bottom: 0;
right: 0;
}
I set iconCls to "icon" but it does not work, I also tried "icon icon:after" and "icon:after" but with no luck.
I use a modern browser and my overlay css is valid, but extjs doesnot seem to understand it. How can I overcome this problem?
The icon element is by default an <img> element. It's contents are replaced by the image. You can't use :before or :after with it, because they form part of the contents that get replaced. You will need to override the treeRenderer in Ext.tree.Column to apply your second image.
looks like you forgot the class prefix in one of youre tests try .icon:after {}

How can I use css 'ids' in combination with server controls in ASP.NET?

My designer created a stylesheet that makes heavy uses of id's. Example:
<div id="globalheader">
<ul id="globalnav">....
css:
#globalheader { width: 715px; height: 100px; margin: 18px auto; position: absolute; top: 0; left: 20; z-index: 9998; }
#globalheader #globalnav { margin: 0; padding: 0; }
#globalheader #globalnav li { display: inline; }
This doesn't display correctly anymore as soon I change one of the div elements to 'runat=server' because this will cause the ClientID to change. How can I solve this?
-Edoode
This is a problem that I don't think is solvable without post generation workarounds, some of them being...
Add class attributes to the html elements and change the style declarations to .globalheader
Leave the html elements as they are and do a find and replace in the stylesheet (the new id names should have a consistent prefix e.g. #ctl00_globalheader)

Resources