More efficient styling on Menus

Hello.

I could use some help learning how to make my CSS styles more efficient.

Below is a sample web page which has two top menus - the purpose being I want to compare color schemes.

It seems to me that there is a better way to style my original top menu so I can tweak the color scheme without having to duplicate a lot of my styles.

What is the most modern way to do this?

<!DOCTYPE HTML>
<html lang="en">


<!-- *************************  HTML HEAD  ********************************* -->
<head>
  <!-- METADATA -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

  <!-- TITLE -->
  <title>sp_top-menus_01.html</title>

  <!-- CSS STYLES -->
  <style media="screen">
    *{
      margin: 0;
      padding: 0;
    }
    
    html, body{
      height: 100%;
    }
    
    body{
      font-family: Helvetica, Arial, Sans-Serif;
      font-weight: normal;
      line-height: 1.4;
      font-size: 0.9em;
    }
    
    p{
      padding: 0 0 1rem 0;
    }

    #pageWrapper{
      position: relative;
      display: block;
      min-height: 100%;
      max-width: 1200px;
      min-width: 840px;
      margin: 0 auto;
    }
    
    #pageInner{
      position: relative;
      width: 100%;
      overflow: hidden;
    }
    
    #mast{
      padding: 5px 0 0 0;
    }
    
    #pageHeader{
      display: flex;
      align-items: baseline;
      padding: 0 0 5px 0;
      line-height: 1.4;
    }
    
    #logo{
      font-family: Georgia, Times New Roman, Serif;      
      font-size: 2rem;
    }

    #menuLinks{
      margin-left: auto;
    }

    /* HOW CAN I MAKE THESE STYLES MORE EFFICIENT? */
    #topMenu,
    #topMenu2{
      width: auto;
      display: flex;
      list-style: none;
      background-color: #48D1CC;                /* Medium Turquoise */
    }

    #topMenu2{
      background-color: #A9A9A9;                /* Dark Gray */
    }

    #topMenu li,
    #topMenu2 li{
      padding: 0.5em 1em 0.4em 1em;
      background-color: #48D1CC;                /* Medium Turquoise */
    }
    
    #topMenu2 li{
      background-color: #A9A9A9;                /* Dark Gray */
    }
    
    #topMenu li.menuCurrent,
    #topMenu2 li.menuCurrent{
      border: none;
      background-color: #008B8B;                /* Dark Cyan */
    }
    
    #topMenu2 li.menuCurrent{
      color: #FFF;
      background-color: #000;                   /* Black */
    }
    
    #topMenu li.menuLast,
    #topMenu2 li.menuLast{
      margin-left: auto;
      padding: 0.5em 1.7em 0.4em 1.7em;
      border: none;
      background-color: #FFD700;                /* Gold */
    }
    
    #topMenu2 li.menuLast{
      background-color: #FFD700;                /* Gold */
    }

    #pageBody{
      position: relative;
      max-width: 100%;
      margin: 30px 0 0 0;
      background-color: #EEE;
    }

    
  </style>
</head>

<!-- *************************  HTML BODY  ********************************* -->
<body>
  <div id="pageWrapper">
    
    <div id="pageInner">
      
      <div id="mast">
        <div id="pageHeader">
          <h1 id="logo">My Company</h1>
        </div>
        
        <ul id="topMenu">
          <li class="menuCurrent">Home</li>
          <li>News</li>
          <li>Articles</li>
          <li>How-To</li>
          <li>Gallery</li>
          <li class="menuLast">Store</li>
        </ul>
        <br>
        <ul id="topMenu2">
          <li class="menuCurrent">Home</li>
          <li>News</li>
          <li>Articles</li>
          <li>How-To</li>
          <li>Gallery</li>
          <li class="menuLast">Store</li>
        </ul>
      </div>
      
      <div id="pageBody">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id justo rhoncus, finibus nisl sit amet, pretium nibh. Aliquam nec turpis auctor justo fringilla aliquet et vitae magna. Vestibulum est mauris, condimentum id pulvinar sed, volutpat eu massa. Morbi cursus lacus in lectus venenatis gravida. Ut facilisis mollis hendrerit. Maecenas a nunc erat. Quisque erat arcu, facilisis sed consectetur vel, efficitur at quam. Pellentesque semper est nec arcu imperdiet, id facilisis neque luctus. Cras in rutrum ante, nec fringilla tortor. Pellentesque ac nibh volutpat, bibendum mi non, ultricies nisl. Sed malesuada condimentum nunc ut imperdiet. Pellentesque faucibus eu lorem aliquam volutpat. Duis gravida aliquet ultricies. Nam eu tempus lectus.</p>

        <p>Duis semper commodo gravida. Aenean dolor mauris, laoreet nec tempus id, congue vitae enim. Morbi at ultrices nisl. Maecenas quis lorem sollicitudin, fringilla elit eget, mollis velit. Morbi gravida consequat nisl, eget accumsan velit. Ut feugiat quis diam ac aliquam. Aenean ornare placerat hendrerit. Donec vel nulla quis sapien laoreet dictum et id nulla.</p>

        <p>Donec eleifend porta ullamcorper. Sed diam tortor, ornare vel gravida non, tristique in sem. Sed vel condimentum odio. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Duis eros libero, gravida at egestas ac, ultricies in erat. Vestibulum condimentum lorem in erat mollis, in fermentum orci consectetur. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum venenatis pulvinar ullamcorper. Etiam sollicitudin vitae metus vel posuere. Quisque viverra, libero id egestas consequat, ipsum lorem varius arcu, semper condimentum ex eros id ex. Curabitur tincidunt felis ultrices, blandit ipsum in, vestibulum mauris.</p>

        <p>Nam ullamcorper diam nunc, sed dignissim augue lacinia ac. Quisque bibendum ipsum consequat, aliquet metus a, tempor elit. Suspendisse porta dui eget lacinia maximus. Donec non sem aliquet, varius urna sit amet, rhoncus dolor. Curabitur nulla sem, finibus sit amet aliquam in, viverra vitae dolor. Suspendisse quis luctus massa, at rutrum ante. Etiam pharetra, metus tempor pharetra molestie, magna dui iaculis ligula, vel suscipit libero odio nec orci.</p>

        <p>Cras condimentum feugiat urna, sagittis viverra lacus consectetur at. Morbi id ex eget urna fermentum tempor ac a enim. Fusce sed vestibulum ipsum. Nunc convallis, ex ut fringilla euismod, dolor felis fringilla mauris, eget bibendum urna justo ut est. Donec consectetur sapien non ultricies viverra. Nam congue vel dolor nec iaculis. Pellentesque imperdiet nibh risus, at facilisis metus aliquam porttitor. Mauris malesuada nec nisi ac pellentesque. Vivamus cursus feugiat mi, at interdum magna suscipit a. Suspendisse potenti. Donec lacinia posuere ex sit amet interdum. Aliquam nec ex mi. Vivamus nec euismod tellus. Vestibulum rhoncus placerat sollicitudin. Nam facilisis non metus ut tincidunt. Vivamus eget semper elit.</p>
      </div>
      
    </div>
    
  </div>
  
</body>
</html>

Hi there UpstateLeafPeeper,

you will find that this…

min-width: 840px

…prevents the page from being responsive.

Here are a few modifications to your code…

<!DOCTYPE HTML>
<html lang="en">

<!-- *************************  HTML HEAD  ********************************* -->
<head>
  <!-- METADATA -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

  <!-- TITLE -->
  <title>sp_top-menus_01.html</title>

  <!-- CSS STYLES -->
  <style media="screen">
    *{
      margin: 0;
      padding: 0;
    }
    
    html, body{
      height: 100%;
    }
    
    body{
      font-family: helvetica, arial, sans-serif;
      font-weight: normal;
      line-height: 1.4;
      font-size: 0.9em;  /* ?????? */
	  font-size: 1em;
    }
    
    p{
      padding: 0 0 1rem 0;
    }

    #pageWrapper{
      /*position: relative; ?????? */
      /*display: block; ??????? */
	  padding: 0.5em;
      min-height: 100%;
      /*max-width: 1200px;  Avoid px units */
      max-width: 75em;
      /*min-width: 840px; This will prevent responsiveness. Avoid px units  */
      margin: 0 auto;
	  box-sizing: border-box;
    }
/*        Not required
    #pageInner{
      position: relative;
      width: 100%;
      overflow: hidden;
    }
  
    #mast{
      padding: 5px 0 0 0;
    }
 
    #pageHeader{
      display: flex;
      align-items: baseline;
      padding: 0 0 5px 0;
      line-height: 1.4;
    }
*/     
    #logo{
	  padding: 0.325em 0;
      font-family: georgia, 'times new roman', serif; /* serif is better suited to print */     
      font-size: 2rem;
    }
/*        Not required
    #menuLinks{
      margin-left: auto;
    }
*/
    /* HOW CAN I MAKE THESE STYLES MORE EFFICIENT? */
    #topMenu,
    #topMenu2{
      width: auto;
      display: flex;
	  flex-wrap: wrap; /* added for responsiveness */
	  margin-bottom: 1em;
      list-style: none;
      background-color: #48D1CC;                /* Medium Turquoise */
    }

    #topMenu2{
      margin-bottom: 2em;
      background-color: #A9A9A9;                /* Dark Gray */
    }

    #topMenu li,
    #topMenu2 li{
      padding: 0.5em 1em 0.4em 1em;
      background-color: #48D1CC;                /* Medium Turquoise */
    }
    
    #topMenu2 li{
      background-color: #A9A9A9;                /* Dark Gray */
    }
    
    #topMenu .menuCurrent,
    #topMenu2 .menuCurrent{
      border: none;
      background-color: #008B8B;                /* Dark Cyan */
    }
    
    #topMenu2 .menuCurrent{
      color: #FFF;
      background-color: #000;                   /* Black */
    }
    
    #topMenu .menuLast,
    #topMenu2 .menuLast{
      margin-left: auto;
      padding: 0.5em 1.7em 0.4em 1.7em;
      border: none;
      background-color: #FFD700;                /* Gold */
    }
    
    #topMenu2 .menuLast{
      background-color: #FFD700;                /* Gold */
    }

    #pageBody{
      /*position: relative;  ??????? */
      /*max-width: 100%; ??????? */
      /*margin: 30px 0 0 0; Not required. Avoid px units   */
      background-color: #EEE;
    }
  
  </style>
</head>

<!-- *************************  HTML BODY  ********************************* -->
<body>
  <div id="pageWrapper">
    
    <!--<div id="pageInner"> This is code bloat -->
      
      <!--<div id="mast"> This is code bloat -->
        <!--<div id="pageHeader"> This is code bloat -->
          <h1 id="logo">My Company</h1>
        <!--</div>-->
        
        <ul id="topMenu">
          <li class="menuCurrent">Home</li>
          <li>News</li>
          <li>Articles</li>
          <li>How-To</li>
          <li>Gallery</li>
          <li class="menuLast">Store</li>
        </ul>
        <!--<br> Not required between block element. Use CSS  for precision -->
        <ul id="topMenu2">
          <li class="menuCurrent">Home</li>
          <li>News</li>
          <li>Articles</li>
          <li>How-To</li>
          <li>Gallery</li>
          <li class="menuLast">Store</li>
        </ul>
      <!--</div>-->
      
      <div id="pageBody">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam id justo rhoncus, finibus nisl sit amet, pretium nibh. Aliquam nec turpis auctor justo fringilla aliquet et vitae magna. Vestibulum est mauris, condimentum id pulvinar sed, volutpat eu massa. Morbi cursus lacus in lectus venenatis gravida. Ut facilisis mollis hendrerit. Maecenas a nunc erat. Quisque erat arcu, facilisis sed consectetur vel, efficitur at quam. Pellentesque semper est nec arcu imperdiet, id facilisis neque luctus. Cras in rutrum ante, nec fringilla tortor. Pellentesque ac nibh volutpat, bibendum mi non, ultricies nisl. Sed malesuada condimentum nunc ut imperdiet. Pellentesque faucibus eu lorem aliquam volutpat. Duis gravida aliquet ultricies. Nam eu tempus lectus.</p>

        <p>Duis semper commodo gravida. Aenean dolor mauris, laoreet nec tempus id, congue vitae enim. Morbi at ultrices nisl. Maecenas quis lorem sollicitudin, fringilla elit eget, mollis velit. Morbi gravida consequat nisl, eget accumsan velit. Ut feugiat quis diam ac aliquam. Aenean ornare placerat hendrerit. Donec vel nulla quis sapien laoreet dictum et id nulla.</p>

        <p>Donec eleifend porta ullamcorper. Sed diam tortor, ornare vel gravida non, tristique in sem. Sed vel condimentum odio. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Duis eros libero, gravida at egestas ac, ultricies in erat. Vestibulum condimentum lorem in erat mollis, in fermentum orci consectetur. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum venenatis pulvinar ullamcorper. Etiam sollicitudin vitae metus vel posuere. Quisque viverra, libero id egestas consequat, ipsum lorem varius arcu, semper condimentum ex eros id ex. Curabitur tincidunt felis ultrices, blandit ipsum in, vestibulum mauris.</p>

        <p>Nam ullamcorper diam nunc, sed dignissim augue lacinia ac. Quisque bibendum ipsum consequat, aliquet metus a, tempor elit. Suspendisse porta dui eget lacinia maximus. Donec non sem aliquet, varius urna sit amet, rhoncus dolor. Curabitur nulla sem, finibus sit amet aliquam in, viverra vitae dolor. Suspendisse quis luctus massa, at rutrum ante. Etiam pharetra, metus tempor pharetra molestie, magna dui iaculis ligula, vel suscipit libero odio nec orci.</p>

        <p>Cras condimentum feugiat urna, sagittis viverra lacus consectetur at. Morbi id ex eget urna fermentum tempor ac a enim. Fusce sed vestibulum ipsum. Nunc convallis, ex ut fringilla euismod, dolor felis fringilla mauris, eget bibendum urna justo ut est. Donec consectetur sapien non ultricies viverra. Nam congue vel dolor nec iaculis. Pellentesque imperdiet nibh risus, at facilisis metus aliquam porttitor. Mauris malesuada nec nisi ac pellentesque. Vivamus cursus feugiat mi, at interdum magna suscipit a. Suspendisse potenti. Donec lacinia posuere ex sit amet interdum. Aliquam nec ex mi. Vivamus nec euismod tellus. Vestibulum rhoncus placerat sollicitudin. Nam facilisis non metus ut tincidunt. Vivamus eget semper elit.</p>
      </div>
      
    <!--</div>-->
    
  </div>
  
</body>
</html>

coothead

@coothead,

Thanks for the response, but not what I expected.

Fwiw, I am stuck with the code that I have for v1.0, and yes, a lot of the things you commented out are necessary in the larger scheme of things.

Apparently my OP wan’t worded very well.

I was asking if there was a way to avoid: #topMenu, #topMenu2, #topMenu3, and on and on.

Coding that way makes your code balloon.

I thought there was a way where I could create a style dedicated to say menu color, and then combine that with the #topMenu which is a style based on the structure of the menu.

As I recall, there is an approach to CSS where instead of building “one-off” styles, you create component styles and then add them together like Legos to get the end result that you want.

Follow me?

The CSS menu code does not look bloated to my eyes,
but then my method of coding CSS is not as advanced
as yours. :biggrin:

coothead

1 Like

That still doesn’t answer what I was asking.

Did what I last said still not make sense?

Yes, that is correct. :biggrin:

coothead

:unhappy:

I recall reading somewhere in the past that there is an approach to CSS where you build a bunch of smaller component ID’s/classes and then add them together to style things.

This allows for component architecture.

So instead of…

#someBigOneTimeUseStyle{


}

You would have something like…

#topMenu{

}

.menuColorBlack{

}

.menuColorGrey{

}

.menuColorBlue{

}

.menuFontSerif{

}

.menuFontSansSerif{

}

and so on...

Then when you style things, you’d combine them as need.

(This is where I need help figuring out how to do this…)

Maybe something like…

<ul id="topMenu" class="<class1><class2><class3">

</ul>

When I tried this it didn’t work.

You obviously have HTML problems…

<ul id="topMenu" class="class1 class2 class3">

So it appears that you want to write bloated HTM
believing that it will make you CSS better. :rolleyes:

Good luck with that. :rofl:

1 Like

Clearly you’re not following anything I’m asking about…

If I knew the name for the approach I’m asking about then maybe I could find the answer myself.

Anyone else?

Try replacing the id #topMenu … with the class .topMenuX then you can set a common style for all of your topMenus.

1 Like

@AllanP,

True, but I was asking about applying multiple IDs or classes to an HTML element.

Isn’t that called SASS or something?

So I would have a base ID/class to style the top menu item. But them I would have another class that just handles the color of the menu. That way I can mix-and-match, versus having to apply all of the styling over and over again to different menus when the only thing changing might be the color.

Guess I’m not explaining things well enough.

Sass is a preprocessor for CSS (it compiles into CSS).

Okay. So what about an answer to my OP?

I’ve never been a fan of CSS naming conventions. That’s basically what you’re looking for. Anytime you want reusable sort of styles, you need to keep away from IDs. You don’t need to follow a specific naming convention that exists. Find what you need to reuse, make them into classes, and just go from there.

If you’re looking for me to answer with a specific naming convention, then I’m sorry, but I just do not follow that sort of stuff.

@RyanReese,

Why do you not like “naming conventions”?

What I was trying to figure out is how to do what we’re talking about?

As I think I posted in my OP, lets say I have a base style for the #topMenu and then I just want to be able to toggle the color-scheme. How would I code that?

What I tried on my own didn’t work.

Fwiw, the reason I am asking about this, is because I wanted to make a bunch of top menus in different colors and use it as a swatch of sorts. And while doing that, I didn’t want a bunch of bloated code.

Use a class for topMenu, such as .topMenuX and then use a class for each colour, such as .red, .blue, .green etc, Then use them like this.
css:
,topMenuX { the style attributes you want }
.red { color:#F00; }
.blue { color:#00F; }
.green { color:#0F0; }
html:

> <div class="topMenuX red">aaa</div>
> <div class="topMenuX blue">aaa</div> 
> <div class="topMenuX green">aaa</div>
1 Like

@AllanP,

That wasn’t my question…

Take a step back and look to see the topmenu differences. Are they all the same dimensions and do they only change colour?

Again, I was looking to create a sample file with a series of the same top menu bar but with different colors so it would look like a swatch.

You know what I mean by swatch, right?

So in that case, it would be easier to “AND” a color style to the base top menu style which would change, and would include things like padding, font, menu bar width, etc.

Take a look at the other thread where I introduced TLAs. Classes can be easily chained.