Can parent height be filled when using absolute positioning?

I need to fill the parent height with color bars, but they are absolutely positioned in the top right corner. Is there a way to achieve this dynamically, adapting to the number of lines in the headline?

Take a look at the image and this example.

CSS for the color columns: (for full CSS, refer to the example)

.header .colorColumn { 
width: 18px; 
height: 70px; 
float: right; 
} 
.header .trackColors { 
position: absolute; 
right: 0; 
top: 0; 
} 

Additional Question:
How can I make the bars appear behind the text but above the gray background? The z-index solution is tricky when both layers need to be absolutely positioned.

Update 1:
The number of color columns is dynamic - how can it be adjusted to work with a variable number of columns?

Answer №1

Is this what you were looking for? The absolute position of the parent and the height: 100% of the color bars...

Here is the link: http://jsfiddle.net/StartStep/jj5b2gnq/5/

.programDetails .header {
    background-color: #404040;
    color: #e6e7e8;
    font-weight: bold;
    font-size: 22px;
    position: relative; /* Used as a reference point for absolutely positioned bars */
}
.programDetails .titles {
    padding: 10px 36px 10px 20px;
}
.programDetails .header .sub {
    font-weight: normal;
    font-size: 14px;
}
.header .colorColumn {
    width: 18px;
    height: 100%; /* Occupies the full height */
    float: right;
}
.header .trackColors {
    position: absolute;
    right: 0;
    top: 0; bottom: 0; /* Stretches the element vertically */
}

JavaScript solution for padding adjustment: http://jsfiddle.net/StartStep/jj5b2gnq/6/

$(document).ready(function() { // Execute as soon as the document is ready
    var columnsWidth = $('.header .trackColors').width(); // Obtain the width of columns
    $('.programDetails .titles').css("padding-right", columnsWidth); // Set the right padding
});

Answer №2

Why not consider a different approach altogether? It appears that you are incorporating elements into your DOM (.colorColumn, .trackColors) solely for styling purposes. This practice is generally discouraged, as markup and styling should be kept separate.

If your goal is simply to add two colored stripes, you might want to consider using a box-shadow instead. Here's an example:

header {
    color: #fff;
    background: #777;
    padding: 8px;
    box-shadow: 
        inset -10px 0 0 red,
        inset -20px 0 0 green;
}

There are numerous other techniques to achieve this effect (gradients, pseudo-elements :before and :after, combining a border with an outline, etc.). The key is to find a method that works best for your specific needs without altering your markup unnecessarily!

Here's a demo to illustrate: http://jsfiddle.net/y0jw9x17/

Answer №3

Dealing with similar issues in the past, I found a solution that worked for me:

When filling a parent container with absolute positioning, I utilize percentages and set the height to 100%.

Instead of using float: right;, which caused confusion for me, I opt for text-align: right; to align elements to the right side.

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

Does the CSS :not() selector function properly when used with distant descendants?

Check out the official documentation for the CSS3 :not() pseudo-class at this link: http://www.w3.org/TR/css3-selectors/#negation Additionally, you may be interested in the proposed enhancement for CSS Selectors Level 4 found here: http://dev.w3.org ...

Unable to see Primereact styles reflected on components

After some investigation, I've pinpointed the issue to be related to preflight from tailwind. Upon reviewing the documentation, I came across this helpful information: CSS Layer It seems that using Tailwind CSS with styled or unstyled modes of PrimeR ...

Attempting to place my icon font in a higher position than my link to achieve a similar effect to the one I am currently experiencing with my

I am currently working on a menu design that incorporates icons (images) above my menu links. However, I have decided to switch from using icon images to utilizing icon-fonts from the 'Font-Awesome' library. Despite this change, I am struggling ...

Sending data from an HTML textarea to a PHP variable without user input

After spending more than two days searching for a solution to this problem, I am now reaching out directly with my question. Although there have been some hints and partial answers, nothing has definitively resolved the issue I am facing, prompting me to m ...

CSS - sticky header causing issues when resizing the browser window

I'm currently facing an issue with the layout of my navbar. While I've managed to create a fixed navbar that stays at the top, the content on the page seems to be hidden underneath it. To address this problem, I attempted to add a 5% margin from ...

What is the best way to extract a CSS rule using PHP?

Below is an example of the stylesheet I am currently using: #thing { display: none; } I am looking to retrieve the value of the 'display' property using PHP instead of Javascript. While I know how to do this with Javascript, I would prefer to u ...

What is the best way to make sure images and text are properly aligned for different screen sizes

I'm a beginner with Bootstrap and could use some assistance. In my code, I have text and images divided into four columns with alignment set based on screen size using media queries in CSS. However, when resizing the window to tablet view (check Scre ...

Creating a visually stunning image grid akin to the meticulously designed layouts found

Forgive my lack of knowledge, but I'm curious about how to create an image or text grid similar to Tumblr using HTML and CSS. I'm looking to achieve a layout like this: ...

Saving a picture to local storage with the file input type in ReactJS

I am attempting to save an image in the browser storage once a user selects an image from their computer. <div className="add_grp_image_div margin_bottom"> <img src={img_upload} className="add_grp_image"/> <input type="file" class ...

How can I make Firefox render the padding in a textarea the identical way as in a div?

In my efforts to ensure a consistent line width inside a textarea across IE8, Firefox, and Safari, I have encountered an issue where Firefox seems to add an extra pixel of padding compared to the other browsers. This results in text wrapping differently an ...

Tips for aligning text to the right within a div using the "justify" text-align property

I want the final sentence of my content to be aligned to the right side while the rest remains justified. I attempted using a <span> within a <p> tag, but it did not work as expected: span.activity-conclusion { font:22px 'Open Sans ...

Issue with jquery .height(value) function not functioning properly on Internet Explorer

I have come across a challenge with the following code snippet: var winheight = $(window).height(); var headerHt = (0.11 * winheight); $(".header").height(headerHt) The purpose of this code is to dynamically adjust the size of .header and other elements ...

Looking to tilt text at an angle inside a box? CSS can help achieve that effect!

Hey there, is it possible to achieve this with CSS or JavaScript? I require it for a Birt report. ...

What is the most concise way to align one table row in the middle and another at the top?

Is there a way to align different rows in a table vertically with minimal code? I have a table with 3 rows, and I want the first two rows to be aligned vertically to the top, while the third row should be vertically aligned to the middle. If I try to def ...

Adjusting the content of mat-cards to fill in blank spaces

I have encountered an issue with the alignment in a list using mat-card. Here is my current layout: https://i.stack.imgur.com/VKSw4.jpg Here is the desired layout: https://i.stack.imgur.com/8jsiX.jpg The problem arises when the size of content inside a ...

Mastering the art of carousel div creation with Bootstrap

Is there a way to create a carousel in Bootstrap 3 where only one div slides at a time, instead of three? I attempted to use divs instead of images in the traditional carousel structure, but it's not functioning as expected. I'm looking for some ...

Capture the contents of a table cell and store it in the clipboard

Is there a way to create a hover copy button in a table cell without actually placing the button inside the <td> tags? The goal is to allow users to easily copy text from a specific column cell by hovering over it. function myFunction() { var c ...

What techniques can be used to resize an image to perfectly fit a square on a webpage?

A challenge on the web page is to display images in a square format of 90 * 90 pixels. However, the sizes of these images are not consistent and may vary from 80*100 to 100*80 or even 90 * 110. The requested solution is to stretch the image as follows: ...

Show only the portion of the image where the cursor is currently hovering

Information in advance: You can check out the current code or view the live demo (hover image functionality not included) at . The concept: I would like to have it so that when I hover my cursor (displayed as a black circle) over the image, only the s ...

Is anyone else experiencing issues with their imagemap due to Twitter Bootstrap?

I'm excited to ask my first question here as a beginner. I am truly grateful for all the assistance and guidance I have received from this site. I hope that the questions I ask can also benefit others in some way :) Although imagemaps may not be used ...