How to center a div horizontally within another div using CSS

I'm attempting to center a block element horizontally on my webpage.

Here's an illustration of what I'm aiming for:

I can't use fixed div dimensions as the design needs to be responsive.

Click here for the demo.

Below is the HTML code:

 <div class = "container">
    <div class="sub">
        <h1> 01</h1>
        <div class = "icon"><img src="img/settings.png" alt="">
            <h2> Power Inside</h2>
            <p>Cum sociis natoque penatibus et magnis dis parturient montesmus. Pro vel nibh et elit mollis commodo et nec augueique</p>
            <a class="iconlink" href="/">Read more</a>
        </div>
    </div>
 <div>

CSS

.container {
    width: 1160px;
    overflow: hidden;
    display: inline-flex;
    margin: 0 auto;
}

.sub{
    padding: 30px;
    display: inline-block;
    vertical-align: top; 
}
....
....

Answer №1

Here is the solution you are looking for.

.container {
    Use flex instead of inline-flex
}

If you still need to use inline-flex, then apply text-align:center; to the parent element.

Answer №2

Having a responsive layout doesn't necessarily mean using only flexible sizing. You can actually incorporate "hard" sizing by utilizing media queries in your CSS file. These queries allow you to specify certain adjustments for elements when the page reaches a specific size. Below is an example code snippet:

@media (max-width: 400px) {
  div {
    width: 20px;
  }
}

If you find this concept a bit confusing initially, don't worry! Once you grasp it and witness its functionality, everything will become clearer and more straightforward.

Answer №3

To create a wrapper div that spans 100% width, simply add a div with width:100%. Inside this div, create another div with your desired total width and margin: 0 auto. Within this second div, you can then create three additional divs with a width of 33% each and set them to float:left.

<div class="container">
        <div class="subcont">
           <div><p>This is div1</p></div>
           <div><p>This is div2</p></div>
           <div><p>This is div3</p></div>
        </div>
    </div>

div:not(.container):not(.subcont){
    float: left;
    width: 33%;
    margin: 0 auto;
}

.container{
    text-align: center;
    width: 100%;
    background: green;
    overflow: hidden;
}

.subcont{
    margin: 0 auto;
    width: 50%;
    background: red;
    overflow: hidden;
}

For an example implementation, check out: http://jsfiddle.net/cbmkg2e5/

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

What is the process for calculating the total sum of input values utilizing JavaScript?

My JavaScript skills are not perfect, and I'm struggling to calculate the total sum of values in the amount input boxes without refreshing the page. Can someone assist me with this challenge? Thank you. function Calculat ...

Remove the float from the final list item in order to resolve display issues in Internet Explorer

I am trying to create a menu with the following HTML structure: <ul> <li id="btnHome"><a href="#">Link One</a></li> <li id="btnAbout"><a href="#">Link Two</a></li> <li id="btnContact"> ...

Interpolation problem with AngularJS translation service

I've successfully implemented a ternary operator in my HTML: {{ qp.QP_TRANSFERS === '1' ? qp.QP_TRANSFER_TYPE_NAME + ' transfer' : 'No transfer' }} In addition, I am utilizing a translation service with JSON objects. He ...

Iterate through the contents of a directory and display the HTML content corresponding to each file

Currently, I am diving into the world of PHP Laravel. The project I am working on involves repeating blocks of HTML code in my template, which I want to streamline using a loop. My idea is to automate this process by implementing a foreach loop in my Vue ...

How to deactivate the <a> tag with Ant Design UI Library

Is there a method in the antd UI library to disable a link? The disabled attribute is not supported by the a tag according to MDN. This code snippet works in React but the link remains clickable when using Next.js. <Tooltip title={tooltip}> <a ...

Adjusting the sound levels of an HTML audio element using JavaScript

I'm looking to adjust the volume of an <audio> element when a user clicks on an image. HTML: <div id="cloud"> <img name="jsVolumeButton" src="images/cloud.png" width = "140px"/> </div> <audio id="player" src="sounds/rain. ...

What causes the CSS width property to vary when using the display:inline property?

There is a piece of code that contains a <ul> element with the default css property display:block. Here is the code snippet: ul,li{ list-style-type:none; padding:0; /*display:inline;*/ } #parent{ width:100%; /*height:50px;*/ background-color ...

Sass Alert: The mixin called roboto-family is missing from the stylesheet. Trace: Problem detected in src/pages/forms/forms.scss at

Greetings, I am diving into the world of Ionic for the first time. Recently, I embarked on a new project in Ionic and decided to integrate a theme. To do so, I copied an .html file, an .scss file, and also created a .ts file. Forms.html <!DOCTYPE html ...

"Exploring the world of Reactstrap: A

I am struggling to understand the documentation for Bootstrap and reactstrap as I have never used them before. For example, changing the Navbar color and background opacity has been a challenge because they require reserved keywords to be passed as props, ...

Is there a way to dynamically modify the text of an HTML button element with jQuery?

My goal is to modify the text value of an HTML code using jQuery... <div class="dropdown"> <button type="button" class="btn btn-secondary dropdown-toggle button-text" data-toggle="dropdown><span>1395</span></button> ...

Problem with the mobile site width due to viewport misalignment

Our website is currently experiencing an issue with the viewport, but it seems to only occur on mobile devices: If you visit the site on your mobile device, you may notice a large gap on the right side of the page. Strangely, this problem does not appear ...

Problems with select tag change events

I encountered an issue with select tag onChange events. When I select a value from the select tag, it should display in a textbox that is declared in the script. It works perfectly when I remove the class "input" from the select tag, but I prefer not to re ...

Utilizing Bootstrap 3: Achieving Varied Font Sizes within a Row with Bottom Alignment

Just starting out with Bootstrap and looking to create a layout with two columns of text in the same row, where the first column has a larger font size than the second. However, I'm running into an issue where the text in the second column is position ...

Unable to Upload Image to MySQL Database

I encountered an unidentified index error when trying to upload images. I added a check to prevent the error, but the images are still not being uploaded. I have tested with legitimate images, but nothing seems to work. I've been struggling with this ...

What is the best way to add a character within a YouTube JSON link?

Fetching json data from a link, the youtube format in the link is http:\/\/www.youtube.com\/watch?v=UVKsd8z6scw. However, I need to add the letter "v" in between this link to display it in an iframe. So, the modified link should appear as ht ...

Can you explain the concept of "entity-body" in relation to Digest Authentication?

Incorporating digest authentication and noticing mention of "entity-body" in the document for auth-int authentication. Is this referring to the HTML header and body, or just the HTML header section? For more information on digest authentication, check out ...

Ways to configure dynamic and responsive divs using CSS exclusively (no javascript needed)!

I am looking to have my main div expand in height based on the categories div, content, or product pictures. Check out the website here Here are the issues I'm facing: The content div and categories div are both within the main div, but the produ ...

Choose a Date from the Calendar and Modify the Link Text

I have a Link Label on my page that triggers a calendar pop-up when clicked. I want the label to update to the selected date in the format '30 JAN 2017' from the calendar. The issue lies with the local variable var dateText =...; although the la ...

The importance of z-index in web design and its compatibility with Chrome

Chrome version 26 seems to be having trouble with z-index, especially when I have two divs. <style> #div1{ position:relative; z-index:1000; } #div2{ position:absolute; z-index:10001; ...

GAE does not have access to background images

Having trouble loading background pictures in my GAE front-end app. "Failed to load resource: the server responded with a status of 404 (Not Found)". My app is a simple website, no GAE backend. Using Eclipse with Google AppEngine Plugin. Background pict ...