Affecting parent element of :focus'd element (pure CSS+HTML preferred)
Unfortunately css doesn't support parent selectors. :(
So the only way to do it is to use javascript like the jQuery parent method.
Update:
CSS Selectors Level 4 will support parent selectors! http://www.w3.org/TR/selectors4/#subject
CSS3 - style on focused parent element
There is no parent selector in css (yet). http://css-tricks.com/parent-selectors-in-css/
You can, however, select it with jQuery:
html
<div class="parent">
<input type="text" />
</div>
css
.parent {
/* parent styles */
}
.parent.active {
/* do stuff when the input is hovered */
}
js
// listen for a focus event on the input,
// and then add a class to the parent
$('input').on('focus', function () {
$(this).parent().addClass('active');
});
http://jsfiddle.net/M64nv/
CSS how to style a parent item when a textarea is in focus?
Not sure what #hallPost-inner
is, but in general CSS selectors cannot ascend, meaning you would need to have the textarea:focus
selector, but then would need to style an ancestor element, which cannot be done in css. Here's a good resource, among many others. The link shows how an easy javascript solution can be achieved as well.
Is there a CSS parent selector?
There is currently no way to select the parent of an element in CSS in a way that works across all browsers.
That said, the Selectors Level 4 Working Draft includes a :has()
pseudo-class that will provide this capability. It will be similar to the jQuery implementation.
li:has(> a.active) { /* styles to apply to the li tag */ }
As of 2022, it is only supported by Safari, and by Chromium browsers behind a flag.
In the meantime, you'll have to resort to JavaScript if you need to select a parent element with full cross-browser support.
CSS solution to make drop-down element visible while focused
The solution has been already proposed, but lacks browser support:
9.4. The Generalized Input Focus Pseudo-class:
:focus-within
The
:focus-within
pseudo-class applies to elements for which the
:focus
pseudo class applies.An element also matches
:focus-within
if one of its
shadow-including descendants matches:focus
.
div {
width: 300px;
line-height: 50px;
text-align: center;
background: #e8e8e8;
border: 1px solid #666;
}
div:hover form, div:focus-within form {
display: block;
}
form {
display: none;
padding: 0 15px;
}
<div tabindex="-1">Hover Me
<form class="search">
<input type="search" placeholder="What are you looking for?" autofocus>
<input type="button" value="Search!">
</form>
</div>
Is there a CSS parent selector?
There is currently no way to select the parent of an element in CSS in a way that works across all browsers.
That said, the Selectors Level 4 Working Draft includes a :has()
pseudo-class that will provide this capability. It will be similar to the jQuery implementation.
li:has(> a.active) { /* styles to apply to the li tag */ }
As of 2022, it is only supported by Safari, and by Chromium browsers behind a flag.
In the meantime, you'll have to resort to JavaScript if you need to select a parent element with full cross-browser support.
Is there a CSS parent selector?
There is currently no way to select the parent of an element in CSS in a way that works across all browsers.
That said, the Selectors Level 4 Working Draft includes a :has()
pseudo-class that will provide this capability. It will be similar to the jQuery implementation.
li:has(> a.active) { /* styles to apply to the li tag */ }
As of 2022, it is only supported by Safari, and by Chromium browsers behind a flag.
In the meantime, you'll have to resort to JavaScript if you need to select a parent element with full cross-browser support.
Related Topics
Can You Add Line Breaks to the :After Pseudo Element
Attribute Selector Where Value Equals Either a or B
Can CSS Identifiers Begin with Two Hyphens
CSS Animation on "Content:" Doesn't Work on Safari and Firefox
Continuously Adjust Element Size as Screen Size Changes
Responsive Design with Media Query:Screen Size
Css: Borders Between Table Columns Only
Issue with Background Color and Google Chrome
Simulating Border-Collapse in Lists (No Tables)
Keep Div Height Relevant to Aspect Ratio
Customize List Item Bullets Using CSS
Affecting Parent Element of :Focus'D Element (Pure CSS+HTML Preferred)
Fieldset Firefox Overflow CSS Fix
Grid-Area Does Not Seem to Work with the Attr Function, Is This by Design
My Z-Index Property Is Not Getting Set