Tips on positioning four div elements inside of another div using the float property

Struggling to align 4 divs side by side within a parent div with a width of 100%. Each child div has a width of 25% without any margin or padding, but they are not displaying correctly!

Here is the code snippet...

<div id="bottomsections">
    <div id="1a">
            <h1>a</h1>

        <p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat</p>
    </div>
    <div id="1b">
            <h1>b</h1>  
        <p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat</p>
    </div>
    <div id="1c">
            <h1>c</h1>  
        <p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat</p>
    </div>
    <div id="1d">
            <h1>d</h1>  
        <p>This is Photoshop's version of Lorem Ipsum. Proin gravida nibh vel velit auctor aliquet. Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet mauris. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odio. Sed non mauris vitae erat consequat</p>
    </div>

Here is the corresponding CSS...

#bottomsections {
    width:100%;
}
#1a {
    width:25%;
    float:left;
    margin:0;
    padding:0;
}
#1b {
    width:25%;
    float:left;
    margin:0;
    padding:0;
}
#1c {
    width:25%;
    float:left;
    margin:0;
    padding:0;
}
#1d {
    width:25%;
    float:left;
    margin:0;
    padding:0;
}

Check out the fiddle for a live example...http://jsfiddle.net/aM2UL/1/

Thank you!

Answer №1

Remember, an ID cannot begin with a number:

/* change #1a to #a1 */
#a1 {
    width:25%;
    float:left;
    margin:0;
    padding:0;
}

http://jsfiddle.net/dfsq/aM2ULE/

Check out this in-depth explanation of valid characters for IDs:

Update: As suggested by Allendar in the comments section, don't forget to clear your floats. You can add another element after your floated divs with clear: both. I personally prefer using the .clearfix class for better semantics:

.clearfix:before,.clearfix:after{content:"";display:table}
.clearfix:after{clear:both}

Use it like this:

<div id="bottomsections" class="clearfix">...</div>

Answer №2

Using numbers in IDs is possible, as explained in this helpful article. However, it is important to be mindful of how you select and use them.

[id="1a"], [id="1b"], [id="1c"], [id="1d"] {
    width:25%;
    float:left;
    margin:0;
    padding:0;
}

To see an example, check out this demonstration on JSFiddle.

Answer №3

In all versions of HTML and CSS, it is important to note that IDs cannot begin with numbers. Additionally, in your current code, all four of your styles are identical. It would be more efficient to use a class instead.

div.inner {
    width:25%;
    float:left;
    margin:0;
    padding:0;
}

Simply set the following:

<div id="1d" class="inner">

By doing this, you will streamline your code, making it easier to manage and minimizing the amount of changes needed in the future.

Answer №4

#sectionsBelow div{ display:inline-block; width:25%}

It has been mentioned by some that starting ids with numbers is considered invalid

Answer №5

It is important to note that IDs cannot begin with a number. You may want to consider changing it to something like #a1. However, if you wish to apply the same styles to all divs within your main div, why not approach it in the following manner?

#bottomsections div{ 
    width:25%;
    float:left;
    margin:0;
    padding:0;
}

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

Learn how to assign an image to a div element when it is collapsed, and then reveal the content when clicked on to expand

I am working on a three div with expand collapse functionality. When I expand one div, I want to set some image to the other divs. And when I go back to normal mode, I need the original content that was inside each div. $("a.expansion-btn").click(functi ...

Creating unique styles for mat-option with Active and Hover effects in Angular Materials

Struggling to find a CSS solution for changing the text color of the active option and on hover. The lack of documentation on styling angular materials in CSS is proving to be an issue. I've tried using .mat-option.mat-selected { background: red; ...

Ways to center align text in a div vertically

I'm working with 2 divs that are floating side by side. The left div contains a part number, which is only one line. The right div holds the product description, which can span multiple lines. I am looking for a way to vertically align the text in t ...

placing an image at the top of the holder

Here is the code snippet I've been working on, aiming to achieve a design similar to this https://i.sstatic.net/OQkNA.png. Below is my code: .service-box { background: #fff; margin: 0 -5px 20px; font-size: 16px; ...

What is causing the floated element to overlap the element below instead of vice versa?

div { background-color: #00FFFF; } p { width: 50px; height: 50px; border: 1px solid black; margin: 0px; } #p1 { background-color: red; float: left; } #p2 { background-color: green; } #p3 { background-color: orange; } #p4 { backgr ...

Step-by-step guide on accessing values from a JavaScript object

I have a dictionary variable declared within a script tag on an HTML page. My goal is to retrieve the values associated with the "Roads" and "Intersections" keys, which change each time the page is refreshed. By capturing these values, I can then use JavaS ...

Ways to trigger a JavaScript notification

I consider myself experienced in scripting, but I can't seem to figure this out. I'm trying to trigger a function on a button click event, so I decided to start by testing the buttonclick event with a basic window.alert. I wrote the following htm ...

Prevent background image animation in CSS

I've encountered an issue with a simple button I created that uses two background images for normal and hover states. Upon testing in various browsers, I noticed that the images animate on hover, causing them to slide up and down. I tried eliminating ...

Fill in the text field according to the option selected from the dropdown menu

I have a function in my JavaScript code that is supposed to fill a textbox based on the selection made in a drop-down menu. Here is an example with just one option out of eight: <script> function UpdateQuantity(){ if (document.getElementByID("Raff ...

Creating a login form using HTML, PHP, and MySQL: A step-by-step guide

UPDATE [13.10.16] After revisiting a previous question of mine on stackoverflow that has low reputation, I realized the reasons behind it and decided to update it to make it more relevant. When I initially asked this question, I was attempting to create ...

How to effectively eliminate the border of a single cell within a Bootstrap Table

Is there a way to remove the border of a single cell in a bootstrap table without affecting the others? I attempted using an id on that specific cell and adding the following CSS code: #borderless-cell { border: 0; } Unfortunately, this solution doesn&ap ...

The <Button> element is incompatible with popups. (HTML, CSS, JS)

I've been struggling with this issue. I used CSS color values to create a smiley face, but when I added a button it messed up the design (adding an unwanted circle around the center of the smiley). I attempted to synchronize the button tag with the po ...

Using a combination of CSS selector, several attributes, and the OR operator

Can you assist me with CSS selectors? Let's say we have a style HTML tag: <style type="text/css"> [...selector...] {color:red;} </style> I want to create a selector with multiple attributes and logical operators, but I am uncertain about ...

Can the site be shown in the background?

I have a unique idea for a games website that I want to share. It's a bit difficult to explain, so please bear with me as I try to convey my vision! The inspiration for this project comes from a website called . Have you seen it before? The concept i ...

Using jQuery, discover if a script tag is present within a paragraph tag

I have encountered a situation where there is a script tag inside an HTML <p> element. For example: <p> <script> function essb_window1149322389(oUrl, oService) { essb_window_stat(oUrl, oService, 55311); }; </ ...

What is the best way to capture a screenshot of a website using its URL?

I've been curious about the possibility of capturing a screenshot of a remote website using only a URL and some JavaScript code. This functionality is often seen on bookmarking sites. I can't help but wonder if this is achieved through a virtual ...

Having trouble with the JavaScript function not working on page load

I'm currently working on writing JavaScript code that will run once the child page, created using the window.open function, has fully loaded. Despite trying methods like window.onload and window.setTimeout, I haven't had any success. I even expe ...

Is there a way to showcase images next to each other within an array in Angular 8, like having 2 images in a row and then another 2 in the next row?

Here is the HTML code I created and tested: <div class="row"> <div *ngFor="let url of urls" class="col-md-6"> <img [src]="url" alt="Image not found" height="250px" width="350px" (click)="imageClick(url)"> </div> < ...

Centering div elements in ASP.NET using C# and Javascript

I am looking to create a functionality on my website where, upon clicking a button, a DIV appears in the center of the browser, overlaying all other content. I have already created the DIV and managed its visibility toggle, but I'm struggling with the ...

What is preventing this brief code from functioning properly? I am trying to extract a value from an input field and add it to a div element

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="//ajax.googleapis.com/ajax/libs/jque ...