This question already has answers here:
Can the CSS :part pseudo-selector be used to style nested web components?
(2 answers)
Closed 8 months ago.
I'm trying to define some style on the :before pseudo-element of a custom element host.
I've been trying variations around the sample below, but nothing seems to work and I can't find information about it. Is that possible at all? If so, what am I doing wrong?
:host(.open):before{
background: grey;
opacity:.4;
position: absolute;
left:0;
top:0;
width:100%;
height:100%;
}
Here's a fiddle to show the issue: https://jsfiddle.net/8uzjb7ew/ . The custom element should have greyish background, but the :before pseudoelement is not rendered.
I'm not sure about specific :host() pseudo element, but you can combine pseudo elements.
div:target{
background-color:orange;
}
div:before{
content:"*";
}
div:not(#D):target:before{
content:"Targeted, not DEF - ";
}
To ABD
To DEF
To GHI
<div id="A">ABC</div>
<div id="D">DEF</div>
<div id="G">GHI</div>
Related
This question already has answers here:
Hide text in html which does not have any html tags [duplicate]
(3 answers)
Closed 2 years ago.
I'm trying to hide #text in CSS
Screenshot
Screenshot2
What kind of CSS should I write to hide this text?
Thank you in advance.
div{
height:100px;
width:100px;
margin:10px;
background:aqua;
}
.invis:hover{
visibility:hidden;
}
.display{
display:none;
}
<div class="invis"></div>
<div class="display"></div>
Depends, you have more options to hide something:
You have 1st:
visibility: hidden;
//or
opacity: 0;
2nd:
display: none;
Visibilty and opacity just make it invisble, but the box model is still acting in your html, so things have paddin margin and stuff.
Display none will actually dont display the element and will hide the element in the source code
This question already has answers here:
Is there a "previous sibling" selector?
(30 answers)
Closed 4 years ago.
I'm trying to affect an outside element when a div is hovered. Something like this:
<div class="affected">
Hi
</div>
<div>
<div class="hover-me"></div>
</div>
CSS:
.hover-me:hover ~ .affected {
color:
}
I've tried with other sibling selectors but it doesn't work.
With pure CSS that's gonna be as tricky as it gets.
An approach, IF you don't need pointer-events (hover, clicks, etc) on the div that contains the hoverable child, is setting the container as actionable div, disabling pointer-events, resetting them on the child, and using some sort of magic to have the siblings in reverse order on your HTML so they can be targeted with sibling selectors (as you cannot target previous siblings)
Something like
body{
/*switches the oder of .affected and .hover-container, so .affected can be bellow in the HTML and be targeted with sibling selectors, while showing above*/
display:flex;
flex-direction:column-reverse;
}
.hover-container:hover ~ .affected{
/*targets the action on the container*/
background:red;
}
.hover-container{
/*disables pointer-events on the container, so entering it won't cause the hover effect on the sibling*/
pointer-events:none;
}
.hover-me{
/*resets pointer-events on the .hover-me child, so the previous :hover we set for the container will work only when hovering this child*/
pointer-events:auto;
cursor:pointer;
}
div{
border:2px solid grey;
margin:20px 40px;
text-align:center;
}
<div class="hover-container">
this is the hover container
<div class="hover-me">hover me</div>
</div>
<div class="affected">
affected
</div>
But that's probably a not so common scenario, at that point you'll be better off with a JS approach.
This question already has answers here:
How can I remove CSS element style inline (without JavaScript)?
(6 answers)
Closed 6 years ago.
I have a div that looks like this:
<div class="wtHolder" style="width:41.66%; height:150px; position:relative;">
Is there something I can add in css for the wtHolder class that will remove width:41.66% of this?
(If you want the background of why I need to do this, see this question.)
yes you can use !important to overwrite it:
.wtHolder {
width: 100px !important;
}
It's a trick , but you can actually use !important
.wtHolder{
width:100% !important;
background-color:black
}
<div class="wtHolder" style="width:41.66%; height:150px; position:relative;">
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 7 years ago.
I have DOM like this
<div class="outer">
<div class="inner">
</div>
</div>
and corresponding CSS is like
.outer {
width: 700px;
}
, where inner could by typeA, typeB.....
Later I found that I want to enlarge outer when particular typeX show up, but
.outer .typeX {
width: 90%;
}
will apply style width: 90% to .typeX div not .outer div. How do I solve this? Is it possible in pure CSS? (Assume .outer is fixed since it is generated by other library)
Unfortunately, what you are looking for would be a parent selector, which does not yet exist in CSS.
Maybe someday (e.g., in Selectors Level 4).
This question already has answers here:
Is there a CSS parent selector?
(33 answers)
Closed 8 years ago.
Is this possible with css only? I have the following:
<div class="container">
<img src=#>
</div>
How do I get .container to have a box-shadow (and other styling) when and only when img is in the state :hover?
As people have stated there is no CSS parent selector, for a number of good reasons as stated in the linked duplicate question.
However, with the code you've shown, you can just apply the hover pseudo-selector to your parent element and it will achieve almost the exact same functionality.
So this:
div:hover{
border:1px solid red
}
Would work only because you have a single child, and would have the issue that if you hover adjacent to the img but not directly on it the parent will still have styles applied because its a block element.
You can convert it to inline-block to limit this, like so:
div{
display:inline-block;
}
div:hover{
border:1px solid red;
}
However, this will have implications for how other sibling elements to the parent flow.
You can use jQuery:
$("span").hover(
function () {
$(this).parent().addClass("add-class");
},
function () {
$(this).parent().removeClass("add-class");
}
);
Here is the demo http://jsfiddle.net/Sv6Av/
You can replace span with another tag such as img
Nope. No parent selector in css yet. You will have to resort to js for now. For more explanation read this