Divs are unable to be positioned at the top of the container

I am struggling with the alignment of blocks inside a container that can be dragged and re-sized within their container. The default position should have 7 divs side-by-side at the top of the container, however, they are appearing stacked on top of each other horizontally. I have tried to figure this out with no success so far and would greatly appreciate any help or guidance. You can see the issue demonstrated in this jsFiddle link below. Thank you.

#calCon {
          height:400px;
          width:700px;
          border:1px solid grey;
          margin:0px;
        }

.date   {
          width:98px;
          height:30px;
          border:1px solid blue;
          background-color:green;
          position:relative;
          top:0px;
        }

<div id = "calCon">
   <div class = "date" style = "left:0;">cell 0</div>
   <div class = "date" style = "left:100px;">cell 1</div>
   <div class = "date" style = "left:200px;">cell 2</div>
   <div class = "date" style = "left:300px;">cell 3</div>
   <div class = "date" style = "left:400px;">cell 4</div>
   <div class = "date" style = "left:500px;">cell 5</div>
   <div class = "date" style = "left:600px;">cell 6</div>
</div>

Answer №1

Remove the inline styling from the .date element and instead apply position: absolute; to it. Check out this Fiddle

*Please note: When using absolute positioning for an element inside a container, make sure to use margin-left instead of left to keep the absolutely positioned element within its parent.

<div id ="calCon">
  <div class="date">cell 0</div>
  <div class="date">cell 1</div>
  <div class="date">cell 2</div>
  <div class="date">cell 3</div>
  <div class="date">cell 4</div>
  <div class="date">cell 5</div>
  <div class="date">cell 6</div>
</div>


.date {
  position: absolute;
  width: 98px;
  height: 30px;
  border: 1px solid blue;
  background: green;
}
.date:nth-of-type(1) {
  margin-left: 0;
}
.date:nth-of-type(2) {
  margin-left: 100px;
}
.date:nth-of-type(3) {
  margin-left: 200px;
}
.date:nth-of-type(4) {
  margin-left: 300px;
}
.date:nth-of-type(5) {
  margin-left: 400px;
}
.date:nth-of-type(6) {
  margin-left: 500px;
}
.date:nth-of-type(7) {
  margin-left: 600px;
}

Answer №2

In order to properly position your elements, it is important to utilize absolute positioning within the container.

To achieve this, you should apply position: relative to your container and position: absolute to your child elements. By doing so, your child elements will be positioned absolutely in relation to the container they belong to.

If you need a visual example, feel free to check out this codepen link: http://codepen.io/JTLR/pen/ojgLy

Alternatively, you can also opt to use float: left or display: inline-block on the child elements to have them display next to each other. Keep in mind that using inline-block may introduce default spacing between elements.

Answer №3

make sure to include display: inline-block; in your date class

Unclear about your objective, but if you want to eliminate the large white spaces between your elements with the class, consider removing the inline style tags that are setting their left position and use float:left; in your CSS instead

Check out this example on jsFiddle

Answer №4

In order to grasp the concept of position: relative, it is important to remember that adding position:absolute to the date class can help resolve any issues you may be experiencing.

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

The Chip Component in MUI dynamically alters its background color when it is selected

Currently, I am working on a project in React where I am utilizing the MUI's Chip component. My goal is to customize this component so that it changes its background color when selected, instead of sticking to the default generic color. https://i.sta ...

Jquery allows for the toggling of multiple checkboxes

I have a group of check-boxes that I would like to toggle their content when checked. Currently, I am using jQuery to achieve this functionality, but I am searching for a way to optimize my code so that I do not need to write a separate function for each ...

Multiple keyup events being triggered repeatedly

Currently, I am developing an Angular 4 application. Within my component's HTML, there is a textbox where users can input text. As soon as the user starts typing, I want to trigger an API call to retrieve some data. The current issue I am facing is t ...

jQuery Datatable pagination issue: not displayed

I need some help with my project where I am using JQuery Datatable. The issue is that I cannot seem to display the pagination and other elements of the datatable. Below are the codes for your review: Although I can fetch data from my service successfully, ...

The CSS "ul" selector targets unordered lists in HTML

How can I target the ul elements highlighted below in my HTML markup without using a class or id for reference? Here is the relevant portion of my HTML code: <div id="mainmenu"> <ul class="menu"> <li class="item-472"> < ...

Tips for inserting a Vue.js variable into a window.open event

Within this snippet of code: <tr v-for="person, index in People" :key='index'> <td> <button type="button" onclick="window.open('/details/'+person.id,'_blank')"> details</butto ...

Overlap of Bootstrap Columns Occurs When Resizing the Browser

While I recognize that this issue is common, I have extensively reviewed similar questions but could not find a solution. <div class="container"> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-6"> <img style="wi ...

What is the best way to implement an alert box in MVC without triggering a page redirection?

I have a specific requirement to display a custom alert box in order to confirm the user's intention to delete an item. Once the item is deleted, another alert box should appear confirming that the deletion was successful, prompting the user to click ...

Using AngularJS in an ASP.NET MVC application to load partial views

In the process of developing an ASP.net MVC application. Currently, I am incorporating a partial view within a div using a combination of jQuery and AngularJS: By performing an AJAX call through AngularJS and assigning the returned data as the content wi ...

When the text exceeds the designated block, it will be fragmented

I have always struggled with text elements not breaking when they exceed the boundaries of their parent element. I understand that setting a max-width of 100px will solve the issue, but it's not an ideal solution for me. I want the text to break only ...

Designing the chosen choice with CSS and HTML: Making it look like a placeholder and keeping it hidden in the dropdown menu

UPDATE: I'm puzzled by the 2 downvotes without any solutions to my problem. Issue: The challenge I'm facing is styling the color of the selected option that acts as a placeholder. I attempted different solutions like this one, but unfortunate ...

Tips for successfully implementing AJAX in your web development project:1

Currently I am delving into PHP and striving to create a webpage that includes a dropdown menu at the top. Each item in the dropdown has its own unique ID attached. Below the dropdown, there is a text box followed by some images as shown below: <dropdo ...

Retrieve the ajax variable containing the external URL

I'm currently troubleshooting why I am unable to retrieve the variable value from an ajax request. Review my code snippet below: planet_finder = function() { var planet_name; $.getJSON("https://api.planetinfo.com/jsonp?callback=?", "", funct ...

Transfer files and data efficiently using Ajax

I am facing an issue while trying to send a file and data together to the Controller. When I only send the file, it works correctly. However, when attempting to send both data and a file, nothing seems to work. contentType: false, processData: false, Rem ...

Another option for replacing <BR> tags with CSS styling

I am currently utilizing the br tag to insert line breaks in my website's faq section. Although the output is accurate, I'm exploring alternatives like implementing linebreak through CSS to enhance responsiveness. During my research, I came acros ...

What is the best way to handle AJAX responses in an onchange event

I'm working on a form where the select option in the input (jurusan) is automatically filled based on the selected value. I need to know how to change the value that appears after selecting an option. For example, if I select option A, it displays th ...

Download the PDF instead of just viewing or downloading it through a link

I need help configuring a link so that when the user clicks "download here," a PDF file is automatically downloaded to their desktop. Can someone provide guidance on how to achieve this? ...

Tips for transferring a variable from a hyperlink to a Flask application

Here is a snippet of my Python Flask code: @app.route('/ques/<string:idd>',methods=['GET', 'POST']) def ques(idd): print(id) And here is the accompanying Javascript code: var counts = {{ test|tojson }}; var text = ...

Ensure that the value is updated upon clicking on the radio buttons and store the value in a session variable for

I'm working on a feature that involves two radio buttons: one for shipped and the other for not shipped. The requirement is that when the first radio button is clicked, an amount of 100 should be added to the total price. If the second radio button is ...

From where was this color of the navigation bar derived?

As I delve into a site previously worked on by someone else, my task is to recreate the navigation they implemented. However, I am unable to locate in the source code what was used for the navigation background. The challenge lies in the fact that after m ...