Remove top margin from first child via CSS3
April 15, 2015
Content management systems are great for allowing end-user editing of content, but they also introduce a lot of variables into the system. One of those unknowns is the element type for the first element in a content area. This is generally not an issue, but with certain border and background appearances, the variablility can cause alignment issues.
The easiest course of action is to remove the top margin from the first element of the content area, regardless of what type it is. Here is how you can accomplish that via css:
Below is the html for area we’re targetting:
Too much space above h2 (See red lines).
Using the child combinator (>) selector in combination with :first-child targets only the very first element of the direct children, regardless of that child’s element type. It should also works on all modern browsers!