Slice cleanly through the heart of the boundary

I want to create a unique border effect when hovering, similar to the one shown in this image:

https://i.sstatic.net/JsXVJ.png

I'm not quite sure how to define this border style. Any suggestions on how to achieve it?

.text {
  width: 100px;
  height: 20px;
  text-align: center;
}

.text:hover {
  border: 1px solid black;
}
<div class="text"> Sample </div>

Answer №1

Implementing hover effect with before and after pseudo-elements

.text {
  position: relative;
  width: 100px;
  height: 20px;
  text-align: center;
}

.text:hover::before, 
.text:hover::after {
  content: '';
  position: absolute;
  border: 1px solid black;
  border-bottom: 0;
  width:100%;
  left:0;
  height: 20%; 
}

.text:hover::after {
  bottom: 0;
  border: 1px solid black;
  border-top: 0;
}
<div class="text"> Sample </div>

Answer №2

To customize the border of a div, you can utilize the ::before and ::after selectors with the same background color as the div itself (which is typically white). This will allow you to create a unique border effect, overriding the default border style.

.custom-border {
  width: 120px;
  height: 30px;
  text-align: center;
  
  position: relative;
}

.custom-border:hover {
  border: 2px solid blue;
}

.custom-border:hover::before,
.custom-border:hover::after {
  content: " ";
  position: absolute;
  background: white;
  top: 8px;
  height: 14px;
  width: 2px;
}

.custom-border::before {
  left: -2px;
}

.custom-border::after {
  right: -2px;
}
<div class="custom-border"> Example </div>

Answer №3

A straightforward solution is to utilize the border-image property

.content {
  display:inline-block;
  padding:15px;
  text-align: center;  
  font-size:20px;
  border:3px solid transparent;
}

.content:hover {
  border-image: linear-gradient(#333 25%, transparent 0 75%,#333 0) 3;
}
<div class="content"> Example </div>

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

Tips for preventing delays in the digest cycle caused by two-way binding

When does two way data binding get triggered in AngularJS? In my AngularJS application, there is a parent directive and a child directive. Parent Directive: angular .module('myApp') .directive('customForm', function(customService ...

Include an additional section in the stunning Radar Chart

I am trying to use the amCharts 4 library to create a Radar graph similar to this example: https://i.sstatic.net/9S0ka.png In the example, there are two categories being compared with bullets surrounding each one, right? The code I currently have is as ...

Grid numbering with CSS in jQuery mobile design

Looking to create a numbered grid where the user inputs a number and the grid is generated. I am considering using CSS, HTML, or Jquery Mobile for an iPad project. I'm unsure of the best approach at the moment. I would like the grid and numbers to aut ...

Guide on integrating CSS into a Vue Bootstrap table design

I am having trouble adding a CSS to my bootstrap vue table template. <template slot="actions" slot-scope="data" :style="{min-width:'150px'}"> <b-button variant="info" @click="showViewModel(data.item)"> <i class="fa fa-e ...

Customizing the color of buttons in MUI 4

Currently, I am utilizing mui V4 for my styling and attempting to incorporate an additional color for my buttons. I understand that it only accepts these predefined colors: expected one of ["default", "inherit", "primary", "secondary"]. To achieve this, I ...

Execute JavaScript code via URL injection

One interesting aspect of the HTML is that it has a feature where it opens a webpage. The specific webpage it opens is determined by the URL, for example: https://mywebsite.com/index.html&audio=disabled In addition to this, there is a useful JavaScri ...

How can I ensure the height of the Bootstrap vertical navigation menu appears consistent on every page

I'm struggling to get the vertical navigation menu to reach all the way down to the footer of the page. I've tried adjusting it, but the closest I came was setting a specific height for the .navbar, which isn't ideal because I want it to be ...

Unveiling the intricacies of CSS specificity

Struggling with the specificity of this particular CSS rule. Despite researching extensively, I still can't seem to grasp it. Can someone help me determine the specificity of the following: #sidebar h1, p em, p a:hover{ ... } ...

What is the best way to update specific content with fresh URLs?

I am interested in keeping certain sections of my page static, such as the header and footer, while allowing the main content to change dynamically. Additionally, I would like the URL to update based on the new content being displayed. Although I am famil ...

Type of element returned

Is there a way to retrieve the element type? I'm interested in applying the style of a class to HTML5 elements without using the class attribute. <!DOCTYPE> <html> <head> <title>My page</title> </head& ...

Struggling with CSS float problems

I'm currently working with the Pure CSS framework and my code resembles this: <div class="container pure-g"> <header class='pure-u-1'> <h1 class='logo'> <a href="#">TEST</a> </h1> &l ...

Display of content visible via stationary div

Recently, I successfully implemented a fixed div that remains at the top of my webpage. However, I encountered an issue where text would appear through the div when scrolling. You can witness this phenomenon by visiting this site and simply scrolling down ...

Quantities with decimal points and units can be either negative or positive

I need a specialized input field that only accepts negative or positive values with decimals, followed by predefined units stored in an array. Examples of accepted values include: var inputValue = "150px"; <---- This could be anything (from the input) ...

Transferring a moving CSS element from one div to another

Can you please advise on the most effective method for moving an element between divs in responsive design? This is the current structure of my page: <div class="container"> <div class="desktop"><h2>Hello, I am displaye ...

Ways to retrieve data from a URL and pass it to JavaScript code

I am currently experiencing difficulties in obtaining the values from an object requested through a link on my website. The issue arises from the fact that I have created a PHP method to retrieve data from the database for the values of a particular objec ...

How can JavaScript be used to apply CSS formatting to text that appears as a new HTML element?

It's unclear if the question accurately reflects what I want to achieve. If I have a form that fades out and is then determined whether to display again through a cookie, can the confirmation message be styled using CSS? For example, I would like to ...

Using Django and Python to pass text strings in views.py

Within my template, I've implemented the following code: <span class="state-txt">{{ state }}</span> This is how it's being handled in my views.py using an if/else loop: if user is not None: if user.is_active: ...

In what way does Google+ make their logo come to life on the left side of the navigation bar when the scrollbar is minimized?

I'm curious about the animation Google+ uses to make their logo slide in on the navigation bar when it shrinks. I've managed to shrink the scrollbar on scroll, but I can't quite replicate their technique for animating the icons. I'm eag ...

Utilizing HTML/CSS with React/Bootstrap: A Comprehensive Guide

Looking for help with integrating HTML/CSS code into React/Bootstrap? I need assistance with coding in React using Bootstrap. How do I effectively use components and props? Even after installing bootstrap, I am encountering errors. Is it possible to dire ...

Align the text field value with the corresponding tooltip content in Vuetify

<v-col class="d-flex align-center"> <v-tooltip bottom> <template v-slot:activator="{ on }"> <v-text-field v-on="on" :value="info.payeeNo" den ...