Inspect CSS :before elements in the web inspector - html

Is there any way to inspect elements that were added via the CSS :before selector, in the Chrome or FF web inspector and inspect their calculated styles (and manipulate them on the fly)?
Setup is
li:before {
display: block;
width: 50px;
height: 50px;
position: absolute;
}
So it's real block element without any HTML, but on inspection he only selects the related LI element. The "force element state"-option does not work either, it's only for interaction states like :hover but not :before

Yes, there is. Inspect the element itself (not the pseudo), the pseudo element's styles should show up below, before the inherited styles.

Pseudo elements now show in Chrome Inspector (as of Chrome 31). See the pseudo element in Chrome's built in inspector. You also can edit the css properties as you'd expect.

Related

how to make pseudo element visible and clickable using css on firefox

I have a button followed by a pseudo element. The button displays 'next' and the pseudo element displays '>'. This is used for pagination.
I have hidden the button but made the pseudo element visible by using css properties
button{
visibility: hidden;
}
button::pseudoElement{
visibility: visible;
}
Now the button is hidden and element is visible. It is also clickable. It works in chrome,safari and ie. But it does not click on firefox. What do I change?
EDIT
This hack worked
button{
color: transparent;
}
button::pseudoElement{
color: black;
}
Any better approach?
Pseudo elements aren't actually inserted before or after the element itself. They are inserted as the first/last child element. So if you hide the "parent" element, their ::before/::after pseudo elements will be hidden as well.
In your case, I would just do the old text-indent: -9999px or font-size: 0 trick on the parent element and reset the pseudo element (text-indent: 0).
With your color: transparent solution, the element will retain its size. With the text-indent trick it's just using the space the pseudo element needs.

Undo hover effect of parent when hovering over child [duplicate]

I know that there does not exist a CSS parent selector, but is it possible to style a parenting element when hovering a child element without such a selector?
To give an example: consider a delete button that when hovered will highlight the element that is about to become deleted:
<div>
<p>Lorem ipsum ...</p>
<button>Delete</button>
</div>
By means of pure CSS, how to change the background color of this section when the mouse is over the button?
I know it is an old question, but I just managed to do so without a pseudo child (but a pseudo wrapper).
If you set the parent to be with no pointer-events, and then a child div with pointer-events set to auto, it works:)
Note that <img> tag (for example) doesn't do the trick.
Also remember to set pointer-events to auto for other children which have their own event listener, or otherwise they will lose their click functionality.
div.parent {
pointer-events: none;
}
div.child {
pointer-events: auto;
}
div.parent:hover {
background: yellow;
}
<div class="parent">
parent - you can hover over here and it won't trigger
<div class="child">hover over the child instead!</div>
</div>
Edit:
As Shadow Wizard kindly noted: it's worth to mention this won't work for IE10 and below. (Old versions of FF and Chrome too, see here)
Well, this question is asked many times before, and the short typical answer is: It cannot be done by pure CSS. It's in the name: Cascading Style Sheets only supports styling in cascading direction, not up.
But in most circumstances where this effect is wished, like in the given example, there still is the possibility to use these cascading characteristics to reach the desired effect. Consider this pseudo markup:
<parent>
<sibling></sibling>
<child></child>
</parent>
The trick is to give the sibling the same size and position as the parent and to style the sibling instead of the parent. This will look like the parent is styled!
Now, how to style the sibling?
When the child is hovered, the parent is too, but the sibling is not. The same goes for the sibling. This concludes in three possible CSS selector paths for styling the sibling:
parent sibling { }
parent sibling:hover { }
parent:hover sibling { }
These different paths allow for some nice possibilities. For instance, unleashing this trick on the example in the question results in this fiddle:
div {position: relative}
div:hover {background: salmon}
div p:hover {background: white}
div p {padding-bottom: 26px}
div button {position: absolute; bottom: 0}
Obviously, in most cases this trick depends on the use of absolute positioning to give the sibling the same size as the parent, ánd still let the child appear within the parent.
Sometimes it is necessary to use a more qualified selector path in order to select a specific element, as shown in this fiddle which implements the trick multiple times in a tree menu. Quite nice really.
Another, simpler "alternate" approach (to an old question)..
would be to place elements as siblings and use:
Adjacent Sibling Selector (+)
or
General Sibling Selector (~)
<div id="parent">
<!-- control should come before the target... think "cascading" ! -->
<button id="control">Hover Me!</button>
<div id="target">I'm hovered too!</div>
</div>
#parent {
position: relative;
height: 100px;
}
/* Move button control to bottom. */
#control {
position: absolute;
bottom: 0;
}
#control:hover ~ #target {
background: red;
}
Demo Fiddle here.
there is no CSS selector for selecting a parent of a selected child.
you could do it with JavaScript
As mentioned previously "there is no CSS selector for selecting a parent of a selected child".
So you either:
use a CSS hack as described in NGLN's answer
use javascript - along with jQuery most likely
Here is the example for the javascript/jQuery solution
On the javascript side:
$('#my-id-selector-00').on('mouseover', function(){
$(this).parent().addClass('is-hover');
}).on('mouseout', function(){
$(this).parent().removeClass('is-hover');
})
And on the CSS side, you'd have something like this:
.is-hover {
background-color: red;
}
In 2022:
This can be now achieved with CSS only, using the :has pseudo-class and the following expression:
div:has(button:hover) {}
Here's a snippet showcasing the original proposition:
div:has(button:hover) {
background-color: cyan;
}
<div>
<p>Lorem ipsum ...</p>
<button>Delete</button>
</div>
See browser support here. At the time of writing, all major browser support it—except Firefox, which still has a flawed experimental implementation.
This solution depends fully on the design, but if you have a parent div that you want to change the background on when hovering a child you can try to mimic the parent with a ::after / ::before.
<div class="item">
design <span class="icon-cross">x</span>
</div>
CSS:
.item {
background: blue;
border-radius: 10px;
position: relative;
z-index: 1;
}
.item span.icon-cross:hover::after {
background: DodgerBlue;
border-radius: 10px;
display: block;
position: absolute;
z-index: -1;
top: 0;
left: 0;
right: 0;
bottom: 0;
content: "";
}
See a full fiddle example here
This is extremely easy to do in Sass! Don't delve into JavaScript for this. The & selector in sass does exactly this.
http://thesassway.com/intermediate/referencing-parent-selectors-using-ampersand

Where can I find the CSS rule related to an element in Firebug?

I'm going over Frontify and I want to inspect an element in Firebug. The element is <div class="mod mod-header fixed open">.
When selecting that element in Firebug's HTML panel, usually you expect to see the styles in the Styles side panel. I see .mod-header listed there but not .fixed or .open. I want to see what these classes do. Why can't I see them?
EDIT: You have to scroll down or click the menu to see those classes.
.fixed is being used as what I would refer to as a scoping selector. A scoping selector may have it's own styles but it's also used to control where it and related CSS selectors can affect other elements. You'll often see modules/components use this approach.
If you look at the first child element of <div class="mod mod-header fixed"> you'll see <div class="row header">. Select that element in your inspector. You'll now notice how .fixed is being used. You'll see the following CSS selector in the inspector window.
.mod-header.fixed .header {
z-index: 10;
padding: 15px 0;
max-width: 100%;
box-shadow: 0 1px 5px rgba(0,0,0,.1);
}
So .fixed and .open are being used to control child elements rather than the element that they're applied to.
It can often be easier to add a single class to the outer most element and setup your CSS selectors accordingly to re-style child elements instead of finding a handful of elements and applying a class to each.

Use different style(Only one) for Chrome and Mozilla

I have this style inside a PHP file that is applied to an Element
line-height: 120px;
display: list-item;
display: -moz-inline;
list-style: none;
I want that if browser is Chrome then display:list-item and if it is Mozilla then display: inline
The above style works well in Chrome, but in Mozilla it is applied as display: list-item
How to apply specific CSS rules to Chrome only?
Here's a bunch of methods, that actually can help you. Just set style for mozilla and then overwrite it by the Chrome hack. But abstracting from that solution: maybe show us some bigger part of code (or even jsfiddle) so we can help you style it properly without any hacks.

Change pseudo :before background color using CSS attr()

I'm trying to use CSS attr() to change background color of a pseudo :before element.
<ul>
<li data-color="#ff0000">R</li>
<li data-color="#00ff00">G</li>
<li data-color="#0000ff">B</li>
</ul>
Here's the CSS
ul {
list-style: none;
}
li:before {
background-color: attr(data-color, color);
content: "";
display: inline-block;
height: 10px;
width: 10px;
}
But the before element doesn't show background color according to the data-color attribute.
But when I add this CSS
li:after {
content: attr(data-color);
}
The :after element shows the data-color attribute content as the content.
Here's the JS fiddle http://jsfiddle.net/b7Rve/
What did I do wrong?
UPDATE
I just reread about color in the Mozilla developer docs. It says that color type is experimental. I guess I still need to wait until it's released.
Please, look at this other thread Setting width with CSS attr().
In short: "according to Mozilla Developer Network's documentation, is only compatible with the CSS content property [...], but cannot (yet) be used to generate values for other properties."
UPDATE MAY 16, 2016:
Looking at Mozilla Developer Network's documentation now is possibile but with caution:
The attr() function can be used with any CSS property, but support for
properties other than content is experimental.
So, actually you can use it but surely browsers' support, altough better than in the past, is still only rare and experimental.