Mastering CSS: Optimizing Div Placement Across Sections

I am currently working on developing a sleek and modern landing page with multiple sections, each designed to catch the eye. This style is all the rage at the moment, featuring large headers, ample padding, bold text, and a visually appealing divider between each section.

My main challenge lies in figuring out how to position the dynamic cards effectively. These cards adjust in size based on factors like viewport dimensions and content length.

[![enter image description here][1]][1]

Thus far, I've managed to achieve a similar effect by estimating the card size (around 200px) and offsetting the top of the card by -200px. While it works on my screen, I know this approach won't translate well across different devices, fonts, or zoom levels. Additionally, I'm struggling with unwanted white space beneath the cards due to the parent container div being calculated without considering this extra space.

Is there a way to ensure that the cards are consistently centered? How can I avoid excessive white space under them? Your insights would be greatly appreciated! Thanks!!

<div className="bg-tertiary text-white text-center pb-10">
  <div className="bg-secondary pb-[100px] px-10">
    <h1 className="uppercase font-black text-2xl tracking-wider pt-10 pb-10 italic">HEADER TEXT</h1>
  </div>

  <div className="relative columns-4 top-[-100px] px-10">
    <MainSectionCard />
    <MainSectionCard />
    <MainSectionCard />
    <MainSectionCard />
  </div>
</div>

edit: Credit goes to @wongjn for the helpful solution!

Answer №1

It appears that both the top and background have solid colors. To add a more stylish touch, consider using a linear gradient as the background for your cards. This gradient can neatly blend two colors together:

tailwind.config = {
  theme: {
    extend: {
      colors: {
        secondary: 'darkblue',
        tertiary: 'skyblue',
      },
    },
  },
};
<script src="https://cdn.tailwindcss.com/3.4.1"></script>

<div class="bg-tertiary text-white text-center pb-10">
  <div class="bg-secondary px-10">
    <h1 class="uppercase font-black text-2xl tracking-wider pt-10 pb-10 italic">HEADER TEXT</h1>
  </div>

  <div class="relative columns-4 px-10 bg-gradient-to-b from-secondary from-50% to-tertiary to-50%">
    <MainSectionCard class="block h-20 bg-white">MainSectionCard</MainSectionCard>
    <MainSectionCard class="block h-20 bg-white">MainSectionCard</MainSectionCard>
    <MainSectionCard class="block h-20 bg-white">MainSectionCard</MainSectionCard>
    <MainSectionCard class="block h-20 bg-white">MainSectionCard</MainSectionCard>
  </div>
</div>

Answer №2

When addressing the second question, my suggestion is to utilize the margin property with a negative value.

Here is an example of how the HTML code would appear:

<Hero />
<Cards class="-mt-[100px]" />

You have the flexibility to adjust the margin value to suit your needs, and any elements preceding the cards will remain unaffected.

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

When the React component's state is updated, an animation is triggered in a subcomponent connected to the parent component

The ProjectsSection component is responsible for rendering a document section with a header and cards. The goal is to trigger a header animation only when the header becomes visible on the screen for the first time and have it run once. However, adding a s ...

Tips for transferring i18n or t function to a dependency in next-i18next

I am faced with a situation where I have two repositories. One repository contains a nextjs application, while the second one houses components that are dynamically loaded into the first repo. Among these components in the second repository are some that ...

Header and Footer Components in ReactJS

My goal is to design a unique Layout component that will display the Header and Footer. This way, I can later use the Layout like <Layout> ... </Layout> In order to achieve this, I have utilized Routing in both the Header and Footer. To imple ...

I am consistently encountering the npm ERR! code 1 error when trying to run the react-scripts build command in my code

I have successfully developed a create-react-app application on my Windows 10 workstation using node v12.19.0 and npm v6.14.8. I have been able to run npm start and npm run build without any issues and access the app smoothly. However, when attempting to ...

Encountering a problem with ApexCharts in Next.js 14 - Uncaught ReferenceError: window is undefined

I am facing a challenge with implementing ApexCharts in my Next.js 14 project. Upon running the project, I encountered an error message in the terminal: view error image here The issue seems to be associated with the use of the window object. Has anyone ...

Aligning a Facebook share button to the center of a webpage

On my webpage, I have the following section: <div style="align:center"> <div id="fb-root"></div> <script src="http://connect.facebook.net/en_US/all.js#appId=217585988283772&amp;xfbml=1"></script> <fb:like hre ...

I possess a button that decreases the value of a number in the database when pressed. I desire for this action to occur repeatedly without fail

I have a button that, when pressed, decreases a number in my database by one. I want this action to decrement the number each time it is clicked. Code in HTML and PHP <form method="post"> <div><?php $username = "root" ...

Javascript auto submission fails to execute following the completion of the printer script

As someone new to javascript, I have a question. I have a function called sendToQuickPrinter() that prints correctly, but after it finishes executing, I need to automatically submit a form to return to my "cart.php" page. I feel like I'm almost there, ...

Tips for including an ID as the final segment of a URL in next.js

I am attempting to have the ID as the last part of the URL, but I am unsure about the syntax. Can someone assist me with this code in Next.js? <Link href="query/{itm._id}"> {Items.map((itm) => ( <Link href="query/{itm. ...

Exclude certain packages from being processed in webpack

After setting up my webpack configuration, everything was running smoothly with the specified packages in the package.json: { "peerDependencies": { "react": "16.13.1", "react-dom": "16.13.1", ...

implementing datepicker on multiple textboxes in jQuery upon button click

I have the ability to show multiple text boxes using jQuery. I am now looking to add a datepicker to those text boxes. Any assistance in finding a solution would be greatly appreciated. Thank you in advance. ...

Try making a series of interconnected fetch requests in Redux Toolkit that rely on the completion of the previous fetch

I am still learning the ropes of Redux and I'm feeling a bit lost. My goal is to make two API calls - one to retrieve an account Id and a category Id, and another to get a list of transactions based on those IDs. The createApi function in my code lo ...

The dropdown menu in the bootstrap is not being properly filled with content on the webpage

In my header file, I am attempting to add a bootstrap dropdown that will display the user id along with two other options in the dropdown menu. Within the header file, I have created a new div element for the dropdown and linked it to a php file where the ...

Understanding JavaScript Regular Expressions

To ensure that no HTML tags are entered into a textarea, I am utilizing the following regex for validation. If any HTML tags are detected in the textarea, I need to display a validation message. The regex being used is: /<(\w+)((?:\s+\w ...

can you explain the concept of a backing instance in react?

Although the concept of a "backing instance" is frequently mentioned in React documentation, I found it difficult to grasp its meaning. According to the React docs: In order to interact with the browser, you need a reference to a DOM node. By attaching ...

Utilizing the combineReducers() function yields disparate runtime outcomes compared to using a single reducer

Trying to set up a basic store using a root reducer and initial state. The root reducer is as follows: import Entity from "../api/Entity"; import { UPDATE_GROUPING } from "../constants/action-types"; import IAction from "../interfaces/IAction"; import IS ...

Placing text and an image within a grid cell does not automatically stack them on top of each other

I have a large div with a grid display, consisting of 3 rows and 2 columns. I am attempting to include text directly below the photo within each cell. This is what my code looks like: <div class="arrange-fixes"> ...

Modifying the value of an animated status bar using the same class but different section

I need the status bars to work individually for each one. It would be great if the buttons also worked accordingly. I have been trying to access the value of "data-bar" without success (the script is able to process the "data-max"). However, the script see ...

Newbie React Developer Struggling to Implement Material-UI Example in React Project, State Functioning Differently from Hooks

I am currently in the early stages of learning React and trying to create a form for a project management web application. For this, I am using material-ui library. I referred to one of the select box component examples from the material-ui documentation ...

Issue: Unable to assign type 'FormDataEntryValue' to type 'string'. Type 'File' cannot be assigned to type 'string'

After retrieving data from the formData, I need to pass it to a function for sending an email. Error: The error message states that 'FormDataEntryValue' is not compatible with type 'string | null'.ts(2322) definitions.ts(119, 3): The e ...