Creating custom outlines for CSS shapes

Exploring the world of CSS shapes has left me wondering - is it possible to give them solid, dotted, or dashed borders? I came across some examples of shapes that caught my eye (shapes), but making them stand out with different border styles posed a challenge.

Initially, I considered overlaying another shape in the background using z-index (http://jsfiddle.net/gYKSd/), however, this only achieved a solid border effect.

Here's an example snippet:

<div class="triangle"></div>
<div class="background"></div>

The CSS used for styling:

.triangle {
    position: absolute;
    top: 14px;
    left: 10px;
    height: 0px;
    width: 0px;
    border-right: 50px solid transparent;
    border-left: 50px solid transparent;
    border-bottom: 100px solid red;
    z-index: 0;
}
    
.background {
    position: absolute;
    top: 0;
    left: 0;
    height: 0px;
    width: 0px;
    border-right: 60px dotted transparent;
    border-left: 60px dotted transparent;
    border-bottom: 120px dotted gray;
    z-index: -1;
}

Answer №1

Using a background div for positioning is one of the more practical methods in CSS when it comes to creating shapes that aren't inherently recognized by browsers.

For example, creating a point burst may involve rotating square copies, but the browser will still see them as individual squares rather than a cohesive shape. This means borders around each square if applied.

While it is possible to create borders using small rotated rectangles positioned on the edges of your desired "shape," the process can be complex and not very efficient.

If drawing shapes dynamically is a requirement, you might want to consider utilizing HTML5 canvas for more flexibility and control over shapes. Check out Intro to canvas drawing for more information.


Additionally, SVG has become a viable option with widespread support from modern browsers. See this link for information on supported browsers.

Answer №2

I believe it may not be achievable, as your method depends on the shapes being constructed using borders.

It is not feasible to append borders onto an existing border.

However, as demonstrated in your example, you can simulate an additional border by utilizing a background shape.

Answer №3

Like mentioned by some before....CSS shapes are created using solid borders and the way those angles interact, meaning altering the border-style could result in a different shape than intended

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

Finding a specific button among a list: tips and tricks

I am currently working on a Selenium script to automate a webpage. My task involves clicking on a button that is part of a list. Here is an image of the web UI I am referring to - the button is labeled as New Account Below is the XML code snippet: <d ...

What is the procedure for retrieving values from a table?

<tbody> <tr> <td><span style="">Price</span></td> <td><span style="">:</span></td> <td><span style="">660</span></td> </tr> <tr> <td><s ...

Bringing in PeerJs to the NextJs framework

Currently delving into NextJs and working on creating an audio chat application, I've hit a roadblock while attempting to import PeerJs. An error message keeps popping up saying 'window is not defined'. import Peer from 'peerjs'; ...

A guide to displaying SVG graphics in a Jupyter notebook with the help of jsdom, D3, and IJavascript

Although I'm not an expert in frontend development, I have been experimenting with Javascript and D3 lately. As someone who typically works on scientific analysis in Python using Jupyter Notebooks, I thought it would be interesting to apply a similar ...

HTML5 number input type formats long numbers differently

In my application, users are required to enter a minimum of 15 digits in a number input field. However, if a user enters a longer number, for example, equal to or greater than 19 digits, and then uses the arrow buttons or keyboard keys, the number is auto ...

The importance of dependencies in functions and testing with Jasmine

In troubleshooting my AngularJS Service and Jasmine test, I encountered an issue. I am using service dependency within another service, and when attempting to perform a Unit Test, an error is thrown: TypeError: undefined is not an object (evaluating sso ...

Easiest method to change cursor to 'wait' and then return all elements to their original state

On my website, there are certain CSS classes that define a specific cursor style when hovered over. I am trying to implement a feature where the cursor changes to a "wait" image whenever an AJAX call is initiated on any part of the page, and then reverts b ...

What is the best way to send a JSON response from a Symfony2 controller?

I am utilizing jQuery to modify my form, which is created using Symfony. The form is displayed within a jQuery dialog and then submitted. Data is successfully stored in the database. However, I am unsure if I need to send some JSON back to jQuery. I am ...

What is the best way to create random integers using JavaScript?

Is there a way to create a function called drawSomething(x, y, color, boolean) that will generate random integers for the position of x and y on the canvas, randomly select a color from red, yellow, or blue, and randomly assign true or false to the boole ...

Using Angular to condense or manipulate an array

I am working with a JSON response that contains arrays of arrays of objects. My goal is to flatten it in an Angular way so I can display it in a Material Table. Specifically, I want to flatten the accessID and desc into a flat array like [ADPRATE, QUOCON, ...

Having trouble populating the chosen response from ajax into a select dropdown

Here is the JSON response I have received: { "respData": [ { "salesTargetId": "c51a411e-44ed-496c-85c0-52d76bd97c64", "userId": "5f32aa84-c9e7-4c27-a292-0f06ffe07b82", "type": "SALES", "month": "April", "year": "2021", ...

Is there a way to simulate a KeyboardEvent (DOM_VK_UP) that the browser will process as if it were actually pressed by the user?

Take a look at this code snippet inspired by this solution. <head> <meta charset="UTF-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> </head> <body> <script> $(this). ...

Creating a line that extends from the left side of the viewport to the full length of an element within a bootstrap column

I'm currently working on creating a title that has a unique design: https://i.sstatic.net/VhEBQ.png The design requires a horizontal line to start from the left side of the viewport and end at the same point where the title ends. I am aiming to keep ...

Sharing a state object with another React file can be accomplished by using props or context to

My first React file makes an API call to retrieve data and save it in the state as Data. import React, { Component } from "react"; import axios from "axios"; import Layout from "./Layout"; class Db extends Component { constructor() { super(); th ...

What are the potential issues with my validation function specifically designed for the select element?

After 3 years of working with JavaScript, I found myself unable to use simple validator libraries. So, I made the decision to create my own. This validator works by adding the attribute jmust to all inputs, select elements, textareas, etc. (I haven't ...

Tips for preventing Chrome from masking the background image and color on an autofill input

Google Chrome Browser has caused the background-color and background-image effects to be removed from the Username and Password input fields. Before autocomplete https://i.stack.imgur.com/Ww7Hg.png After autocomplete https://i.stack.imgur.com/hbG2C.png ...

How to save the value of `this.$route.params.id` in a Vue.js data property?

I am currently working with Vue.js 3 and have a sample code for routing and passing parameters. Below is the Home.vue page: <template>   <div class="home">     <h1>       All Destinations     </h1>     < ...

Choosing the initial offspring of an element that do not share a common parent

My question might not be perfectly phrased, for which I apologize. Is there a CSS method to select only the first child of an element without affecting others that are not grouped under the same parent? For instance: <div class="parent"> <div ...

Tips for passing an array between components in Angular 2

My goal is to create a to-do list with multiple components. Initially, I have 2 components and plan to add more later. I will be sharing an array of tasks using the Tache class. Navbar Component import { Component } from '@angular/core'; impor ...

Explore the associative array within a JSON using jQuery to extract and manipulate the values within the array

I'm working with a JSON file containing surnames and first names in an array, along with other objects. How can I specifically extract the names "Jhon" and "Jason"? Below is a snippet from my JSON file: [{ "surname": "Vlad", "first_name": [ ...