简体   繁体   中英

Bootstrap columns in flexbox not wrapping on smaller screens

I have a section of my website that I am using the CSS below on 2 divs, and one a tag in order to have the content vertically aligned in the center.

The problem is that with the flex style properties, when the window is < 768px ideally the content would change layout and each col-md-4 would stack on top of one another.

This is not happening, instead the columns just become really skinny and are displayed still side by side. Is there any way to fix this? Preferably trying to stay away from table-cell formatting

.about-us-nav {
    display: flex;
    align-items: center;
    justify-content: center;
}

.about-us-nav a {
    font-size: 20px;
    color: #52361D;
    background-color: #885A31;
    border-color: #52361D;
    width: 200px;
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.how-help-container {
    margin-top: -25px;
    display: flex;
    align-items: center;
    justify-content: center;
    position:absolute;
    z-index: 2;
}

There are two things you should consider:

  1. When you apply display: flex to an element it becomes a flex container which comes with several default styles.

    One of the defaults is flex-direction: row , which aligns flex items (child elements) along the horizontal axis. To switch to a vertical direction you need to specify flex-direction: column .

    Another default is flex-wrap: nowrap , which forces flex items to remain on a single line (even if they overflow the container).

    From your question:

    The problem is that with the flex style properties, when the window is <768px ideally the content would change layout and each col-md-4 would stack on top of one another. This is not happening, instead the columns just become really skinny and are displayed still side by side.

    It sounds like the flex items are not wrapping. Try adding this to the flex container:

     flex-wrap: wrap; 
  2. If you want the flex items to stack vertically when the window is < 768px, use a media query with the flex-direction property.

     @media screen and (max-width: 768px) { .your-selector-here {flex-direction: column;} } 

Note on browser support:

Flexbox is supported by all major browsers, except IE < 10 . Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes . For a quick way to add prefixes use Autoprefixer . More details in this answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM