Challenges arise when utilizing the foundation 6 grid for image overlays within a React application

Having some trouble using the foundation 6 xy grid for the first time and aligning three images with text overlays at the bottom of each image. I am struggling to make the background of the text fill the full width of the image while also ensuring it is responsive. The 100% width of the newsArticle__pCont class seems to be greater than the parent cell, which is confusing me. Below is my current attempt:

class News extends Component {

    renderArticlePreview(article) {
        if(articleCount <= 2) {
            articleCount++;
            return (
                <div key={article.id} className="cell small-12 medium-4 newsArticle__cont--firstThree">
                    <img className="newsArticle__img--overlay" src={article.imageUrl} />
                    <div className="newsArticle__pCont">
                        <a href={"/article/" + article.id}><p className="newsArticle__title--overlay">{article.title}</p></a>
                    </div>
                </div>
            )
        }
    }

    render() {
        const { news } = this.props;

        return (
            <div>
                <Header />
                <div className="grid-container">
                    <div className="grid-x grid-padding-x newsArticle">
                        { news.map((article) => this.renderArticlePreview(article)) }
                    </div>
                </div>
            </div>
        )
    }
}

export default News;

.scss

@import './Helpers.scss';

.newsArticle {

    .newsArticle__cont--firstThree {
        position: relative;
    }

    .newsArticle__title {
        text-align: center;
        font-size: 24px;
        font-weight: 500;
        color: #000000;
    }

    .newsArticle__title--overlay {
        font-size: 16px;
        font-weight: 500;
        color: #FFFFFF;
        text-align: center;
        margin-bottom: 0px;
    }

    .newsArticle__pCont {
        background-color: rgba(0, 94, 154, 0.75);
        height: 45px;
        position: absolute;
        bottom: 0px;
        z-index: 10;
        width: 100%;
    }

    @include screen(sm-only) {
        .newsArticle__img {
            margin-top: 10px;
        }
    }

    @include screen(md) {
        .newsArticle__img {
            margin: 10px;
        }

        .newsArticle__p {
            margin: 10px;
        }
    }
}

Answer №1

The main reason why your text appears too large at the moment is due to a few factors:

  1. It is set to position absolute, which removes it from the normal flow and ignores the padding of its container.
  2. Its width is set to 100%, causing it to expand to the full width of its parent element, including any padding.

To resolve this issue, a simple solution would be to switch from using padding-x (where gutters are inside the cell) to margin-x (where gutters are created with margins, excluding them from the cell width). This adjustment would update your grid layout to appear like this:

<div className="grid-x grid-margin-x newsArticle">

You can see this change in action on Codepen: https://codepen.io/kball/pen/zEGoaR

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

Designing a form within a div container while ensuring that the content remains contained

I've been struggling to perfect this formatting for quite some time now. While I've come across many similar examples to what I'm aiming for, I just can't seem to get it right. My goal is to create a form that will "popup" using JS for ...

Is there a way to adjust the text color of a label for a disabled input HTML element?

If the custom-switch is active: the label text color will be green. If the custom-switch is inactive: the label text color will be red. A JavaScript (JQuery) method call can be used to activate one button while deactivating another. The Issue It appe ...

Problems Arising from the Combination of Django and External JavaScript

I am currently working on a project that uses Django 1.11 and JavaScript, but I am encountering an issue. The JavaScript code works fine when run within the HTML file, as it successfully loads the JSON data. However, when I move everything to the static fo ...

Maximizing the power of Webpack alongside Google Maps API

I have been using Webpack along with the html-webpack-plugin to compile all my static files. However, I am facing an issue when integrating it with the Google Maps API. Here is the code snippet: var map; function initMap() { map = new google.maps.Map(d ...

Distinct styling for the start and subsequent lines of an anchor element using ::before pseudo-element

I'm currently working on a project that requires adding a decoration in front of some anchor links (A). Right now, I am using ::before to achieve this. However, there is an issue where some of the links will line-break and the second line and subseque ...

Finding the median value within a collection of objects

I am working with an array of objects containing book details: const booksData = [{"author":"john","readingTime":12123}, {"author":"romero","readingTime":908}, ...

Issue with jQuery animation delay functionality experiencing issues specifically in Google Chrome browser

While browsing through similar questions, I couldn't find one quite like mine where the effect was working on one window but not on a subsequent one. I'm using jQuery library version 3.4.1. The code has been tested and is functioning as expected ...

Is there a single code that can transform all standard forms into AJAX forms effortlessly?

When manipulating my 'login' form, I love using this code to submit it to the specified url and display the response in the designated id. The method of submission is defined as well. It's a great solution, but is there a way to streamline ...

Tips for maintaining the state of a component that is only hidden

After delving into a previous query, I discovered that React automatically retains the state for child components. This explains why the documentation emphasizes: What Shouldn't Go in State? React components: Develop them within render() based on un ...

Why is my React component not being updated with Routes?

I'm new to using react-router and I'm struggling with it for the first time. Here is the code snippet: App.tsx import React from 'react'; logo = require('./logo.svg'); const { BrowserRouter as Router, Link, Route } = require ...

``Incorporating project environment variables into a React component for an imported module

In my Create React Project, I utilize local environment variables for configuration. One of my components relies on these env variables to set itself up, importing them using process.env.MY_VAR; I'm interested in moving this component into an npm pa ...

SheetJS is experiencing difficulties parsing dates correctly

Need help exporting an HTML table to an Excel file using the SheetJS library. Here's my code snippet: var table = document.getElementById("tableToExport"); var ws = XLSX.utils.table_to_sheet(table, { sheet: "Raport Odorizare", da ...

Tips for adapting the position of a floating div according to its height and environment

Important Note: The code below utilizes the rubuxa plugin for handling JS sortables. Javascript: function querySelector(expr){return document.querySelector(expr)} var container = querySelector('.ITEST'); var sortable = Sortable.create(container, ...

Ensuring optimized imports with Eslint: Avoid importing from the root directory and prioritize tree-shaking techniques

I am looking to prevent direct imports from the root of a specific dependency (@material-ui/icons) in my application and ensure effective tree-shaking. Allowed - import AccessAlarmIcon from "@material-ui/icons/AccessAlarm"; Not Allowed - imp ...

Every time I attempt to create a fresh project using gatsby-cli, I encounter an error

❗ Command failed: C:\projects\my-blazing-fast-site\node_modules\pngquant-bin\vendor\pngquant.exe --version ❗ pngquant pre-build test failed ℹ compiling from source ✖ Error: pngquant failed to build, make sure that lib ...

Transforming Input Background Color with jQuery based on Matching PHP Array Entry

I am looking for a solution to implement form validation in PHP. The form has 3 fields: user, id, and email. When a user enters their name or id in the respective input fields, I want to check if the entered value matches any of the values in the PHP arra ...

Discovering the power of Next.js Dynamic Import for handling multiple exportsI hope this

When it comes to dynamic imports, Next.js suggests using the following syntax: const DynamicComponent = dynamic(() => import('../components/hello')) However, I prefer to import all exports from a file like this: import * as SectionComponents ...

Learn how to eliminate all text characters from a string using jQuery, leaving only the numerical values behind

My webpage features a variety of different products, each with their own values. When trying to calculate the total sum of these products and display it in the shopping cart, I encountered an error displaying NaN. How can I remove this NaN from the strin ...

Building a TTL based schema in NestJs with MongooseIn this guide, we will explore

In my NestJs(TypeScript) project, I am attempting to create a self-destructing schema using the mangoose and @nestjs/mongoose libraries. Unfortunately, I have been unable to find a clear way to implement this feature. While I know how to do it in an expres ...

Maximizing jQuery DataTables performance with single column filtering options and a vast amount of data rows

My current project involves a table with unique column select drop-downs provided by an amazing jQuery plug-in. The performance is excellent with about 1000 rows. However, the client just informed me that the data could increase to 40000 rows within a mont ...