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

The download attribute is not functioning properly on both Chrome and Edge browsers

I've been trying to use the download attribute, but it doesn't seem to be working. I have also attempted to use the target attribute to see if there are any issues with downloading from the server or from a web (https) source. Unfortunately, noth ...

Arrange the side by side display of uploaded image previews

Could someone please assist me with aligning my image upload preview in a row, similar to a brand/partners slider? The current preview output is not displaying as desired. Here is the code I have: <div class="image-gallery"> <div class ...

Having trouble getting the CSS class to work in MVC4 with jQuery implementation

I recently created a view page where I implemented some jQuery code: <script type="text/javascript"> $(document).ready(function () { var hdrname = $('#headername').text(); alert(hdrname); if (hdrname == "Pending Assignment") { ...

Tips for correctly specifying the theme as a prop in the styled() function of Material UI using TypeScript

Currently, I am utilizing Material UI along with its styled function to customize components like so: const MyThemeComponent = styled("div")(({ theme }) => ` color: ${theme.palette.primary.contrastText}; background-color: ${theme.palette.primary.mai ...

obtain an inner element within a container using the class name in a div

I am attempting to locate a span element with the class of main-tag within a nested div. However, I want to avoid using querySelector due to multiple elements in the HTML file sharing the same class and my preference against using IDs. I realize there mig ...

Button's focus event doesn't trigger on iPad

I am facing an issue with adding a bootstrap popover to my website. The popover should appear when the user clicks a button using the focus event. This functionality works fine on desktop browsers, but on the iPad, it seems like Safari on iOS does not trig ...

Use two fingers to scroll up and down on the screen

I am currently developing a sketch web application (using angular) that utilizes one finger gestures for drawing. My goal is to enable vertical scrolling in the sketch content by using two fingers. However, when attempting to scroll with two fingers, Safa ...

Adjusting the view to focus solely on the visible portion of the webpage

Is there a way to achieve zooming in and out on a website similar to how it works on the site ? I want only the visible area to zoom in or out when users interact with their browser. I searched online for a solution but couldn't find one. Any suggesti ...

Twig template delivers HTML entities

I am dealing with an issue related to the twig template. Instead of displaying HTML markup, it is showing raw HTML characters. <?php /* HomepageController.php */ namespace App\Controller; use Sensio\Bundle\FrameworkExtraBundle\Co ...

Element is missing the necessary "key" property. (React and TypeScript)

Upon running the following code for reactJS and typescript, I encountered the error below: I have also included the import statement import 'bootstrap/dist/css/bootstrap.min.css'; in Index.tsx. Is there a solution to resolve this issue? npm s ...

Issue loading the built-in Image component in Next.js version 10

I have been trying to incorporate an image into my Next.js v.10 application by utilizing the newly added Image component. Below is the code snippet: import * as React from "react"; import Image from "next/image"; export const TestImag ...

"Taking a break" and indulging in Sass

When transferring Sass classes from another project, I wanted to create a wrapper to contain these styles locally. Here is how my wrapper is currently set up: .my-wrapper { @include "framework-main" } Initially, everything seemed fine. However, I soo ...

html Comparison of Documents

My goal is to compare HTML documents to see if they have the same tags in the same arrangement, regardless of different inner text and attribute values. I am only interested in comparing the general tag structure, such as: <html> <head> </h ...

Obtain the Encoded Data

Unfortunately, I do not have control over domain.com. However, I am able to provide a redirect URL parameter like the one shown below: www.domain.com?retUrl=www.example.com%3Fparameter%3Dvalue Following the provision of retURL (www.example.com?parameter= ...

Achieving overlapping div layouts with CSS styling

Displayed above are 4 different divs: red, green, and blue. I am seeking to have the blue div positioned directly below the green div, which contains the text "JACKSON". Additionally, I do not want the green div to expand beyond its content, nor do I want ...

Improving the retrieval of API data using personalized React hooks when searching by modifying keywords

I'm just starting out with React Hooks and recently wrote a small code snippet that displays a list of courses to users. This code includes two main components, CourseList and Course, as well as a custom hook called useCourseList. Here's the code ...

Illustrating SVG links

I'm working on a basic svg animation project where I want to create a simple shape by animating a line under a menu link. The goal is to have a single line consisting of a total of 7 anchors, with the middle 3 anchors (each offset by 2) moving a few p ...

"Learn the process of connecting an HTML5 element to Angular 2 source code while in a saving mode

In this Angular2 implementation, I have created some HTML code for user access. However, I am facing an issue where the data is not binding at the Angular2 source code side. HTML Code <form class="form-horizontal" novalidate [formGroup]="EmployeeForm" ...

Exploring the depths of Javascript scope with the Polymer starter-kit 2

When working on my-app.html ... <paper-icon-button icon="my-icons:menu" onclick="this.$._joesDrawerToggle()"></paper-icon-button> ... <script> Polymer({ is: 'my-app', ... _showPage404: function() { thi ...

What is the best way to add a CSS link if a request is not made using AJAX?

My webpage, 'foo.html', retrieves data from a database using AJAX ('ajax.html?options=option1') to populate a table. The table is styled nicely with CSS that is linked to 'foo.html'. However, I also want the ajax.html page to ...