Creating a Triangular Button with Icon Design with Bootstrap 4

I'm attempting to design a CSS triangular button that is positioned in the top left corner of a div using Bootstrap 4.

Here's my concept as crafted with Photoshop:

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

However, after numerous attempts, this is the closest I've come to achieving it:

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

I want to style it so that the button can adapt its colors based on one of the btn-*style* classes, allowing for versatility in color selection.

Any suggestions?

Below is the triangle CSS code snippet:

.triangle-up-left {
    float          : left;
    width          : 0;
    height         : 0;
    margin         : 0;
    padding-bottom : 10%;
    padding-left   : 10%;
    overflow       : hidden;
    text-align     : center;
    }

.triangle-up-left:after {
    content           : "";
    display           : block;
    width             : 0;
    height            : 0;
    margin-left       : -500px;
    border-bottom     : 500px solid transparent;
    border-left-width : 500px;
    border-left-style : solid;
    }

And here is the current div layout I am utilizing:

<div class="row border rounded p-2 mx-2 mt-3 shadow-sm">
    <a href="#" role="button" class="btn btn-warning triangle-up-left"><i class="far fa-star"></i></a>

    <div class="d-none d-sm-none d-md-block col-md-1 p-0 m-0  mx-sm-auto">
        <img src="...image.."" alt="Image" class="img-fluid rounded-circle m-3"></a>
    </div>

    <div class="col-md-9 pl-sm-0 pl-md-5 m-0 ">
    some text
    </div>

    <div class="col-sm-1 col-md-2  p-0 pt-4 m-0  text-center ">
        Some text
    </div>
</div>

Answer №1

Here is a code snippet that does not rely on any Frameworks, which could be useful for you.

function myFunction(){
alert("Hello, it's working");
}
.box{
  background-color:white;
  width:50%;
  height:100px;
  border:1px solid gray;
  border-left:4px solid gray;
}
button>span{
  color:black;
  font-weight:bold;
  position:absolute;
  left:12px;
  z-index:1;
  top:15px;
  
}
button{
  cursor:pointer;
  background-image:url();
  width:50px;
  height:50px;
  background-color:transparent;
  border:none;
  font-size:1.2em;
  border-radius:7px;
  
}
button:focus {outline:0;}
button::before,
button::after {
    content: '';
    position: absolute;
    top: 7px;
    left: 7px;
    border-color: transparent;
    border-style: solid;
}

button::after {
    border-width: 1.3em;
    border-left-color: yellow;
    border-top-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <div class="box">
    <button onclick="myFunction()"><span>☆</span></button>
  </div>
</body>
</html>

Answer №2

To tackle the issue at hand, I employed a background linear gradient solution. It's worth noting that this workaround is specifically designed for white backgrounds. This is because I utilized a white triangle mask in the ::after pseudo element to ensure compatibility with the btn-*style* bootstrap classes. If you decide to alter the background color of the row, remember to adjust the color of the ::after mask accordingly!

UPDATE 1: I positioned the triangle absolutely to prevent any interference with the bootstrap col system (you can remove it if desired)

.row {
    position:relative;
    min-height:100px;
}

.triangle {
    display:block;
    width:50px;
    height:50px;
    font-size:21px;
}

.triangle::after{
    content:"";
    position:absolute;
    top:0;
    left:0;
    right:0;
    bottom:0;
    background:linear-gradient(to left top, #ffffff 49%, transparent 50%);
}

i {
    position:absolute;
    top:5px;
    left:5px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" media="screen" rel="stylesheet"/>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="container">
    <div class="row border rounded mx-2 mt-3 shadow-sm">

        <a href="#" class="triangle btn-warning position-absolute">
            <i class="fa fa-star-o" aria-hidden="true"></i>
        </a>

       
    </div>

    <div class="row border rounded mx-2 mt-3 shadow-sm">

        <a href="#" class="triangle btn-info position-absolute">
            <i class="fa fa-star-o" aria-hidden="true"></i>
        </a>

        
    </div>

    <div class="row border rounded mx-2 mt-3 shadow-sm">

        <a href="#" class="triangle btn-danger position-absolute">
            <i class="fa fa-star-o" aria-hidden="true"></i>
        </a>

        
    </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

Scroll Behavior Issue in Bootstrap 4.4.1 Accordion Component on Google Chrome - Possible Bug?

I've been using Bootstrap accordions for quite some time, but a new issue has recently cropped up. If you play around with the code below (preferably in full screen mode), scroll down until the accordion is visible, then open the middle card by toggl ...

Enhance the numerical value displayed in jQuery UI tooltips

I have folders with tooltips displaying text like '0 entries' or '5 entries' and so on. I am looking for a way to dynamically update this tooltip number every time an item is added to the folder. The initial count may not always start a ...

Can Python be used to determine if a website is responsive?

I am currently utilizing Python3 alongside BeautifulSoup. I am interested in determining whether a website is responsive or not. Initially, I considered checking the meta tags of a website to see if it contains something like this: content="width=device- ...

Making a form select element responsive within a bootstrap 4 card component

Check out the code snippet I have here: This is how I envisioned it to look, and it works perfectly at large resolutions. However, when I scale it down to tablet size, the resizing of the select element doesn't seem to work properly. Here's a s ...

Error: Unable to locate module '@material/core/Grid'

After cloning a repository that is built with MUI, I noticed that certain components were already imported and working seamlessly. These components include: import Card from '@mui/material/Card' import CardActions from '@mui/material/CardAct ...

Incorporate text into the URL of the image

Got a URL of an image like this: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg', and looking to add 240x240 within the URL. Current Url: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg Desire ...

Vue.js fails to display multiple uploaded images

I have been working on implementing a multiple image upload feature using vue.js. So far, everything seems to be functioning correctly. However, I am facing an issue where the HTML does not display thumbnails of the images I have selected. Additionally, I ...

interactive navigation menu with countdown feature

Looking to create an HTML/jQuery dropdown menu, but encountering a problem. The goal is for the div to slide down when the mouse hovers over the "games" navigation button and only disappear after 5 seconds of the mouse being out, not instantly. Here's ...

Experience the smooth transition effects of sliding down the login box when hovering over an image or div

Is there a way to switch the hover js transition effect to a different transition type like fadeIn, fadeOut, or simple transitions? Check out this link for more details. Here is an example of the HTML code: <img id="login-trigger" src="http://www.join ...

Angular 6 - Implementing a collapsible mobile menu with a click event

On my static Angular 6 page, I have tabs and I'm trying to figure out how to collapse the mobile menu when an element is clicked. I need a solution similar to the jQuery code shown below, but I can't use jQuery. $('.navbar-nav>li>a&ap ...

Processing AJAX request without reloading the page using PHP

I need assistance with creating an image cropping tool that allows users to upload a picture, crop it, and display it on the page as an image.jpg rather than as base 64 (data:image). I have tried using HTML and JavaScript but it seems impossible, so now I ...

Persistent Footer and Internet Explorer Compatibility

I'm facing an issue where my sticky footer doesn't stick to the bottom when there's a floating element in my content. Instead, it sits at the end of the non-floated content. My goal is to have the footer at the bottom of the page window if ...

Tips for ensuring Opera browser compatibility with buttons using the CSS background image property

Is there a way to make background images work on buttons in Opera web browser using CSS? I've tried using the background image property, but it doesn't show up when I use Opera. Strangely enough, it works fine in Firefox. However, I have noticed ...

CSS: Create a form with two input fields where the left field is adjustable in width and the right field fills up

------------- -------------------------------------------------- | some unique text | | some more original content, varying length | ------------- -------------------------------------------------- |<---------------------- 100% width ------------------ ...

The function I created is having trouble connecting to the controller.js file

My JavaScript controller file function ServicesCtrl($scope) { console.log("Greetings from ServicesCtrl"); $scope.message = "Hello!"; } The main HTML file <html> <head> <title>The Complete Web Development Stack</ti ...

Combine Two Values within Model for Dropdown Menu

I am currently facing a situation where I have a select box that displays a list of users fetched from the backend. The select box is currently linked to the name property of my ng model. However, each user object in the list also contains an email proper ...

Ways to avoid a child element from overlapping another element

(I acknowledge that the question may sound a bit off, so please feel free to edit it if you can rephrase it better). So, I have this: jsFiddle As you can see, I'm attempting to recreate a basic Windows 7 aero effect (just for curiosity, fun, and lea ...

What is the method for calculating values entered into dynamic input fields?

I am facing a slight issue with my HTML form code... Within my code, there are several input fields that are being generated dynamically. Each dynamic input field contains a numerical value which I need to use for mathematical calculations. The calculati ...

What is the method used to create the scrolling effect on this website?

I am in the process of creating my own personal website and I would like it to function similar to the following example: Instead of the typical scrolling, I'm interested in transitioning between different sections on the page. Could this be achieved ...

What is the best way to position the logo on the left side and the navigation on the right

How can I position my logo to the left of the navigation bar? This is my current HTML code: <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> <link rel="icon" type="image/png" href="SENCOR_Logo.ico"> & ...