HiveBrain v1.2.0
Get Started
← Back to all entries
patternhtmlMinor

Making CSS rules for links in header more DRY

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
headercssmorelinksfordryrulesmaking

Problem

I want to implement the cleanest amount of CSS through proper use of inheritance. All links need to be a shade of white, so lets say #fff

HTML


  
    BruxZir
  
  
    ≡ 
  


CSS

.header {
  background-color: #333;
  color: #fff;
  height: 36px;
  width: 100%;
  line-height: 36px;
}

/* should be replaced with image */
.headerLogo {
  display:inline;
  font-size: 24px;
  line-height: 36px;
  padding-left: 6px;
}
.headerLogo a{ 
  color:#fff;
  text-decoration:none;
}

.headerMenu{
  float:right;
  font-size: 36px;
  margin-right: 6px;
}

.headerMenu a {
  /* needs the same rules for color, font-size, no text-decoration */
}


Here is my Codepen

I am thinking that since many of these rules will apply to the rest of the links in the header, I can set this up somehow for better inheritance. Also wondering if I would be better served using `s instead of `s for the logo and the menu?

Solution

The worst aspect of your code is the markup, not the CSS. Unless you need the div as an actual styling hook, you can safely discard it. After all, you're not styling it in any meaningful way (what you do have can just as easily be applied to the descendant tags).


    BruxZir
    ≡ 


However, the inclusion of semantic tags might be a better way to go:


    BruxZir
    
        ≡ 
    

Code Snippets

<div class="header">
    <a href="/" class="headerLogo">BruxZir</a>
    <a href="#" class="headerMenu">&equiv;</a> <!-- other HTML and JS left out -->
</div>
<header>
    <h1><a href="/" class="logo">BruxZir</a></h1>
    <nav>
        <a href="#" class="menu">&equiv;</a> <!-- other HTML and JS left out -->
    </nav>
</header>

Context

StackExchange Code Review Q#39905, answer score: 7

Revisions (0)

No revisions yet.