How to extract CSS 100% from a specific div and not the entire webpage

Struggling with calculating using calc in CSS led me to discover that my current div is showing an incorrect 100% value.

Check out this brief example:

html, body, .calender {
position: relative;
height: 100%;
overflow: hidden;
}

.appointmentsA {
background: blue;
}

.appointmentsB {
background: red;
}

.calender {
display: table;
width: 100%;
table-layout: fixed;
padding: 10px 10px 0 10px;
}
.calender .row {
display: table-cell;
padding: 0 10px;
}

.appointments {
height: 100%;
}

.appointments:before {
content: "test";
position: absolute;
top: calc(100% - 18px);
color: #fff;
}
<main class="calender">
<div class="row">
<h1 class="title">ABC</h1>
<div class="appointments appointmentsA"></div>
</div>
<div class="row">
<h1 class="title">DEF</h1>
<div class="appointments appointmentsB"></div>
</div>
</main>

The colored blocks of red and blue match the height of the container row and its main parent element.

Is there a way to achieve the true 100% height of these colored blocks without using JavaScript? If we can accurately set the colored block height to 100%, then the ":after" element will display correctly instead of cutting off text.

Example Image: example

Answer №1

Are you looking to apply a color to the full-height column? If so, you can achieve this by adding position: absolute to the title:

* {
  box-sizing: border-box;
}

html,
body,
.calender {
  position: relative;
  height: 100%;
  overflow: hidden;
}

.appointmentsA {
  background: blue;
}

.appointmentsB {
  background: red;
}

.calender {
  display: table;
  width: 100%;
  table-layout: fixed;
  padding: 10px 10px 0 10px;
}

.calender .row {
  display: table-cell;
  padding: 0 10px;
}

.appointments {
  height: 100%;
}

.title {
  position: absolute;
}
<main class="calender">
  <div class="row">
    <h1 class="title">ABC</h1>
    <div class="appointments appointmentsA"></div>
  </div>
  <div class="row">
    <h1 class="title">DEF</h1>
    <div class="appointments appointmentsB"></div>
  </div>
</main>

Alternatively, for a colored division:

* {
  box-sizing: border-box;
}

html,
body,
.calender {
  position: relative;
  height: 100%;
  overflow: hidden;
}

.appointmentsA {
  background: blue;
}

.appointmentsB {
  background: red;
}

.calender {
  display: table;
  width: 100%;
  table-layout: fixed;
  padding: 10px 10px 0 10px;
}

.calender .row {
  display: table-cell;
  padding: 0 10px;
  position: relative;
}

.appointments {
  height: 100%;
  position: absolute;
  width: 100%;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

.title {
  position: relative;
  z-index: 2;
}
<main class="calender">
  <div class="row">
    <h1 class="title">ABC</h1>
    <div class="appointments appointmentsA"></div>
  </div>
  <div class="row">
    <h1 class="title">DEF</h1>
    <div class="appointments appointmentsB"></div>
  </div>
</main>

Answer №2

It seems like I've grasped your point correctly.

h1 is considered a block level element in the structure. The divs .appointments are positioned after it. One approach is to assign the background-color of the h1 to match the corresponding .appointment and eliminate the bottom margin. Alternatively, you can enclose the h1 within appointment, as illustrated in the code snippet below.

html,
body,
.calender {
  position: relative;
  height: 100%;
  overflow: hidden;
}

.appointmentsA {
  background: blue;
}

.appointmentsB {
  background: red;
}

.calender {
  display: table;
  width: 100%;
  table-layout: fixed;
  padding: 10px 10px 0 10px;
}

.calender .row {
  display: table-cell;
  padding: 0 10px;
}

.appointments {
  height: 100%;
}
<main class="calender">
  <div class="row">
    <div class="appointments appointmentsA">
      <h1 class="title">ABC</h1>
    </div>
  </div>
  <div class="row">
    <div class="appointments appointmentsB">
      <h1 class="title">DEF</h1>
    </div>
  </div>
</main>

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

Utilizing Tailwind CSS for Advanced DIV Placement

My goal is to arrange the DIVs inside the container in a way that the top DIV (containing text) overlaps the DIV next to it. Below is a sketch of what I have in mind: https://i.sstatic.net/kUVGT.png This is the HTML code I have written for this layout (Ta ...

Center text in the v-text-field label

Greetings! I've attempted various methods to center the label in my v-text-field, but unfortunately none of them have been successful. Please see the code snippet below: HTML Code <v-text-field dark class="centered-input" label="your code">< ...

Personalizing Web Push Alerts (Google Chrome)

I successfully implemented a web push notification for Google Chrome using Google Project and Service Worker. One thing I'm curious about is how to customize or style the push notification. The plain message box doesn't quite cut it for me – I ...

CSS: Creating a dynamic layout to center the card on screens of all sizes

My code currently places the profile card in the center of the screen regardless of screen size, but it often appears too small. How can I adjust the positioning of the profile card to almost fill the screen? @import url("https://fonts.googleapis.com/cs ...

What is the best way to center align a Bootstrap 4 modal window?

I'm facing a small issue here, I would like to have the modal display in the center. Instead of it appearing like this https://i.sstatic.net/R518w.jpg, I want it to look like this https://i.sstatic.net/wwUo7.jpg. Here is the code: <!-- The Modal -- ...

To accurately determine the width of a div element, it is essential to include the web browser width in the CSS

Is there a way to calculate the width of a div named incontent in the index.php file, based on the width of the browser screen and other data? I have heard about using JavaScript to get the screen width, but the challenge lies in the fact that the stylin ...

Swap out the current image for a different one

When a user clicks on different image or color options, I want to change the main image displayed. Below are the links to the alternative images: https://i.sstatic.net/DxJEb.jpg This is the HTML code: <div class="container"> <p class="img-main" ...

How can I locate and modify a particular cell within an HTML table using just JavaScript?

For my project, I am required to develop a functional shopping cart with a remove function. If an item is already in the cart, it should update the existing data when added again. Let's say I have the following items in my cart: Name: Quantity: Pric ...

What's the best way to restrict characters in a paragraph?

I am working on an Angular application and I need to restrict the number of characters in a paragraph (p) to 50, after which it should break to a new line. Here is the template I am using: <section class="echeq-element-html-display border-box" [ng ...

React.js query: Image caught in a loop, highlighting a specific section

Hello everyone! I'm currently facing a challenge where I need to transfer an element from one div to another when clicked, with a transition delay (animation being secondary). Located at the bottom of the page are three icons. When clicking on the le ...

Creating a sleek horizontal scrolling list with the least amount of rows using tailwindcss

Is there a way to create a 3-row list with horizontal scroll without the unwanted space between items? I've been using tailwindcss and grid to achieve this layout, but I'm encountering issues with spacing: https://i.stack.imgur.com/WeRyQ.png Cu ...

HTML list with padding

I need to display two lists side by side. In one list, I want to insert some space between two li elements (an empty li). For example, I am aiming for something that looks like this: A B A B A A B I tried adding an empty li, which worked ...

Addressing the issue of reversed keyboard navigation order for radio buttons caused by the flex-direction styling

Scenario: We have a set of radio button cards grouped together, which we want to arrange differently for small and large screens. I am looking for a solution that allows us to achieve this without duplicating the HTML code and maintain accessibility. Pre ...

React css modules to enhance styling in your web project

I am in the process of incorporating a CSS module into the login component. Currently, I have a style.css stylesheet located in the src/styles folder, which is responsible for loading global styles. However, I now want to customize the login component by a ...

What is the best way to efficiently pass a prop from a Parent styled component to its children's styled components, regardless of how deeply nested they are?

I am utilizing these customized components: import styled, { css } from 'styled-components' const Container = styled.div` background-color: ${props => props.theme === "light" ? "hsl(0, 0%, 98%)" : "hsl(235, 24%, 19% ...

Vertically align the text

Is there a way to center this text vertically within the divs? I've attempted adjusting the position using relative/absolute, and modifying the top and left properties of the paragraph, but none of these methods have been successful. div.roxo{ ...

How can I align a fixed-width div beside a floating div that is in percentage?

Having encountered various challenges (not to come across as a complaining troublemaker), my current struggle lies in determining the most effective way to float two divs alongside each other. One with a fixed width and the other with a percentage width to ...

What is the best way to create scrollable content inside a container?

To accommodate more fields within my fixed-position container, I need to increase its height. However, instead of doing this, I believe adding a scroll bar to the container and making its contents scrollable would be a better solution. The challenge now i ...

JavaScript is unable to identify the variable containing parsed JSON

I have been working on coding some gauges for the index page of the R/Shiny dashboards used by our company. The layout consists of several styled divs that link to different R apps. Recently, our executives have shown interest in having fuel-gauge style g ...

Setting the row property of a frameset using CSS: a step-by-step guide

What is the best way to set the frameset row property using css or jquery for various browsers? ...