InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS selectors

    Simple Selectors

    • type selectors
    • universal selector (*)
    • attribute presence and value selectors
    • attribute substring selectors
    • class selectors
    • two classes (.c1.c2)
    • id selectors

    Pseudo-classes

    • :link
    • :visited
    • :hover
    • :active
    • :focus
    • :target
    • :lang
    • :enabled and :disabled
    • :checked
    • :root
    • :nth-child
    • :nth-of-type
    • :nth-child vs :nth-of-type
    • :nth-last-child
    • :nth-last-of-type
    • :first-child
    • :first-of-type
    • :last-child
    • :last-of-type
    • :only-child
    • :only-of-type
    • :empty
    • :not

    Pseudo-elements

    • ::first-line
    • ::first-letter
    • ::before and ::after
    • ::placeholder

    Combinators

    • child vs descendant selector
    • adjacent sibling selector (E1 + E2)
    • general sibling selector (E1 ~ E2)

    Specificity

    • Selector specificity
     
    • Home
    • > Tutorials
    • > CSS
    • > CSS selectors

    CSS adjacent sibling selector

    on Mar 22, 2016

    Sometime we need to define css style for adjacent sibling elements. It can be defined by using syntax E1 + E2. It will match when E2 is adjacent to E1 and have same parent. The style will apply to E2.

    Example – adjacent sibling selector

    In this example:

    • In box1 vertical space above h2 is reduced when it is preceded by h1
    • In box2 vertical space above h2 is default
    <style type="text/css">
    div {
      width:100px; background-color: lightgray;
    }
    .box1 h1 + h2 {
      margin-top: -20px;
    }   
    </style>
    
    <div class="box1">
      <h1>h1</h1><h2>h2</h2>
    </div>
    <div class="box2">
      <h1>h1</h1><h2>h2</h2>
    </div>
    refresh done
    try it online

    Related

    • CSS - child vs descendant selector examples
    • CSS general sibling selector

    Specification

    • CSS 3 adjacent sibling combinators specification

    Suggested posts:

    1. jQuery – check if element is visible in viewport ofter scroll
    2. CSS ::before and ::after examples
    3. CSS text-align – align text left, right or justify
    4. Css :last-child selector – last child element
    5. CSS :not – negation pseudo-class
    6. PHP regex – word boundary examples
    7. CSS animation-play-state
    8. CSS :root
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS selectors, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress