Achieving Proper Content Alignment Across Various Browser Resolutions Using CSS

I am dealing with this specific layout:

<div class="container">
  <div class="row">
    <div class="category">
      <div class="label">Label1: </div><div class="content">Content1</div>
    </div>
    <div class="category">
      <div class="label">Label2: </div><div class="content">Content2</div>
    </div>
  </div>
  <div class="row">
    <div class="category">
      <div class="label">Label3: </div><div class="content">Content3</div>
    </div>
    <div class="category">
      <div class="label">Label4: </div><div class="content">Content4</div>
    </div>
  </div>
</div>

I am aiming to ensure that each row occupies its own row if there is enough space available. However, in cases where the window size decreases or the resolution isn't ideal, I want the content to move to a new line at each category. Currently, when space is insufficient, it greatly impacts the formatting of the content. The CSS code I am using looks like this:

.container {
    width: 600px;
    background-color: black;
    color: white;
    padding: 4px;
}
.row {
    clear: both;
}
.category {
    width: 300px;
    margin-top: 2px;
    margin-bottom: 2px;
}
.left {
    float: left;
}
.right {
    float: right;
}
.label {
    float: left;
}
.content {
    display: block;
    overflow: hidden;
    color: black;
    background-color: white;
    text-align: center;
}

My current issues revolve around two main points. Firstly, the container does not expand to accommodate the vertical size of its contents. Secondly, resizing the container width causes significant distortion instead of transitioning to the next line as intended. In the actual project, the container width is set as a percentage of the body width, meaning it needs to be responsive and function properly on different screen sizes.

https://jsfiddle.net/pja7meLd/

Answer №1

To ensure the background color extends to match the height, it is necessary to clear the floats. However, a more effective approach would be to utilize flexbox for the rows and category elements. This way, you gain greater control over the layout without the need to clear floats.

.container {
  background-color: black;
  color: white;
  padding: 4px;
}

.row {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.category {
  width: 300px;
  margin-top: 2px;
  margin-bottom: 2px;
  display: flex;
}

.content {
  display: block;
  overflow: hidden;
  color: black;
  background-color: white;
  text-align: center;
  flex-grow: 1;
}
<div class="container">
  <div class="row">
    <div class="category ">
      <div class="label">Label1:&nbsp;</div>
      <div class="content">Content1</div>
    </div>
    <div class="category ">
      <div class="label">Label2:&nbsp;</div>
      <div class="content">Content2</div>
    </div>
  </div>
  <div class="row">
    <div class="category ">
      <div class="label">Label3:&nbsp;</div>
      <div class="content">Content3</div>
    </div>
    <div class="category ">
      <div class="label">Label4:&nbsp;</div>
      <div class="content">Content4</div>
    </div>
  </div>
</div>

Answer №2

After some adjustments to the container and .right classes, I was able to achieve the desired outcome:

.container {
  /* Updated with additional line */
  position: absolute;
}
.right {
  float: left;
}

https://example.com/some-link/

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

What unique capabilities does canvas offer that are not available with SVG?

Currently tackling a project that requires choosing between SVG and Canvas. After extensive research, it seems like SVG outperforms Canvas in many aspects. Is there anything that Canvas can do but SVG cannot? I'd love to hear your insights. ...

Choose the grandparent of an element's parent

Is there a way to select the grandparent element of a child in Javascript without having to chain the .parentElement method twice? Specifically, I am looking for a method that can substitute .parentElement.parentElement when selecting the desired element, ...

Determine the dimensions of child components within Svelte templates

I am currently in the process of transitioning a VanillaJS website to Svelte, and I need to have divs randomly positioned on the webpage. It's important to note that each div's size can vary depending on its content, so they cannot have a fixed s ...

Exploring the possibilities of combining CSS classes with Tailwind CSS!

Exploring the integration of Tailwind CSS classes with Next.js for a project I am working on. Is there a method to combine Tailwind CSS classes with Next.js classes? For instance: <img src={user.picture} className="rounded"{styles.pfp}>< ...

Prevent individual elements from shifting around on browser resizing within a React form

Having issues with a React form that includes an image gallery and input fields. import React, { Component } from 'react'; import ImageGallery from 'react-image-gallery'; import { Container, Row, Col, InputGroup, Button, FormControl, ...

Leveraging jQuery, Ajax, and PHP for generating and organizing HTML blocks - requiring sound logic

I am in the process of developing a website that compares different products. I have implemented code that retrieves and scrapes data for comparison when a user performs a search. Right now, I am using an ajax request for each company (currently 6 compani ...

Is it possible to eliminate the black bars from YouTube HQdefault image using only CSS?

(After searching extensively, I couldn't find the exact same question and answer) I have a dilemma with displaying YouTube's hqdefault thumbnails on my page. It seems that they are 480 by 360 pixels, resulting in black bars at the top and bottom ...

selenium for extracting data from span class tag

After trying to click the button with the text "details," I encountered a "no element found" error. It seems like the tag is within a child class... span _ngcontent-tbi-c3="" class="ng-star-inserted">Details</span Below is my Python code snippe ...

Matching the heights of child elements in different block elements using CSS (flex/grid)

Hey there! I need some help with a markup issue. I want to replicate the layout shown in this picture. The challenge is ensuring that the grey line between the upper and lower sections of cards on the same row are aligned perfectly. In the image, you can s ...

Troubleshooting a problem with CSS styling in a jQuery bounce effect

In my current project, I am experimenting with creating a unique 'hovering' link. When the user hovers over an image and then moves the mouse away, I want the link to 'jump' back instead of using jQuery animation. +-----------+ | I ...

The Drop-down menu with carousel in Bootstrap is experiencing difficulties displaying its sub-menu items

I am currently facing an issue where the sub-menu items are hidden behind the content in a bootstrap carousel. Despite everything else working fine, this particular problem is causing a headache. JSFiddle: https://jsfiddle.net/Saneesh/pwmyvsw6/65/ Is th ...

Blur function not performing as anticipated

I am attempting to achieve a blur effect on a dialog pop-up. Currently, I am using the primeng p-dialog component for this purpose. <p-panelMenu [model]="items" [style]="{'width':'300px'}"></p-panelMenu> <p-dialog head ...

Smooth scrolling is not functioning on laptops but is working on mobile phone browsers on a Next.js website

I'm currently experiencing difficulties while trying to implement smooth scrolling in my Next.js application with the help of Tailwind CSS. Strangely enough, the smooth scrolling feature seems to be working flawlessly on mobile browsers, but it just d ...

What's the best way to display navigation menu items exclusively for mobile view?

When viewing my navbar on desktop, it appears like this: The mobile display is working perfectly. I am looking to have the items in the navbar displayed one by one on desktop and remove the menu icon entirely. This is my current code: <nav class="na ...

What purpose is served by the use of '.src' in Next.js?

When working with Next.js, I encountered a situation where I needed to set a background image. After doing some research, I came across a solution that involved adding .src at the end of the image reference like this: import bgImg from '../public/imag ...

Incapable of inputting text to communicate with chatbot users

Description Incorporating a chatbot into the Flask-based backtesting platform involves utilizing a sidebar toggle button on the right side. By clicking this button, users can reveal the sidebar containing the chatbot. Expected Behavior Users should ...

My node.js code is not producing the expected result. Can anyone point out where I may be going wrong?

I've been working on a BMI calculator where I input two numbers, they get calculated on my server, and then the answer is displayed. However, I'm having trouble receiving the output. When I click submit, instead of getting the answer, I see a lis ...

Employ PHP to adjust the size of an image with the help of $_GET.REQUEST

Is there a way to resize an image on mouse hover using PHP with $_GET instead of CSS or JavaScript? I am looking for a method to achieve this without the need to upload a file. For example, if I have HTML code with an <img> tag and an image, how can ...

Tips for detecting if text appears in bold on a webpage using Selenium

I came across a pdf document with the following content: <div class=**"cs6C976429"** style="width:671px;height:18px;line-height:17px;margin-top:98px;margin-left:94px;position:absolute;text-align:left;vertical-align:top;"> <nobr ...

Peruse through the Vignettes: exclude the `source` and `R code` files in the results

For my R package on GitHub, I created an HTML vignette. When using browseVignettes, the content displayed flawlessly in the browser looks like this: HTML, source, or R code, it opens the same file in three different versions.</p> However, I only wan ...