Is it possible to create a traditional article layout using CSS Flexbox and make it responsive without the need for JavaScript or manipulating the DOM?

My goal is to achieve the following :

https://i.sstatic.net/jI83o.png

I attempted to use this code. It functions as a desktop version but not on mobile. I was advised to code with a mobile-first approach. I am looking for a solution that allows me to create choreographies for both desktop and mobile without using JavaScript, preferably something that complements flexbox.

html {
box-sizing: border-box;
font-size: 100%;
}
*, *:before, *:after {
box-sizing: inherit;
}

.grid {
  display: flex;
  flex-wrap: wrap;
  margin-right: -10px;
  margin-left: -10px;
}


.col-m-1 {
width: 8.333%;
}
.col-m-2 {
width: 16.667%;
}
.col-m-3 {
width: 25%;
}
.col-m-4 {
width: 33.333%;
}
.col-m-5 {
width: 41.667%;
}
.col-m-6 {
width: 50%;
}
.col-m-7 {
width: 58.333%;
}
.col-m-8 {
width: 66.667%;
}
.col-m-9 {
width: 75%;
}
.col-m-10 {
width: 83.333%;
}
.col-m-11 {
width: 91.667%;
}
.col-m-12 {
width: 100%;
}




@media (min-width: 769px){
.col-d-1 {
width: 8.333%;
}
.col-d-2 {
width: 16.667%;
}
.col-d-3 {
width: 25%;
}
.col-d-4 {
width: 33.333%;
}
.col-d-5 {
width: 41.667%;
}
.col-d-6 {
width: 50%;
}
.col-d-7 {
width: 58.333%;
}
.col-d-8 {
width: 66.667%;
}
.col-d-9 {
width: 75%;
}
.col-d-10 {
width: 83.333%;
}
.col-d-11 {
width: 91.667%;
}
.col-d-12 {
width: 100%;
}
}
<head>
<meta charset="utf-8">
</head>
<body>
 <article class="grid article">
    <div class="my-img-supclass col-m-12 col-t-4 col-d-4">
      <img src="img.jpg">
    </div>
    <div class="col-m-12 col-t-8 col-d-8">
      <h2 class="article-h2"><span>a h2 title</span></h2>
      <p class="article-p">some content</p>
    </div>
  </article>
<body>

This setup isn't effective for mobile as the image ends up at the bottom due to the second div placement.

Answer №1

If you're looking to create a responsive layout, consider using CSS Grid. To get started, check out the helpful grid cheat-sheet on the CSS-Tricks website here. This feature is widely supported by all major browsers.

The concept involves defining your layout as a grid within the body or a container div, with specific named regions for different elements like images, titles, and body content.

You can also adjust the grid layout within a media query to cater to different screen sizes. For example, I've set my media query to activate at 800px to showcase a mobile view in the snippet below. Clicking "full screen" will display the desktop view.

body {
  display: grid;
  grid-template-areas:
      "hero-image article-title"
      "hero-image article-body";
}

.hero-image {
  grid-area: hero-image;
  background: lime;
}

.article-title {
  grid-area: article-title;
  background: yellow;
  color: red;
}

.article-body {
  grid-area: article-body;
  background: magenta;
}

@media (max-width: 800px) {
  body {
    grid-template-areas:
        "article-title"
        "hero-image"
        "article-body";
}
<div class="hero-image">image</div>
<div class="article-title">title</div>
<div class="article-body">body</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

Create copies of Div elements for them to revert back to their original state following

It seems like a simple task, but I'm struggling to figure out how to do it. On a single page, I have multiple divs: <div id="playerid-1"><a href="javascript:loadplayer(1);">[LOAD PLAYER]</a></div> <div id="playerid-2">& ...

``What is the best way to adjust the layout of

I've been using the CSS code below to center items on my page: #MainBox { width: 488px; height: 181px; position: absolute; left: 50%; top: 236px; margin-left: -244px; //this being half the width } However, when viewing the pag ...

Prevent Material UI Chips from altering color upon activation

Chip number 3 in the image below appears with a slightly darker background color when clicked, but this change is unnecessary as only the delete button has functionality. Therefore, the change in color serves no purpose. How do I prevent the background co ...

Keep the element positioned at the bottom of the scrollable area

Help! I can't seem to get the footer of my scrollable block element to stay at the bottom. It only sticks if the content (in this case, a table) is taller than the container itself. How can I ensure that the footer always stays at the bottom? I attem ...

What is the method for assigning a data-group value to an HTML element generated in C#?

In my current situation, I am faced with the task of conditionally sliding up or hiding sets of controls and elements. I came across a helpful answer on Stack Overflow from user QBM5 which suggests assigning data-groups to the controls that need to be slid ...

CSS menu with disappearing sub-menu on hover

I'm currently tackling a CSS-only menu project, but I'm struggling with keeping the sub-menu visible when moving away from the li element. The height should span 100% of the page, and the sub-menu should mirror the main menu, appearing to the ri ...

Guide on Deleting Specific Item Using SQL Data Source Delete Statement

I am facing an issue when trying to remove a specific item from a grid view in my SQL server database. I have configured DataKeyNames and linked a sql data source with the grid view. The delete command is set as follows: DeleteCommand="DELETE FROM product ...

Fade In/Out Scroll Effect

As the user scrolls down the page, I have a slider that smoothly slides from right to left. What I'm trying to achieve is for the slider to fade out when the last slide is no longer in view during downward scrolling, and fade back in as the last slid ...

Utilizing the change() and contains() functions with an HTML <select> element in jQuery

Currently, I am working on a feature where images change based on the value selected from a dropdown menu. The code provided below is a simplified version to convey the main idea. Here's the gist of what I have (feel free to ask any questions; please ...

Hiding the SVG element with pattern definitions using the 'display: none' property makes the patterns completely disappear from view

When I include a set of SVG definitions at the top of my body, I encounter an issue where the svg tag with zero height and width creates unnecessary space at the beginning of the document. To resolve this, I initially use display:none, but this leads to pr ...

How to locate a specific element using Simple HTML DOM Parser

<div class="date">June<b>20</b><strong>friday</strong></div> Seeking to extract just the month from within this div, however the challenge lies in the fact that the month is not enclosed by any specific element. Is ther ...

What is the best way to organize hundreds of unique select names and their corresponding selected options in an object using key-value pairs?

I have a dynamic table of data displayed on a webpage in table format, with various select tags that change based on the number of applications. The table structure is shown in the image linked below: IMAGE LINK: https://ibb.co/RCYwchv Each select tag ha ...

Is there anyone who is able to provide an answer to this question

Can anyone help me with connecting a Python program to HTML? I want the output from the Python program to be displayed on the front end when an HTML button is clicked. Any guidance on how to solve this issue would be greatly appreciated. I'm looking t ...

How to handle untagged strings while web scraping with BeautifulSoup (bs4) technology?

While utilizing bs4 for web scraping, I encountered an issue. I was able to extract the desired strings within tags without any problems, but there is one string that doesn't seem to have any associated tags (or maybe I missed it). The structure of ...

Showing data from the POST request in a dialog box

Greetings to all! I am in the process of building a webpage to test my API. The goal is to send a form to my nodejs backend and receive a JSON response back. So far, sending the request is functioning properly and I can track its progress on my VPS conso ...

The height of the navigation bar adjusts to fit the image

I'm currently struggling with my html and css learning project. The height of the navigation bar is not adjusting properly to the size of the image. I have tried various solutions, but nothing seems to work. Any guidance would be appreciated! htm ...

Utilizing Jquery to automatically scroll to a specific div on page load by setting an

I am attempting to automatically scroll to the div specified in the URL. The URL may include one of 21 different IDs such as: url.com/test#lite1 url.com/test#lite2 url.com/test#lite3 This scrolling action should happen when the page loads (so that user ...

Apply a unique font to the gridview that is not universally available on all systems

Can I use a custom font for my gridview that is accessible to all users, even if it doesn't exist in all systems? Is there a way to include the font in the project folder so that it can be viewed by everyone? ...

In an HTML table, configure a column to expand to fill the remaining space while also having a minimum width of 300

Is there a way to create a column in the middle of an HTML table that fills the remaining space with a minimum width of 300px? Take a look at this JSfiddle for reference. HTML: <table> <tr> <td class="fixed-width">Fixed width col ...

Using a local variable within quotes in Angular 4 (HTML) and utilizing ngFor

In my current project, I am utilizing Angular2-Collapsible. The objective is to display the details upon clicking a table row. Below is the code snippet provided. Specifically, if I add [detail]= "detail1", the collapsible-table-row-detail will appear when ...