Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Foundations Selectors Link and User Action Pseudo-Classes

chrishj
chrishj
1,753 Points

Why does :focus {} not affect the submit button when tabbing thru?

Right at 8:30 in this video Guil removes the "a" element selector from the pseudo class :focus selector, then tabs thru the whole refreshed page showing that each item when focused displays a yellow background... except for the submit button. Why is this?

In CodePen I did however notice that if you select by using... input:focus {} the yellow background color will display on the submit button while focused either by "tabbing" or via mouse click.

2 Answers

Hi chrishj,

I think this is the relevant css your question is referring to:

input[type="submit"] {
    width: 100px;
    height: 40px;
    border: none;
    background: steelblue;
    color: white;
    font-size: inherit;
}

:focus {
    background-color: yellow;
}

When the submit button has focus then both of those selectors will match the element. Both are trying to set the background color which creates a conflict that the browser has to resolve.

It first looks at specificity. The attribute selector has greater specificity because it has 1 type selector (the input element) and 1 class selector (attribute selectors and pseudo classes count as class selectors). The focus selector only has 1 class so it's lower in specificity. This is why the steelblue background remains. The more specific selector wins.

When specificity matches then the browser will use whichever one comes later in the css. You discovered this when you tried input:focus This selector is made up of 1 type selector and 1 class selector so it now matches the specificity of the attribute selector input[type="submit"] It's a tie with specificity so the one that comes later gets applied which is the yellow background.

Shawn Denham
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree
Shawn Denham
Python Development Techdegree Student 17,802 Points

Without pouring through white papers (you don't pay me enough for that ;) ha! ) I'd say it's because submit buttons belong to the "input" group of elements in HTML. They are treated different than all the other HTML elements because they actually have user interaction where all of the other HTML elements ( <a> <p> <header> etc.. ) just assist with how the page will be rendered.

Hope that makes sense :)