When hovering over a child element in firefox - html

When hovering over a child element in firefox using this rule .parent:hover > .child { /*style*/ }, the child element is treated as not part of the parent element and therefore not styled. like in the snippet below, in firefox if you hover over the button, the child element is affected but when the div is hovered it will not changed.
But in chrome hovering over the parent and child will affect the child.
I find this useful to what am working on right now, so is there a way I can achieve the same effect in firefox?
button {
position: relative;
}
button:hover > div {
background-color: #67BDFF;
}
button:hover > div:before {
content: "SMURF!";
position: absolute;
font-weight: bolder;
font-size: 20px;
top: 10px;
}
button > div {
position: absolute;
top: 18px;
left: 0;
padding: 40px;
}
<button>
hover over me and my child will turn smurf
<div>
i'll remain smurf if you over over me cus am part of my parent, but not in firefox
</div>
</button>

<button> elements are only allowed to contain phrasing content (read more) - so technically a <div> is not allowed to be inside a <button>. Because this HTML is non-compliant, you'll see different behavior in each browser.
Here's a cross-browser way to do what your code was trying to do, which works in Firefox:
button {
width: 300px;
}
button + div {
padding: 40px;
position: relative;
width: 220px;
}
button:hover + div,
button + div:hover {
background-color: #67BDFF;
}
button:hover + div:before,
button + div:hover:before {
content: "SMURF!";
position: absolute;
font-weight: bolder;
font-size: 20px;
top: 10px;
}
<button>
hover over me and my child will turn smurf
</button>
<div>
i'll remain smurf if you over over me cus am part of my parent, but not in firefox
</div>

Related

Child element not positioned correctly relative to parent element?

I have code blocks on my website that look like this:
This pre block has a child button, as shown below.
I am applying the following CSS to the button element:
pre[class*="language-"] button {
position: absolute;
top: 5px;
right: 5px;
font-size: 0.9rem;
padding: 0.15rem;
border: ridge 1px #7b7b7c;
border-radius: 5px;
text-shadow: #c4c4c4 0 0 2px;
}
code[class*="language-"] button:hover {
cursor: pointer;
background-color: #bcbabb;
}
However, the button appears to be located top: 5px; right: 5px; of the HTML document instead of the pre (ignore the toggle).
Image
I removed the positioning from the button, and it now appears kinda correctly (at least somewhat in the right place) like so:
Image
I can't figure it out why the CSS isn't correctly positioning it...
absolute
The element is removed from the normal document flow, and no space is created for the element in the page layout. It is positioned relative to its closest positioned ancestor, if any; otherwise, it is placed relative to the initial containing block. Its final position is determined by the values of top, right, bottom, and left.
MDN - CSS - Position - Absolute
Add position:relative to pre so that the button can be absolutely positioned inside the pre
pre[class*="language-"] button {
position: absolute;
top: 5px;
right: 5px;
font-size: 0.9rem;
padding: 0.15rem;
border: ridge 1px #7b7b7c;
border-radius: 5px;
text-shadow: #c4c4c4 0 0 2px;
}
code[class*="language-"] button:hover {
cursor: pointer;
background-color: #bcbabb;
}
pre {
position: relative;
}
<pre class="language-js">
<code class="language-js">
some code
</code>
<button>Copy</button>
</pre>

Preserve dropdown hover when circular button and dropdown are separated by a gap

The main issue is caused by a button being circular. But I really want a circular button, which is leading to hovering problems!
All I want is to have a circular <a> button that when hovered-over will reveal another element below it, like a div or another a tag. These two elements are separated by a gap.
Then I should be able to move my mouse down and hover over the revealed element and click on it or whatever. But of course if you unhover the original <a> then the other element will disappear, especially since there is a gap between the two elements. What is the best way to make it so that I can move my mouse from element 1 to element 2 without element 2 vanishing during mouse travel?
Ideally this shouldn't require JS.
I've created a basic setup for this so far to get started:
body {
padding: 30px;
height: 100vh;
}
#myBtn {
background-color: grey;
padding: 20px;
border-radius: 50%;
}
#hoverInfo {
display: none;
margin-top: 40px;
background-color: green;
width: 100px;
height: 50px;
}
#myBtn:hover + #hoverInfo, #hoverInfo:hover {
display: block;
}
<a id="myBtn" href="/">
button
</a>
<a id="hoverInfo" href="/">hover info</a>
Here's an explanation of an old solution attempt of mine:
My first solution to stop element 2 from vanishing upon downward movement of the mouse was to put an invisible hoverable element between elem 1 and 2 which would keep elem 2 active while the mouse moves down to it. And this would work great, IF all elements were rectangular. But my elem 1 is circular!
This means that there is literally one single pixel of contact between the middle hover buffer element and elem 1 because there are those circular "corner" gaps between elem 1 and the invisible middle element. So whenever you move your mouse down, you are still going to miss that middle hover element 99% of the time.
And you can't put it behind elem 1 either to fill in those circular "corners" because the circular element has a bounding box that you can only see in inspect element and this bbox prevents you from filling up those "corners" with an area that actually interacts with the mouse, therefore making this solution useless. It's quite confusing in my explanation but try it out if you manage to implement this "solution".
The first solution to come to mind is wrapping the circular button into a parent div, which will be the div that will activate the hover effect. This way, you can add padding-bottom to imitate the gap look while still making the "gap area" trigger the hover effect. In the snippet below, I made the wrapper div have a red background so you can see how it works. If you remove the red background, it should function as intended.
https://codepen.io/xenvi/pen/yLONOEa
body {
padding: 30px;
height: 100vh;
}
.buttonWrapper {
display: inline-block;
margin-bottom: 40px;
background: red;
}
#myBtn {
background-color: grey;
padding: 20px;
border-radius: 50%;
}
#hoverInfo {
display: none;
background-color: green;
width: 100px;
height: 50px;
}
.buttonWrapper:hover+#hoverInfo,
#hoverInfo:hover {
display: block;
}
.buttonWrapper:hover {
margin: 0;
padding-bottom: 40px;
}
<div class="buttonWrapper">
<a id="myBtn" href="/">
button
</a>
</div>
<a id="hoverInfo" href="/">hover info</a>
You can solve this easily using a pseudo element that will make the hoverable area bigger and that you activate only on hover:
body {
padding: 30px;
height: 100vh;
}
#myBtn {
background-color: grey;
padding: 20px;
border-radius: 50%;
position:relative;
}
#myBtn:before {
content:"";
display:none;
position:absolute;
top:90%;
left:0;
right:0;
height:28px;
}
#hoverInfo {
display: none;
margin-top: 40px;
background-color: green;
width: 100px;
height: 50px;
}
#myBtn:hover::before {
display:block;
background:rgba(0,0,255,0.2); /* to illustrate */
}
#myBtn:hover + #hoverInfo, #hoverInfo:hover {
display: block;
}
<a id="myBtn" href="/">
button
</a>
<a id="hoverInfo" href="/">hover info</a>

Change a single word background in a paragraph with CSS only (without changing HTML)

As a part of my study project, I need to change the background of a single word ("adventure") inside a paragraph. I'm not allowed to change HTML code, can use CSS only.
<p class="section_text">
Welcome to adventure!
</p>
The only idea I have is to set a background to a pseudo-element ::after and play with position relative/absolute, but it doesn't feel right.
.section_text {
position: relative; }
.section_text::after {
content: "adventure";
background-color: #04128f;
color: #0f0;
width: 65px;
height: 20px;
position: absolute;
top: 0;
left: 90px; }
Are there any smart ways to do that (it should work in the last version of Chrome)?
P.S. Can not use JS or jQuery neither. Exclamation sign background shouldn't be changed.
Set an intrinsic font-size to the :root selector. ex. :root {font-size: 5vw}. This makes the font responsive to viewport width.
Make the background of <p> the highlight color (ex red) and set its width: max-content.
Next <p> needs position: relative and then z-index: -1
Add two pseudo elements to <p>
p::before {content: 'Welcome to';...}
/*and*/
p::after {content: '!';...}
Assign position: absolute, z-index: 1, and background: white
Finally set right: 0to p::after so the exclamation mark is always at the very end
Note: the property/value content: 'Welcome to\a0'; on p::before selector has \a0 at the end. This is the CSS entity code for a non-breaking space (HTML entity: )
:root {
font: 400 5vw/1 Consolas
}
p {
position: relative;
width: max-content;
z-index: -1;
background: red;
}
p::before {
position: absolute;
content: 'Welcome to\a0';
background: white;
z-index: 1
}
p::after {
position: absolute;
content: '!';
background: white;
z-index: 1;
right: 0
}
<p>Welcome to adventure!</p>
Edit: to change background-color for an arbitrary position word, see zer00ne's answer above. I didn't read the question thoroughly, so I wasn't aware that the OP wants the word adventure not adventure!
The smartest way to change any arbitrary word is to wrap it inside a span tag. Here's the workaround for changing the background of the last word: From your delivered code, display: inline-block for p tag, and don't set width for ::after element.
.section_text {
display: inline-block;
background: #3333;
position: relative;
}
.section_text::after {
content: 'adventure!';
position: absolute;
right: 0;
height: 100%;
z-index: 1;
background: red;
}
<p class="section_text">
Welcome to adventure!
</p>

:focus ring removal on :before pseudo-element

I have a button with span included which then has a pseudo-selector, :before applied to it in some cases.
When the element (button) receives focus, the :before is also receiving focus and the focus ring, resulting in something like this:
While I'd like to keep the focus ring on the button itself, I'm having a difficulty removing it from the contained :before element. See this JSFiddle:
https://jsfiddle.net/uhgsj6cp/3/
The HTML/CSS is fairly basic:
.btn {
width: 100px
}
.btn>span {
position: relative;
}
.btn>span:before {
display: block;
content: '•';
font-size: 32px;
position: absolute;
left: -13px;
top: -13px;
}
<button class='btn btn-default'>
<span>Text</span>
</button>
Reduce the line-height then hide the overflow:
.btn {
width: 100px
}
.btn>span {
position: relative;
}
.btn>span:before {
/*display: block; not need*/
content: '•';
font-size: 32px;
position: absolute;
left: -13px;
top: 0;
overflow:hidden;
line-height:0.4;
}
<button class='btn btn-default'>
<span>Text</span>
</button>
I was able to ALMOST remove that artifact outline with adjusting line-height and top on the pseudo element.
However, you could also try using the HTML • to produce the bullet within the button.

Display another container on input:focus using only css

I want to have some css properties on input:focus so how can I do that?
My scenario is; when input get focus I want to show another div so how can I do that using only css?
On hover I can do that using ">" but on focus is not working and I don;t understand why :(.
so this is my code:
<div class="containerTooltipXxx">
<p class="paragraphClass">
Some text...<br /><input type="radio" /><br />More text...
</p><div class="blocks">
<label>Field</label> <input></input></div>
</div>
.containerTooltipXxx{
padding: 20px;
position: relative;
float: left;
display: inline-block;
border: 2px solid lime;
margin: 50px;
}
.paragraphClass{display: none;}
.containerTooltipXxx:hover > .paragraphClass, .containerTooltipXxx:focus > .paragraphClass{
display: block;
position: absolute;
top:-5px;
left: 50px;
background: red;
opacity:.9;
}
very important, the html hierarchy cannot be changed.
Thank you.
fiddle
using CSS you can only point to the next sibling elements. Here since the p tag is out of the parent div it is not possible using css.
I know that you don't want to change the HTML order but still I am showing it for example.
Moving p tag inside the div.blocks can do this with only CSS
.blocks input[type="text"]:focus ~ .paragraphClass {
display: block;
position: absolute;
top:-50px;
left: 50px;
background: #ccc;
opacity:.7;
}
DEMO
.containerTooltipXxx:hover > replace this by
.containerTooltipXxx:focus ~ .paragraphClass
{
display: block;
position: absolute;
top:-5px;
left: 50px;
background: red;
opacity:.9;
}
Your first hover selector is fine, but the second is wrong.
What you are doing with .containerTooltipXxx:focus > .paragraphClass, is selecting the immediate child .paragraphClass from a focused .containerTooltipXxx. Focus can only be used on things with input, and your container is just a div.
What you would need is a parent selector, but these are currently not available. They will be most likely in CSS4. http://www.w3.org/TR/selectors4/#subject
Currently, your best bet would be using javascript. Make an event listener for focus on the input box, and then programmatically apply a visible class to what you want to show.