How can I align a fixed-width div beside a floating div that is in percentage?

Having encountered various challenges (not to come across as a complaining troublemaker), my current struggle lies in determining the most effective way to float two divs alongside each other. One with a fixed width and the other with a percentage width to occupy the remaining space.

The structure of my HTML Code is as follows:

<div class="wrapper">
        <div class="sidebar">
        </div>
        <div class="content">
            <div class="tile">
                <h1>Catalogs</h1>
                <p>1</p>
            </div><!--
            --><div class="tile">
                <h1>Products</h1>
                <p>2</p>
            </div><!--
            --><div class="tile">
                <h1>Specifications</h1>
                <p>3</p>
            </div><!--
            --><div class="tile">
                <h1>New Products</h1>
                <p>4</p>
            </div>
        </div>

        <div style="clear:both"></div>
    </div>

As for my CSS code:

@charset "utf-8";
/* CSS Document */
/* WEB FONTS

font-family: 'Ubuntu', sans-serif;
font-family: 'Oswald', sans-serif;
font-family: 'Francois One', sans-serif;

*/
body {
    background-color:#191919;
    margin: 0 auto;
    overflow:hidden;
    font-family: 'Ubuntu', sans-serif;
}
.wrapper{
    width: 100%;
    min-height: 900px;
    margin: 0 auto;
}
.sidebar{
    width: 20%;
    height: 100%;
    float: left;
    color:white;
    background-color:#191919;
    margin:0 auto;
    display: block;

}
.content{
    width: 80%;
    height: 1000px;
    float: right;
    color:white;
    background-image:url(Assets/background1.png);
    background-size: cover;
    background-repeat: no-repeat;
    margin:0 auto;
}
.tile{
    width: 25%;
    height: 100%;
    display:inline-block;
    margin: 0 auto;
    color:white;
    background-color:rgba(0,0,0,0.7);
    transition: opacity 1s;
    opacity: .3;
    float: left;
}
.tile:hover {
    opacity: 1; 
}
.tile h1{
    font-family: 'Francois One', sans-serif;
    background-color:rgba(110,0,1,0.77);
    width: 100%;
    height: 50px;
    text-align:center;
    line-height: 50px;
}
.tile p{

}
#item {
    padding-left: 25px;
    font-family: 'Francois One', sans-serif;
}
a {
    color:white;
    text-decoration:none;
}

This arrangement, solely reliant on CSS, aims to keep my sidebar menu at a fixed width while allowing the content to occupy the remaining 'x' pixels. Unfortunately, when the page size reduces significantly, the sidebar text overlaps. Hence, the reason for desiring a fixed width.

My attempt to address this dilemma was through jQuery:

function widthAdjuster(wrapper, sidebar, content, tile){
    var wrapperWidth = $(wrapper).width();
    var sidebarWidth = $(sidebar).width();
    var newWidth = wrapperWidth - sidebarWidth;
    var newWidgetWidth = newWidth/4;

    $(content).css("width", newWidth);
    $(tile).css("width", newWidgetWidth);
}

function masterFunc(){
    widthAdjuster('.wrapper','.sidebar','.content','.tile');
    $(window).resize(widthAdjuster('.wrapper','.sidebar','.content','.tile'));
}

This function automatically adjusts the content width and resizes each tile accordingly. Upon window resize, it recalculates the dimensions. However, I am curious if there is a pure CSS solution to prevent the need for jQuery adjustments that may be criticized by reviewers?

Answer №1

I've successfully implemented this solution in the past, simply update your CSS with the following:

<style>
body {
background-color:#222222;
margin: 0 auto;
overflow:hidden;
font-family: 'Open Sans', sans-serif;
}
.container{
width: 100%;
min-height: 800px;
margin: 0 auto;
}
.sidebar{
width: 25%;
height: 100%;
float: left;
color:white;
background-color:#333333;
display: block;
}
.content{
height: 800px;
float: right;
color:black;
background-image:url(Assets/background2.png);
background-size: cover;
background-repeat: no-repeat;
}
.box{
width: 30%;
height: 100%;
display:inline-block;
color:white;
background-color:rgba(0,0,0,0.6);
transition: opacity 0.8s;
opacity: .5;
float: left;
}
.box:hover {
opacity: 1; 
}
.box h2{
font-family: 'Roboto', sans-serif;
background-color:rgba(255,255,255,0.7);
width: 100%;
height: 40px;
text-align:center;
line-height: 40px;
}
.box p{

}
#info {
padding-left: 20px;
font-family: 'Roboto', sans-serif;
}
a {
color:black;
text-decoration:none;
}
.sidebar{
width:350px;
float:left;
margin:0 -350px 0 0;

}
.content{
position:absolute;
right:0;
left:350px;
}
</style>

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

Using Strapi to run basic raw SQL queries with MySQL

Trying to execute this query in my test but struggling with the correct syntax. return strapi.connections.default.raw(` delete from nurses; delete from users_permissions_user;`); }); Since this is not using PostgreSQL and MySQL ...

Storing and retrieving an HTML editable array using COOKIES in JavaScript

In my code, I have created an editable array where you can input student information. A button computes the grade for each student at the end of the line. Additionally, there are buttons to add rows for students and columns for different assignments. Now, ...

Single-Page Design Ascend

I'm facing a dilemma with my one-page website layout project. Instead of the conventional scroll down effect, I'd like to implement a design similar to www.xsbaltimore.com where users have to scroll up to view other sections. However, I'm un ...

What is the best way to conceal an element so that it only becomes visible when a user begins to input text

Hey there! I'm in the process of adding a search feature to my Jekyll site, and I've opted to use Simple-Jekyll-Search, which you can check out here: Link. Take a peek at what's inside my search.html: <input type="text" id="my-search-in ...

Using box-shadow with table rows

I am encountering an issue trying to add a box-shadow to tr elements within a table. The problem arises when the box-shadow does not display unless all the tr elements are set with a display property of block, as suggested in this workaround: Box Shadow in ...

What is the best way to modify the color of an individual row in an HTML table by simply clicking a button within that specific

this is my unique table content echo"<table id=\"unique_company\">"; while($row=mysql_fetch_array($res)) { $s= $row['company_id']; $r= $row[&a ...

There was a glitch encountered while constructing (Verifying type validity) with Prisma

There was an issue in the node_modules/@prisma/client/runtime/library.d.ts file on line 1161, specifically error TS1005 where a '?' was expected. 1161 | select: infer S extends object; | ^ 1162 | } & R ...

Looking for a particular root node in a tree structure?

I am currently working on converting docx files to xml and using DOM to locate the parent node of a specific element. For instance <chapter> <title> <label>Chapter 1 </label> </title> ...

Utilize Angular2's input type number without the option for decimal values

Is there a way to prevent decimals from being entered in number inputs for Angular 2? Instead of using patterns or constraints that only invalidate the field but still allow typing, what is the proper approach? Would manually checking keystrokes with the ...

Utilizing Node.js to create a REST API that allows for seamless communication with a MongoDB database through

Currently, I am developing a web application utilizing the MERN framework (MongoDB, Express, Node.js for back-end, React for front-end). One specific part of my web application requires frequent access to a collection in the MongoDB database (every 50 ms) ...

Error encountered in PHP due to a spacing issue in the GET

<?php $current_subject = $_GET['subject_id']; $current_content = $_GET['note_id']; echo "<form method=\"post\" action=mainpage.php?subject_id=".$current_subject."&note_id=".$current_content.">"; ?> <in ...

Displaying Bootstrap 4 dropdown menu on hover

I've noticed that the dropdown menus in Bootstrap 4 are triggered by click by default, but for some reason, my menu is appearing on hover without any additional CSS or JS. This poses a problem for mobile development. I could create a jQuery solution, ...

Update the content inside the extension by modifying its innerHTML

Just starting out with JavaScript, I'm attempting to create a basic extension that can generate a random number. document.getElementById("submit").onclick = function() { a = document.getElementById("in1").value; b = document.getElementById("in2 ...

What is the best way to partition JSON data from an API request in React and display it in various sections within the component

There are 2 JSON objects that contain sales period details based on Month to date and year to date. The data includes information such as Units Sold, Gross Revenue, Year to Date Totals, Month to Date Averages, Expenses, Net Revenues, and Per Unit values. I ...

Arranging Text and Images in HTML/CSS to Ensure Optimal Placement When the Window Size Changes

Hello fellow coders! I've recently started diving into the world of website development and I have a query regarding positioning elements and handling window resizing. Can anyone help me out? I'm trying to center an image, a message, and a passw ...

Leveraging jQuery to target elements with a minimum of two child elements

How can I use jQuery to specifically target .sortable <ul> elements that have 2 or more children (<li> elements)? Is there a way for jQuery to select only <ul> elements with at least 2 <li> children? Thanks in advance! The current ...

Display the content as a clickable link using Jquery

Here is the view I have created using .NET MVC3: function DisplayingGrid () { var Geo=$('#ddlGeo').val(); var Vertical=$('#ddlVertical').val(); var Month=$('#ddlMonth').val(); if(Vertical=="All") { var Flag=1; ...

Unable to retrieve cookies from the client side | Working with NodeJS and JavaScript

Code for Handling Cookies on the Server-Side: res.cookie('test', 'value', { expire: 400000 + Date.now(), httpOnly: false }); res.writeHead(302, { 'Location': 'localhost:4000/test', }); res.end(); Code for Acce ...

I'm having trouble getting these margin classes to work properly in Bootstrap. Can anyone help me figure out what

I'm new to using Bootstrap and struggling with adding margins between elements. I tried adding mb-12 to the navbar class and mt-12 to the container class, but it doesn't seem to be working as expected. Everything else in my code is functioning co ...

What is the advantage of using event.target over directly referencing the element in eventListeners?

Suppose there are several buttons in an HTML file and the following code is executed: const buttons = document.querySelectorAll('button'); buttons.forEach((btn) => { btn.addEventListener('click', (e) => { console.log(btn.te ...