Dynamic CSS containers that adjust according to browser and screen dimensions

01) In my attempt to create a row with two content boxes, I encountered an issue. The first box is supposed to display an image and the second box text. However, I am struggling to make it responsive and interactive based on different browser and screen sizes.

I aim to achieve this desired outcome while ensuring it adapts to varying screen sizes:

This is why I included :

<div class="inner"></div>
in an effort to control the maximum dimensions, but unfortunately, it did not work as intended.

HTML:

    <head>
<link rel="stylesheet" type="text/css" href="rich_text.css">
</head>

<div class="inner">
    <div class="feature left">
        <span class="image"><img src="http://SITE.co.uk/images/bg3.png" alt="" />

        </span>
        <div class="content">
            <h2>Total Facebook Image Likes</h2>
            <p>65 </p>
            <ul class="actions">
                <li><a class="button alt" href="#">LINK</a></li>
            </ul>
        </div>
    </div>
</div>

CSS: CSS LINK

02) Another observation I made was that the final output appears distorted if the image size is too large.

I attempted to address this by including:

<span class="image"><img style="height:400px;max-width:400px; src="http://SITE.co.uk/images/bg3.png" alt="" />

However, the image fails to load properly.

Answer №1

If you're looking to create a responsive design that adjusts based on screen width, consider using percentage or viewport units like %, vh, and vw.

Take a look at this example:

/* For demonstration purposes only */
.row { margin-bottom:1em;}
.red {background:red;}
.blue {background:blue;}

/* Set all divs inside a row to be 50% width */
.row div {
  width:50%;
  box-sizing:border-box; /* Prevent paddings or borders from affecting width calculation */
  padding: 1em;
  color:#FFF;
}

/* By default, divs have auto width, usually spanning 100% of their parent element */
.row.default div { width: auto; }

/* Floating divs to the left will position them next to each other horizontally */
.row.floated div { float:left; }

/* Adding overflow:auto to the parent of floated divs prevents layout issues */
.row.floated { overflow:auto; }

/* Rows can have any width, with children resizing accordingly to always maintain a 50% width relationship with the parent */
.row.maxwidth {max-width:400px;}

/* Children can be styled as inline-block elements instead of floating */
/* Note: Spaces between inline-block elements in HTML can affect layout, so use caution */
.row.inline div {display:inline-block;}

/* Using viewport units */
/* vw = viewport width */
/* vh = viewport height */
/* 50vw = 50% of viewport width */
.row.vw div { width: 50vw; }
<h2>1. Default Behavior</h2>
<div class="row default">
  <div class="red">default block</div>
  <div class="blue">default block</div>
</div>

<h2>2. Percentage Width (based on parent)</h2>
<div class="row">
  <div class="red">50% width block</div>
  <div class="blue">50% width block</div>
</div>

<h2>3.1 Percentage Width + Floating</h2>
<div class="row floated">
  <div class="red">50% width block - floated</div>
  <div class="blue">50% width block - floated </div>
</div>

<h2>3.2 Parent with max-width of 400px</h2>
<div class="row floated maxwidth">
  <div class="red">50% width block - floated</div>
  <div class="blue">50% width block - floated </div>
</div>

<h2>4. Percentage Width + Inline-block</h2>
<div class="row inline">
  <div class="red">50% width inline-block</div><div class="blue">50% width inline-block</div>
</div>

<h2>5. Viewport Width (based on viewport)</h2>
<div class="row vw floated">
  <div class="red">50vw width block - floated</div>
  <div class="blue">50vw width block - floated</div>
</div>

The code is extensively commented to help you grasp the functionality and explore different approaches. I suggest utilizing percentage widths along with floating for simplicity. Alternatively, you can leverage comprehensive grid systems like Bootstrap featuring a ready-to-use 12-column grid without needing to construct a custom layout grid.

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

Firebase web authentication is most effective upon the second attempt

I am currently working on a website that interacts with Google's firebase to read and write data. The website has both anonymous and email authentication enabled. Users can view the data anonymously, but in order to edit or write new data, they must s ...

Merging the Select and Input elements side by side using Bootstrap grid system

Is there a way to combine all form group items like Select and Input using only the bootstrap framework? I attempted the method below, but there is an extra space between the Select and Input Box. Check out the DEMO ...

Clicking on Google Code Prettify does not trigger syntax highlighting

I utilize Google Code Prettify for syntax highlighting my code. Below is the HTML snippet I use: <head> <script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script> </head> <body> ...

`I noticed that the CSS appears differently when viewed in Mozilla compared to Chrome``

Why do the images with the same CSS property appear different in Chrome and Mozilla? I want them all to float left with equal margins between them, covering the complete area horizontally despite having varying image widths. I'm unsure why there is a ...

Overlay text on top of image with fading transparency when hovering over it

Is there a way to make the transparent layer that appears when hovering over an image on a card only cover the image itself and not extend onto the footer of the card? Currently, the image on the card covers most of it, and I want the hover overlay to beh ...

How can one transfer information from a client to a server and complete a form using JavaScript?

Can the information be transferred from a client to the server using just JS, then fill out a form on the server, and finally redirect the client to view a pre-filled form? I am considering utilizing AJAX to send a JSON object, but unsure if it will be s ...

I've encountered an issue when attempting to use innerHTML for DOM manipulation

I've been attempting to remove a specific list item <li> from the inner HTML by assigning them proper IDs. However, I'm encountering difficulties in deleting it. How can I delete these list items after clicking on the cross button? Feel fr ...

Is it possible to resize an object using JavaScript?

Is it possible to change the size of an object when loading specific data by clicking on navigation? <object id="box" width="250" height="250" data=""></object> Although the JS code loads the data, it does not change the size. document.getEl ...

Designing a Scrollable tbody Element while Preserving the Integrity of the tbody Columns

Currently, I am attempting to implement a scrollable tbody in order to run a React package smoothly. The challenge lies in achieving a scrollable tbody without having to resort to using display: block on the tbody element. Unfortunately, this solution dis ...

Issues arise when the sub-menu does not function properly and overlaps with other elements of

I've been working with a client on their website and encountered an issue. On the mobile version of our menu, when I hover over a menu item that has several sub-menu items, those items display properly, but there is an element below them showing thro ...

Ways to ensure that a div, header, container, and other elements completely cover the viewport

Currently, I am attempting to create a header that spans the entire viewport regardless of screen size. This header will be split horizontally into 3 equal sections, each occupying one third of the header space. I experimented with setting the header hei ...

What is the process for updating a collection in MongoDB database with PHP?

After writing a code snippet to gather user input through a form and update a collection based on the entered product ID, I am encountering issues with incorrect results. It seems that I am unable to fetch the correct product ID value, which is causing t ...

Styling images side by side with CSS in Internet Explorer

I've encountered a CSS dilemma. I have a collection of images that I want to present side by side using an unordered list within a fixed width container. The goal is to have 3 images per row before moving on to the next set of 3 images. Each list ite ...

"Creating visual art with processing in 2D using P5

Recently, I came across a webpage about rendering 2D objects in processing using JavaScript. Here is the link to the page: Upon trying out the example code provided on the page in a new P5 project, I noticed that the code structure looked like this: HTML ...

Obtaining data from a website with Java: A step-by-step guide

I am trying to retrieve the SIC Code from the following URL: . However, when I run my code, I encounter the following error: public static void main(String[] args) { try { Document doc = Jsoup.connect("http://www.manta.com/c/mx4s4sw/bowflex-ac ...

Tips for deleting part of the URL at the end of all hyperlinks on a webpage

I have a large number of links on the same page that I would like to clean up by removing everything from specific characters until the end of the URL. Currently, there are too many links like this scattered throughout my page. The string "$amp;rs" or "&a ...

Headerbar overlapping Div when scrolling

I'm currently trying to create a fixed header bar that allows the content of the page to scroll beneath it rather than displaying above it. This functionality is working on multiple pages, but I'm experiencing issues with my calendar page. When ...

Enhance user experience with jQuery UI sortable and the ability to edit content in real

I am facing an issue with jquery sortable and contenteditable. The problem arises when I try to use jquery sortable along with contenteditable, as the contenteditable feature stops working. After searching on Stack Overflow, I found a solution. However, up ...

Clicking on a flex card will trigger the opening of a new page where the parsed data will be displayed in a stylish manner using JavaScript

Currently, I am attempting to showcase the data retrieved from the following URL: . My objective is to format the parsed data with a specific style. However, I have encountered difficulties accomplishing this using `window.open()`. Any assistance in resolv ...

When you click on the button, the section will not be displayed

I'm facing an issue with my code. I have a set of five buttons and corresponding sections. When a button is clicked, the active and active-btn classes are supposed to be added to that button as well as the corresponding section element with the same i ...