Divs that are 30% of their parent's width will not align vertically and will not fit in a parent div that is 100% width

I am attempting to vertically center the three child-divs <div class="section"> as 3 rows in one column within

<div class="container_page_2">
.

When I refer to vertical alignment, I mean having an equal distance between the top of the page and the first <div class="section">, and also between the bottom of the page and the last/third <div class="section">.

I assumed that by placing them inside a wrapper (

<div class="center-container">
) and centering that wrapper inside the main container
<div class="container_page_2">
would solve my issue, but unfortunately, it did not work.

I prefer not to use display: table; because it is considered outdated.

<div id="parent-container">
  <div id=child-container>
      <!-- #page2 -->
      <div class="container_page_2">
          <div class="center-container">
              <div class="section">
                  <div class="left-half s1">

                  </div>
                  <div class="right-half s1">

                  </div>
              </div>
              <div class="section">
                  <div class="left-half s2">

                  </div>
                  <div class="right-half s2">

                  </div>
              </div>
              <div class="section">
                  <div class="left-half s3">

                  </div>
                  <div class="right-half s3">

                  </div>
              </div>
          </div>
      </div>
  </div>
</div>
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}

/* *** index.html - START *** */

body, html {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

#parent-container {
    height: 100%;
    width: 100%;
    overflow: hidden;
    position: relative;
}
#child-container {
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px; /* exact value is given through JavaScript */
    overflow: auto;
    scroll-snap-type: both proximity;
}

.container_page_2 {
    width: 100%;
    height: 100%;
    background-color: green;
    scroll-snap-align: center;
    position: relative;
}

.container_page_2 .center-container {
    position: absolute;
    height: 100%;
    width: 100%;
    left: 0;
}

.container_page_2 .center-container .section {
    /* position: relative; */
    margin: 1% 0;
    width: 100%;
    height: 30%;
    float: left;
    border: 2px solid rgba(0, 0, 0, 0.5);
    box-sizing: border-box;
    /* z-index: 1; */
    /* text-align: center; */
}

/* *** index.html - END *** */

Here is the CodePen for the above code.

This particular web-page is part of a project-website that includes a total of 3 pages. The following divs are used for scroll-snap:

<div id="parent-container">
  <div id=child-container>

For further context, you can view the entire project here.

What are your thoughts?

Answer №1

By combining Flexbox with vh units, you can significantly simplify your CSS.

The use of vh units eliminates the need for multiple height: 100%; styles, as you can now apply a single height: 100vh; to the .container_page_2 element.

With Flexbox, aligning the child element .center-container vertically becomes effortless. Just add

display: flex; flex-direction: column; justify-content: center;
to the .container_page_2 element.

Please note: I've set the height of each row to 30px for clearer vertical alignment demonstration.

Flexbox can also be utilized to achieve the red/green division of elements within each .section.

Answer №2

Take a look at the following code snippet. By adding display:flex and justify-content: center to .container_page_2 .center-container .section, you can achieve the desired layout. Feel free to reach out if you have any further questions or need additional adjustments.

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}

/* *** index.html - START *** */

body, html {
    height: 100%;
    width: 100%;
    overflow: hidden;
}

#parent-container {
    height: 100%;
    width: 100%;
    overflow: hidden;
    position: relative;
}
#child-container {
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px; /* exact value is given through JavaScript */
    overflow: auto;
    scroll-snap-type: both proximity;
}

.container_page_2 {
    width: 100%;
    height: 100%;
    background-color: green;
    scroll-snap-align: center;
    position: relative;
}

.container_page_2 .center-container {
    position: absolute;
    height: 100%;
    width: 100%;
    left: 0;
}

.container_page_2 .center-container .section {
    /* position: relative; */
    margin: 10px 0;
    width: 100%;
    height: 30%;
    border: 2px solid rgba(0, 0, 0, 0.5);
    box-sizing: border-box;
    /* z-index: 1; */
    /* text-align: center; */
    display: flex;
    justify-content:center;
}

.container_page_2 .section .left-half {
    /* display: block; */
    width:45%;
    height: 100%;
    background-color: red;

}

/* *** index.html - END *** */
<div id="parent-container">
        <div id=child-container>
            <!-- #page2 -->
            <div class="container_page_2">
                <div class="center-container">
                    <div class="section">
                        <div class="left-half s1">

                        </div>
                        <div class="right-half s1">

                        </div>
                    </div>
                    <div class="section">
                        <div class="left-half s2">

                        </div>
                        <div class="right-half s2">

                        </div>
                    </div>
                    <div class="section">
                        <div class="left-half s3">

                        </div>
                        <div class="right-half s3">

                        </div>
                    </div>
                </div>
            </div>
        </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

Tips for displaying and hiding content in Angular2

I have a component that toggles the visibility of elements by clicking a button. This is the snippet of my HTML code: <div *ngFor="let history of histories | sortdate: '-dateModified'"> <p><b>{{ history.remarks }}</b& ...

Once the input is disabled, what is the best way to delete the previously inserted text?

Here's the scenario: I have two options that display different questions each. If a user decides to switch their option at the last minute, any inputs within option 1 will hide and show option 2. However, all input texts need to be removed and disabl ...

What is the most concise way to align one table row in the middle and another at the top?

Is there a way to align different rows in a table vertically with minimal code? I have a table with 3 rows, and I want the first two rows to be aligned vertically to the top, while the third row should be vertically aligned to the middle. If I try to def ...

Send Selected Input From Radio Button To Form Using a Function

Within the main php page titled tipComp.php, there is a script designed to submit a selected value from a radio button within a form: <script> $('input[type=radio]').on('change', function() { $(this).closest("form& ...

Linking various nodes together using the capabilities of the Web Audio API

I am experimenting with audio and trying to achieve a few specific goals: Adjust the overall volume of the audio. Modify the volume and delay of a particular channel (left). This is the code I have been working on: var audioContext = window.AudioContex ...

Obtain both the key and value from an Object using Angular 2 or later

I have a unique Object structure that looks like this: myCustomComponent.ts this.customDetails = this.newParameter.details; //the custom object details are: //{0: "uniqueInfo", // 5: "differentInfo"} The information stored in my ...

The Codepen demo for SemanticUI is not functioning properly

Click here to view the example on CodePen $('.ui.sidebar').sidebar({ context: $('.bottom.segment') }) .sidebar('attach events', '.menu .item'); I am currently trying to replicate this specific functiona ...

Issue with Font Awesome (6.2) Duotone not displaying correctly in Bootstrap 5 breadcrumb divider

I attempted to modify the Bootstrap 5.2 breadcrumb divider and include a Font Awesome Duotone icon, but it is not displaying. I am unable to figure out what the issue might be. Any suggestions would be greatly appreciated. Thank you. One interesting disco ...

Please enter the text in the field provided at the bottom using IE10

Why is the text inside my input appearing at the bottom in IE10, while it displays in the middle on FF and Chrome? http://jsfiddle.net/PXN2e/2/ input.form-text { color: #999999; font-size: 14px; height: 30px; line-height: 30px; paddin ...

Tips for accessing jQuery UI tab elements and adjusting visibility for specific panels

How can I retrieve the panel numbers of jQuery UI tabs and adjust their visibility using CSS? I am looking to identify the panel numbers of each tab and control the visibility of certain tabs. <div id="tabs"> <ul> <li><a href="#"> ...

Trouble with table class functionality persists following insertion of SQL database

After successfully setting up the table layout and connecting it to a database, I encountered an issue with the class not working as expected. Despite being able to retrieve records and display them in the table, the desired layout was missing. <bo ...

Managing and keeping track of a universal value within the success callback function among numerous Ajax POST requests

I am faced with a challenge involving two HTML elements: 1). a div element that functions as a multiple select, created automatically by a predefined widget, and 2). a button that triggers an onclick event. On the JavaScript side, I have a global variable ...

Retrieve the child element index of a specific element using jQuery

I am dealing with a group of 'DIV' elements, each containing a list of 'p' elements. Take a look at the example below: <div class="container"> <p>This is content 1</p> <p>This is content 2</p> ...

What is the best way to center these icons vertically in my navigation menu using CSS?

My website has a navigation menu with third-party icon libraries (Material/FontAwesome) in use. I am facing an issue where the icons are not aligning vertically with the anchor text. Adjusting the font size of the icon to match the text results in it appe ...

Incorporating a Bootstrap form within a form group

Can a select element be inserted inside a horizontal form in Bootstrap? I am trying to add a form with a select element inside a form group. Check out the code on Bootply. Note: Copy the code below as Bootply sometimes removes certain form tags. The is ...

Using jQuery to extract the value of an object from an <li> element and updating the class attribute of the <li> element

There is a div element: <div id="ddDistr" class="searchBoxSortDistrict" tabindex="1"> <ul id="ddd"> </ul> </div> I have inserted HTML from json into it: $("#ddd").html(" "), $.each(dis, function( index, value) { $("#dd ...

Responsive Material-UI horizontal navigation bar

Hey there! I'm working on developing a UI using Material-UI, and I want to make it so that the menu can be collapsed into a slide-in style menu when accessed on a mobile device. Currently, I'm using tabs which allow for sliding left and right, b ...

jQuery SlideUp and SlideDown causing Margin Bug in Internet Explorer 7

When clicking the "more info" or "less info" buttons to slide content up or down, a spacing glitch is created in IE7. Using show/hide instead of slideUp/slideDown seems to solve the issue. Is there a way to make sliding work in IE7 without causing this pro ...

CSS: Is it possible to make a div even smaller than the invisible padding of a font?

link to code: http://jsfiddle.net/xVCrn/1/ (best viewed in chrome / webkit) I'm attempting to create a 1px margin for the red section within the dark button area, but I am struggling to adjust the height of the red part. =( The objective is to: ...

Any tips on how to personalize the loading animation for single pages using CSS and GIFs?

presentLoading(message) { this.loading = this.loadingCtrl.create({ dismissOnPageChange: false, spinner:'hide', content:`<img class="load" src="assets/dual.gif" />`, }); this.loading.present(); } Hello there! I am working with the ...