Create a navigation bar for the homepage with a transparent background, while utilizing a black background for all other views on the website. Utilize

As a CSS novice, I've encountered an issue with my Bourbon Refills horizontal navigation bar in my application. The problem is that on the homepage, the navbar is semi-transparent and shows the background image, but I want it to be a different color on all other views (for instance, black instead of transparent). However, I don't know how to achieve this without duplicating the navbar code. There must be a more efficient solution. Currently, I include the navbar in my index.html.erb file as a partial

<%= render 'partials/navbar' %>
. Any guidance on this matter would be greatly appreciated!

navbar.scss:


  header.navigation {
  /* Variables */
}

_navbar.html.erb:


  <header class="navigation" role="banner">
    <div class="navigation-wrapper">
      <a href="#" class="logo">
        <!-- Image goes here -->
      </a>
      <a href="#" class="navigation-menu-button" id="js-mobile-menu">MENU</a>
      <nav role="navigation">
        <ul id="js-navigation-menu" class="navigation-menu">
          <!-- Menu items go here -->
        </ul>
      </nav>
    </div>
  </header>

Answer №1

To change the background color of your navigation bar only when a specific class is added to it, you can target the .navigation-wrapper and apply a black background color by adding the class .black.

.navigation-wrapper.black {
    background-color: black;
}

You can conditionally add this class to the navigation bar based on whether you are on the homepage or not.

For instance, you can set a boolean instance variable in the controller like @black_header = true, then use it in your .erb file as follows:

<header class="navigation-wrapper <%= @black_header? + 'black' : '' %>" role="banner">

If all pages except the homepage should have a black header, consider using the instance variable for pages where you don't want a black header and defaulting it to black for the rest.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to locate and list all video links, and include options for "Play Video" and "Download Video"?

I have a project where I am using PHP to crawl and generate video links from the web. Now, I am looking to implement an option for users to either "Play Video" or "Download Video" when a video link is detected, along with adding a video player if the user ...

Fill your background with a stunning background image

I have encountered a common issue where the existing solutions do not seem to solve my problem. The background image I am working with is taller than it is wide, and I want its height to be 100% of the body's height. To achieve this, I attempted the f ...

What are the differences in using ng-controller versus data-ng-controller?

When working with Angularjs, how do I determine whether to use data-ng-controller or ng-controller? In terms of current best practices for good software design, when is it appropriate to use each one? The information cited suggests that data-ng-controlle ...

Showing objects based on the proportions of the container

I am currently working on a component that will be utilized in numerous widgets within our web application. Is there any method to adjust the visibility of elements based on the size of a div? <div class="container" style="width:100%"> <div> ...

Tips for zooming out a Bootstrap 4 table

I am working with a bootstrap table that looks like this: <div class="table-responsive"> <table> .... </table> </div> Current display on mobile: https://i.sstatic.net/6b6qa.png desired display on mobile: https:// ...

What is the method for subtracting pixels from a percentage value?

Is there a way to accomplish a similar effect to this: height: calc(100% - 50px); height: -webkit-calc(100% - 50px); height: -moz-calc(100% - 50px); In browsers that do not support these properties? I have a responsive div with two children. The height ...

Learn how to dynamically disable or hide the "Today" button in an angular-ui datepicker based on whether it is the weekend

What is the process to hide or disable the "Today" button based on a condition, for example, if today is a weekend? The goal is to conceal the "Today" button in such scenarios. <div class="form-group"> <div class="input-group datePicker"> ...

Tips for displaying a message in a concealed field with jQuery

I have an input field that saves values for checking a specific condition. When the condition is false, I want to display a message. If the field is hidden, the message will not be displayed. HTML <input type="hidden" id="year1_req_fund_hidden" name= ...

What is the best way to align a Font Awesome icon within an SVG circle?

Can anyone help me position the trophy inside the circle, just below the number 2000? <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7974746f686f697a6b5b2e352835 ...

HTML anchor tag failing to redirect to specified link

I need to populate the URI property from an API result into an HTML format. I attempted to achieve this by calling a function and running a loop 3 times in order to display 3 links with the URI property of each object. However, the code is not functioning ...

When integrating React with Tawk, the user's name and email are automatically encoded before being sent

My code within the componentDidMount() function initializes a widget popup when the page loads. It then sets the name and email using parameters received from the previous page. const fullName = this.state.data[0]; console.log(fullName); const e ...

Employing regular expressions within Notepad++ to insert whole numbers into img src within table elements

I have a large table filled with numerous items that require individual images to be added. Let's take a look at the code snippet below. <tr><td><img src=".jpg" width=100 height=100/></td></tr> Although I could manually ...

Is it achievable through CSS alone to eliminate the bullet point in a UL list when it consists of just a single item?

Is it feasible to achieve a solution using CSS alone that remains functional even on IE8, ensuring the following criteria: If a <ul> list contains 2 or more items, then maintain the bullets in front of each item as per usual. When there is only one ...

Badging with Angular, HTML, and Bootstrap

I've been attempting to integrate Bootstrap 5's badges into my Angular application, but they don't seem to be displaying correctly together. While using Angular 13, the html within the app-root component appears clustered without proper for ...

Newbie: Troubleshooting Vue Errors - "Vue is not recognized"

I'm currently at the beginning stages of learning Vue and am practicing implementing it. However, I keep encountering the errors "vue was used before it was defined" and "Vue is not defined". Below are excerpts from my HTML and JS files for reference. ...

Utilizing JQuery's .append() method for dynamically modifying the layout of a table

I have encountered an issue with my HTML table where I want to dynamically change the layout of the table based on the browser size. Here is a snippet of my basic code: <tr> <td class="row">E1</td> <td class="row">E2</td> ...

Blur function not performing as anticipated

I am attempting to achieve a blur effect on a dialog pop-up. Currently, I am using the primeng p-dialog component for this purpose. <p-panelMenu [model]="items" [style]="{'width':'300px'}"></p-panelMenu> <p-dialog head ...

Can we implement attribute selectors in Material-UI while utilizing makeStyles?

Is it possible to extract all the div elements with specific Mui class names such as access-MuiPickersCalendarHeader-switchHeader, access-MuiPickersDay-day? My components are styled using StylesProvider which adds "access" as a prefix to the original Mater ...

What steps should I take to trigger a notification when no selection has been made?

When the product size is not set, the text "Must pick 1 size for the product" should be displayed. However, I am facing an issue with displaying this text. Can someone guide me on how to properly use the select and input elements? <select name="product ...

Unable to show additional columns beyond 6 while using Bootstrap's row-cols feature

Currently working on developing an auction website where I plan to showcase the listings as cards in columns. I have successfully managed to display a varying number of cards on different screen sizes, but I am facing an issue with showing more than 6 card ...