Create a layout of div elements aligned to the right, utilizing a parent container div that automatically adjusts its size to fit the

My attempt at this design:

https://jsfiddle.net/j75hxxa2/1/

The goal is to have the block positioned on the right side and eliminate the extra gray area.

By applying

float: right;

To the parent element, the child elements become very small. Trying to adjust their width with

width: 50%;

Causes them to break into a new line.

Is there a straightforward solution to this issue?

(I also suspect that

margin-top: 1px;

Is not functioning correctly.)

Thank you for any assistance!

Answer №1

An improved method involves utilizing a table structure:

HTML:

<table>
  <tr><td>Text</td><td>Text</td><td>Text</td><td>Text</td></tr>
  <tr><td>I</td><td>II</td><td>III</td><td>IIII</td></tr>
</table>

CSS:

table {
    border-collapse: collapse;
    background: #000;
    border-top: 1px solid #777;
    border-left: 1px solid #777;
}

table tr td {
    border-right: 1px solid #777;
    border-bottom: 1px solid #777;
    color: #fff;
}

Answer №2

To achieve the desired layout, remember to include "float: left;" in the .cell code block. Feel free to customize the background color as needed.

.cell {
  width: 24.8%;
  float:left;
}
#table {
margin-right: 1px;
margin-top: 1px;
background-color: #f22;
width:100%;
height:37px;
}

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

Form dynamically loaded with ajax and embedded within a div using x-editable

Is there a way to load a form directly into a div using an ajax call? The main page already has all the necessary CSS and script files for x-editable forms. I've tried loading the HTML code directly inside the div, but unfortunately, the x-editable ...

Change the color of the first element when hovering over any of its siblings using only CSS

I am looking for a solution using only CSS We have 3 circles here. When I hover over circles with the class name "Mycircle", the circle with the class name "BigCircle" should change to red color HTML <div class="BigCircle"></div> <div cl ...

Utilizing jQuery's multiple pseudo selectors with the descendant combinator to target specific elements

My code includes the following: <div class="a"><img></div> and <div class="b"><img></div> I want to dynamically enclose img tags with a div and utilize $(":not('.a, .b) > img") for ...

The "Location..." header always points me back to the same destination

Hope you're all doing well. I've been having some trouble with using header("Location...") to redirect from one page to another on my website. For some reason, it keeps redirecting me back to the same page. I've gone through my code multiple ...

Internet Explorer not recognizing attribute selector in jQuery

My JavaScript code utilizes the css selector [style="display:none"], and it functions correctly in Chrome, Firefox, Opera, and Safari on Windows. However, in Internet Explorer 11, it behaves unexpectedly. To test this issue, simply click on the buttons (e ...

The final li element in the second unordered list is targeted by the selector ul li:last-child

Check out this JsFiddle Having trouble with the pseudo code :last-child. I want the style to apply to the li containing E, but it's affecting the last li in the .subNav. I've attempted a few solutions below, but none have worked. nav ul:first- ...

Compatibility of Bootstrap 4 with collapsible elements and display utilities

I have noticed an issue with Bootstrap 4 display utilities not functioning properly with the .collapse class, a feature that worked seamlessly with Bootstrap 3. Despite my efforts to research this problem, I have been unable to find similar inquiries regar ...

How can I position four DIV elements to the right of each other inside a parent DIV?

I am attempting to align 4 divs next to each other within a parent div at specific positions. The proposed div structure is as follows (referred to as the maindiv): <div> <div>1</div> <div>2</div> <div>3< ...

Apply the transition property to all elements except for the text inside a specific tag

I'm wondering if it's possible to apply a hover transition effect to everything in this tag except the text within it. The desired effect is for the button to fade away while the text remains visible. Below is the code I have written so far: ul ...

What is the significance of $() in jQuery?

I understand that $() is a selector, but I find it puzzling as to what it actually selects since there is nothing inside the parentheses. Is this a common practice or frowned upon in coding conventions? An example of where I have seen it used is when maki ...

"click on the delete button and then hit the addButton

I have created a website where users can save and delete work hours. I am facing an issue where I can save the hours at the beginning, but once I delete them, I cannot save anything anymore. Additionally, when I reload the page, the deleted data reappears. ...

Use jQuery to categorize <a> links by their text content

Hi, I'm new to jQuery and could use some guidance. Due to the not-so-great output of Go's html/template, I have to rely more on JavaScript (which I'm not very good at). I have a list of unordered links that need to be organized into "paren ...

Anomalies observed in label transition while in active state for input field

Can you explain why there is a gap in the label when it's positioned at left:0? I'm aiming to achieve a UI similar to Material design but struggling with label positioning. I've experimented with using translateY(-6px), however, this method ...

Elements on the page appear and disappear as you scroll down

Whenever my scroll reaches the bottom of element B, I want my hidden sticky element to appear. And when I scroll back up to the top of element B, the sticky element should be hidden again. Here are my codes: https://i.sstatic.net/J49dT.jpg HTML <htm ...

What is the best method for clearing DOM variables and storage when a new innerHTML is loaded dynamically in a Single Page Application?

When I load Dynamic HTMLs with JS into a div on an index page, the content of the div gets updated dynamically based on user actions using the JQuery.load function. The loaded HTML includes JS files that are also added to the DOM and work as expected. How ...

I'm keen on creating a marquee animation using Tailwind and Next.js

I'm working on creating a marquee animation using Tailwind CSS and Next.js, and I've made some progress. However, the issue I'm facing is that the items are not displaying one after the other smoothly; there seems to be a delay when the seco ...

Creating a vertically centered scrollable <div> with Bootstrap 4: A step-by-step guide

My goal is to create a webpage layout like the one shown below: https://i.sstatic.net/xn7UE.jpg In this design, the red box represents a scrollable div whose size adjusts according to the content. The main background featuring the man and the navbar shou ...

Utilize LESS Bootstrap as a guide for incorporating content

I have been struggling to properly integrate Bootstrap into my LESS file. Currently, I am adding Bootstrap to my project using NPM. If I simply use the following import statement in my LESS file: @import (reference) 'node_modules/bootstrap/less/boot ...

swapping out an external CSS file in React for a new CSS file

My React app is quite large and includes four main CSS files (darkLTR, lightLTR, darkRTL, lightRTL), which may not be the most efficient setup. The templates were provided by my boss, and I was instructed to use them instead of Material UI, which I initial ...

What is the process for altering the background color in this html5up template?

I am struggling to change the brown background color on my website. Despite updating various color values in the CSS file such as'backgroundcolor', I seem unable to make any changes. I have tried altering multiple color entries, but nothing seems ...