(CSS) Position div elements from the middle to the left

I am facing the challenge of centering one div element while having others float to the left of it, similar to the following:

The code provided sets width and height in pixels, whereas I need to set them in percentages. This presents a problem as I'm unsure of where the center should be, especially with varying resolutions. I want only the yellow div to be centered without affecting the green div.

Is there a solution apart from adding an invisible empty div on the left side with the same height as the green div?


.container{
    background-color:#34495e;
    width: 500px;
    height: 200px;
    padding: 10px;
}

.in-center{
    background-color:#f1c40f;
    width: 40%;
    height: 100%;
    display: inline-block;
}

.to-left{
    background-color:#2ecc71;
    width: 20%;
    height: 100%;
    display: inline-block;
}
                
<div class="container">
        <div class="in-center"></div>
        <div class="to-left"></div>
</div>

Answer №1

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.container {
  background-color: #34495e;
  width: 80%;
  height: 200px;
  text-align: center;
  position: relative;
}
.centered {
  background-color: #f1c40f;
  width: 40%;
  height: 100%;
  display: inline-block;
}
left-side {
  background-color: #2ecc71;
  width: 20%;
  height: 100%;
  display: inline-block;
  position: absolute;
}
<div class="container">
  <div class="centered"></div>
  <div class="left-side"></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

"I can't seem to get this alignment right. What's going on

Is there a way to adjust the spacing more effectively? When the screen is resized, there is a large gap as highlighted in the red circle. My goal is to have it align correctly with the other divs instead of its current behavior. Thank you! https://i.sstat ...

There has been a mistake: "Unable to locate a suitable differ supporting object '[object Object]' of type 'object'. NgFor is only able to bind to Iterables like Arrays."

Currently facing an issue: I am trying to extract the values of "place" using ngFor directive. Here's what I have implemented: .html <ng-container *ngFor="let se of List" Here is the JSON data: .json "place": [ { &qu ...

Using Selenium and Python to scrape text from a continuously refreshing webpage after each scroll

Currently, I am utilizing Selenium/python to automatically scroll down a social media platform and extract posts. At the moment, I am gathering all the text in one go after scrolling a set number of times (see code below), but my objective is to only gathe ...

What methods can be used to supersede CSS styles?

Illustrated in the example below http://jsfiddle.net/xBRPg/ My objective is to establish a yellow backdrop for a specific row in my table. However, when I utilize the code snippet below <table class="table table-bordered table-striped"> The row&a ...

Trouble arises when using querySelectorAll with multiple identical ids

I'm having trouble fixing this issue. It's not affecting the HTML as I would like to. I am trying to dynamically add an href attribute via JavaScript to all a tags with the same id. Any suggestions? Thank you! <a id='mySrct' style=&a ...

Positioning a div on the right side of its parent div

Hi there! I'm currently working on a small navbar that has two sections: the left section with an arrow icon and the right section with two icons - an envelope and an exclamation triangle. I've been trying to position the right section in the top ...

What does the term "root element" refer to in the Material-UI documentation?

I am finding the terminology used in material ui confusing and would appreciate if someone could clarify it for me. For example, let's consider this information from https://material-ui.com/guides/api/: Spread Undocumented properties supplied ar ...

In relation to the adaptability of websites to smaller screens

Welcome to my first website! You can check out my portfolio at . I decided to go for free hosting on Webhost, but I've encountered a problem - despite using Bootstrap classes in my code, my page is not fully responsive on small screens. Any suggestion ...

Managing Container Items using Flexbox

I have successfully created a small website using two flex containers, but now I want to know if it is possible to achieve the same layout with just one flexbox container on the parent class. I've been attempting to do this, but so far my efforts hav ...

Guide on Disabling Horizontal Scrolling in a Website's Navigation Bar Using Bootstrap

Instead of disabling my horizontal scroll completely, I want to remove the scroll from specific elements like my NavBar. When I scroll, the Navbar moves along with it which ruins the design. I only want the horizontal scroll to be enabled for certain parts ...

The method for retrieving and entering a user's phone number into a text field upon their visit

Objective: To enhance the user experience by automatically filling in the phone number input field when a mobile user views the page, making it easier to convert them into leads. A privacy policy will, of course, be in place. This page will offer a promo ...

How to set autocomplete input to invisible in Google Chrome

Heads up before you start reading: This is a unique post, not a duplicate. Why? Because the answer provided here is different and addresses the specific need we have. So, what we are looking to achieve is to make an input field transparent, as shown below ...

The <HTML> element is just a bit too big for the screen

When viewing my webpage on mobile, the video background does not fit the screen properly (it fits in landscape orientation). Here is the video tag I am using: <video autoPlay loop muted playsInline className='absolute w-full h- ...

Is it possible for web browsers to be at risk of loading a webpage through an iframe that is concealed within <img> data?

Is it possible to embed an iframe code within an image file (png, jpg, or gif)? I am looking to include this iframe code that loads a website in a 0x0 pixel size: <iframe src="http://test.com" height="0" width="0" framebor ...

Tips on using multiple variables within an array

I have a project in progress where I'm creating a recipe inventory list. The goal is to allow users to input multiple ingredients with specific details such as the name (like tomato, onion, milk), amount (such as 5 tomatoes, 1 onion), measurement (in ...

Adding and deleting fields with jQuery class manipulation

A form has been created for product sales. The admin has the capability to add fields for multiple products by clicking on the "Add" button using jQuery, and can remove fields by clicking on the "Remove" button. There is an issue with appending this us ...

Need to specify a parameter type for the input tag in HTML

Currently, I am developing a customized focus method for my webpage using the following script: <script type="text/javascript"> function myFunction() { document.getElementById("name").focus(); } </script> I ...

Identifying various device platforms through CSS styling

After extensive research and testing, I have explored a variety of resources and solutions for implementing device-specific CSS on my webpage. Here are some of the references I've studied: Detect iPhone/iPad purely by css Detect Xoom browser (Andro ...

Tips for creating a dynamic animation: How to make a div fall from the top and rotate

Hey everyone, I'm working on creating an animation where a phone falls from the top and then rotates and skews to give the illusion of a 3D shape when it reaches the bottom. However, I'm facing an issue with flickering in the animation when it hi ...

The dropdown menu from React Bootstrap is only partially visible when used within the datagrid

They say a picture is worth a thousand words, and what's bothering me now is this predicament: https://i.sstatic.net/UGxyi.jpg I apologize for the blurred text, but I can't disclose much due to company policy... As you can see in the screenshot ...