The max-width attribute is ineffective for descendants of the flexbox item

When setting display:Flex to the .container element and placing two children within, I attempted to give a max-width to the second flex-Item child (.box), but it seems to not be working as expected.

body {
  font-family: sans-serif
}

.container {
  display: -webkit-box;
  display: -moz-box;
  display: -webkit-flex;
  display: -moz-flex;
  display: flex;
}

.par {
  position: relative;
  margin-left: 7px
}

.round {
  height: 17px;
  width: 17px;
  background: #cacaca;
  border-radius: 50%
}

.box {
  padding: 10px 16px;
  box-shadow: 0 3px 15px 0px rgba(0, 0, 0, 0.2);
  background: #fff;
  position: absolute;
  font-size: 14px;
  color: #333;
  max-width: 200px;
  left: -11px;
  top: 28px;
}

.box::after {
  content: "";
  position: absolute;
  height: 10px;
  width: 10px;
  background: #fff;
  box-shadow: 2px 2px 5px -1px rgba(0, 0, 0, 0.17);
  transform: rotate(-137deg);
  top: -5px;
  left: 16px;
}
<div class="container">
  <div>Title</div>
  <div class="par">
    <div class="round"></div>
    <div class="box">This is a Paragraph Text</div>
  </div>
</div>

Link to Js Fiddle

Answer №1

The width of a child with position: absolute is determined by its parent with position: relative.

In this scenario, the width of .box is influenced by .par, which is too narrow to accommodate the word "Paragraph". As a result, the width of .box is determined by the longest word in its content.

It's not possible to set a dynamic width for .box without exceeding 200px and maintaining the current width of .par. There are two potential solutions to this issue:

  1. Set .par's width to 200px
  2. Assign a fixed width of 200px to .box

Answer №2

To ensure proper functionality, it is advised to eliminate the position: relative property from the .par class, as well as remove the left and top properties from the .box class. Instead, consider applying styles to the .box class such as margin: 8px 0 0 -11px;. This adjustment will enable the max-width attribute to function accurately.

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

Having trouble with ng-click not correctly updating values within ng-repeat

Here is the code snippet: <div ng-repeat="data in products"> <div class=edit ng-click="dataUI.showEdit = true; content = data;"> </div> <div ng-repeat="renew in data.renewed"> <div class=edit ng-click="dataUI ...

What is a solution to prevent style.css from being recognized as the Jekyll Page?

Currently, I am utilizing an expression {% assign mypages = site.pages | sort: "order" %} {% for page in mypages %} {% unless page.exclude %} <a href="{{page.url|absolute_url}}"> {{ page.shortname }} <span class="rate">{% include indexmod.h ...

What is the best way to retrieve the current CSS width of a Vue component within a flexbox layout grid after it has been modified?

There's something about this Vue lifecycle that has me scratching my head. Let me simplify it as best I can. I've got a custom button component whose size is controlled by a flex grid container setup like this: <template> < ...

Convert JSON data into an HTML table with custom styling

If you have a set of JSON data that you want to convert into an HTML table, you can use the following code: $.each(data, function(key, val) { var tr=$('<tr></tr>'); $.each(val, function(k, v){ $('<td>' ...

Error: The @use directive must come before any other rules in Angular

Error message: Issue: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): Error Details: HookWebpackError: Module build failed (from ./node_modules/sass-loader/dist/cjs.js) ...

Ways to deactivate webfonts specifically for Safari

Recently, I have been experiencing font crashing issues with the webfont on Safari. Is there a way to disable this font specifically for this browser? ...

Storing form data in temporary local storage using a JSON object array within a PhoneGap project

I am currently developing a data acquisition system using PhoneGap and facing an issue with storing form data temporarily on local storage using JSON. The data should remain visible even after closing and reopening the application by pressing the Get Data ...

What is the process for accessing the data attributes of div elements and then transferring them to a cookie?

Although this question may have been answered before, I couldn't find a specific solution. Imagine I have the following div elements... <div class="listings-area"> <div itemtype="http://schema.org/Product" itemscope=""> <a class="lis ...

Is it possible to create a mouse-following effect with lighting using Javascript

Recently, I've been honing my Javascript skills and decided to create a follow-mouse function. After successfully implementing it, I started brainstorming a new concept that I'm unsure is achievable. Is there a way to develop an "orb of vision" ...

What is the best way to keep fixed input at the bottom of the page?

Let me begin by stating that I struggled to come up with a more appropriate name for this question than the one above. My issue is not limited to a fixed input; it's much more complex. Let me delve into the details of my problem here. The Scenario I ...

What is the best way to redirect in PHP depending on the operating system?

Looking for a simple QR code script implementation that redirects users based on their operating system. Here's what I need: User visits URL Script detects the user's operating system (Android, iOS, other) Redirects Android users t ...

Change the background color on hover without affecting the text color

I am having some trouble with my code. I can only change the color of the "popup1_btn" button when hovering, but the text color remains the same. If I use .popup1_btn a:hover, it only changes the background and text color of the text, not the entire button ...

disable hover effect

Kindly visit the link below and click on 'Story': Upon mouseover, the animation can be observed: How can I remove this effect? The CSS I am using for this is as follows: .storypageThumb1 { float:left; width:175px; height:91px; ...

The width of my root container does not display at a full 100% in mobile dimensions

After creating a simple React.js project, I set up the folder structure using `npx create-react-app`. Additionally, I added some styling with `* { margin: 0; padding: 0; box-sizing: border-box }`, and specified background colors for the body and #root elem ...

Why is it that consolidating all my jQuery plugins into one file is ineffective?

Prior to this, I included the following scripts: <script type="text/javascript" src="{{MEDIA_URL}}js/plugins/json2.js"></script> <script type="text/javascript" src="{{MEDIA_URL}}js/plugins/jquery-msdropdown/js/jquery.dd.js"></script&g ...

Ensuring my website doesn't load within an iframe using PHP

I attempted to prevent my site from loading in other locations, but unfortunately, my efforts were unsuccessful even after using the following code. <?php header(X-Frame-Options: allow-from https://example.com); header(X-Frame-Options: deny); ?> Is ...

Eliminating a line break that appears after incorporating a list into a paragraph

Currently, I am in the process of developing a website where I need to input information. To display this information, I decided to use a list within a paragraph. However, I noticed that after the list name, the text moves to another line which is not wh ...

methods for extracting header information using JavaScript

Is there a way to retrieve values from the header URL using JavaScript? Consider this scenario: www.example.com/content.html?param=value How can one extract this information upon redirecting to the page content.html? What techniques could be employed f ...

The outcome of data binding is the creation of a connected data object

I am attempting to link the rows' data to my view. Here is the backend code I am using: [Route("GetCarCount")] [HttpGet] public async Task<long> Count() { return await _context.Cars.CountAsync(); } ...

An easy way to create a hover effect on a URL link and dynamically change the

Is there a way to achieve the following? When hovering over various links, the image on the right changes. If no hover occurs, the default image will be displayed. https://i.stack.imgur.com/QXPXt.png ...