Different option for positioning elements in CSS besides using the float

I am currently working on developing a new application that involves serializing the topbar and sidebar and surrounding them with a form tag, while displaying the sidebar and results side by side.

My initial attempt involved using flex layout, but I have not been successful in achieving the desired outcome. I was only able to make it work using float. However, my preference is to utilize flex if it can be done.

Furthermore, it is important that the results are not nested within the same form tag as it contains its own set of tags which could lead to errors.

.app {
    background-color: rgba(255,0,0,0.3);
    padding: 40px;
}

.form {
    display: unset;
}

.topbar {
    width: 100%;
    background-color: rgba(0,255,0);
}

.sidebar {
    width: 20%;
    float: left;
    background-color: rgba(0,0,255,0.3);
}

.results {
    width: 80%;
    float: left;
    background-color: rgba(0,255,255,0.3);
}

.app:after {
    content: "";
    display: table;
    clear: both;
}
<div class="app">
    <form class="form">
        <div class="topbar">Topbar</div>
        <div class="sidebar">Sidebar</div>
    </form>
    <div class="results">results</div>
</div>

Answer №1

To achieve a similair behavior as the float type using flexbox, you can utilize the properties margin-left/margin-right: auto. However, in order to do so, you will need to slightly adjust the markup structure by adding a container with display: flex that contains the desired elements as direct children. Here's an example:

.app {
    background-color: rgba(255,0,0,0.3);
    padding: 40px;
}
    .form__container {
        display: flex;
     }
        .form {
            width: 20%;
        }

.topbar {
    width: 100%;
    background-color: rgba(0,255,0);
}

.sidebar {
    background-color: rgba(0,0,255,0.3);
}

.results {
    width: 80%;
    margin-left: auto; /* important for achieving float effect */
    background-color: rgba(0,255,255,0.3);
}
<div class="app">
    <div class="topbar">Topbar</div>
    <div class="form__container">
        <form class="form">
            <div class="sidebar">Sidebar</div>
        </form>
        <div class="results">results</div>
    </div>
</div>

Answer №2

Wow, shoutout to @Paulie_D for coming through with the perfect answer to my question! Huge thanks for that! :)

.container {
    display: grid;
    grid-template-columns: 20% 80%;
    background-color: rgba(255,0,0,0.3);
}

.sidebar {
    grid-column: 1 / span 1;
    background-color: rgba(0,0,255,0.3);
}

.main-content {
    grid-column: 2 / span 1;
    background-color: rgba(0,255,255,0.3);
}
<div class="container">
    <div class="sidebar">Sidebar</div>
    <div class="main-content">Main Content</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

Choose the current parent item using Angular's ng-selected and $index

http://plnkr.co/edit/fwwAd4bn6z2vxVN2FUL7?p=preview On the Plunker page linked above, I am trying to achieve a specific functionality. I would like the 3 dropdown lists to display values A, B, and C initially. When a 4th dropdown option is added, it shoul ...

Internet Explorer version 8 and above tend to load HTML at a slower pace compared to other web browsers

I have encountered a problem while loading ten separate dropdown lists, each containing around 13,000 options from the same data set. Despite confirming that the issue lies within the HTML rather than the code used to populate them (by saving and directly ...

Preparing my JSON data for visualization on a chart

I have successfully retrieved data using this API, but now I need to transform it into a chart within my react project. As a newcomer to JS and React, I am struggling to create a chart with the JSON data. My objective is to display prices by bedrooms over ...

Align content distributed evenly will not display a division

I am currently using React to code and learning by doing so. I have two different images displayed, each followed by a div element with a height of 20px and a brown background color. I have set the height to "100%" and justifyContent to "space-between", bu ...

Adjusting hyperlinks placement based on window size changes using jQuery

On resizing the screen, I am moving the sub-nav links to the '#MoreList' It works well initially, but when the window is expanded again, the links remain in the #MoreList instead of going back to their original positions if there is enough space ...

Issue with Vue.js: routes are not found upon page refresh

Here is a basic vue-routing example: const Foo = { template: '<div>foo</div>' } const Bar = { template: '<div>bar</div>' } const routes = [ { path: '/foo', component: Foo }, { path: '/ba ...

Can the name of the Grunt task target be utilized within attributes?

I have implemented the grunt-replace task to make some content changes in the index.html file. However, I am looking for a way to avoid repeating code unnecessarily. The code snippet below is just an example of what I am trying to accomplish: replace: { ...

Was not able to capture the reaction from the Angular file upload

I've been attempting to upload a single file using the POST Method and REST Calling to the server. Despite successfully uploading the module with the code below, I encounter an error afterwards. Is there anyone who can lend assistance in troubleshooti ...

Having difficulty entering text into the Material UI TextField

I am encountering an issue with a button that opens up a Material UI Dialog containing a TextField. However, I am unable to click into the TextField to input any text. Additionally, when clicking on the button to open the Dialog, I receive the error messag ...

Adding a loading event listener to an object in JavaScript: A step-by-step guide

I'm currently deep into developing a game using sprites in JavaScript. I've been trying to incorporate an event listener that verifies whether the sprite images have loaded before beginning the game. Employing object-oriented programming, I' ...

Declining the request to incorporate an external website into my web platform

While checking the console errors in Google Chrome, I encountered the following error message: The page 'https://website.com' was blocked from framing because a higher-level ancestor violates the Content Security Policy directive: "frame-an ...

Tips for maintaining the current route in Vue.js after a page refresh while running the Vue.js project in development mode on a specific port?

In my router.ts file, I have defined two routes: export default new Router({ mode: "history", routes: [ { path: "/", component: require("./components/dashboard/Dashboard.vue")}, { path: "/counter", component: require("./components/ ...

Troubleshooting Azure typescript function: Entry point for function cannot be determined

project structure: <root-directory> ├── README.md ├── dist ├── bin ├── dependencies ├── host.json ├── local.settings.json ├── node_modules ├── package-lock.json ├── package.json ├── sealwork ...

When a Grid Item is enclosed in a link, it does not extend over rows

After avoiding HTML/CSS for a long time, I finally decided to dive in and create a simple CSS grid website with 3 items in the grid. Everything was working well and looking exactly as I wanted it to: However, I had the idea of using the entire Aside-Items ...

The progress bar in Next JS 13 seems to be malfunctioning and is not displaying any

After transitioning to the new Next JS 13 app directory, I encountered an issue where the progress bar at the top does not function when the page loads. In the previous version, Next JS 12, there was a package named nprogress and nextNprogress that simpli ...

Run the command "node index.js" to simultaneously start and stop the server

After installing WSL2 and the Ubuntu 22.04 distribution on Windows 11, I set up my environment, installed nvm, Node version 16.17.1, and then ran npm init in my folder. Following that, I installed Express and created an index.js file with a simple structur ...

Angularjs scope's parent id

I'm looking to access or manipulate a scope's grandparent without making things overly complicated. Leveraging the controller as syntax, I can reference the parent controller like so: this.applicationCtrl.something with applicationCtrl > parent1C ...

Creating dynamic SQL queries for bulk inserting data into Postgres using Vercel

Can anyone help me with creating an SQL bulk insert query using the sql helper from @vercel/postgres? I have a array of objects with different property types (number, string, date) and need to dynamically build the query. Simply creating a string and passi ...

Access to this page via the Odesk API in node.js is restricted and unauthorized

/** * Here is an example of how to use the oDeskAPI * * @package oDeskAPI * @since 09/22/2014 * @copyright Copyright 2014(c) oDesk.com * @author Maksym Novozhylov <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data ...

Exploring the asynchronous nature of componentDidMount and triggering a re-render with force

I am puzzled by the behavior in the code provided. The async componentDidMount method seems to run forceUpdate only after waiting for the funcLazyLoad promise to be resolved. Typically, I would expect forceUpdate to wait for promise resolution only when wr ...