Seeking assistance with CSS - need to adjust block layout based on media query - considering using flexbox

I struggle with HTML5/CSS and have a simple task of laying out three DIVs differently based on screen size, using @media declarations.

Layout 1                                Layout 2

+----- container -----------------+    +----- container ---------------------+
| +-----------------------------+ |    | +------------+  +-----------------+ |
| | header text                 | |    | | thumbnail  |  | header text     | |
| +-----------------------------+ |    | | image      |  +-----------------+ |
| +------------+  +-------------+ |    | |            |  +-----------------+ |
| | thumbnail  |  | body text   | |    | +------------+  | body text body  | |
| | image      |  | body text   | |    |                 | text body text  | |
| |            |  | body text   | |    |                 | body text ..... | |
| +------------+  +-------------+ |    |                 +-----------------+ | 
+---------------------------------+    +-------------------------------------+

The basic structure is as follows:

<div id="container">
  <div id="header">header text</div>
  <div id="thumbnail"><a href="xxx"><img src="yyy" /></a></div>
  <div id="bodytext">Some free-form text to be wrapped</div>
</div>

The thumbnail images are fixed in size. My current approach involves:

In Layout 1, I use float:left for the 'thumbnail' and 'bodytext' divs. In Layout 2, I apply padding-left: [image width]px; to the 'header' and 'bodytext' divs, along with absolute positioning of the 'thumbnail'.

However, this solution feels inelegant and makeshift, with concerns about compatibility across different browsers and devices.

Is there a better alternative such as flexbox?

Answer №1

If you're looking for a solution, consider using the CSS Grid. To see the solution clearly, try viewing it in full screen and experimenting with the Grid Generator tool.

#container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  grid-column-gap: 10px;
  grid-row-gap: 10px;
}

#header {
  grid-area: 1 / 1 / 2 / 3;
}

#thumbnail {
  grid-area: 2 / 1 / 3 / 2;
}

#bodytext {
  grid-area: 2 / 2 / 3 / 3;
}

@media (max-width:600px) {
  #header {
    grid-area: 1 / 2 / 2 / 3;
  }
  #thumbnail {
    grid-area: 1 / 1 / 2 / 2;
  }
  #bodytext {
    grid-area: 2 / 2 / 3 / 3;
  }
}

/* Additional Styling for Snippet */

#container > div { background: #5548B0; color: #fff; text-align: center; font-size: 2em; padding: 5%; }
<div id="container">
  <div id="header">Header Text</div>
  <div id="thumbnail">
    <a href="stackoverflow.com"><img src="https://loremflickr.com/320/240"></a>
  </div>
  <div id="bodytext">Some free-form text to be wrapped</div>
</div>

Answer №2

Utilizing CSS grid appears to be the best approach. One adjustment I've implemented to Manoj's solution is incorporating the min-content directive to ensure a row adjusts to the smallest height required for content rendering, while also spanning the thumbnail cell over rows 1 and 2.

#container {
  display: grid;
  grid-template-columns: 320px 1fr;
  grid-template-rows: min-content 1fr;
  grid-column-gap: 8px;
}

#header {
  font-size: 2em;
  color: white;
  grid-row: 1;
  grid-column: 2;
}

#thumbnail {
  grid-row: 1 / span 2;
  grid-column: 1;
}

#bodytext {
  color: red;
  grid-row:2;
  grid-column: 2;
}

@media (max-width:600px) {
  #header {
    grid-column: 1 / span 2;
  }
  #thumbnail {
      grid-row:2;
      grid-column: 1;
  }
}

/* Additional Styling for Snippet */

#container > div { 
     background: #5548B0; 
     text-align: center; 
 }
<div id="container">
  <div id="header">Header Text</div>
  <div id="thumbnail">
    <a href="stackoverflow.com"><img src="https://loremflickr.com/320/240"></a>
  </div>
  <div id="bodytext">Some free-form text to be wrapped. Some free-form text to be wrapped. Some free-form text to be wrapped. Some free-form text to be wrapped.</div>
</div>

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 is the best way to link HTML transformations across several classes?

Is it possible to combine multiple transforms in a single element to create a unique end result? It seems that when applying multiple transform properties to an element, only one of them will be recognized. For example, trying to transform a div with bot ...

Stroke on SVG Rect disappears suddenly without explanation

My rectangle seems to be losing some of its stroke randomly, sometimes after around 50 seconds. I can't figure out why this is happening, could you help me out? Here is the link to the fiddle - http://jsfiddle.net/nhe613kt/368/ Below is the relevant ...

The PHP/MySQLi registration form is displaying a blank white page without any visible output or error messages

Hey everyone, I'm currently working on a simple registration form in PHP and no matter how many times I try to run it, all I get is a blank white page. There are no errors or outputs, just pure emptiness. I was hoping someone could shed some light on ...

Is it possible to replicate, modify, or recycle a purely HTML and CSS component in Angular?

On my Angular website, I am looking to reuse a component across more than 30 pages. While the majority of the pages have similar CSS, the content inside varies. I know I can simply insert <app-example></app-example> on all the pages, using < ...

Is it possible to emphasize a duration of 25 minutes on an analog clock by utilizing CSS gradients?

I am in the process of incorporating a countdown timer but with an analog clock rather than a digital one. My objective is to emphasize the next 25 minutes as a circle sector that commences from the minute hand on the clock. Here is what I have developed ...

Heroku encounter an H14 error - "the absence of running web processes"

An H14 error occurred during deployment on Heroku. Here is my Procfile: web: gunicorn -w 4 -b 0.0.0.0:$PORT -k gevent main:app Error log on Heroku: 2017-01-23T10:42:58.904480+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method ...

Adjusting the box sizing of a contentEditable DIV to conceal the first and last characters

I am having trouble with a ContentEditable div that requires the hiding of characters, specifically the first and last character. I need to overlay a layer on top of the "#" character to make it appear as if the character and the border line beneath it are ...

How about adding various colors in the background with layers?

I was recently given a task by a client to convert their picture art into CSS code. While I have successfully completed most of the task, I am facing difficulty in coloring the div similar to the example provided by the client. Here is the client's ve ...

How can I find the last element that was selected using XPath in the browser console

Need help with XPath: $x(("//div[@class='ag-header-row']))[1] I'm working with an array of divs, but I want to select the last one. The [1] is necessary due to multiple rows with this class. I’ve heard about using [last()], but unsure w ...

Is JSF h:outputStylesheet converter the solution for creating dynamic CSS?

Recently, I came across the <h:outputStylesheet/> tag with a converter attribute. I tried attaching a dummy (pass-through) converter to it, but to my surprise, nothing happened. Upon further investigation, it appears that the converter is not even in ...

Binding iframes in Angular 6

Is there a way to display iframe code stored in a variable within a div? Here's the HTML code: <div class="top-image" [innerHTML]="yt"></div> And here's the TypeScript code: yt = '<iframe class="w-100" src="https://www.you ...

How come the previous sibling element appears on top when the initial sibling is set to a position of absolute?

I have encountered an issue with two sibling sections. I applied position:absolute to the first section, but the second section is overlapping it. Even after trying to use position:relative on the second section, the problem persists. https://i.stack.imgur ...

Personalizing the React Bootstrap date picker

I am currently working on customizing the react-bootstrap-daterangepicker to achieve a specific look: My goal is to have distinct background colors for when dates are selected within a range and when the user is hovering over dates to make a selection. I ...

Applying FadeIn Effect to Background Image on Hover

For several applications, I have utilized this jQuery code that swaps images with a smooth fading effect. It's incredibly straightforward. $(document).ready(function() { $("img.a").hover( function() { $(this).stop().animate({ ...

Obtain the value of a two-handle slider using jQuery

Here is the HTML and JS setup I have for implementing a jQuery two-handle range slider: .html <p> <input class="amount" readonly style="border:0; color:black; background: transparent; text-align: center;" type="text"> </p> <div cla ...

Looking for assistance with accessing elements using the "document.getElementById" method in Javascript

Below is the code snippet I am working with: <html> <head> <script> function adjustSelection(option) { var selectList = document.getElementById("catProdAttributeItem"); if (option == 0) { ...

Prevent CSS3 columns from splitting paragraphs apart

What is the best way to prevent the browser from splitting paragraphs when using CSS3 columns? Below is the code I am currently using: <div class="container"> <div class="box"><!-- text --></div> <div class="box">< ...

Is it possible to create a masonry-style layout with two columns that is responsive without

Is there a way to organize multiple divs of varying heights into two columns that display immediately one after the other, without relying on JavaScript or libraries like packery or masonry? I've experimented with using display: inline-block in this ...

Alignment issue with Bootstrap's vertical radio buttons

When the screen size reaches 768px for mobile phones, I am experiencing an issue with stacking radio buttons. I have a local @media override that sets the radio-inline display to block, but it causes the first Radio Button "0" to be slightly offset and n ...

Tips for displaying only one image when clicked and hiding other divs:

Currently tackling a project that involves JavaScript and I've hit a roadblock. Before you dive into the text, take a look at the image. Check out the picture here. I have successfully created a slider, but now I want to implement a feature where cli ...