Determining the dimensions and placement of a triangular div element relative to its containing parent

I created a child div that forms a colorful triangle shape:

 .shape-triangle {
     width: 0;
     height: 0;
     border-left: 25px solid transparent;
     border-right: 25px solid transparent;
     border-bottom: 50px solid green;
     /* placed on top of an image */
     position: absolute;
     top: 20px;
     left: 110px;
}

This colored triangle is nested within a Bootstrap column containing a background image:

<div class="col-12 col-lg-6 text-center">
    <div class="shape-triangle"></div>
    <img src="assets/background.png" alt="Image">
</div>

The triangle sits above a grayscale shapes background image:

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

However, when I resize the screen or browser window, only the background image resizes while the triangle remains fixed in size and position:

https://i.sstatic.net/5l1ZF.png

What adjustments can be made to ensure that the colored triangle maintains proportionality in size and position relative to the background image?

Answer №1

To ensure the div rescales appropriately with either the screen size or its parent container, it is essential to use the unit % for the .shape-triangle class. Using px as a unit will not yield the desired results, as it maintains a fixed size while the parent element .col-12 is subject to change. Additionally, employing the unit rem for borders allows for consistent sizing relative to the root font.

My recommendation would look similar to this code snippet (adjust the % and rem values as needed):


.shape-triangle {
     border-left: 2.5rem solid transparent;
     border-right: 2.5rem solid transparent;
     border-bottom: 5rem solid green;
     /* maintain position and size */
     position: absolute;
     top: 20%;
     left: 50%;
     width: 0;
     height: 0;
}

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

Turning a bootstrap form input into a clickable element by adding a font-awesome icon

I am facing an issue with my date input field and a font awesome icon placed on top of it. The appearance is correct, but I am unable to click on the input when the mouse is hovering over the icon. Despite trying to adjust the z-index, the problem persists ...

Two separate Bootstrap dropdowns are failing to function independently from each other

Can anyone help me prevent both buttons from displaying a drop-down when only one is clicked? https://i.stack.imgur.com/nvtbm.png Below is my HTML code: <form action="{{ url_for('crudSEapi.gcp_image_search') }}" method="post&q ...

HTML - HREF standard for efficiently linking to directories in web development

Have you ever noticed this seemingly irrelevant detail: <a href="./some-file.html">Some File</a> And this one too: <a href="some-file.html">Some File</a> Both of these codes lead to the same URL. But which one is more efficient, ...

Make sure all of the inner tags are properly contained within their respective containers

In my explanatory bubble, I aim to include text within the explain-header div as a container for my explanations: /*Explain Bubble*/ .explain-container { position: relative; overflow: hidden; max-width: 70vw; max-height: 50vh; background-c ...

Streaming videos using Flask with a custom template

After successfully implementing the DWA path planning algorithm in C with Python bindings, I have decided to create a web application demo. The concept involves a "robot" following the mouse using DWA, while allowing users to draw walls for objects. My ini ...

Trimming Text with JQuery

My goal was to truncate a string and add an ellipsis at the end using jQuery, but my code doesn't seem to be working as intended. Below is the jQuery code I wrote: $(".link-content > h4").each(function(){ var len = $(this).text().length; ...

CSS - Issue with table cell height exceeding in Mozilla Firefox and Internet Explorer

Examining the code below <div style="border:solid 5px orange;width:400px;height:400px"> <div style="display:table;border:solid 1px red;width:100%;height:100%"> <div style="display:table-row;border:solid 1px blue"> <div sty ...

Implementing interactive dropdown menus to trigger specific actions

I have modified some code I found in a tutorial on creating hoverable dropdowns from W3. Instead of the default behavior where clicking on a link takes you to another page, I want to pass a value to a function when a user clicks. Below is a snippet of the ...

Moving the center div to the top of the content for mobile screens

Would like some help in positioning the col-md-6 div to the top of my website on mobile, causing both col-md-3 elements to stack on top of each other. <div class="row"> <div class="col-md-3"> <div class="box"> & ...

What is the correct way to retrieve the location offset of a specific DOM element?

As I attempt to determine the offsets of an element utilizing element.getBoundingClientRect(), a challenge arises when the webpage is extensive in vertical length and involves scrolling. The function then provides me with the offsets of the element relat ...

The material UI tabs text is missing the ellipses due to a coding error

As a newcomer to web development, I am experimenting with adding an ellipsis to the span element within the tabs. <div class="MuiTabs-scroller MuiTabs-fixed" role="tablist" style="overflow: hidden;"> <div class="MuiTabs-flexContainer"> ...

Looking for an Angular Material component that displays a list of attributes?

I am trying to create a dynamic list of properties in Angular using Material Design that adjusts for different screen sizes. For example, something similar to this design: https://i.stack.imgur.com/EUsmV.png If the screen size decreases, the labels shou ...

Efficiently determine optimal menu item dimensions using CSS automatically

Is it possible to automatically position menu items using CSS so that the position is recalculated when the menu item text changes? Currently, I have a menu created with ul li's and I am using padding for positioning. However, if the text becomes long ...

PHP Ajax Image Uploading and Storage

Is there a way to effortlessly upload an image and save it in the uploads folder without any page refresh? Not only that, but can we also have the uploaded image displayed on the screen within a designated div section? Unfortunately, I seem to encounter a ...

The disappearing act of the Bootstrap 4 hamburger icon, despite the navbar expanding afterwards

Currently in the process of building my initial website using bootstrap 4, and encountering difficulties with my collapsible navbar and the hamburger icon. My goal is to have the navbar expand on medium screens, but switch to a hamburger icon on smaller sc ...

Overflow leads to the entire page scrolling

My goal is to design a webpage that remains static without any scrolling. Some specific elements within the page may be allowed to scroll independently, but I am focused on preventing the entire page from scrolling. The main issue arises when a deeply nest ...

Refreshing an iframe located on disparate domains

There is a webpage called "main.jsp" within the domain "domain1". This page contains an iframe that loads content from another domain known as "domain2". Essentially, "main.jsp" serves as a common content platform, with the iframe displaying content from v ...

The iOS platform is experiencing issues with displaying the entire page, making it difficult to

Struggling with my website for Freecodecamp. Works fine on desktop, but iOs is a nightmare. My contact page isn't showing at all, and 75% of the bottom is cut off. Even worse, I can't click on what's visible. Help needed! Only tested on iPho ...

The downward trajectory of the Hamburger Menu is uncompromised, yet it refuses

Currently, I am utilizing Ruby on Rails 7 and Bootstrap 5. I encountered an issue with my hamburger menu functionality - it opens successfully but fails to close. Strangely, all other dropdowns work perfectly fine. Below is the snippet of the problematic c ...

Gradually fade with JavaScript

My JavaScript code below is used to recursively reload a specific DIV with the results of a data query. The issue I'm facing is that each time the DIV, identified by id="outPut", is reloaded, all the data fades in and out due to the fadeTo function. ...