When a single column is achieved, aligning with justify-content space-between

I'm working on styling a div that has two children using flexbox and flex-wrap. I want the layout to adjust without needing media queries - so that when it reaches a certain width, the children are spaced evenly (justify-content: space-between), but when it wraps into a single column, they are centered instead of being aligned to the start as they currently are.

Here is the code snippet that I believe is relevant for this issue:

HTML and CSS

.content-container {
    display: flex;
    justify-content: center;
    width: 100%;
}

.content {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 82%;
}

.content h1 {
    min-width: 20rem;
}

.content h6 {
    min-width: 15.5rem;
}
<div class="content-container">
    <div class="content">
        <h1>'content1'</h1>
        <h6>'content2'</h6>
    </div>
</div>

Answer №1

Hopefully, this solution meets your requirements. Unfortunately, I couldn't find a way to address your issue using pure CSS without media queries or JavaScript. As a workaround, I changed the space-between to space-around for the .content class (given that you set it at 82%, it shouldn't make much difference).

If you prefer not to use space-around, I believe finding a solution without media queries or JS would be challenging.

.content-container {
  display: flex;
  justify-content: center;
  width: 100%;
}

.content {
  display: flex;
  flex-wrap: wrap;
  place-items: center;
  justify-content: space-around;
  width: 100%;
}

h1 {
  width: min-content;
}

h6 {
  width: min-content;
}
<div class="content-container">
    <div class="content">
        <h1 class="content__1">'content1'</h1>
        <h6 class="content__2">'content2'</h6>
    </div>
</div>

Answer №2

Would you like it formatted this way?

.container-content {
    display: flex;
    justify-content: center;
    width: 100%;

}

.section {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    text-align: center;
    width: 82%;
}

.section-heading {
    min-width: 20rem;

}

.section-subheading {
    min-width: 15.5rem;

}
<div class="container-content">
    <div class="section">
        <h1>'content1'</h1>
        <h6>'content2'</h6>
    </div>
</div>

Answer №3

It seems that achieving your desired outcome without using a @media query in CSS is not possible.

Option 1: CSS without a @media query

If you prefer to avoid using a @media query, consider Option 1. Keep in mind that the difference between space-around and space-between is significant - while space-between aligns elements all the way to the edges, space-around does not.

Here is an example of space-between:

https://i.sstatic.net/12Ekd.png

And here is an example of space-around:

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

...

If Option 1 meets your requirements, there's no need to explore further. However, there are alternative solutions worth considering if flexibility is essential. Other methods can be employed to achieve the desired layout where elements are aligned to the left and right when not stacked on top of each other.

The challenge lies in dynamically switching between space-between and space-around based on window width, which cannot be accomplished solely through CSS without utilizing a @media query, JavaScript, or jQuery. Check out the additional proposed strategies below.

Option 2: CSS with a @media query

...

Option 3: JavaScript

...

Option 4: jQuery

...

Note: The addition of text-align: center; and line-height: 50px; in the .content class aids in vertically and horizontally centering text within containers. Feel free to amend as necessary. Furthermore, setting border: 2px solid red; helps illustrate the distinction between space-between and space-around.

Answer №4

Give this a try.

.content-container {
    display: flex;
    justify-content: center;
    width: 100%;
    text-align:center;
}

.content {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    width: 82%;
    text-align:center;
}

.content h1 {
    min-width: 20rem;
}

.content h6 {
    min-width: 15.5rem;
}
<div class="content-container">
    <div class="content">
        <h1>'content1'</h1>
        <h6>'content2'</h6>
    </div>
</div>

Answer №5

This unique solution utilizes only flexbox for styling, without the need for media queries or JavaScript. Are you satisfied with this approach?

.content-container {
    display: flex;
    justify-content: center;
    width: 100%;
}

.content {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 82%;
    text-align: center;
}

.content h1,
.content h6 {
    flex: 1;
}

.horizontal-spacing {
  flex: 1;
  min-width: calc(20rem + 15.5rem);
  height: 0;
}
<div class="content-container">
    <div class="content">
        <h1>'content1'</h1>
        <span class="horizontal-spacing"></span>
        <h6>'content2'</h6>
    </div>
</div>

Answer №6

Here's a neat trick using the clamp() function to switch between fixed width and full width layouts based on a parameter (in this case, 50rem)

.content-container {
  display: flex;
  justify-content: center;
  width: 100%;
}

.content {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  width: 82%;
  text-align: center;
}

.content > * {
  width: clamp(15rem, (50rem - 100vw)*9999, 100%);
  outline: 1px solid red;
}
<div class="content-container">
  <div class="content">
    <h1>'content1'</h1>
    <h6>'content2'</h6>
  </div>
</div>

For more in-depth information, check out my article at: https://css-tricks.com/responsive-layouts-fewer-media-queries/

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

Unordered list not floating properly - successful in JSFiddle but not displaying correctly in web browser despite updating Chrome and Firefox

Having trouble floating an unordered list? Check out this jfiddle example that aligns a list next to some text: Here is the updated jfiddle link: https://jsfiddle.net/8qzygaog/ I created a test document based on the example, but it's not working as ...

How can you create a jQuery fade in effect for a single <li> element

I'm in the process of developing a task management app that generates a new li element every time a user adds an item. However, I am facing an issue where fadeIn() is activating for every li on the page whenever a new item is added. Does anyone have s ...

Troubles arise when the left column shifts to the top with widths larger than 1440

Having a WordPress site with the Divi theme, I encounter an issue where the menu on the left-hand side is being displayed above the page content when the screen width exceeds around 1440 pixels. This problem persists on every page that contains this colum ...

Tips for creating a full-screen background image using HTML and CSS

I am looking to achieve a full-screen background image using HTML and CSS. I have configured all the necessary properties for the background image. Here is where my background image is located: [![enter image description here][1]][1] I have attempted to ...

Accessing desired option from dropdown in Laravel

Can someone help me with a simple issue I'm having? I'm attempting to obtain the selected value from a dropdown list. Dropdown List: <select name="category_id" class="form-control selectpicker"> <option value="">Select C ...

Change the value of the material slide toggle according to the user's response to the JavaScript 'confirm' dialogue

I am currently working on implementing an Angular Material Slide Toggle feature. I want to display a confirmation message if the user tries to switch the toggle from true to false, ensuring they really intend to do this. If the user chooses to cancel, I&ap ...

Utilize jquery and css to effectively stack images and control their positioning on hover

Is there a way to create a stack of images where the image behind moves to the front on mouseover, and the image in the front goes to the back? The top image would be full size, with each image below being slightly smaller and offset to the side for select ...

Mobile Mode Issue with Bootstrap 4 Navbar Example

Currently, I am exploring the Bootstrap material and trying to understand how to integrate their content with a jinja2 file that I am preparing for a django project. Upon attempting to copy and paste their example code (specifically this example http://get ...

Methods for Expanding a Div within an Oversized Row, Despite Height Constraints

I have a situation where I need to adjust the height of a cell in a table. One of the cells contains a larger element that causes the row to expand vertically. I also have another cell with a div element set to 100% of the cell's height. However, I wa ...

Integrates an interactive feature within the select tag (<select>)

I'm struggling with creating a drop-down menu using the <select> element. My goal is to have a question mark (?) displayed next to each option in the menu, which will provide a brief explanation when hovered over. You can see an example of what ...

Assistance requested with Javascript for an HTML dropdown menu

My question involves utilizing two HTML drop down menus. <select> <option value="">Please Select</option> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option ...

The IFrame event functions properly in HTML, but encounters issues when used in an HTA environment

In this unique HTML document, a message dialog will be displayed when a button inside an iframe is clicked: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script language="VBScript" type="text/vbs ...

How can you eliminate the space below the items in Bootstrap pagination?

How can I remove the strange gap/space below the pagination items in Bootstrap? https://i.sstatic.net/zGhOD.png Here's the code snippet: <header role="navigation"> <!-- justify-content-end is only available on bootstrap 4 --> <n ...

A guide to implementing hover and mouse click effects on ImageList using Material UI version 5.11.11

I have successfully created a list of images using MaterialUI, but now I would like to enhance the user experience by adding a mouse hover effect to highlight the image under the cursor. Below is the code snippet for my image list. How can I accomplish t ...

When seen on a mobile device, the background image is set to repeat along the

Having trouble getting my background image to repeat on the y-axis when viewed on a mobile device. On desktop, the page doesn't scroll and the background image fills the screen perfectly. However, when switching to tablet or mobile, scrolling is neces ...

Create a Boostrap navbar with a form and links aligned to the right using the navbar

I'm trying to use navbar-right on a navbar-form and a navbar-nav, but the form ends up on one row and the nav links on another row on the right. How can I make the navbar display the brand on the left and have a search field followed by nav links on t ...

When the button is clicked, bind the value to an object using the specified key in

I'm fairly new to Vue and I'm looking to generate buttons based on my data and show their information when clicked. The 'pets' object contains keys for id and info. (My actual data is more extensive and my code is a bit more complex, bu ...

Modifying the default hover color of a specific row in AgGridReact

How can I eliminate the row color changing behavior in AgGrid tables when a row is selected and hovered over using React? Currently, a default dark gray color is applied when I hover over certain elements of a selected row, which is not desired in my imple ...

Creating a div that automatically resizes to fit a specific width, with excess content wrapping to the next row

In our project, we currently have a design with 4 div elements arranged in two rows and two columns. However, the client has requested that we allow for multiple div elements within a container that spans 100% width. The inner divs should adjust to fit si ...

Align dropdown navigation menu/sorting dropdown in the same position as the button

Exploring the world of dropdown menus in CSS has led me to encounter some challenges. I am striving to ensure that the dropdown boxes appear on the same line as the button that triggers them. Below is the CSS code I have been working with: /* global style ...