What is the best way to make images wrap in flexbox in a 2x2 grid and ensure the hover effect functions correctly?

I have a layout with two flexboxes - one for text and the other for category links. I'm struggling to align the category pictures in a 2 by 2 grid format. I've tried using row wrap and centering the contents, but it's not working as intended. I also attempted various methods with containers, but it always ends up as a 1 x 4 grid instead of 2 x 2. Additionally, I'm having trouble getting the hover effect to perfectly fit the image, even though the height and width are set to 100%. How can I resolve these issues?

.flex-container {
    display: flex;
    justify-content: center;
    flex-direction: row;
    width: 800px;
}

.flex-container > div {
    margin: 10px;
    padding: 20px;
    font-size: 20px;
    text-align: center;
    flex-flow: row wrap;
}

.one {
    flex: 1 1 auto;
}

.two {
    flex: 1 1 auto;
}

div.two a > img {
    padding: 10px;
    margin: 10px;
}

a {
    text-decoration: none;
}

h1 {
    display: block;
    font-size: 1.3em;
    margin: 0.67em 0;
    font-weight: bold;
}

h2 {
    display: block;
    font-size: 0.8em;
    margin: 0.83em 0;
    font-weight: bold;
}

.container {
    position: relative;
    width: 50%;
}

.image {
    display: block;
    width: 200%;
    height: auto;
}

.overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100%;
    width: 100%;
    opacity: 0;
    transition: .5s ease;
    background-color: darkorchid;
}

.container:hover .overlay {
    opacity: 1;
}

.text {
    color: Black;
    font-size: 18px;
    position: absolute;
    top: 30%;
    left: 100%;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    text-align: center;
}
<section>
    <div class="flex-container">
        <div class="one"> <h1> Welcome to Delicious Book store, <br>
            where you can find so many delicious food!</h1>
        <p> As a specialized book store, we have many cooking books on
            holiday specials, vegetarian, desserts and cultural cuisines!</p>
        </div>

        <div class="two"> <h2> Shop By Category </h2>

            <div class="container">
                <a href="category.html">
                    <img src="images/categories/holiday.jpg" alt="holiday image" class="image">
                    <div class="overlay">
                        <div class="text">Holiday</div>
                    </div></a>
            </div>

            <div class="container">
                <a href="category.html">
                    <img src="images/categories/holiday.jpg" alt="holiday image" class="image">
                    <div class="overlay">
                        <div class="text">Dessert</div>
                    </div></a>
            </div>
            
            <div class="container">
                <a href="category.html">
                    <img src="images/categories/holiday.jpg" alt="holiday image" class="image">
                    <div class="overlay">
                        <div class="text">Vegetarian</div>
                    </div></a>
            </div>
            
            <div class="container">
                <a href="category.html">
                    <img src="images/categories/holiday.jpg" alt="holiday image" class="image">
                    <div class="overlay">
                        <div class="text">Cultural Cuisine</div>
                    </div></a>
            </div>

        </div>

    </div>

</section>

Answer №1

I have utilized a flex container to arrange your boxes, adjusting the CSS for both the width of the boxes and their content. Additionally, I included width: 100% in your .flex-container.

.flex-container {
    display: flex;
    justify-content: center;
    flex-direction: row;
    width: 100%;
}

.flex-container > div {
    margin: 10px;
    padding: 20px;
    font-size: 20px;
    text-align: center;
    flex-flow: row wrap;
    width: 500%;
}

.one {
    flex: 1 1 auto;
}

.two {
    flex: 1 1 auto;
}


a {
    text-decoration: none;
}

h1 {
    display: block;
    font-size: 1.3em;
    margin: 0.67em 0;
    font-weight: bold;
}

h2 {
    display: block;
    font-size: 0.8em;
    margin: 0.83em 0;
    font-weight: bold;
}

.container {
    position: relative;
    width: calc(50% - 20px);
    margin: 10px;
}

.image {
    display: block;
    width: 100%;
    height: 100%;
    min-height: 75px;
}

.overlay {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    height: 100%;
    width: 100%;
    opacity: 0;
    transition: .5s ease;
    background-color: darkorchid;
    min-height: 75px;
}

.container:hover .overlay {
    opacity: 1;
}

.text {
    color: Black;
    font-size: 18px;
    position: relative;
    text-align: center;
    padding: 10px;
}

.images-container{
    display: flex;
    flex-wrap: wrap;
}
<section>
    <div class="flex-container">
        <div class="one"> <h1> Welcome to Delicious Book store, <br>
            where you can find so many delicious food!</h1>
        <p> As a specialized book store, we have many cooking books on
            holiday specials, vegetarian, desserts and cultural cuisines!</p>
        </div>

        <div class="two"> <h2> Shop By Category </h2>
          <div class="images-container">
            <div class="container">
                <a href="category.html">
                    <img src="https://via.placeholder.com/150" alt="holiday image" class="image">
                    <div class="overlay">
                        <div class="text">Holiday</div>
                    </div></a>
            </div>

            <div class="container">
                <a href="category.html">
                    <img src="https://via.placeholder.com/150" alt="holiday image" class="image">
                    <div class="overlay">
                        <div class="text">Dessert</div>
                    </div></a>
            </div>
            <div class="container">
                <a href="category.html">
                    <img src="https://via.placeholder.com/150" alt="holiday image" class="image">
                    <div class="overlay">
                        <div class="text">Vegetarian</div>
                    </div></a>
            </div>
            <div class="container">
                <a href="category.html">
                    <img src="https://via.placeholder.com/150" alt="holiday image" class="image">
                    <div class="overlay">
                        <div class="text">Cultural Cuisine</div>
                    </div></a>
            </div>
           </div>
        </div>

    </div>

</section>

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

Craft an Interactive Content Carousel

I have been searching for a responsive slide solution for two days with no luck, so I am reaching out for help. I am looking for a simple jQuery code for a slideshow that is lightweight and doesn't require a plugin. Here is the code for the slideshow: ...

I’m looking to incorporate my new CSS style file into the theme on OpenCart. How can I go about doing that?

When it comes to my homepage, I'm looking to have a unique stylesheet that differs from the default one. I want one that can override the default styles with my custom sheet. Despite trying numerous solutions, I haven't been able to achieve the ...

Is there a way to trim the backdrop of the <body> element to fit its content box perfectly?

Exploring a particular style rule: body { font-family: Georgia, serif; font-size: 100%; line-height: 175%; margin: 0 15% 0; background-color: #d2dc9d; background-clip: content-box; } During testing of a page utilizing this rule, the backgroun ...

overrule styling in react native

In React Native, I am looking to modify a specific style within a sub-component. For instance, I have defined a style called CircleShapeView: const styles = StyleSheet.create({ CircleShapeView: { width: 50, height: 50, borde ...

What is the best way to organize blog posts by using hashtags?

I am looking to implement a filtering system for blog posts using hashtags. This feature should display only the blog posts with matching hashtags and move them to the top of the page. Additionally, there should be a button at the top to show all posts. ...

Ways to position a dropdown submenu on the left-hand side in Bootstrap 4

I'm currently utilizing Bootstrap 4 with a multilevel dropdown feature as shown in the image below: https://i.sstatic.net/xgVLp.png My goal is to shift the submenus to the left side, in the opposite direction from how they are currently displayed. De ...

Populate any void area when <td> is not occupied in <tr>

I have a table with two rows and columns. If one of the columns is empty, it should move up to the upper row. For example: |td1 | td2| |td3 | | |td5 | td6| This is what I want: |td1 | td2| |td3 | td5| |td6 | | Below is the code snippet that ...

Using jQuery to remove all inline styles except for a specific one

Just a quick inquiry: Our Content Management System (CMS) utilizes CKEditor for clients to modify their websites. The front-end styles include the use of a pre tag, which we have customized to our desired appearance. However, when their team members copy a ...

The Move-class only applies to items within a transition-group that have not been removed if other items were removed

My item list is displayed in a flex-box, forming a matrix with several rows and items per row. I use the <transition-group class="move"> to apply a simple move transition to the <div v-for="item in items" :key="item.id"> move { transition: t ...

Altering button appearance when mouse hovers in ASP.NET

Is there a way to modify the shape and style of a button in ASP.NET when hovering with the mouse? Specifically, I am looking to create a circular button effect during hover using ASP.NET. ...

Tips for identifying when a View has completed rendering?

I am facing a situation where I have multiple GWT widgets displayed within a single view. Within this view, the content is structured as follows: FlowPanel mainPanel = new FlowPanel(); RootPanel.get().add(mainPanel); Label label = new Label("test"); mai ...

Moving from one page to another

I am attempting to create a transition effect between sections within a single-page application. All the sections are contained on the same page, with only one section displayed at a time while the rest are set to display none. When a specific event is tri ...

Reveal unseen information on the webpage upon clicking a link

I have successfully implemented a fixed header and footer on my webpage. The goal is to make it so that when a user clicks on a link in either the header or footer, the content of that page should appear dynamically. While I have explored various styles, ...

Creating a FadeIn animation for a hidden div element by using the display:none property

Struggling to implement a fadeIn function for a div tag with a display:none property. How can I make the div tag visible and smoothly fade in while still keeping the display:none property? Attempted solution so far: <div class="graphs_line_based cle ...

Steps for using selenium to select a radio button

Looking to leverage Selenium in Python 3.9 for button clicking automation. Link to the website https://i.stack.imgur.com/KWsKG.png The goal is to click on "Effective Date", then select "July 2021", and finally click "OK". Initially, the first two steps ...

Troubleshooting form validation errors within HTML

Hey folks, I'm facing an issue with my HTML form validation setup. Currently, I have a combination of JSP on the backend to handle form data extraction and jQuery for validating the data within my HTML file. The problem arises when I try running my HT ...

Styling for 'checked' state within an ngFor iteration

Each checkbox in my list is assigned to a different department, with each department having its own color. When a box is unchecked, only the border is in the color of the department. When checked, the background changes to the assigned color https://i.ssta ...

Trouble with :active CSS selector not functioning when clicking on child element in IE8

I am dealing with the following HTML structure: <div id="ctr__Wrapper" class="wrapper"> <div id="ctr" class="control clickable"> <img src="logo.png"> </div> </div> Here is the CSS code for this setup: .contr ...

<ul> hover effect and text </ul>

Looking for a solution to activate hover state on both the box and text when rolling over tiled images with text underneath. Check out this example in the Fiddle: http://jsfiddle.net/techydude/GF8tS/ Any suggestions on how I can achieve this effect? ...

Step by step guide on loading a html project by clicking in a django project

I'm currently working on a Django project and I have two HTML files - sid.html and page2.html. The main page is sid.html. I want to load another page, page2.html, fully onto my window (not within a div) when a particular div is clicked. I've atte ...