Steps for aligning an image in the center above two divs

I've been attempting to accomplish something without success. The image below should speak a thousand words.

My goal is to position div 3, located in div 2, perfectly centered between div 1 and 2 to achieve the desired outcome:

Here's my HTML and CSS code:

HTML

<div id="container">
    <div id="leftSide">
        <!-- 1. -->
    </div>

    <div id="rightSide">
        <!-- 2. -->
        <div id="circle">
            <!-- 3. Contains the image -->
        </div>
    </div>
</div>

CSS

#container{
    width: 600px;
    float: left;
    padding: 0;
    margin: 0 auto;
}

#leftSide{
    width: 300px;
    height: 300px;
    float:left;
    background-color: blue;
}

#rightSide{
    width: 300px;
    height: 300px;
    float:left
    background-color: red;
}

#circle{
    width: 100px;
    height: 100px;
    margin-left: 30px;
    background-color: black;
}

I'm open to suggestions on how to approach this problem. Thank you for any assistance provided. Much appreciated.

Answer №1

If you're looking to adjust the positioning of your elements on a webpage, utilizing the CSS position property could be an effective solution.

CSS Positioning Example:

#container{
width: 600px;
float: left;
padding: 0;
margin: 0 auto;
position:relative;
}

#leftSide{
width: 300px;
height: 300px;
float:left;
background-color: blue;
}

#rightSide{
width: 300px;
height: 300px;
float:left
background-color: red;
}

#circle{
width: 100px;
height: 100px;
margin-left: 30px;
background-color: black;
position:absolute;
top:/* INSERT VALUE HERE */;
left:/* INSERT VALUE HERE */;
}

top:50px; moves the element down by 50px

left:50px; shifts the element to the right by 50px

Answer №2

To achieve this effect, use position:absolute; on the #circle element and position:relative on the #container element. Make sure to apply the specific positions indicated below.

Check out the code in action here: http://jsfiddle.net/a081j6bv/1/

#container{
 position:relative;
}

#circle{
 position:absolute;
 left:0;
 top:0;
 bottom:0;
 right:0;
 margin:auto; 
}

Answer №3

If you want to make the "circle" div have a static height/width, you can achieve this by using absolute positioning. Position it 50% from the left and top, then apply a negative margin equal to half the size of the circle div.

HTML:

<div id="container">
    <div id="leftSide">
        <!-- 1. -->
    </div>

    <div id="rightSide">
        <!-- 2. -->
    </div>

    <div id="circle">
        <!-- 3. Contains the image -->
    </div>
</div>

CSS:

#container{
    width: 600px;
    float: left;
    padding: 0;
    margin: 0 auto;
    position:relative;
}

#leftSide{
    width: 300px;
    height: 300px;
    float:left;
    background-color: blue;
}

#rightSide{
    width: 300px;
    height: 300px;
    float:right;
    background-color: red;
}

#circle{
    width: 100px;
    height: 100px;
    background-color: black;
    position:absolute;
    top:50%;
    left:50%;
    margin-top:-50px;
    margin-left:-50px;
}

JSFiddle

Answer №4

To achieve the desired effect, it is necessary to apply relative positioning to the #container and absolute positioning to the circle element.

#container{
    width: 600px;
    float: left;
    padding: 0;
    margin: 0 auto;
    position: relative;
}

#leftSide{
    width: 300px;
    height: 300px;
    float:left;
    background-color: blue;
}

#rightSide{
    width: 300px;
    height: 300px;
    float:right;
    background-color: red;
}

#circle{
    width: 100px;
    height: 200px;
    position: absolute;
    left:0;
    right: 0;
    top:0;
    bottom:0;
    margin: auto;
    background-color: black;
}
#circle img{
   width: 100px;
   height: 100px;
}
<div id="container">
    <div id="leftSide">
        <!-- 1. -->
    </div>

    <div id="rightSide">
        <!-- 2. -->
        <div id="circle">
            <!-- 3. Contains the image -->
            <img src="http://i.imgur.com/lrg4uy5.jpg"/>
            <img src="http://i.imgur.com/RLKixQW.png"/>
        </div>
    </div>
</div>

Answer №5

When it comes to your preferences and requirements, you have the option to select one of the 4 templates below:


#1 Circular center using position, top, bottom, left, right, and margin attributes

#container {
    height: 300px;
    /* preparing #container for centering #circle */
    position: relative;
}
#leftSide {
    background-color: blue;
    float: left;
    height: 100%;
    width: 50%;
}
#rightSide {
    background-color: red;
    float: right;
    height: 100%;
    width: 50%;
}
#circle {
    background-color: black;
    border-radius: 50%;
    height: 140px;
    width: 140px;
    /* centering #circle inside #container */
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}
<div id="container">
    <div id="leftSide"></div>
    <div id="rightSide">
        <div id="circle"></div>
    </div>
</div>


#2 Centered circle using position, top, left, margin-top, and margin-left attributes

#container {
    height: 300px;
    /* preparing #container for centering #circle */
    position: relative;
}
#leftSide {
    background-color: blue;
    float: left;
    height: 100%;
    width: 50%;
}
#rightSide {
    background-color: red;
    float: right;
    height: 100%;
    width: 50%;
}
#circle {
    background-color: black;
    border-radius: 50%;
    height: 140px;
    width: 140px;
    /* centering #circle inside #container */
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -70px;
    margin-left: -70px;
}
<div id="container">
    <div id="leftSide"></div>
    <div id="rightSide">
        <div id="circle"></div>
    </div>
</div>


#3 Central circle using position, top, left, and transform properties

#container {
    height: 300px;
    /* preparing #container for centering #circle */
    position: relative;
}
#leftSide {
    background-color: blue;
    float: left;
    height: 100%;
    width: 50%;
}
#rightSide {
    background-color: red;
    float: right;
    height: 100%;
    width: 50%;
}
#circle {
    background-color: black;
    border-radius: 50%;
    height: 140px;
    width: 140px;
    /* centering #circle inside #container */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
<div id="container">
    <div id="leftSide"></div>
    <div id="rightSide">
        <div id="circle"></div>
    </div>
</div>


#4 Central circle using Flexbox

Please note that the HTML snippet below differs from the previous examples.

#container {
height: 300px;
background: linear-gradient(90deg, blue 50%, red 50%);
/* preparing #container for centering #circle */
display: flex;
align-items: center;
justify-content: center;
}
#circle {
background-color: black;
border-radius: 50%;
height: 140px;
width: 140px;
}
<div id="container">
    <div id="circle"></div>
</div>

Answer №6

Hi everyone, I encountered a similar issue when I was just starting out. In order to get the desired effect, I ended up adjusting the container's position to relative and the image's position to absolute. It worked like a charm! - Happy coding!

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

Tips for changing form field values using jQuery

Is there a way to use jQuery to retrieve the values of multiple checkboxes and insert them into a hidden form field? Here is how my checkboxes are structured: <form> <input type="checkbox" name="chicken" id="chicken" />Chicken <in ...

What is the best way to obtain a direct file link from a server URL using JavaScript?

After acquiring a file located at /home/johndoe/index.html, I am utilizing a tool like XAMPP to host, with the folder /home being hosted on localhost. The variables in play are as follows: $server_addr = "localhost"; $server_root = "/home"; $file_dir = " ...

The use of "runat="server" in the HTML Head tag is preventing C# embedded code blocks from being converted to client-side code

After I included runat="server" in the head tag and added a CSS href with the value of <%= WebAppInstance %>, I noticed that the <%= WebAppInstance %> was not being converted to client-side code. To provide clarity on my question, please refer ...

JavaScript - Retrieving the variable's name

Whenever I invoke a function in JavaScript with a variable, like this: CheckFunction(MyVariable); CheckFucntion(MyVariable2); Suppose I have a function that checks if the input is a number: function CheckFunction(SOURCE){ //THE CODE ITSELF }; I want to ...

What is the best way to modify this CSS code for use in a React Native

Encountering an issue while trying to apply this CSS property in a React Native project. border-radius: 50% / 100%; Attempting the following: borderRadius: '50% / 100%' An error message is displayed stating "Java.lang.string cannot be cast to ...

When you click on the button, the section will not be displayed

I'm facing an issue with my code. I have a set of five buttons and corresponding sections. When a button is clicked, the active and active-btn classes are supposed to be added to that button as well as the corresponding section element with the same i ...

Revamp the current webpage to display attribute values in textual format

As I peruse a webpage, I notice that there is room for improvement in terms of user-friendliness. The page is filled with a list of movie titles, each accompanied by a link to IMDb. However, the IMDB user rating is only visible when hovering over the titl ...

Tips for customizing the transparency of a Bootstrap dropdown menu

I'm currently working with .navbar-default { z-index:99; opacity:0.8; } However, I am looking to adjust the opacity of dropdown menu items to 0.9. I have attempted several solutions without success. Here are a few examples: .dropdown-menu>li> ...

Modal Pop-ups Failing to Open on Subsequent Attempts

My table consists of buttons on the right-hand side for a menu. The first button in the menu is supposed to open a modal box upon clicking. However, the first buttons in the subsequent rows do not function as expected and fail to open the modal. Refer to t ...

Is it mandatory to include a DOCTYPE/DTD in XHTML5?

Currently in the process of developing a new language that converts to XML, I am facing the limitation of not being able to support DOCTYPE/DTD. Would it be possible for me to utilize XHTML5 without needing to declare , or will I have no choice but to in ...

Is Javascript the best choice for managing multiple instances of similar HTML code?

I am currently facing the challenge of dealing with a lengthy HTML page consisting of around 300 lines of code. The majority of the code involves repetitive forms, each identical except for the ID (which varies by number). Is it considered appropriate or ...

A guide to creating a personalized horizontal dashed separator in Material UI

I'm looking to customize a divider template by turning it into a horizontal dashed divider, but I'm struggling to override it. Even when attempting an sx edit with a borderRadius, the divider doesn't change as expected. source code: import ...

Tips for changing the color of shapes when hovering over an image state

I have arranged three images in a column and applied column-count: 3; along with setting border-width for those images. Now I am curious about how to create the hover state for these images. Here is the HTML code snippet: <div class="wrap"> < ...

What is the method to conceal an element after detecting and locating a specific class using jQuery?

Can someone please assist me today with the following task: Step 1: <div id="htmlData"> <div class="col-md-12"> <div class="pull-left"> <h3>Report: <strong>Travel </strong></h3> ...

There was an error loading the resource as the Object does not have the method 'adGallery'

For the past two days, I have been attempting to implement the jQuery plugin "ad-gallery" on my website without success. I must mention that my knowledge in JavaScript and CSS is limited, so the issue could be something simple. The peculiar thing is that ...

The absence of an eye icon in the password field when using bootstrap 5

I'm having trouble positioning the eyeball icon to the right side of my form when using Bootstrap v5 and FontAwesome v5. Instead, the password field is being shifted further to the right compared to the username field, and the eye icon is not displayi ...

Turn off the feature of map scrolling on OpenStreetMaps

Is there a way to prevent mouse interactions and scrolling in an open maps iframe? I have already tried adding the attribute scrollwheel="false" to no avail. Are there any CSS methods to achieve this? <iframe id= "mapsource" scrollwheel="false" src="ht ...

The alignment of the timeline in HTML CSS becomes distorted once it reaches the fourth element

As a newcomer to HTML, CSS, and JS, I am currently embarking on the task of creating a personal website for a course project. One of the pages on my website showcases a timeline featuring dates and information. While I have referred to a tutorial on W3scho ...

Deactivate multiple input fields by utilizing various radio buttons

I need help with enabling and disabling input fields based on radio button selection. When the first radio button is selected, I want to disable three input fields, when the second is selected, only two specific input fields should be enabled (SHIFT START ...

Why am I not receiving any results from the communication between JavaScript and PHP using an HTTP GET request?

Currently, I have a small JavaScript program running within an HTML5 canvas and included a HTTP GET request function in my JavaScript code. The function itself is functioning properly as I tested it with multiple examples from the internet, all of which wo ...