Ensure that the element remains aligned horizontally within the window when it is resized

Looking for a solution where two elements interact differently based on window size. The first element is a container with a width of 1245px, relative position, and centered relatively to the window (with a grey background-opacity of 0.3). The second element is an absolute div (#grey-left), a simple rectangle positioned to the left of the window, with its right point behind the container.

The goal is to ensure that the width of the rectangle where these two elements interact always stays the same, regardless of the window size. Since the container's width is fixed, the #grey-left must adjust its width proportionally to the window width.

Is it feasible using just CSS, without jQuery?

Code:

#grey-left{
    height: 420px;
    top: 84px;
    left: 0px;
    position: absolute;
    width: ?? ;
}

#container {
    position: relative;
    margin: 0 auto;
    width: 1245px;  
}

Answer №1

My approach to achieving this without using JS would be as follows:

Link to Example

HTML Structure:

<div id="grey-left">
    <div class="wrapper">
        You can add your content here if needed.
    </div>
</div>
<div id="container"></div>

CSS Styling:

html, body {
    overflow-x: hidden;
}

#container {
    width: 1245px;
    height: 800px;
    background-color: #ddd;
    opacity: 0.6;
    margin: 0 auto;
    position: relative;
}
#grey-left {
    width: 1000px;
    position: absolute;
    top: 80px;
    right: 50%;
    margin-right: 500px;
    height: 200px;
    background-color: #e5e5e5;
}
#grey-left .wrapper {
    width: 200px;
    position: absolute;
    right: 0;
    top: 0;
}

The key technique involves giving #grey-left a wider width and offsetting it against the center of the window. Additionally, you should utilize overflow-x: hidden to prevent a horizontal scrollbar.

If not required, you have the option to remove .wrapper along with its associated styles.

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

Fluid carousel and dynamic loading

Currently, I am experiencing an issue with the Liquid Slider while using ajax, specifically related to height. Below is the script I am using: var api2 = $.data( $('#slider-7')[0], 'liquidSlider'); $.ajax({ complete: function( ...

Exploring the Next and Previous Posts within the Same Categories on Blogger

First off, I recommend reading this article on implementing Next and Prev Buttons for Blogger Theme: Next and Prev Buttons for Blogger Theme Got the gist of what I'm aiming for? Let's move on to my query: I've already embedded code into th ...

Toggle two elements simultaneously by clicking on one of them - displaying one element while hiding the other

I am facing an issue with toggling the visibility of two divs using a button click event. One div is initially hidden with a display property set to none, while the other div is visible. My goal is to have this behavior toggle on each click event - the fi ...

Style large and small text side by side with CSS styling

I've been trying to figure out how to position a smaller biography-style text next to this large title, but I'm having trouble finding a solution. I've experimented with float: left, float: right, and display: flex in my CSS code. Below is t ...

spontaneous angular rotations in a constantly shifting CSS environment

Is it possible to apply a random CSS rotation to an image just once, rather than having it continuously spin when hovering over a leaflet map? http://jsfiddle.net/J03rgPf/mzwwfav4/3/ I want the random CSS rotation to be set only one time. How can I achie ...

Ways to shift pictures sequentially in a horizontal line?

I am currently working on a project in asp.net where I need to rearrange 'x' number of images in a row. To explain the scenario, let's say we have 5 images labeled as 1, 2, 3, 4, and 5. Initially, they are in the order of 1, 2, 3, 4, 5. Then ...

Chrome browser not triggering first click event for AJAX jQuery checkbox

My Java application is built with Struts 1.1, utilizing actions, forms, and business objects. To enhance performance, I have integrated AJAX into my code to fetch data quickly and display it in a grid. However, I encountered an issue where clicking on a ch ...

Symfony 3 - Assetic dump functions properly in development environment but encounters issues in production environment

Recently, I have started working with Symfony 3 to develop a web application. In order to consolidate my CSS and JS files, I decided to use the Assetic bundle in dev mode, which seemed to be functioning properly without any issues. However, when I switche ...

Clicking on a jQuery dropdown will focus the input box

I am working on a jQuery dropdown menu that contains an input box. My goal is to automatically focus on the input box when the dropdown is clicked. Here is the HTML code for the textbox: <input type="text" name="username" id="username"/> And here ...

Utilizing JQuery for parsing information

I am working on a project that involves displaying Google Maps API on one tab and images on another within a container. The goal is to have the image in the second tab change when a location is selected from the list. To achieve this, I have set up a hidd ...

Display the JSON output of the Instagram post

My goal is to obtain an access token from the Instagram API. I am using AJAX in Laravel to send my data and retrieve the access_token in JSON format. Here's the code snippet of my view that utilizes AJAX with jQuery: var $data; $(document).read ...

Keep the div anchored to the bottom of its container

I created a minimalist CSS bar chart, but each bar originates from the top. .wrap { width: 400px; position: relative; overflow: hidden; } .barchart { display: inline-block; margin-left: 0.2px; } .barchart.column { width: 11px; margin-lef ...

Utilizing jQuery to target a DOM element dynamically

When using jQuery, I am looking to perform a specific action whenever a particular DOM element appears. Similar to the .live() or .on() methods, I want to hide the element as soon as it is detected. ...

The Chrome extension takes control of the new tab feature by redirecting it to a custom newtab.html

I have a website https://example.com where users can adjust their site preferences, including enabling night mode. To enhance the user experience, I developed a Chrome extension for https://example.com that transforms Chrome's new tab with a custom ne ...

What are the best practices for implementing server and client-side validation using jQuery Tools?

Hey there, I've set up a form within a jQuery overlay and here's the code: <div class="profile_about_edit_container" id="profile_about_edit_container"> <form id="profile_edit_form" name="profile_edit_form" method="post" action="vali ...

Resetting the state of toggle/click states in AJAX and jQuery

Currently, I am encountering a small dilemma with a .on function and AJAX in conjunction with a mobile menu. The mobile menu is located in the header of a site that relies heavily on AJAX for its content loading. This poses an issue because when an AJAX ca ...

Adding a div to a different webpage using jQuery

I am currently working on a small admin panel and I was curious if it is feasible to use jQuery to add a div box inside the index.php file after clicking a button in the panel? If this is possible, an example demonstrating how to do so would be greatly ap ...

What could be the reason for this JavaScript malfunctioning in both Chrome and Safari browsers?

Why is it that the javascript works fine in Firefox for the gallery on the page below, but doesn't seem to be functioning properly in Chrome and Safari? In Chrome, the big image isn't displaying but the thumbnails are, and in Safari nothing show ...

Harmonizing Vue and JQuery compatibility

Is there full compatibility between Vue.js and JQuery? How about Vue.js and JQueryUI? I have experience using both frameworks without any issues in integration. Can anyone point out any potential conflicts that may arise? ...

Using JQuery to delete data from a cookie

I have a delicious cookie that I want to savor before removing one element from it based on the widget ID. if (thisWidgetSettings.removable) { $('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) { ...