Ways to reset alignment in a media query

I have organized two divs around another div with the ID of "about." When viewing in a full-size browser, the content appears centered both horizontally and vertically, which is my desired layout. However, when the window size is reduced, I would like these two divs to stack on top of each other rather than being displayed side by side.

In my attempt to achieve this, I aim to reset the properties of the wrapper element (about). Here is my CSS:

#icon{
    border: solid;
    border-color: red;
    border-width: 2px;

    max-width: 200px;
}
#about{
    min-height: 100%;
    min-height: 100vh;  

    <!-- The following two lines horizontally and vertically center the about section -->
    <!-- If removed, the content stacks responsively as intended,
         but loses the vertical alignment and shifts to the top of the page -->
    display: flex;
    align-items: center;
}
#aboutInfo{
    border: solid;
    border-color: red;
    border-width: 2px;  
    padding: 0;
}
@media (max-width: 768px){      
    #icon {
        max-width: 100px;
    }
    <!-- RESET ATTEMPT -->
    #about {
        float: none;
        width: 100%;
    }       
}

How can I adjust the centering behavior so that the elements stack up when the window is resized, while still maintaining the centered layout on larger screens? Below is the corresponding HTML structure:

<div class="container text-center">
    <div class="row">
        <div id="about">

        <div class="col-sm-4 padding-0">
            <img src="img/html5.png" id="icon">
        </div>


        <div class="col-sm-6 padding-0">
            <div id="aboutInfo">
                <h1>Lorem Ipsum.</h1>
                <p>
                  Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br> 
                  Suspendisse malesuada lacus commodo enim varius, <br> non gravida ipsum faucibus. 
                  Vivamus pretium pulvinar <br> elementum. In vehicula ut elit vitae dapibus. 
                  Cras ipsum <br> neque, finibus id mattis vehicula, rhoncus in mauris. 
                  In <br> hendrerit vitae velit vel consequat. Duis eleifend dui vel <br> tempor 
                  maximus. Aliquam rutrum id dolor vel ullamcorper. <br> Nunc cursus sapien 
                  a ex porta dictum.
                </p>
            </div>
        </div>

        </div> < !-- end of about -->
    </div> <-- end of row -->
</div>

Answer №1

If you want to achieve this effect, follow these steps:


body {margin: 0}

#icon {
  border: 2px solid red;
  max-width: 200px;
}

#about {
  min-height: 100%;
  min-height: 100vh;  
  display: flex;
  align-items: center;
  justify-content: center; /* added */
}

#aboutInfo {
  border: 2px solid red; 
}

@media (max-width: 768px) {      
  #icon {max-width: 100px}
  #about {flex-direction: column} /* added */
}

<div class="container text-center">
  <div class="row">
    <div id="about">
      <div class="col-sm-4 padding-0">
        <img id="icon" src="https://placehold.it/200x150" alt="">
      </div>
      <div class="col-sm-6 padding-0">
        <div id="aboutInfo">
          <h1>Lorem Ipsum.</h1>
          <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br> 
            Suspendisse malesuada lacus commodo enim varius, <br> non gravida ipsum faucibus. 
            Vivamus pretium pulvinar <br> elementum. In vehicula ut elit vitae dapibus. 
            Cras ipsum <br> neque, finibus id mattis vehicula, rhoncus in mauris. 
            In <br> hendrerit vitae velit vel consequat. Duis eleifend dui vel <br> tempor 
            maximus. Aliquam rutrum id dolor vel ullamcorper. <br> Nunc cursus sapien 
            a ex porta dictum.
          </p>
        </div>
      </div>
    </div> <!-- end of about -->
  </div> <!-- end of row -->
</div>

By default, the parent flex-container stacks children row-wise. However, you can change this by setting the flex-direction property to column.

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

Retrieve image from database and add it to the bxslider carousel

Currently, I am utilizing jquery.bxslider.js to create a slideshow of my images. Within my HTML code, the structure looks like this: <ul class="slide"> <li> <a href="#"> <img src="../img/pic1.png" id="1" /> </a> ...

Utilize the Power of JQuery Range Slider to Dynamically Update CSS Property Values (specifically column / fr and column-gap)

Currently, I am in the process of creating a JQuery range slider that can dynamically update the columns and column-gaps of my photo grid. While searching for a solution, I came across a relevant post where someone had shared their code. However, there see ...

Ensure that the CSS data-step attribute is replaced with the use of a span element

Hi there, I'm currently working with a step bar template that you can find at the following link: http://codepen.io/mattdrose/pen/qEZBge My issue arises when dealing with the complete step, which is represented by the following code: <li class="i ...

Tips for showing text containing angle brackets within the innerText property of a richtextbox

As I parse through a text file, I come across some text enclosed in angle brackets, for example "<sometext>". To exhibit this text, I am utilizing the richtextbox (jquery.cleditor.js). tbxImgSpellCh.InnerText = text; Unfortunately, when displayed i ...

Why isn't the dropdown menu displaying beneath the background in bootstrap / HTML / CSS?

Having trouble creating a navbar with a dropdown menu on the right? I encountered an issue where the menu doesn't show up when clicked. However, after removing all CSS styles, the button starts working. .info { margin-top: 15%; display: table; ...

CSS fails to style links

Apologies in advance for the seemingly trivial and highly specific question, but I'm really struggling with this one. I'm attempting to style two <li> elements that are nested within a <nav><ul><li><a href="" ...

The Bootstrap4 navbar menu malfunctions when adjusting the browser size

Having an issue with the Bootstrap4 navbar from CDN. When I adjust the browser resolution, the navbar menu options do not display properly. I have checked my code but can't seem to find the error. Any suggestions would be greatly appreciated. Here is ...

The form transmits the URL parameter rather than executing an update

I'm having trouble with my code and can't seem to figure out why it's behaving this way. When I try to update a recordset, the page reloads upon hitting the submit button and multiple URL parameters appear in the address bar. There are two ...

The CSS style tag does not function in Safari

I am trying to dynamically load background images using CSS based on the display resolution using the @media keyword. This method works perfectly in Firefox and Chrome, but unfortunately, it does not seem to work in Safari. Is there something I might be ...

Modify iframe form action URL dynamically upon user visiting the URL with a specified parameter

Below you will find a code example: The page URL is: <html> <body> <h1>main page</h1> <iframe src="http://example2.com"> <form id="test" action="http://example3.com?id=1"> ... </form&g ...

How can I add a left margin of 5 pixels in Bootstrap?

I have 2 buttons and I want to add some space between them. In CSS, I can use margin-left: 5px However, I can't find the exact class in Bootstrap for this. div class="d-flex"> <button (click)="showPreviousYear()&quo ...

What could be causing my Wordpress page to be cropped at the bottom in Internet Explorer, yet not in Firefox?

Currently, I have a WordPress theme installed on www.williamcoit.com. On the Services page, all content is displayed correctly in Firefox, but it gets cut off at Local Services when viewed in Internet Explorer. What steps can I take to resolve this issue? ...

Creating an art exhibit in an illuminated amp lightbox using Django

Currently, I am immersed in a project that involves Django on the back-end and amp on the front-end. However, I am facing some challenges in connecting both tags, particularly when it comes to implementing a lightbox feature. What I aim to achieve is a li ...

Issue when attempting to animate an SVG point using translateX transformation

I am attempting to create a basic animation using the translate X property on a section of my svg when hovering over the element. Here is the code I have so far: <html> <style> .big-dot:hover { transform: translateX(20px); animat ...

What is the best way to align divs side by side in HTML?

I am faced with a challenge in displaying the zoomed-in section of an image on the right side instead of below it. The image is contained within a div and I have attempted using align=right without success since it's not actually an image but a contai ...

The CSS Grid container fails to resize based on the content inside

I am facing an issue with the grid element inside a parent container with display: grid;. The parent container has a maximum size and allows scrolling for the grid if necessary. The problem is that the grid element does not expand to fit its children. Whe ...

Tips for preventing automatic sliding in a bootstrap 4 carousel on mobile with JavaScript or other methods

Can someone assist me in stopping my carousel from auto-sliding on mobile devices only? I have attempted solutions from similar queries on this platform, but they have not worked for me. Any suggestions would be greatly appreciated. Below is an example of ...

Issues with both horizontal and vertical bootstrap navigation bars functionality

My attempt to incorporate both a vertical and horizontal Bootstrap menu on my webpage led to the layout being disrupted. Is there a solution for this issue? Below is the code for my horizontal menu: <html lang="en"><head> <meta charset ...

Spin picture and optimize margins

I need help rotating an image by 90 degrees that is positioned between two boxes. The issue I am facing is that the rotated picture overlaps with the two boxes in this scenario. Example 1: Incorrect formatting CSS: .box{ height:50px; width:200px ...

What is the best way to remove <a> tags from an XML document?

I'm dealing with <a> tags within an XML file and I need to remove only the <a> tags. For instance: <test>This is a line with <a name="idp173968"></a> tags in it</test> I am unable to use str_replace to replace t ...