Achieve Vertical and Horizontal Alignment of a div within another div using the :after selector

I currently have this HTML and CSS code:

html {
  overflow-x: hidden;
  overflow-y: hidden;
}
body {
  margin: 0;
}
.triangle-down {
  width: 90%;
  height: auto;
  padding-left: 50%;
  padding-top: 50%;
  overflow: hidden;
}
.triangle-down:after {
  content: "";
  display: block;
  width: 0;
  height: 0;
  margin-left: -500px;
  margin-top: -500px;
  border-left: 500px solid transparent;
  border-right: 500px solid transparent;
  border-top: 500px solid #9CE08F;
}
div {
  float: left;
  margin: 0.5%;
}
<div class="triangle-down">
  <div>&nbsp;</div>
</div>

My goal is to align the div within the triangle-down element both vertically and horizontally. Alternatively, I could replace the div with text and align it accordingly. The challenge lies in using the :after selector and being unable to modify the CSS to achieve the desired alignment. I am familiar with methods like position:relative, position:absolute, and display:inline-block for alignment purposes.

However, the complexity arises from maintaining the triangular shape of the design. If I adjust the CSS properties of the triangle-down class, I risk losing the triangular form altogether.

The desired outcome is something similar to the following image:

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

Answer №1

.btn {
    position: relative;
    display: inline-block;
    height: 0px; width:100%;
    text-align: center;
    color: white;
    background: gray;
    line-height: 50px;
    text-decoration: none;
    padding-bottom:42%;
    background-clip:content-box;
    overflow:hidden;
}
.btn:after {
    content: "";
    position: absolute;
    top:0px; left: 0;
    background-color:inherit;
    padding-bottom:50%; width:57.7%;
    z-index:-1;
    
    -webkit-transform-origin:0 0;
    -ms-transform-origin:0 0;
    transform-origin:0 0;
    
    -webkit-transform: rotate(-30deg) skewX(30deg);;
    -ms-transform: rotate(-30deg) skewX(30deg);
    transform: rotate(-30deg) skewX(30deg);
}
/** FOR THE DEMO
body{background: url('http://lorempixel.com/output/people-q-c-640-480-1.jpg');background-size:cover;} **/
<a href="#" class="btn">Hello!</a>

Answer №2

Check out the code snippet below

.up {
    width: 0px;
    height: 0px;
    margin-bottom: 30px;
    border-style: inset;
    border-width: 0 100px 173.2px 100px;
    border-color: transparent transparent #007bff transparent;
    float: left;
    transform:rotate(540deg);
    -ms-transform:rotate(540deg);
    -moz-transform:rotate(540deg);
    -webkit-transform:rotate(540deg);
    -o-transform:rotate(540deg);
  display:block;
}

.up p {
    text-align: center;
    top: 59px;
    left: -47px;
    position: relative;
    width: 93px;
    height: 93px;
    margin: 0px;
     -webkit-transform:rotate(540deg);
      transform:rotate(540deg));
    -ms-transform:rotate(540deg));
    -moz-transform:rotate(540deg));
     -o-transform:rotate(540deg);
}
<div class="up">
<p>some information text goes here</p>
</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

Incorporating Node.JS variables within an HTML document

After building a simple site using Express, I discovered that Node.js variables can also be used with Jade. exports.index = function(req, res){ res.render('index', { title: 'Express' }); }; This is the code for the index file: ext ...

CSS and markup that appear the same are producing different renderings in the browser

I recently purchased the Ambrosia bootstrap theme, but I am facing difficulties with setting up the login page. You can view my login page here. The issue I am encountering involves the Twitter, Facebook, and Info icons that are positioned to the left of ...

The scrolling function halts as soon as the element that was middle-clicked is deleted from the

I am knee-deep in developing my own React virtualization feature and encountering a minor annoyance. When I middle click on an item in the list and start scrolling, the scrolling stops once that item is removed from the DOM. My initial thought was that the ...

Can you explain the distinction between using element~element versus element+element?

Can you explain the variation between these two CSS selectors? When using them, I seem to receive the same outcome. In the HTML: <div>One</div> <p>Two</p> CSS Example #1: div+p { background:red; } This code assigns a red backgr ...

Navigate the scroll bar horizontally one by one instead of all at once, due to a width problem

The issue I'm facing involves determining the correct width for scrolling left or right. This results in the navigation tabs not scrolling correctly one by one. Each time I click the scroll right button, it adds more width than necessary and causes th ...

Tips for preventing scrolling on iOS in Chrome or Safari using CSS, JavaScript, or jQuery

I've successfully implemented an input element with a click event listener that triggers a function to make another element visible using the CSS rule "display:block;". The element in question has the following styling rules: .elementExample { d ...

Unable to display HTML content on a page using the foreach callback in JavaScript

I have a function that iterates through each element of an array and generates HTML content on the page while updating some properties using elements from the array. I am utilizing forEach to iterate over the elements of the array and innerHTML to display ...

What is the best way to delete previously entered characters in the "confirm password" field after editing the password

Is there a way to automatically remove characters in the confirm password field if characters are removed from the password field? Currently, when characters are entered into the password field, characters can also be entered into the confirm password fiel ...

Steps to retrieve the chosen value and visible text from a dropdown menu when clicking a button in Angular version 7

When the button is clicked, I want to display the selected text (green..) and value (1..) from a dropdown select. I attempted using ngmodel but was only able to capture the value, and an empty option is appearing in the select field. Below is the provide ...

Toggle visibility of a div with bootstrap checkbox - enforce input requirements only if checkbox is marked

Lately, I've been facing a strange issue with using a checkbox that collapses a hidden div by utilizing bootstrap. When I include the attribute data-toggle="collapse" in the checkbox input section, the div collapses but it mandates every single one o ...

Styling conditionally with Dash Bootstrap using an IF statement

I am looking to implement a feature where text in a column is displayed in two different colors based on its value, using Dash bootstrap and various HTML components. For example, consider the following table: Value Yes No Yes Yes No The goal is to displa ...

What are some ways to employ ul and li elements to create columns that are consistently aligned, even when some columns may be empty?

In my HTML document, I have multiple sections with unordered lists of items. In a specific ul section, the list looks like this: <ul> <li>item 1</li> <li>item 2</li> <li>ck-item 2</li> <li ...

What sets apart a .class element from element.class in CSS?

I was browsing through this tutorial http://www.w3schools.com/css/tryit.asp?filename=trycss_nesting and noticed a confusing point. Why did they use .marked p instead of p.marked? When I tried changing it to p.marked, the text didn't turn white as expe ...

Having difficulty expanding the window to full size with headless Chrome using Selenium in Python

I am struggling to make the headless chrome window reach its full size. Despite my efforts, the window size remains at the default of 800px by 600px. Can someone assist me in setting it to full screen? Here is my code snippet: from selenium import webdriv ...

javascript The unchecking of a checkbox is not functioning

I am facing an issue with this JavaScript code. The problem is that when I uncheck a value, it removes all the values from the result instead of removing just the specific unchecked value. Can someone please help me with this? <script> window.addE ...

Achieving Perfect Alignment for Absolutely Positioned Divs in

I'm currently facing an issue where the image I'm trying to center horizontally also moves the parent div downward when I try to vertically center it using top-margin. This is not the desired outcome. If you'd like to see what I mean, I&apo ...

solve the issue of images getting resized in bootstrap 4

I'm facing a challenge with maintaining the size of logo images within their "circle containers" regardless of the browser width. The containers are set at 100px by 100px using the following CSS: .timeline-image::before { content: ""; width: ...

The Python Django site is failing to detect and implement CSS updates

Currently, I am in the process of developing a Django website where I have successfully loaded my CSS and HTML files as static resources. In this project, I am utilizing two CSS files: one for Bootstrap and another for my custom styling. However, I encoun ...

The Three.js IEWebGL Plugin

After developing a code that successfully rotates a cube with trackball using the three.js library and webgl, I encountered compatibility issues when trying to run it on Internet Explorer 10. Despite downloading IeWebgl to assist in making it run on IE10 ...

Tips for swapping content between table rows in JavaScript

Struggling to rearrange a table with a new order due to performance issues. Here is a simplified version of the structure: <table> <tr id="row1"></tr> <tr id="row2"></tr> <tr id="row3"></tr> <tr id="row4">&l ...