Animating a div in CSS3 to expand horizontally from left to right without affecting its original position

I am currently in the process of developing a calendar using HTML, CSS, and JavaScript. The main purpose of this calendar is to showcase upcoming and past events. However, I am facing difficulties in ensuring that my event blocks occupy the remaining space in the calendar from left to right. You can view a snapshot of the current calendar layout below.

Below is an example of how the table cells are structured within my project:

.cell-container{
  display: flex;
  justify-content: center;
  margin: 0px;
  align-items: center;
  width: 200px;
  height: 100px;
  border: 1px solid black;
}

.date-label{
  font-weight: bold;
}

.table-cell{
  display: flex;
}

.inner-container{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.event{
  border-bottom-left-radius: 8px;
  border-top-left-radius: 8px;
  width: 150%;
  background-color: cornflowerblue;
  position: relative;
  top: 0px;
  left: 0px;
}
<div class="table-cell">
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">23</label>
      <div class="event">Event @4:00 pm</div>
    </div>
  </div>
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">24</label>
    </div>
  </div>
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">25</label>
    </div>
  </div>
</div>

The issue I am encountering is that the event block continues to expand both left and right instead of solely expanding towards the right. I have experimented with using position: sticky and position: absolute, however, the outcome remains the same.

I would greatly appreciate any guidance on whether it is possible to constrain the expansion direction of a div element or if there are alternative solutions to resolve this challenge. Thank you in advance for any assistance!

Answer №1

To achieve the desired outcome, adjust the CSS properties as follows: set justify-content to flex-start, remove align-items: center from the inner-container class, and add align-self: center; to the date-label class for centering.

.cell-container{
  display: flex;
  justify-content: center;
  margin: 0px;
  align-items: center;
  width: 200px;
  height: 100px;
  border: 1px solid black;
}

.date-label{
  font-weight: bold;
  align-self: center;
}

.table-cell{
  display: flex;
}

.inner-container{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.event{
  border-bottom-left-radius: 8px;
  border-top-left-radius: 8px;
  width: 150%;
  background-color: cornflowerblue;
  position: relative;
  top: 0px;
  left: 0px;
}
<div class="table-cell">
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">23</label>
      <div class="event">Event @4:00 pm</div>
    </div>
  </div>
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">24</label>
    </div>
  </div>
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">25</label>
    </div>
  </div>
</div>

Answer №2

To expand from the left, you need to remove the align-items property in the inner container or change it to either start or normal. To keep the date number centered, set the align-self property to center. Check out the CSS code snippet below for reference.

.cell-container{
  display: flex;
  justify-content: center;
  margin: 0px;
  align-items: center;
  width: 200px;
  height: 100px;
  border: 1px solid black;
}

.date-label{
  font-weight: bold;
}

.table-cell{
  display: flex;
  align-self: center;
}

.inner-container{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.event{
  border-bottom-left-radius: 8px;
  border-top-left-radius: 8px;
  width: 150%;
  background-color: cornflowerblue;
  position: relative;
  top: 0px;
  left: 0px;
}
<div class="table-cell">
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">23</label>
      <div class="event">Event @4:00 pm</div>
    </div>
  </div>
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">24</label>
    </div>
  </div>
  <div class="cell-container">
    <div class="inner-container">
      <label class="date-label">25</label>
    </div>
  </div>
</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

I am experiencing difficulty with the function from flip.to not functioning properly on my Next.js project

I was given this script by flip.to <script> !function(b,e){ (b[e] = b[e] || []).push({ flipto: { bookingEngine: "Demo", companyCode: "XX", code: "YYYY", ...

Exploring Ngu-Carousel in Angular 7: Importing JSON data for a dynamic display

After attempting to import data from JSON and display it using ngu-carousel, I encountered an error. The error shows "length of undefined" Subsequently, when I try to click on the previous/next button, another error appears. This error states "innerHTML ...

Looking to connect information

I have written the following code and I am trying to bind data to ng-model. When I use ng-model = "value", I can see the value in the text field, but I want to update the value when the text input is changed. <tbody ng-repeat="action in model.editAct ...

What could be causing my difficulty in locating an HTML element using jQuery/JS string interpolation?

In my project, I have a set of div blocks each identified by a unique numerical id. For instance: One of the tasks in my project is to select the 29th element following a specific chosen element. For example, if I choose the first div with id 1, then I s ...

Right-align SELECT-OPTIONS text

Here are the screenshots of the form I'm currently working on. I am aiming to create a select box in the form where the text within the options is aligned to the right. After an option is selected, the chosen text should be displayed as illustrated i ...

What could be the reason my homing missile algorithm is not functioning properly?

The code I'm currently using for my projectile was heavily inspired by an answer I found on a game development forum, but it's not working as expected. Most of the time, the initial direction of the projectile is perpendicular to the target inste ...

Adding Bootstrap component via ajax request

I'm facing an issue with injecting a Bootstrap component using ajax. I usually include a select element like this: <select class="selectpicker" data-width="75%"> Most of the HTML code is generated dynamically through javascript, which you can ...

Center and align pictures within a stationary container

I am working on a simple webpage that has a fixed footer. In this footer, I want to have 5 images centered with equal distances between them. Below is my HTML/CSS : Footer div : div.fixed { position: fixed; bottom: 0; right: 0; width: ...

Adjusting the height of a jQuery Mobile collapsible post-expansion

My jQuery collapsible is not resizing properly to fit the expanded content. Below is an example of the collapsible structure: <div class="row collapsibles" data-role="collapsible"> <h1>Supercategory A</h1> <div class="col-xs-7 ...

Incorporating a JavaScript npm module within a TypeScript webpack application

I am interested in incorporating the cesium-navigation JavaScript package into my project. The package can be installed via npm and node. However, my project utilizes webpack and TypeScript instead of plain JavaScript. Unfortunately, the package is not fou ...

Mastering the art of grouping by a key and generating sub-objects from a plain array of key-value pairs in JavaScript ES5 without relying on third-party libraries

My dataset consists of an array of objects, each containing 4 keys: [ { "team": "USA", "team_profile_id": "10", "player": "Captain America", "player_id": "10X1" }, { "team": "USA", "team_profile_id": "10", "player": "The ...

Issue with Restangular/angularjs object not refreshing after remove() operation

I am currently using Restangular within my AngularJS application. I have an index view that displays all messages, and I can edit and add messages to the list without any issues. However, when I attempt to use the remove() function on an element, the index ...

ui-grid row size set to automatically adjust using rowHeight : 'auto'

Has anyone else experienced this strange behavior with ui-grid? When I set the rowHeight to auto, each cell in the same row ends up with different heights. One of the cells contains multiline data, which seems to be causing issues for ui-grid. I've ev ...

Troubleshooting Problem with CSS Display in Spring Security 3.2

After deciding to integrate 'Spring security 3.2.0' into my current web application, which was originally developed without Spring and utilizes Primefaces & EJB 3, I encountered a challenge. Upon starting the basic configurations, I noticed that ...

Displaying JSON data from the API on a webpage within a NodeJS weather application

Currently, I am in the process of developing a weather application using NodeJS. I have successfully retrieved JSON formatted data from the weather site's API. Nonetheless, I am perplexed about how to transmit this data to the application. Below is a ...

HTML/CSS Inactive: Troubleshooting and Solutions

There seems to be an issue with the :active state, if anyone can spot the problem and provide assistance, it would be greatly appreciated. Here is the HTML code: http://pastebin.com/4wCi3L2Z And here is the CSS code: http://pastebin.com/jZvgdDaA Thank y ...

Angular2 data binding does not update when properties change

I'm struggling to establish the connection between the fields and the component in order for them to update when the properties change in OnDataUpdate(). The field "OtherValue" successfully binds two ways with the input field, and the "Name" field di ...

What is the best way to showcase data from input fields within a bootstrap modal dialog?

After the user has entered their details and clicks submit, I would like to present the information in a Bootstrap modal with a confirmation button below. This serves as a preview of the data before it is saved to the database. Here's what I have so ...

Error: Attempting to access a property 'notesObjectInService' that is undefined on the object

I'm currently facing an issue where my controller is unable to load an object from a service called notesFactory. The console.log(typeof notesFactory) statement returns 'undefined', causing notesObjectInService to trigger an exception. Desp ...

PHP mail function does not properly align HTML table outputs

After fetching data from the database and inserting it into an HTML table, everything appears fine. However, upon pressing the send button to email the content, sometimes the alignment of the HTML table is disrupted in the email. It seems like specific col ...