How to position a div in the center of another div using HTML

I'm struggling to center the bottom div within a container that includes 3 other divs. Despite trying multiple solutions found online, nothing seems to work. This task should be simple, but for some reason, I can't get it right.

.container  {
position: relative; 
width: 968px;   
margin: 0 auto; 
text-align: center;
}

.text       { 
position: absolute;  
width: 968px; 
height: 100px;">
}     

.bg_image       {
position: absolute;   
width: 968px; 
height: 545px; 
top: 150px; 
background-image: url(x.jpg);
} 

.bg_image2      { 
position: absolute; 
display: block; 
width: 50%; 
margin: 0 auto;   
height: 200px; 
top: 545px; 
background-image: url(x.png); 
background-repeat: no-repeat;
}

Answer №1

Initially, there appears to be a mistake in your CSS code within the .text section - please remove the extra "> after height: 100px; Additionally, without including your HTML code, it's difficult for us to provide an accurate answer to your question.

Answer №2

Perhaps this code snippet can provide some assistance.

 <!DOCTYPE html> 
    <html > 
    <head> 
     <style type="text/css"> 
    #outer{ position:fixed; top:0; left:0; width:100%; height:100%; } 
    #inner1{ width: 50%; height: 20%; top: 25%; margin: 0 auto; position: relative; background:orange; } 
    #inner2{ width: 50%; height: 40%; top: 25%; margin: 0 auto; position: relative; background:red; } 
     </style> 
    </head> 
       <body> 
         <div id=outer> 
           <div id=inner1> </div> 
           <div id=inner2> </div> 
         </div> 
       </body> 
 </html>

Answer №3

To center a div on the page, set a fixed width such as width:100px; and apply margin:0 auto;.

Follow these steps for successful centering.

.centerDiv{
width:100px;   /*choose your desired width.*/
height:100px; /*adjust height as needed.*/
margin:0 auto; /*essential for centering the 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

Unusual hue in the backdrop of a daisyui modal

https://i.stack.imgur.com/fLQdY.png I have tried various methods, but I am unable to get rid of the strange color in the background of the Modal. Just for reference, I am using Tailwind CSS with Daisy UI on Next.JS. <> <button className='btn ...

Having trouble with animating the background color of an input button using jQuery?

I'm completely confused as to why the background color isn't changing despite investing a significant amount of time into it: <input type="button" class="front-consultation-btn" value="Get A Free Consultation"> <script> $(' ...

Adjust the stroke and fill colors of an SVG element when hovering over it

I am facing a challenge with an SVG image that I have: https://i.stack.imgur.com/r4XaX.png When hovered over or clicked, it should change to https://i.stack.imgur.com/EHRG2.png Current Icon <svg width="24" height="24" viewBox="0 0 24 24" fill="non ...

Considering the advantages and disadvantages, is it advisable to implement HTML 5 for a website revamp?

Currently, I am involved in the re-write and redesign of a major website. To ensure that my work is well-informed, I have been conducting extensive research on HTML 5. However, prior to adopting it for this particular design implementation, I would like to ...

Tips for designing scrollable overlay content:

I am currently in the process of building a page inspired by the design of Hello Monday. Right now, I have added static content before implementing the parallax effect and auto-scroll. Here is my progress so far: Check out the Sandbox Link One challenge ...

jQuery function failing to produce the intended results

I am trying to apply the active class to the last img element using HTML and jQuery Below is my code in HTML and jQuery: function initialSteps() { $(".row").find("column:last").find("img:first").className += " active"; // this does not work either ...

Manipulating contextual selectors

Have you ever tried reverting or overriding attributes from contextual selectors? .card { padding-top: 20px; ... } .card .card-header { padding: 0 20px; } .card .card-header.news { padding-top: 0px; } Do you think it's possible to elimina ...

The height set to 100% is causing issues with the padding-bottom property

Currently, I have a setup that includes: A div with width set to 100% and height set to 100% An SVG element inside the div with default width and height of 100% My issue is that when applying padding to the div, the left, right, and top padding seem to w ...

Is there a way to turn off step navigation in bootstrap?

Displayed below is a visual representation of the bootstrap step navigation component. Presently, there is an unseen 'next' button located at the bottom of the page. When this 'next' button is pressed, it transitions from 'step-1 ...

The footer row in jqgrid is experiencing issues with functionality

I am trying to create a jqgrid table with three footer rows to display a total at the end of each grid. Following the guidance in this helpful post from Oleg on How to create two footer rows in jqgrid, I was able to modify the code provided to show three t ...

When hovering over individual links within a set of blocks, ensure that the other links are not affected or disrupted

I need help creating a forum category link guide. Similar to this example... Specifically, I want it to display as Forums > The Financial Roadmap Two issues I am facing are: when hovering over the link, it shifts down one row and how can I add blocks lik ...

Access an HTML file in Text Edit on a Mac directly from a web browser

Is there a way to utilize Javascript or another appropriate script to open an HTML file in Text Edit on my Mac? I have created a local web page using Text Edit that has different tabs linking to other Text Edit files within the page. I am looking for a m ...

Styling images side by side with CSS in Internet Explorer

I've encountered a CSS dilemma. I have a collection of images that I want to present side by side using an unordered list within a fixed width container. The goal is to have 3 images per row before moving on to the next set of 3 images. Each list ite ...

Selection of child elements using CSS selectors

I have the following HTML snippet and I want to target the second child with the class name "embed": <div class="embed-container">test</div> This is what I tried: .feature-row .threecolumns .caption:nth-child(2) { border: 1px solid red; ...

Flip the Script: JQuery slideshow in reverse

Hey there, I've been tinkering with a JQuery slideshow and encountering an issue. All my images in the HTML are stacked on top of each other, and I attempted to modify the code so it starts with the last image. However, my attempts have not been succe ...

Tips for choosing a particular list item using jQuery: retrieve the attribute value of 'value' and showcase it on the webpage

I have a task that requires the following: Implement an event listener so that when you click on any list item, the value of its "value" attribute will be shown next to this line. In my HTML, I have an ordered list with 8 list items. The values range fro ...

show tab focus outline only

In search of a straightforward and effective method for focusable elements to display an outline only when the tab key is pressed, without showing it when using a mouse in React applications. (looking for something similar to :focus-visible that function ...

The background color of the columns is overwhelming

I would like to create a TV remote control design using bootstrap, but I am encountering some issues. The background color is overflowing and I'm unsure how to fix it. Please take a look at the code and feel free to offer any suggestions! @media scre ...

The addClass and removeClass functions seem to be malfunctioning

Hey everyone, this is my first time reaching out for help here. I looked through previous questions but couldn't find anything similar to my issue. I'm currently working on a corporate website using bootstrap3 in Brackets. I've been testing ...

Creating consistent image sizes in Bootstrap

I'm currently using Bootstrap 4 to showcase multiple images on my website. I have attempted to apply the Grid system as per Bootstrap's guidelines, but the display of the images is not aesthetically pleasing due to their uneven sizes, which can b ...