The flow of the DIV elements is not functioning as expected

I'm struggling with positioning div classes in the normal way. I tried to create DIV boxes like this:

li {
    width:200px;
    height:200px;
    background:red;
    float:left;
    list-style:none;
    margin-right:20px;
    margin-bottom:50px;
    position:relative;
}

li div {
    width:100%;
    height:100%;
    position:absolute;
}

However, the boxes ended up being positioned absolutely instead of in 2 columns with a gap of 20px and 50px underneath. They seem stuck as if inline, except for the 20px margin-right that's acting strange. The margin-bottom is working fine though.

Even after copying code from TutsPlus.com's CSS3 Essentials video, the issue persists. Everything seems fine with HTML5, but I can't figure out why I can't get the desired layout for these boxes.

Answer №1

Elements with absolute positioning are automatically taken out of the usual flow. By setting their width to 100%, they become the same width as the li elements and end up overlapping each other.

Answer №2

If you're having trouble with the <li> elements not appearing in two columns, one solution is to set the width of the <ul> container to 440px (which is calculated as 200 + 20 px (width of each <li> with margin) multiplied by two). Here's the CSS code:

ul {
    width: 440px
    }

Check out this jsFiddle for a live example.

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

Issue with Bootstrap navbar not highlighting section items when scrolled to

I have recently developed a webpage using html5, css3, and bootstrap. However, I am facing an issue with the navbar menu not highlighting the items that are pointing to specific sections on the page. I would like the navbar menu to automatically highlight ...

Ensuring proper alignment of images on image slider specifically for Chrome browser

While updating a website, I encountered an issue with the image slider displaying two images side-by-side. However, in Chrome, the images were not aligned properly as shown here: The website utilizes PHP and HTML to fetch data from a database and display ...

Choosing CSS/JS selector: Pick the final element with a class that does not match

My goal is to extract the most recent td element from the latest tr within an HTML table, ensuring that the latest td does not belong to the disabled class. I am attempting to achieve this using pure JavaScript with CSS selectors (without relying on jQuer ...

Open the menu directly on the visible screen or current location of the webpage when the sticky button is clicked

I have been working on creating a navbar that will scroll down upon clicking a button. Everything seems to be functioning correctly, except for one issue that I can't seem to figure out. The problem is that when my sticky navbar is clicked while at th ...

Bootstrap4: combining card header and navigation into a single row

How can we use Bootstrap 4 to position navigation and text within a Card Header in order to have them display on a single row? <div class="card"> <div class="card-header"> Heading <ul class="nav nav-pills card-header-pills"> ...

Position a div in the center of a section

Having trouble centering a <div> within a <section>? I've tried setting margin-left and margin-right to auto without success. Any other suggestions? Check out the jsFiddle illustrating the issue: http://jsfiddle.net/veWKh/ ...

CSS:Tips for removing borders with border-radius in CSS

I have a div that has been styled with a left-hand border, and it's causing me some trouble. You can view the styling here on Jsfiddle. Is there a way to remove just the left hand side border without affecting the rest of the design or creating an un ...

The css() function appears to be failing to apply the background property to the body element

I have tried using these different methods in my code to solve the issue, but none of them seem to work. Please suggest an alternative approach for resolving this problem. // Approach 1 if ("${actionBean.backgroundImage}" != null){ $(document.body). ...

"Offspring div popping up beyond the bounds of its parent div

I've been working on a project where I need to insert a div containing an image into another div. The parent div already has two other child divs set up and functioning perfectly. Here is the HTML code for the parent and child divs: <div id="conte ...

CSS Text ellipsis displays only the beginning of each paragraph

I want to add ellipsis to a text, but it keeps including the first line of all paragraphs. What I actually need is for only the first line of the content to have the ellipsis applied. Here is an example of the content: When using websocket to send message ...

Resizing the Bootstrap dropdown with textbox to fit the size of the content

Check out this fiddle I made with a Bootstrap dropdown. The dropdown includes an input text box at the top. How can I adjust the width of both the textbox and dropdown container to fit the content size? For example, if I have a long text like 'ASPHALT ...

Maintaining Object Position in 2D Transforms

I am trying to create multiple perspective-transformed rectangles in the lower right corner of a canvas by using ctx.transform: ctx.transform(1, 0, -1, 1, 10, 10). My goal now is to adjust the size of the drawing based on a variable scale=n, while keeping ...

Yii2 Gridview - Horizontal Scroll Bar at the Top of the Page

I found a helpful post on Stack Overflow about creating a Yii2 Gridview with a fixed first column (Yii2 : Gridview with fixed first column), and now I'm looking to add a horizontal scrollbar at the top of the grid. My users really appreciate being ab ...

What is the rationale behind placing the CSS outside of the React function components, near the imports?

Recently, I encountered an issue with loading CSS inside a React function component using Material UI. Even though I managed to resolve it, I am still intrigued by the underlying reason. Initially, I had something like this setup where I placed both makeSt ...

Creating a customized Paypal form with user-selected options

I want to make it easy for visitors to my website to pay through Paypal by simply clicking on a link that will take them directly to the checkout page. What I'm looking for is a way to provide basic options such as: Allowing users to enter their na ...

What are the steps for implementing flex-grow?

While exploring the documentation and issues of Bootstrap v4 (here), I have not come across any indication of plans to support a property like `flex-grow`, which would allow for a layout as shown below: <div class="d-flex"> <div class="fle ...

Sass list of Bootstrap version 4 grid dimensions

I'm attempting to integrate Bootstrap v4 variables into my project, which are contained within an array (_variables.scss): $grid-breakpoints: ( xs: 0, sm: 544px, md: 768px, lg: 992px, xl: 1200px ) !default; How can I reference it in my SAS ...

Creating uniform row heights in CSS Grid Layout

It seems that achieving this using Flexbox is impossible because each row can only be the minimum height required to fit its elements. However, I'm curious if this can be done using the newer CSS Grid. Just to clarify, I am looking for equal height f ...

Vertical Spacing in Bootstrap 3: Eliminating Gaps Between Divs

I am looking to eliminate the gap between certain div elements and the div positioned below it. This space is created when one div is taller than another in a column. For instance, take a look at this example: http://www.bootply.com/Hc2aO5o4bG Essential ...

Styling with CSS: Utilize flexbox to adjust the layout of two elements, ensuring they

Trying to float an image to the right side of a paragraph is proving to be more challenging than anticipated. 100px +-------------------------+ +------------+ | | | | | ...