What is the best way to horizontally align a div container that houses floating divs using CSS?

I am currently working on a layout that involves a main div container, div#title-box, being vertically and horizontally centered on the page. Inside this main container, I need three additional div containers: div#logo, div#title, and div#subtitle (see code for their specific positioning).

My challenge lies in ensuring that while div#logo has a fixed width, the other two divs can expand dynamically. How can I make div#title-box wrap around all three divs while keeping everything centered? Additionally, div#title-box should not have a fixed width.

If you have any suggestions or solutions, they would be greatly appreciated!

Here is a complete example of the code if you'd like to experiment with it:

<!DOCTYPE html>
<html>
<head>
<style>
   div#title-box {
   max-width: 500px; 
   display: block;
   height: 600px;
   margin: 0 auto;
   position: relative;
   }

   div#logo {
   padding: 0;
   margin: 0;
   position: absolute;
   top: 100px;
   left: 5px;
   width: 100px;
   height: 100px;
   overflow: auto;
   background: #ff0000 no-repeat;
   background-size: 100% 100%;
   border-radius: 15px;
   float: left;
   }

   div#title {
   padding: 0;
   margin: 0;
   position: absolute; 
   left: 110px;   
   top: 100px;
   bottom: 20px;
   right: 10px;
   overflow: auto;
   float: left;
   }

   div#subtitle { 
   padding: 0;
   margin: 0;
   position: absolute;    
   top: 140px;
   bottom: 20px;
   right: 10px;
   left: 110px;
   overflow: auto;
   float: left;
   }


</style>
</head>
<body>
<div id="title-box">
  <div id="logo">
  </div>  
  <div id="title">
    <h1>Hello</h1>
  </div>  
  <div id="subtitle">
    <h3>A A A A A A A A A A A A A A A!</h3>
  </div> 
</div>

Answer №1

Experiment with a similar approach:

   div#header-container {
       width: 500px; 
       display: inline-block;
       height: 600px;
       margin: 0 25%;
       position: relative;
       left:-250px;

   }

Answer №2

When you implement the following CSS property:

position: absolute

in your code, it causes the element to be removed from the normal document flow.

I have made changes to your CSS in this updated version: http://jsfiddle.net/asBmS/. Now, the parent div should always enclose its child divs. Does this align with your desired outcome?

Answer №3

If you're looking for a clever solution to center your content without adjusting the width, consider this little 'hack' that involves tweaking the offset margin-left property. It may require further adjustments based on your specific requirements.

For a detailed look at how it works, check out this link: http://jsfiddle.net/asBmS/15/

#logo, #title, #subtitle {
    /*offset hack*/
    margin-left: 10% !important;
}

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

Comparing GET and POST actions in mod_rewrite search form

Hey there, thank you for taking the time to address this query. I have implemented a live search form using jQuery on my website. The search bar is present on every page and functions well. Now, I am looking to enhance its functionality. When users press ...

The flex container cannot contain all of the content due to an

I am looking to implement a flexbox dropdown menu similar to this: https://i.sstatic.net/Cer9D.png After researching online, I found a simple dropdown menu example. However, I want to customize it using flexbox and its properties. The issue I encountered ...

Looping through items with ng-repeat and creating a submenu with M

When constructing a menu from a JSON file with submenus using the JQuery.mmenu plugin, I encountered an issue when trying to implement the submenu structure with ng-repeat. The raw HTML approach worked perfectly, but as soon as I introduced ng-repeat to dy ...

Can using .wrap( ) negate the styling effects of the wrapper?

I am struggling with centering a button on my webpage. Currently, I have the following code for creating the button: var button = ($('<button>', { "id": "jspsych-free-sort-done-btn", "class": "jspsych-free-sort", ...

How can you delete a specific class name from a chosen element in VueJs?

My Vue.js method is set up like this. In the changeRoute function, I am able to change the class name by using e.target.className = 'clicked';. However, I am facing difficulty when trying to remove that class name from other elements using pre.re ...

How to target the first and last elements of sequences with a specific class using CSS

My webpage contains numerous div elements with the class line, each containing a strong.datum element. I want to designate the first and last element of each sequence with the class .active. If this can't be achieved using CSS, is there a way to accom ...

Creating a unique CSS style sheet tailored specifically for Internet Explorer and Microsoft Edge – here's how!

Struggling with internet explorer acting up and causing glitches on my website? Check out my problem here. The Issue: An interactive image on my website is not functioning properly in IE & Edge. The image should expand when hovered over, but glitches ...

`Why won't the checkbox uncheck when the dropdown is changed in jQuery?`

I have a roster of users, each with a unique checkbox for selection. When I adjust the dropdown menu, a new group of users is chosen and marked as selected. However, I am struggling to uncheck the previously selected checkboxes based on the last dropdown c ...

Using React components to activate a click effect on the parent component

I have a React component called Card that has an onclick function triggered when the card is pressed. However, within this component, there are child elements such as sliders and buttons that perform their own actions. When clicking on these child elements ...

Loading content dynamically into a div from an external or internal source can greatly enhance user experience on a website. By

As I work on my website, I am incorporating a div structure as shown below: <div class="container"> <div class="one-third column"> <a id="tab1" href="inc/tab1.html"><h2>tab1</h2></a> </div> & ...

Is using tables still considered a recommended practice for organizing a layout such as the one shown in the [IMG]?

Looking for recommendations on arranging images in the footer, in line with the design mockup using HTML/CSS. Wondering if tables are still a viable option for this purpose? ...

What method can I use in webpage coding to achieve this special highlight effect, and what is the official term for it?

Need help figuring out how to make an icon change from blue to white when selected. I've searched through Bootstrap, CSS, and HTML, but haven't found the solution yet. Any suggestions would be appreciated! https://i.stack.imgur.com/RK1PD.png ...

What makes the transparent border colors on <tr> appear too dark?

For instance, take a look at this code: http://jsfiddle.net/WaJy7/ In my attempt to apply a semi-transparent border to every <tr> element, I've encountered an issue where the color of the border appears darker than intended for all rows except ...

Excessive empty space appears on my mobile screen

Is there a solution to remove the white space issue that only occurs in mobile view? Here is an image of the problem: Untitled: https://i.sstatic.net/6WcGy.png I attempted using responsive instead of fluid, but it only resulted in the image being scaled ...

Fetch information post search action through Ajax on a single PHP page

Looking for some guidance on using AJAX and PHP together. I've set up a page that uses AJAX to load data, and it's displayed with pagination. However, I'm struggling to redirect the search results back to birds.php. birds.php <script ty ...

Exclude specific IDs from Bootstrap 4 scrollspy functionality

I have consecutive sections, and in between them I have an aside without a corresponding anchor. The issue is that the scrollspy is active with the previous section. I want to adjust the scrollspy so that nothing is highlighted as active at that point, and ...

Creating an HTML table by populating it with data from a JavaScript array

Imagine you have a JavaScript array structured like this: [ "Heading 1", "Heading 2", "Data 1", "Data 2", "Data 3", "Data 4", ] Your task is to generate an HTML table from this array that looks like the one in this link: https://i.stack.imgur.com/7laUR. ...

Displaying database records in custom fragments using Spring Boot and Thymeleaf

I'm struggling with displaying database records on a webpage in a specific format. To achieve this, I've created a thymeleaf fragment to act as a template for each record in my database. However, I can't figure out how to make these fragment ...

Show the exact file name for the CSS file being generated by grunt-contrib-compass

I need help specifying the output name of the CSS file in my grunt-contrib-compass task. Currently, it outputs as index.css. Current Output <%= yeoman.app %>/specialdir/themes/index.css Desired Output <%= yeoman.app %>/specialdir/themes/my-th ...

Is it possible to implement a hover animation within another hover animation in WordPress?

Trying to incorporate two hover animations for the images in my photo galleries. When hovering over an image, I want a text to appear in a black overlay, and then when hovering over that text, it should become underlined. Looking for something similar to ...