Create a triangular shape to fit on the right side of a 'li' element

After defining the following HTML:

<ul class="steps d-flex">
   <li class="step">Basic Details<div class="triangle triangle-right"></div></li>
</ul>

With this given CSS:

.steps li {
    @extend .text-center;
    position: relative;
    overflow: hidden;
    flex: 1 1;
    align-items: center;
    justify-content: center;
    background: $grey-600;
}

The current appearance is like this:

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

I aim to create a triangle pointing towards the right of the div, similar to this setup:

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

Note how the payment section has a triangle that seems to be indicating 'done'

How can I achieve this effect?

Answer №1

Building a breadcrumb or steps-progress component using CSS is easily achievable with the clip-path property and CSS calc() function. In this example, I utilize a CSS variable --s to define the size of the arrow according to your preference:

/*QuickReset™*/ * { margin: 0; box-sizing: border-box; }
body { font: 1rem/1.4 sans-serif; }

/* STEPS COMPONENT */

.steps {
  --s: 2rem; /* size of the arrow */
  display: flex;
}

.steps > * {
  flex: 1;
  text-align: center;
  text-decoration: none;
  color: #333;
  font-size: 1.4rem;
  background: #ddd;
  padding: 1.5rem 1rem;
  /* Arrow shape */
  clip-path: polygon(0% 0%, calc(100% - var(--s)) 0, 100% 50%, calc(100% - var(--s)) 100%, 0 100%, calc(0% + var(--s)) 50%);
  margin-left: calc(var(--s) * -0.7);
}

.steps > :first-child {
  clip-path: polygon(0% 0%, calc(100% - var(--s)) 0, 100% 50%, calc(100% - var(--s)) 100%, 0 100%);
  margin-left: 0;
}

.steps > :last-child {
  clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0 100%, var(--s) 50%);
}

.steps .is-active { background: #0bf; }
<nav class="steps">
  <a href="">Details</a>
  <a href="" class="is-active">Payment</a>
  <a href="">Done</a>
</nav>

Answer №2

To start, the creation of a chevron element is necessary. Utilizing CSS is a simple method to achieve this task. Once created, it can be incorporated into your project.

My recommendation is to utilize the flexbox technology as it offers a more efficient and stylish approach to solving these types of challenges.

Below is an illustration pertaining to your inquiry.

.chevron-right {
  box-sizing: border-box;
  position: relative;
  display: block;
  transform: scale(5.5);
  width: 22px;
  height: 22px;
  border: 2px solid transparent;
  border-radius: 100px
}

.chevron-right::after {
  content: "";
  display: block;
  box-sizing: border-box;
  position: absolute;
  width: 10px;
  height: 10px;
  border-bottom: 1px solid;
  border-right: 1px solid;
  transform: rotate(-45deg);
  right: 8px;
  top: 4px;
  color: white;
}

.container {
  font-size: 30px;
  display: flex;
  flex-direction: row;
  background-color: lightgrey;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.payment {
  margin-left: 20px;
  margin-right: 20px;
}

.done {
  margin-left: 20px;
  margin-right: 20px;
}
<div class="container">
  <div class="payment">Payment</div>
  <i class="chevron-right"></i>
  <div class="done">Done</div>
</div>

Answer №3

Looking at the HTML and CSS you've provided, it seems like you're attempting to create a triangle using a div that has the class of "triangle-right". To achieve this right-pointing triangle effect, you can utilize either the ::before or ::after pseudo-elements along with the border technique to shape the triangle. Follow these steps:

.triangle-right::before {
    content: '';
    position: absolute;
    top: 50%;  /* centers the triangle vertically */
    left: 100%;  /* positions the triangle to the right of its containing 
div */
    width: 0; 
    height: 0;
    border-top: 10px solid transparent;  /* adjust size as necessary */
    border-bottom: 10px solid transparent;  /* adjust size as necessary */
    border-left: 10px solid $grey-600;  /* customize size and color as 
necessary */
    transform: translateY(-50%);  /* perfects the vertical centering 
*/
}

By applying this CSS code, your triangle will point towards the right side and be displayed on the right edge of the element with the class of "triangle-right". Modify the border dimensions accordingly to alter the triangle's size.

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

Unusual SVG Cubic Bezier Animation Transforming Curves into Points and Lines

As a beginner in SVG CSS animation, I am currently working on morphing three curved paths into three rectangles using cubic bezier routes. While I have managed to make it work, there is an issue during the transition where parts of it skew inward in a stra ...

Problem with Silverstripe: Replicating Site configuration or settings menu

I have duplicated the Settings menu (siteConfig) folder in order to create a new CMS menu for inputting company information. The functionality of the menu is working correctly, however, the CSS styling is not loading properly. Image: https://i.stack.imgur ...

Change the font style of individual characters within a string using CSS or JavaScript, instead of applying it to the entire

Is it feasible to assign a specific font to a particular character? I would like: #test_id{ font-family: "digitalfont"; font-family-of-, : "HelveticaNeue-Light"; } In order to set a font for a comma and a separate font for the rest, do I need to ...

What could be the reason for my new course not being recognized?

Looking to modify the behavior of a button where, when clicked, it hides a specific div element, changes its text display, and toggles between classes using addClass/removeClass for subsequent click events. Unfortunately, the intended functionality is not ...

Aligning text and lists using Bootstrap on both desktop and mobile devices

I want to format a text with a list similar to the images provided https://i.stack.imgur.com/6giDH.png for desktop and tablet view, but for mobile display I would like it to look like this https://i.stack.imgur.com/7zSXi.png This is my current approach ...

Tips for maintaining consistent formatting of a div element across various sizes

Looking to create a Bootstrap Jumbotron that features a background image, text (h1 and p), and a call-to-action button that scales appropriately for different viewports without sacrificing formatting. Essentially, the goal is for the div to behave like an ...

How can I dynamically change the orderBy parameter based on conditions in an Angular application?

I need to dynamically change the orderBy parameter in a table row based on the result of a method. Here is an example of my current tr setup - <tr class="pl" ng-repeat="thing in things | orderBy:'oldId':reverse" ng-class="{'row-error&apo ...

"Unlock the secrets to commanding respect in the web form layout by mastering the art

I have a fieldset that includes a legend with potentially long text content. I need the legend to only take up 50% of the width of the fieldset. Currently, when the legend is too lengthy, it still occupies 50% of the space but causes the entire fieldset ...

I'm looking to center my btn button vertically and horizontally over two images using Bootstrap 5 in a responsive manner. How can I achieve this?

Any tips on how to position my btn button both vertically and horizontally in the center on top of two images? I want it to look like this: https://i.sstatic.net/Rezrd.png This is the code for my button: <a class="btn btn-primary" href=" ...

Steps for Removing Multiple CSS Styles from an Element's Style:

When my application generates inline styles, I sometimes need to remove the height, width, and max-width styles from certain elements. I have identified the element using this code: const elems = window.$('.myElements'); window.$.each(elems ...

Having trouble making an HTML5 video work on Android? It seems to play fine when clicking the play button,

It has come to my attention that html5 video on Android devices is unable to autoplay. Currently, the video only plays on the device when the user manually clicks on the play button. <video width="640px" height="360px" src="media/video/Moments_of_Every ...

I'm having trouble modifying the border color of a TableCell component in material-ui

I am struggling to change the border color of a table cell. Despite numerous attempts, I have been unsuccessful in my efforts. Can someone please assist me with this? const customTheme = createTheme({ overrides: { MuiTableCell: { root: { ...

Issue with Flex display not being applied within Material UI Grid Item

I am attempting to position an element at the end of a Grid Item (horizontally). In order to achieve this, I am enclosing my component in the following div: Here is the complete code snippet: <Grid container spacing={8} alignItems={'stretch' ...

Adjusting height in CSS can be done dynamically based on the content within

Currently, I am working on a project where I need the submenu to adjust based on content. The issue is that right now, the submenu has a fixed capacity of 4 items. For example, when you click on 'sale', it displays 4 submenu items perfectly. Howe ...

Having four videos displayed on one webpage can cause the browser to function at a slower

I have videos that are around 30 seconds long and 15mbs. I'm wondering if it's feasible to include them on a page or if I should opt for cover images instead. I would like to use the video as a separator similar to the design showcased at Does a ...

Using node appendChild() in HTML for animating elements

As someone who is brand new to the world of web development, I am trying my hand at creating a webpage that expands as a button is clicked. Recently, I stumbled upon this helpful link which includes some code: The HTML code snippet is: <ul id="myList" ...

How to Retrieve Checkbox Values from Multiple Rows Using JavaScript

I have a variety of module rows that allow users to manage access rights by selecting specific options. My goal now is to extract the checked boxes from these checkboxes with the name "config{{$field->id}}". Below is the current functioning code. HTM ...

Aligning SVG shapes within each other

I recently encountered a scenario where I needed to position SVG shapes in the center of each other with varying scales. For instance, placing a rectangle or triangle within the center of a circle. While I found some solutions that worked for shapes like ...

Disabling autocomplete in the DOCTYPE HTML tag in Visual Studio Code prevents it from appearing when

When I type !, it should show up like in the image. However, it's not working. Could it be because Visual Studio Code got an update? Or did I make a mistake on my end? I found a workaround by using html:5, but I'm still curious to know if this ...

The minimum height increases as the inner divs expand

Can you help me with the code below? html <div class="content"> <div class="col"> </div> <div class="col"> </div> <div class="col"> </div> <div class="col"> </div> ...