Flexbox causing an inline-level element to wrap to a new line

Why is the <strong> element, an inline element, being pushed to a new line?

This configuration involves nested flex elements, which may seem complex but should not be too difficult to understand. It could be possible that I have a misconception about how flexbox operates.

Check out: http://codepen.io/leads/pen/mEYOay

* {
  margin: 0;
  padding: 0;
}
.container {
  display: flex;
  border: 1px solid red;
  height: 200px;
  max-width: 600px
}
.left {
  flex: 1;
  background: rgba(0, 0, 0, .3);
}
.middle {
  flex: 0 0 200px;
  background: rgba(0, 0, 0, .4);
  text-align: center;
  display: flex;
  flex-direction: column;
}
.middle button {
  flex: 0 0 50px;
}
.middle p {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
}
strong {
  background: red;
}
.right {
  text-align: center;
  flex: 0 0 100px;
  background: rgba(0, 0, 0, .5);
}
<div class="container">
  <div class="left">
    <p>some content in the top left</p>
  </div>
  <div class="middle">
    <p>Lorem ipsum <strong>dolor</strong> sit amet, consectetur adipisicing elit.
    </p>
    <button>CTA</button>
  </div>
  <div class="right">
    <p>CTA</p>
  </div>
</div>

Answer №1

The <strong> element is placed as a child within a flex container that has the CSS property flex-direction: column.

As a result, it will be positioned vertically along with its sibling elements (including any anonymous flex items, similar to what is seen in your provided code snippet.)

Additionally, keep in mind the following:

  • When elements are children of a flex container, their display value is adjusted to behave like block-level elements.
  • In simpler terms, properties such as display: inline, display: table, etc., will take on the behavior of display: block within a flex formatting context.

If needed, an alternative approach can involve utilizing flex auto margins for crafting your design layout:

* {
  margin: 0;
  padding: 0;
}
.container {
  display: flex;
  border: 1px solid red;
  height: 200px;
  max-width: 600px
}
.left {
  flex: 1;
  background: rgba(0, 0, 0, .3);
}
.middle {
  flex: 0 0 200px;
  background: rgba(0, 0, 0, .4);
  text-align: center;
  display: flex;
  flex-direction: column;
}
.middle button {
  flex: 0 0 50px;
  margin-top: auto;   /* NEW */
}
.middle p {
  margin-top: auto;   /* NEW */
}
strong {
  background: red;
}
.right {
  text-align: center;
  flex: 0 0 100px;
  background: rgba(0, 0, 0, .5);
}
<div class="container">
  <div class="left">
    <p>some content in the top left</p>
  </div>
  <div class="middle">
    <p>Lorem ipsum <strong>dolor</strong> sit amet, consectetur adipisicing elit.
    </p>
    <button>CTA</button>
  </div>
  <div class="right">
    <p>CTA</p>
  </div>
</div>

Revised Codepen Link

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

Left-align the text above the div (in relation to it)

My current issue involves aligning h2 text to the left relative to a centered div in the mobile view. How can I achieve this alignment with a media query in the provided CSS? https://i.sstatic.net/sIJb4.jpg CodePen Here is the relevant HTML: <sectio ...

Guide for including a script reference in an HTML file through a Windows .BAT file

Looking to add a script reference to an HTML file using a Windows .BAT file Here's the code I've attempted: for %%f in (*.txt) do ( for /f "eol= delims=" %%v in (%%f) do ( if "%%v"==">Line-X" ( echo %%v>> %%f2 echo ^& ...

How can I use jQuery to show the text value of a selected checkbox, dropdown menu, and flipswitch in a popup?

This form's HTML is set up for input with a popup display feature upon submission. Currently, only text can be displayed in the popup. It doesn't show the text of selected checkboxes or dropdown lists - just numbers for the dropdown and "ON" for ...

AngularJS - Custom directive to extract a property value from an object

Currently, I am using the following for loop to retrieve the parent category: angular.forEach(queryTicketCategories, function(category) { if(category.id === $scope.ticketCategory.parentId) { $scope.parent = category; } }); I am looking fo ...

Load dialogue before loading the page

Is there a way to display a dialog box without using the dialog ui to show error events on a web page? <div id="downloadcredit" style="width:100%;position:fixed;height:100%;min-height:overflow:hidden;background-color:rgba(127,127,127,0.4);display:none; ...

Choosing elements in HTML using jQuery from an Ajax GET response

As a programming student, I am currently working on developing a basic website using HTML, JavaScript, and jQuery for the front-end, while utilizing node.js and Express frameworks for the back-end. In my project, I have used Ajax to retrieve data and then ...

Is it possible to apply alternate row colors to elements other than tables, such as div tags?

After encountering issues with my Handlebars template, I decided to create rows without using a table. Now, I am looking to implement zebra stripe CSS and wondering if that property can be applied universally. Thank you for your help. ...

Using setInterval() in codeigniter does not function as expected

The setInterval() function is functioning properly in corePHP, but it does not seem to work in CodeIgniter. I am trying to retrieve data from another controller without having to reload the page. CONTROLLERS home.php <?php defined('BASEPATH&apos ...

What is the best way to utilize CSS to center an element in relation to the scroll bar?

For a group project, I successfully centered a div on a webpage. However, I ran into an issue where the website's contents are centered taking the scroll bar width into consideration. This means that the web page contents adjust to be centered without ...

Center the ASP menu horizontally on the page

I'm having trouble centering the menu bar horizontally. The number of main menu items can change with each user login. Currently, the menu bar is starting around 30% from the left. I have attempted using display: inline-block and display: table for #m ...

Pictures acting erratic following the implementation of two setTimeout functions

I encountered an issue while developing a pong game using html/css/javascript. Everything was going smoothly until I introduced a second setTimeout() function. Here is the snippet of my html code: <!DOCTYPE html> <html> <head> <scrip ...

What is the best way to implement an if-else structure in this code?

Can anyone help me with converting this code into an if/else statement? I want it to display certain content if 'Rental' is true, and other content if it's false. The PHP tags are making it difficult for me to follow. <?php if( get_ ...

I'm looking to develop a straightforward panorama application for Windows Phone 7 utilizing PhoneGap/Cordova – any tips on where to start

After searching extensively, I couldn't find any examples of PhoneGap/Cordova-based HTML5 apps for Windows Phone 7 that demonstrate how to create a panorama or pivot style app. These are key features of the OS UI that I want to incorporate into my app ...

What is the sequence in which jQuery events are triggered?

In my HTML code, I have the following: <a href="#" class="topHierarchy big-link" data-reveal-id="myModal" > Both classes, "topHierarchy" and "big-link", are used to attach jQuery event handlers (onClick). big-link triggers a popup box to show, whi ...

Highlighting table rows when hovering in IE with JQuery - compatibility across all versions

I have a table on my jsp page with multiple rows and columns. I want to ensure that visitors can easily follow along when they interact with the table - by highlighting the row or column they hover over with a different background color. Although the **hov ...

Achieve perfect alignment both vertically and horizontally within a container-fluid using Bootstrap

As the heading suggests, I'm having trouble centering content inside a column within .container-fluid in Bootstrap 4. Below is the code I'm using, but I can't figure out what's wrong. Can someone please assist me? <link href="htt ...

variety of heights in the product grid for an eclectic look

Hey there! I'm currently experimenting with a simple product grid for testing purposes only. It's not a final design, just trying to work out some kinks. One issue I'm facing is with the heights of the elements. When text spans over two line ...

Is there a way to extract a variable's value from the URL and distribute it to all links across the website, excluding those on the homepage?

Recently, I was attempting to achieve something similar to this concept how to retrieve variable values from the URL and pass them to all links on a website? However, I encountered an issue where simply visiting the homepage or any other page without the ...

Style your div with CSS to include a border around the content

As a CSS beginner, I am attempting to create a div element with a border, similar to the design shown in the image below: https://i.sstatic.net/fGy2u.png Here's my HTML code: <div class="mystyle"> <h3>header message<h3> <p& ...

There seems to be an issue with iterating through table rows and carrying out jQuery AJAX calls

I have been attempting to run the following code: var table = document.getElementById("tab"); for (var i = 0, row; row = table.rows[i]; i++) { //rows can be accessed using the "row" variable assigned in the for loop ...