Creating a notification system similar to Facebook's in an HTML button using just HTML and CSS

I'm attempting to create a button with a notification feature that mimics the style of Facebook notifications. I want a small red circle in the top right corner, showing a numerical value.

However, I'm running into some difficulties. I can't seem to position the circle correctly or get the number to display inside it.

This is what my button currently looks like:

<button class="btn btn-blue" id="noteBtn">Notes <span class="notification">1</span></button>

And here is the CSS code I've been using:

.notification:before {
  content: ' \25CF';
  font-size: 5em;
  color:red;
}

I was working on this JS Fiddle:

http://jsfiddle.net/N8cjB/5/

Answer №1

<span> and I have a rocky relationship, so I switched it to a <div>

Check out the solution here: http://jsfiddle.net/aXvqW/3/

edit Adjusted the position outside the button as requested.

Answer №2

Keeping it simple, you can achieve this without using :before.

Simply apply the border-radius property to the span element, and to center the number, utilize the text-align: center property.

.notification {
      border-radius: 30px;
    text-align: center;
    border: red;
    width:20px;
    height:10px;
    background: red;
    padding: 2px;

} 

You can test it out in this JSFiddle link

Answer №3

Modify the HTML code by changing <span> to <div>, adjust the button's CSS properties to have a position:relative;, and modify the notification's CSS properties with a position:absolute;. You can then place them anywhere on your webpage.

View the changes here: http://jsfiddle.net/N8cjB/26/

Code

.button{
  margin:20px 0px;
  border:1px solid #e2e2e2;
  background:#f9f9f9;
  padding:5px 10px;
  position:relative;
  cursor:pointer;
  transition:all 0.4s;
}

button:hover{
  background:#333;
  border:1px solid #333;
  color:#fff;
}
.notifications {
      position:absolute;
      border-radius:2px;
      font-size:14px;
      background-color:red;
      color:#fff;
      padding:2px 5px;
      top:-10px;
      right:-10px;
    }
<button class="button">
Notes<div class="notifications">1</div>
</button>

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

Custom stylesheet for individual users?

On my website, users can pick a template for their webpage. After selecting a template, I would like them to have the ability to customize certain styles like the font color. Is there a way to achieve this? I was thinking about saving the user's cus ...

Tips for Ensuring Consistent Logo Appearance Across Various Browsers

The text I added to a website appears perfectly aligned on Chrome and Explorer, but on Safari, it's positioned below the logo. How can I resolve this issue without affecting the view in other browsers? The desired layout should show as "LOGO | TEXT," ...

What is the best way to hide the background of an extension's HTML?

Currently, I am working on developing a Chrome extension for a university project. However, I am facing challenges in making the background or body of the extension's HTML completely transparent to achieve a cleaner interface. The issue specifically l ...

What is the best way to find information in a multi-page table?

I've implemented a table with pagination and search functionality to look up data within the table. However, currently the search only works within the current page of the table rather than searching the entire dataset. Is there a way to modify the se ...

Issue with Bootstrap modal not dismissing when using click function

This is my custom function and div. When I click the function, it executes successfully but the modal window remains visible. <script type="text/javascript> $(document).ready(function () { $("#btnRespuesta").click(function () { //al ...

Creating an image in jpg format from an html file using a button

I have a unique HTML code for an email signature from my company and I am looking to add a button that can generate a JPG or PNG image of the signature without having to resort to PrintScreen and manual cropping in editing tools like Paint or Photoshop. Ho ...

CSS - Combining Selectors and Pseudo Classes to Boost Styling

When it comes to grouping selectors in CSS, there is an easy way to do it like this: .class1 #id1, .class2 #id2, .class3 #id3 { } Applying pseudo classes on these grouped selectors can be a bit repetitive. Is there a method to group multiple selectors a ...

Press the smiley icon and drag it into the designated input box

Is there a way to select and copy a smiley/emoji from a list and paste it into an input field? Although the Inspect Element Q (console log) shows that the emoji is being clicked, I am having trouble transferring it to the input field. Here is the HTML cod ...

Fading Out with JQuery

My page contains about 30 same-sized divs with different classes, such as: .mosaic-block, .events, .exhibitions, .gallery, .sponsors, .hospitality, .workshops, .lectures { background:rgba(0, 0, 0, .30); float:left; position:relative; overf ...

The lifecycle of XMLHTTPRequest objects in JavaScript - from creation to destruction

After years of working with traditional compiled object-oriented languages like C++ and .NET programming, I decided to dip my toes into JavaScript for a new project. As I was experimenting with AJAX, I stumbled upon a perplexing aspect related to how objec ...

SSI stands for Server Side Includes, a feature that allows

I have multiple versions of the same HTML page, each with only one hidden variable that is different. This variable is crucial for tracking purposes. Now, I am exploring options to rewrite this by incorporating a HTML file with a hidden variable. Here is ...

Removing the switcher outline in Bootstrap Switch: a step-by-step guide

I have implemented the bootstrap-switch - v3.3.1, however, I want to remove the default blue outline that appears around the switcher when toggling it on or off. Despite setting style="outline: 0 none; for the input, the outline remains visible. Below is ...

Employing IF conditions within a form to refine options

I am looking to streamline the options in my form based on the car and transmission selected. I have set up the form, but I am struggling with the JavaScript code. I need help figuring out how to only display certain color options when Car 1 is chosen alon ...

Inquire regarding the header image. Can you confirm if the current HTML and CSS for this is the most efficient approach?

This is a preview of my website in progress (Disclaimer: I'm currently learning HTML to build this site, design comes next. I know it's not the conventional way to design a site, but oh well) Check out the site here Is the HTML code for the hea ...

Controlling z-buffering for transparent textures in Three.js

Creating a simple map with flat, paper-like trees protruding from it in WebGL using THREE.js has been quite a challenge. The issue lies in understanding how the library handles z-buffering. Despite numerous attempts to tweak parameters like renderer.sortOb ...

navigate to a different section on the page with a series of visuals preceding it

When I try to navigate to a specific dom element on another page, the page initially works fine but then jumps to somewhere before that element. I believe this issue occurs because the page calculates the position before images load, and once the images ar ...

Inquiring about the usage of div elements in constructing an HTML webpage

When designing my own web pages, I consistently encounter issues with using divs for a multi-column layout. Despite utilizing the float attribute to align columns left or right, resizing the browser window often causes them to stack on top of each other an ...

Choose the Nth option in the dropdown list using programming

Currently, I have a script that dynamically populates a select box with unknown values and quantities of items. I am looking to create another script that mimics the action of selecting the Nth item in that box. Despite my research, I have been unable to ...

Tips for transferring data to index.html from Fetch API

Trying to transfer data from an API to my website has been a challenging task. The structure of my directories is as follows: project > server.js | public public > index.html | styles.css A snippet of my code in server.js shows the process: ...

Creating a button for every individual row within a table using a combination of PHP and HTML

I need assistance with my current PHP and HTML code that generates buttons on each table row: <!DOCTYPE HTML> <h2 class="text-center">Consulta</h2> <br> <table class="table table-striped"> <tr class="in ...