Parent: Using Flexbox vs Child: Choosing between inline-block or inline elements (Advantages of utilizing CSS3 flexbox?)

I've been exploring the capabilities of CSS3's new "display: flex" property and trying to determine its advantages. So far, I haven't discovered any significant benefits beyond creating a virtual horizontal line within each flex box container for aligning items. Isn't this similar to what can be achieved with display:inline or display:inline-block for inner items? What unique features does this property bring to CSS design?

Case 1: (using flex in parent)

<html>
<head>

<style>
div{background:blue;display:flex;}
p{background:yellow;}

</style>
</head>

<body> 
<div>
<p>This is para1.</p>
<p>This is para2.</p>
<p>This is para3.</p>
</div>
</body>

</html> 

Case 2: (Using display inline to childs)

<html>
<head>

<style>
div{background:blue;}
p{background:yellow;display:inline;}

</style>
</head>

<body> 
<div>
<p>This is para1.</p>
<p>This is para2.</p>
<p>This is para3.</p>
</div>
</body>

</html> 

Case 3: (Using inline-block in childs)

<html>
<head>

<style>
div{background:blue;}
p{background:yellow;display:inline-block;}

</style>
</head>

<body> 
<div>
<p>This is para1.</p>
<p>This is para2.</p>
<p>This is para3.</p>
</div>
</body>

</html> 

Please, help me understand the purpose of using flex and how it can enhance CSS designs!

Answer №1

Although this question may seem off-topic and broad, I have decided to provide an answer as it could be beneficial for users who are new to Flexbox.

Below are some examples that showcase the capabilities of Flexbox compared to standard block/inline-block/inline elements (which may require a lot of hacks/tricks).

If you want to delve deeper into this topic, I recommend reading this article: A Complete Guide to Flexbox


Properties for the flex container (with display: flex;)

The justify-content property, which defaults to flex-start

div {
  background: blue;
  display: flex;
  justify-content: space-around;   /* distribute the remaining space around each item */
}

p {
  background: yellow;
  margin: 5px;
  padding: 5px;
}
<div>
  <p>This is P1</p>
  <p>This is P2</p>
  <p>This is P3</p>
</div>

The align-items property, defaulting to stretch

div {
  background: blue;
  display: flex;
  justify-content: center;      /* hor. center */
  align-items: center;          /* ver. center */

  /* gave a height so the vertical centering can be seen */
  height: 150px;
}

p {
  background: yellow;
  margin: 5px;
  padding: 5px;
}
<div>
  <p>This is P1</p>
  <p>This is P2</p>
  <p>This is P3</p>
</div>


Properties for the flex items, which are the immediate children of the flex container

The order property, with a default of 0

div {
  background: blue;
  display: flex;
}

p {
  background: yellow;
  margin: 5px;
  padding: 5px;
}

p:nth-child(2) {
  order: 1;              /* move 2nd item last */
}
<div>
  <p>This is P1</p>
  <p>This is P2</p>
  <p>This is P3</p>
</div>

The flex-grow property, which defaults to 0

div {
  background: blue;
  display: flex;
}

p {
  background: yellow;
  margin: 5px;
  padding: 5px;
}

p:nth-child(2) {
  flex-grow: 1;           /* make 2nd item fill the remaining space */
}
<div>
  <p>This is P1</p>
  <p>This is P2</p>
  <p>This is P3</p>
</div>

The margin property, defaulting to 0

div {
  background: blue;
  display: flex;
}

p {
  background: yellow;
  margin: 5px;
  padding: 5px;
}

p:nth-child(2) {
  margin-left: auto;        /* push the 2nd/3rd item to the right */
}
<div>
  <p>This is P1</p>
  <p>This is P2</p>
  <p>This is P3</p>
</div>

Answer №2

Not completely sure if this is the exact solution you are seeking, but here goes nothing -

display: flex; is considered more advanced compared to display: inline; or display: inline-block;. However, it is only fully supported starting from IE11, while most other browsers handle it well. Once you grasp the concept, managing display: flex; becomes much easier which is why the latest Bootstrap 4 framework is built upon it.

If you're looking for a detailed explanation of the benefits of these display properties, I highly recommend checking out this informative blog post which greatly assisted me in my initial learning phase.

To delve into the best practices and tricks for implementing them effectively, take a look at this insightful blog. It even includes a playground demo where you can experiment with various scenarios.

Here's an example showcasing the common use of display: flex;:

div {
  background: blue;
  height: 100px;
  margin-bottom: 10px;
  width: 100%;
}

div.flex {
  display: flex;
}

p {
  background: yellow;
  display: inline-block;
  width: 30%;
  margin-left: 1%
}
<div>
  <p>This is para1.This is para1.This is para1.</p>
  <p>This is para2.This is para2.</p>
  <p>This is para3.</p>
</div>

<div class="flex">
  <p>This is para1.This is para1.This is para1.</p>
  <p>This is para2.This is para2.</p>
  <p>This is para3.</p>
</div>

<span>When using <strong>display: flex;</strong>, notice how the child elements maintain consistent height regardless of content. Hopefully, this drives the point home.</span>

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

In Reactive.js, the horizontal use of 'justify-content' in Flexbox is not supported

I'm having trouble using flexbox to evenly distribute components in a row with 'justify-content'. For some reason, the components are only lining up in a column. It seems like React is not recognizing the two components generated from props ...

Ways to eliminate the top space in the background image

After implementing a basic HTML code to add a background image to my webpage, I noticed there is still some empty space at the top of the page. I attempted to adjust the position of the background image using the following code, but it only lifted it upwar ...

Attempting to align three sections horizontally

I'm having trouble with my current HTML code and I can't seem to figure out what the issue is. As a beginner in HTML, I am struggling to understand. My goal is to have three columns: one on the left taking up 20% of the space, one in the center t ...

What is the best way to add animation to the hide/show function?

<div class="overlay"></div> CSS: .overlay::after { position: fixed; top: 0; left: 0; width: 100%; content: ""; z-index: -1; height: 100%; transit ...

What steps should I take to achieve this specific style for my WordPress website post?

Check out the image link here I'm looking to position the share and read more buttons at the bottom of the div, similar to "Sample Post 14". The trick seems to involve using excerpt or dummy text. Is there a way to achieve this styling with the skele ...

Instructions on how to load an HTTP file inside a Bootstrap modal

Is it possible to load a file located at an http:// address into a modal window? Something like this: <a class="btn btn-info btn-large" data-toggle="modal" href="#http://localhost/login.html"> <i class="icon-user icon-white"></i> Login ...

Utilize UI Kit to incorporate fluid margins dynamically

How can I dynamically add internal margins between cards in a responsive layout? The following code ensures responsiveness: Large Screens display 4 Cards. Medium Screens display 3 Cards. Small Screens display 2 Cards. Micro Screens display 1 Card. &l ...

Issue: Keeping the mat-form-field element in a single line

I am facing an issue with incorporating multiple filters for each column in an angular material table. I cannot figure out why the filter input is not moving to a new line under the header. Here is an image for reference -> Angular material table with m ...

Locate specific phrases within the text and conceal the corresponding lines

I have a JavaScript function that loops through each line. I want it to search for specific text on each line and hide the entire line if it contains that text. For example: <input id="search" type="button" value="Run" /> <textarea id ...

There seems to be a clash between Modal Semantic UI React and Bootstrap causing issues

I created a project using .net core MVC. I have integrated the semantic-ui-react modal by importing its library and linking the CSS for it to function properly. However, I encountered an issue where the bootstrap CSS was conflicting with the CDN for semant ...

Adjusting the Size of Dialog Windows in Lightswitch HTML Client

When using the MS Lightswitch HTML Client, if a dialog menu option contains too many characters to fit within the length of the dialog window, the text will be cut off and replaced with (...). Is there a way to add a scroll bar or adjust the text size in ...

Title Bar: Excessive gap; inaccurate navigation to nearby link

I've been trying to align a horizontal navigation bar right below a banner and link directly to specific sections on the same page. No matter what I do, I can't seem to remove the white space between the banner image and the navigation bar. It ...

What is the best way to automatically close a modal box after submitting a form?

Currently, I am utilizing the CSS modal box developed by Paul R. Hayes. More information about this can be found here - Within the modal box, I have integrated a form. This form is aimed at an iframe that remains concealed on the same page. My objective i ...

Is there a bug causing the Admin LTE content height issue? Looking for solutions to fix it

What's the reason behind the oversized footer? The footer appears normal when I resize or zoom the page, why is that? How can this be resolved? ...

Equal Sized <li> elements within Bootstrap Cards

I've been working with the following "template", and there are multiple cards like this on the settings page I'm currently developing. <!-- Card template --> <div class="card mt-3 shadow-sm"> <div class="card-hea ...

I'm puzzled as to why the color isn't showing up on my navigation bar even though I used "padding: 10px !important;"

I really need help with this, I'm quite new to all of this and it's really confusing me. When I remove this line, the color shows up again, but when I add it back in, it just disappears. I've been following a tutorial on YouTube on how to cr ...

Using CSS grid can allow for paragraphs to overlap on top of each

Currently working on constructing a biography page filled with text content, and I aim to utilize CSS GRID instead of floats. I encountered an issue in getting the text to occupy all the available white space within the layout. I have attached an image to ...

Using JQuery to visually enhance the menu selection by displaying an image and altering the background color upon hovering

Hello fellow JQuery/Javascript beginners! I recently put together a menu and wanted to add some interactive elements. Specifically, I want the background color to change when a user hovers over an item (simple enough with CSS), but I also want to include a ...

What's the reason for my badge being an oval shape?

I am attempting to design a badge that hovers above my cart icon and shows the total number of items currently in the cart. However, I have encountered an issue where the badge appears as an oval shape rather than a perfect circle. Can anyone assist with ...

Foundation Unveil Modal hidden from view

I'm currently integrating modals using Foundation 5 in a Rails application. The issue I'm facing is that the modal only works when the page is not scrolled down. If you scroll to the bottom of the page and try to activate the modal by clicking ...