Rely on the content to establish the width, rather than the parent element

My elements include the following:

.nav {
  width: 100%;
  background: red;
  display: flex;
}

.parent {
  background: blue;
  flex-grow: 1
}

.child {
  background: yellow;
}
<div class="nav.">
  <div class="parent">
    <div class="child">
      Logo
    </div>
  </div>
</div>

I am striving for the Logo to be tinted in Yellow while still displaying the parent's color, which is blue.

This essentially implies that I do not want the .child element to take on the full width of its parent, but rather to adapt its own width based on its content.

By adapting to its content, I mean avoiding assigning it a fixed width like 100px or 200px; instead, I prefer it to have a flexible width.

How can this CSS layout goal be achieved?

Answer №1

If you set your child element to display: inline-block, it will only take up as much space as its content (plus any padding).

.nav {
  width: 100%;
  background: red;
  display: flex;
}

.parent {
  background: blue;
  flex-grow: 1
}

.child {
   display:inline-block;
  background: yellow;
}
<div class="nav.">
  <div class="parent">
    <div class="child">
      Logo
    </div>
  </div>
</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

Angular.js does not trigger validation for input[type='date'] if only a part of the date is entered (such as just the day)

There is an input[type='date'] with no additional validation rules. The issue arises when only a portion of the date is entered (for example, just the day and month). In this case, if you enter '01/02/YYYY', it will be recognized as an ...

Alignment of link items within a Bootstrap 4 hamburger menu

Is there a way to display the menu items below instead of to the left? Check out this jsfiddle for more details. Additionally, I am looking to keep the hamburger icon in place and prevent it from moving when clicked. Here is a GIF showing the current beh ...

Error: Unable to locate or read the file "style.scss" for import

I'm currently in the process of setting up a new vuejs project from scratch, but I've encountered an error when trying to import "style.scss" into my component. Specifically, the issue arises in the Login.vue file where I include the style.scss. ...

Are there any tools available for converting Arabic HTML to PDF for free?

Converting an HTML page in Arabic to a PDF seems to be a challenge with itextsharp. Below is an example of HTML content with Arabic text: <div> <table border="1" width="500px"> <tr> <td colspan="2"> ...

Problem with CSS/SASS dropdown navigation bar

Having trouble with a design issue, I have successfully recreated the Twitter navigation bar and now I want a dropdown menu to appear when hovering over the 'settings' text. However, I'm facing difficulties as the overflow is hidden in the n ...

Issues with responsive design

Frontend mentor projects have been a tricky challenge for me lately, especially when it comes to serving images of different viewport sizes on desktop. I've tried using the picture element and specifying at what width the image should be served, but f ...

Regular expression for selecting characters and numbers in jQuery using a selector

Can someone help me figure out how to select all IDs like this: r1 r2 r3 etc. and not the other IDs like helloWorld I attempted using the CSS selector with jQuery but I'm having trouble understanding how. I tried $('#r[0-9]') but it didn& ...

Tips for capturing the interaction between a jQuery Dialog and its Parent

Within the parent HTML, there is a call that triggers a JavaScript function. <div class="data"> <form:input title="Building" styleClass="content contractorDisable" maxlength="5" size="6" path="fireImpairForm.bldCode" />&nbsp; <a hre ...

As I scroll, the background color of my body vanishes

Seeking assistance with a CSS issue. I have set the height property of my body to 100%, which works well when all content is visible on screen. However, when I need to scroll due to window size changes, the body color disappears and only the HTML backgro ...

Enhance your HTML rendering with Vue.js directives

Check out this cool code example I created. It's a simple tabs system built using Vue.js. Every tab pulls its content from an array like this: var tabs = [ { title: "Pictures", content: "Pictures content" }, { title: "Music", c ...

When including external links, the domain is always placed at the beginning of the link, even if the link

Issue: Hyperlinks are displaying our domain name before the actual link. The text stored in our database is as follows: To learn more about Rich Habits <a href=”http://www.externaldomain.com”>click here.</a> When we echo this text in our ...

Drop-down menu for every individual cell within the tabular data

I'm working with a large HTML table that looks like this: <table> <tr> <td>value1</td> <td>value2</td> </tr> <tr> <td>value3</td> <td>value4 ...

What is the best way to optimize Bootstrap 5 grids for mobile responsiveness?

Within my portfolio, I have a projects section that switches between having the description on the left with an image on the right and vice versa. Despite my efforts to make it responsive, I'm struggling to figure out how to ensure the image appears a ...

What is the best way to adjust the background when the keyboard disappears?

I am currently developing an Android application using PhoneGap. I have encountered an issue where, after entering text in a textbox and hiding the keyboard, there is a delay in adjusting the background screen. When the keyboard is hidden, a white area app ...

Substitute the information in the table with new data

I am working with an HTML table that has 5 columns, one of which contains dates. I need to convert the date format only if the data is not empty. To achieve this, I am utilizing moment.js for date formatting. While the date format conversion works perfect ...

What is the best way to choose the third td element inside a tr tag in an HTML table?

In my table shown below <table> <tr> <td> <table> <tr> <td>Name 1</td> <td>Red</td> ...

Opacity for ghost images in HTML5 drag-and-drop interface

Is there a way to make the ghost image for draggable elements in Chrome render on a transparent background, similar to how Firefox does it? I'm seeing that in Chrome (Chromium 45), the ghost image is displayed on a white background. For Example: http ...

Dynamic Bootstrap sizing

Currently, I am dealing with a wrapper width of 1045px and considering using the "Twitter Bootstrap" framework. However, I am facing some confusion regarding the grid layout in Twitter Bootstrap. As I understand it, the default responsive wrapper width in ...

Following a successful ajax response, the functionality of the JavaScript ceases to operate

Once an Ajax request is successfully sent, the jquery.hover() function no longer works with the new content on the page. Is there a method to refresh the script without reloading the entire div through another ajax request, in order to make it functional ...

The JSON output containing a <br /> tag (escaped) was not properly interpreted by the browser

I encountered an issue with a JSON file that looks like this: [ { "titel": "Das \"Hexenbödele\" bei Lengstein", "vorspann": "Sage vom Ritten, übertragen von P. Beda Weber. Im Wald oberhalb von Lengstein und von diesem Ort nicht weit ...