Mushfiqur Rahman
Mushfiqur Rahman

Reputation: 492

Is there any way to add link on before or after class?

I wants to add one additional menu item on this menu with css before/after I added it with css after class, but is there any way to add link with js or jquery on this newly added after menu ? I don't wants to add this Home menu with html.

ul {
  display: block;
  list-style: none;
  margin: 0;
  padding: 0;
  background: #d89b00;
  margin: 10px;
  padding: 0;
  position:relative;
}
ul li {
  display: inline-block;
  margin: 0;
  padding: 0;  
}
ul li a {
  display: block;
  padding: 15px 20px;
  line-height: 20px;
  color: #fff;
  font-family: Raleway, sans-serif;
  line-height: 1;
  font-size: 14px;
  letter-spacing: 1px;
  text-decoration: none;
}
ul li a:hover {
  color: #dff2fa;
}

ul li a::after {
  position: absolute;
  top: 100%;
  left: 0;
  z-index: -1;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  padding: 16px 20px;
  color: #dff2fa;
  background: #19799f;
  content: attr(data-title);
  transition: background 0.3s;
  transform: rotateX(-90deg);
  transform-origin: 50% 0;
}
ul:before{
  content:"Home";
  right:0;
  padding:10px;
  color:#fff;  
}
   
<ul id="main-menu">
  <li><a href="https://www.google.com" target="_blank">Info</a></li>
  <li><a href="https://www.google.com" target="_blank">About</a></li>
  <li><a href="https://www.google.com" target="_blank">Contact</a></li>
</ul> 

Upvotes: 1

Views: 79

Answers (1)

Devsi Odedra
Devsi Odedra

Reputation: 5322

You can use prepend() as below to achieve this.

$('#main-menu').prepend('<li><a href="https://www.google.com" target="_blank">Home</a></li>');
ul {
  display: block;
  list-style: none;
  margin: 0;
  padding: 0;
  background: #d89b00;
  margin: 10px;
  padding: 0;
  position:relative;
}
ul li {
  display: inline-block;
  margin: 0;
  padding: 0;  
}
ul li a {
  display: block;
  padding: 15px 20px;
  line-height: 20px;
  color: #fff;
  font-family: Raleway, sans-serif;
  line-height: 1;
  font-size: 14px;
  letter-spacing: 1px;
  text-decoration: none;
}
ul li a:hover {
  color: #dff2fa;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="main-menu">
  <li><a href="https://www.google.com" target="_blank">Info</a></li>
  <li><a href="https://www.google.com" target="_blank">About</a></li>
  <li><a href="https://www.google.com" target="_blank">Contact</a></li>
</ul>

Upvotes: 2

Related Questions