Tips for adding both a border and bottom border to a button

How can I style a button like this example?

Below is my current code:

.button {
  border-color: red;
  border-radius: 8px
}

.button::after {
  content: '';
  background-color: red;
  display: block;
  width: inherit;
  height: 3px;
}
<button class="button">Click me </button>

The issue I'm facing is that the button's width extends to 100% of the screen, but I need it to be the width of the button itself. The challenge is that the button text is dynamic, so I can't specify a fixed width for it.

Answer №1

To ensure that the button does not expand to full width, wrap it in a div with display: inline-block. Additionally, use relative positioning on the div so that it can contain an absolutely positioned element within. To add spacing between the button and the line below as seen in the image, apply some padding to the element. For the :after pseudo-element, make it absolutely positioned and set its width: 100%.

div {
  position: relative;
  padding: .75rem .5rem;
  box-sizing: border-box;
  display: inline-block
}

button {
  background: transparent;
  border-radius: .5rem;
  border: 3px solid #EF544F;
  padding: .5rem;
  color: #EF544F;
  font-weight: 600;
  font-size: 1.2rem
}

button::after {
  content: '';
  background-color: #EF544F;
  display: block;
  position: absolute;
  width: inherit;
  height: 4px;
  width: 100%;
  bottom: 0;
  left: 0;
  border-radius: 999px;
}
<div>
  <button>Click me</button>
</div>

Answer №2

It appears to resemble the image displayed:

.button
{
  border-radius: 0.5em;
  padding: 0.4em 0.8em 0.2em 0.8em;
  color: red;
  font-weight: bold;
  border: 2px solid red;
  background-color: transparent;
}

.button::after
{
  content: '';
  background-color:red;
  display: block;
  height: 3px;
  position: relative;
  left: -1.3em;
  bottom: -1em;
  width: calc(100% + 2.6em);
}
<button class="button">Tab Tile</button>

Answer №3

consider using a "min-width" value

  .button::after{
         content: '';
         background-color:red;
         display: inline-block;
         width: auto;
         height: 3px;
         min-width:100px;  
   }

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

Employing Modernizr along with jQuery for Animation in Cases when CSS3 Transitions are Absent

Can Modernizr and jQuery be combined to create a transition effect similar to CSS3 if it's not supported? My current approach looks like this... <div class="hoverable"> <p>This div changes both width and height on hover</p> </di ...

Can image data be extracted from a canvas without needing to physically render the canvas?

I am currently working on an Angular application that utilizes Chart.js to create dynamic charts in various views. Additionally, the application has the capability to export data to a PDF format using the PDFMake JavaScript library, which is a basic tool a ...

HTML Element Split in Two by a Line in the Middle

Greetings to all, after observing for a while I have decided to make my first post here (and just to clarify, I am an electrical engineer, not a programmer). I am in search of creating a unique element in HTML where I can detect clicks on both its upper a ...

Card Alignment with Bootstrap

I need help aligning the last two elements in the card. I want the first one (a button) to be on the left, and another element (a Div with multiple social icon images) on the right side. Can someone provide guidance on how to achieve this layout? <!D ...

Choose a particular element within an element class using a variable as the selector in jQuery

Is there a way to dynamically add a class to a specific element in an array based on a variable in a for loop, rather than random selection? I need to target elements with the variable provided and possibly apply the class to more than one element if neces ...

Any advice on how to horizontally center my css slideshow?

After using jsfiddle for the file, I encountered an issue with centering the slideshow at the bottom. Despite my efforts, it remains aligned to the left margin. Check out the end results here: https://jsfiddle.net/n6kae2rn/ https://jsfiddle.net/n6kae2rn ...

Ensuring consistent input width in a table for all columns in reactjs and bootstrap, regardless of their content

I'm currently designing a bootstrap table in reactjs, complete with a filter beneath each column header. Take a look at the code snippet below: <table className="table table-striped"> <thead> <tr> <th>#</th> ...

Unusual behavior in ReactJS and HTML5 video player observed

I currently have two React presentation components that include HTML5 video tags. The first component, named Homepage, is as follows: export default class Homepage extends React.Component { render() { return( <div className="Homepage"> ...

The navigation on my mobile collapses whenever I use the Hello bar app

I am facing a problem with my Shopify theme. I recently installed an app called "Hello Bar" to show offers on all pages, but it seems to be incompatible with my theme. The developers of the app suggested adding the following code to my CSS file: @media ...

Angular Appreciation Meter

Looking to create a rating system using Angular. The square should turn green if there are more likes than dislikes, and red vice versa (check out the stackblitz link for reference). Check it out here: View demo I've tried debugging my code with con ...

Ways to define the background color of a form group

I am trying to customize the background color of a form-group element. To illustrate my point, take a look at the image below In the image above, I have highlighted the div in question and I want to change its background color from within the form-group. ...

Issue with $http.get in fetching information from PHP server

Dealing with duplicate keys error in AngularJS, PHP, and MySQL I have been working on a web-based system and encountered an issue with ng-repeat displaying duplicate keys. I am unsure how to resolve this issue. The select all brand functionality seems to ...

Height not being applied to DIV container

I'm facing an issue with my CSS code that is causing images to break the row after reaching the end of the DIV. However, the container behind it is not adjusting its height according to the images. The height should expand along with the images. Here ...

What could be the reason for my GitHub Pages site not appearing on the screen?

I am diving into the world of website creation and hosting, embarking on a journey to share my code on Github and showcase it using Github pages. After setting up a new repository and uploading my project files including an HTML file, a CSS file, and an i ...

Images in the center with a div overlay on top

I'm attempting to place a div or span on top of an image that is positioned above another image. Both images are centered and resized to fit. The height of my images is greater than the width, but they are not set at a fixed size. I want the div to pe ...

What causes offsetHeight to be less than clientHeight?

INFORMATION: clientHeight: Retrieves the height of an element, taking into account padding offsetHeight: Obtains the height of an element, considering padding, border, and scrollbar Analyzing the Data: The value returned by offsetHeight is expected to ...

The hovering of the mouse over a dropdown menu causes a division on the page to shift

Creating a website with a dropdown menu and slider division can be tricky. When hovering over the menu, the submenu displays but causes the slider division to shift down. Does anyone have suggestions on how to fix this issue? Below is the code: <html& ...

Contrasts between space and the greater than selector

Can you explain the distinction between selector 6 and selector 7 as outlined on the w3 website? Inquiring minds want to know. ...

Tips on maintaining the Parent menu in a hovered state when the mouse is over the child menu within a Dropdown Menu

I have been working on creating a dropdown menu that functions correctly. However, I am facing an issue where the top menu, when hovered, turns white, but as soon as I move down to the submenus, the top menu reverts back to its original color. Is there a ...

Resizing the elements within an iframe: Troubleshooting problems with embedding Bandcamp players

My goal is to embed a Bandcamp music player on my WordPress blog page, but I'm facing a challenge. The maximum width of the current player is 700px and I need it to be 900px. After consulting with someone at Bandcamp, they directed me to the old versi ...