Center text within CSS shapes

.myButton {
  float: right;
  border-top: 40px solid pink;
  border-left: 40px solid transparent;
  border-right: 0px solid transparent;
  width: 25%;
}
<div class="myButton">
  Submit
</div>

This snippet is the code I have written to create a button with a unique shape, similar to the image below. However, I am facing an issue where the text "Submit" is not centered as intended and appears lower than expected. Can anyone provide a solution to this problem?

https://i.stack.imgur.com/CGOQq.png

Answer №1

To create a unique background effect, you can utilize the linear-gradient property. This technique involves setting a fixed height and then applying padding that is equal to the height multiplied by the square root of 2:

.my-button {
  border: 0;
  height: 40px;
  background: linear-gradient(45deg, transparent 40px, pink 40px);
  padding-left: 56.5691px; /* 40 × √2 ≈ 56.5691 */
}
<button class="my-button">Submit</button>

You can also achieve a similar result using an absolutely positioned pseudo-element:

.my-button {
  background-color: pink;
  position: relative;
  height: 40px;
  margin-left: 40px;
  border: 0;
}

.my-button:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  /* Move pseudo-element to the left to 100% of its width */
  transform: translateX(-100%);
  border-top: 40px solid pink;
  border-left: 40px solid transparent;
  border-right: 0px solid transparent;
}
<button class="my-button">Submit</button>

Answer №2

The problem lies in the fact that you are using a top border instead of a background, causing your text to appear off-center within your shape. To resolve this issue, you can utilize positioning to manually adjust the position of your text:

.myButton {
  float: right;
  border-top: 40px solid pink;
  border-left: 40px solid transparent;
  border-right: 0px solid transparent;
  width: 25%;
  position: relative;
}

.inner {
  position: absolute;
  top: -30px;
  left: 40px;
}
<div class="myButton">
  <div class="inner">Submit</div>
</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

Enhance the appearance of a custom checkbox component in Angular

I developed a customized toggle switch for my application and integrated it into various sections. Recently, I decided to rework it as a component. However, I am encountering an issue where the toggle switch button does not update in the view (it remains t ...

The content at the center of the page is shifting to the right side when the browser is resized

I am currently working on creating a random quote generator at the following link: http://codepen.io/Kestvir/pen/peqjZZ. During the html/css aspect of the project, I have run into an issue with the content inside a transparent background. Whenever I resi ...

How can I use jQuery to create a new div element if it is not already present

I am familiar with how to add a class if it does not already exist, for example: $("ul:not([class~='bbox'])").addClass("bbox"); Alternatively, if(!$("ul").hasClass("class-name")){ $("ul").addClass("bbox"); } However, I am unsure how to c ...

Why won't the main bar column in Bootstrap extend to the entire width of the page?

I've set up a basic row with two child columns: the sidebar column with className='col-md-2' and the mainbar column with className='col-md-10'. The parent row has a display:flex property to position the two children side by side. & ...

What is the best way to ensure that the same information is included on every page of

Currently developing a dynamic website where certain components such as the fixed header, menubar, and footer are common across multiple pages. What is the best way to include these fixed components on every webpage? I am utilizing JSP and JavaScript for ...

Is it possible to refresh resources in Node.js without the need to restart the server?

Is there a middleware or library that allows access to files added after the server starts without requiring a restart? I currently use koa-static-folder, but it seems unable to handle this functionality. ...

Utilizing Vue CSS variables as inline styles

Incorporating CSS variables in my component has allowed me to dynamically set the width of a div based on computed data within the setup() setup(props) { const progressBar = PositionService.getProgressBar(props.position); const progressWidth = `${p ...

Switch the visibility of an HTML input styled as a "spinner"

I have managed to create a CSS-based method for toggling the visibility of span or input elements depending on whether a radio button is checked. I got inspired by this insightful question on Stack Overflow. However, I encountered an issue where this togg ...

The functionality of cloning in jQuery may encounter an issue where the text field remains enabled if the user selects an option labeled "other

Currently, I am working on a jQuery clone with my existing code and everything is functioning well. In the first scenario, if the user selects other from the dropdown menu, the text field becomes enabled. In the second scenario, when the user clicks ...

Upon loading the webpage, Firefox prompts the user to download the site

While my website functions properly on Internet Explorer, I encounter an issue when trying to open it on Firefox. Instead of opening the page, Firefox prompts me to download the file and opens the Download File dialog. This problem also occurs at times in ...

Tips for aligning a Material UI FAB button in the center and keeping it in the center while resizing the window

Is there a way to center a MaterialUI FAB button and keep it centered while resizing the window? The current positioning is not centered, as seen in the screenshot below, and it does not adjust with window size changes. Below is the code for the current F ...

Surprising Results when Using getBoundingClientRect()

Check out this code on CodePen In my current project, I'm attempting to position the footer div at the bottom of the page in such a way that it aligns with the maximum value of the bottom coordinates of both the menu and content divs (Math.max(menu, ...

Transform the check box into a button with a click feature

I've customized a checkbox to resemble a button, but I'm encountering an issue where the checkbox is only clickable when you click on the text. Is there a way to make the entire button clickable to activate the checkbox? .nft-item-category-lis ...

Update the button functionality according to the button's unique identifier

I am trying to dynamically change the button's redirect link based on its ID in my NEXT.JS project, but as a newcomer to this framework, I am unsure of how to accomplish it. I understand that this modification should be done after rendering, possibly ...

Chaos in the IE8 drop-down menu situation

I'm having trouble with my code for a menu with drop-downs in ie8. It works fine in all other browsers but there seems to be an issue with the positioning of the main menu and submenus. Any thoughts on what might be causing this? <ul id="nav" clas ...

Ways to determine the height of a responsive div's background image

I have a container with a background image. Inside this container, there are additional elements like my navigation bar. My goal is to make the container have the same height as the background image. I've attempted the following: .bg { ...

Retrieving chosen items from dynamically generated checkboxes using Angular 2+

I have a piece of HTML code where I am trying to capture the value of all selected checkboxes whenever any checkbox is checked or unchecked, triggering the (change) event. <div class="checkbox checkbox-primary" *ngFor="let category of categories; let i ...

Verifying internet connectivity and updating content using jQuery and JavaScript

Upon loading the page, the following functionality occurs without triggering a click event: The updated code attempts to determine if the internet connection is active. If the connection is off, the 'link' on the page will be disabled (non-click ...

Issues with webpage responsiveness, width not adjusting correctly

Just completed coding my website and now I'm facing a new challenge. I am focusing on making it responsive for tablet screens (768px) starting with the header section, but for some reason, the modifications I make in the responsive.css file are not ta ...

I'm having trouble retrieving and rendering data in HTML using AngularJS and Spring

After fetching the data, I am unable to see it in the HTML code. The web browser console displays the JSON data but it is not visible in the HTML code. Here is my AngularJS code: $scope.refresh = function(){ fetchData(); }; $scope.fetchData = functio ...