Adjust the width of child elements based on their content in order to fill the entire width of the container

I have a navigation menu consisting of 9 items. I want each item to have a width that adjusts to the content plus padding, so they can fill the full width of the parent element. Additionally, I need it to be responsive at specific widths. How can I achieve this?

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

Answer №1

If you want to customize the layout, try adjusting the flex-grow property within each div based on the length of the text in the corresponding nav item.

nav {
  display: flex;
  flex-direction: row;
  align-items: stretch;
}

nav div {
  background-color: grey;
  color: white;
  margin: 2px;
  padding-top: 5px;
  padding-bottom: 5px;
  text-align: center;
}

#div1 {flex-grow: 7;}
#div2 {flex-grow: 5;}
#div3 {flex-grow: 9;}
#div4 {flex-grow: 6;}
#div5 {flex-grow: 4;}
<nav>
  <div id="div1">Hello</div>
  <div id="div2">World</div>
  <div id="div3">Lorem Ipsum</div>
  <div id="div4">Dolor Sit</div>
  <div id="div5">Amet</div>
</nav>

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

Issues arise with HTML and CSS when users begin to scroll the page

Could really use some assistance here! I created this website for a friend, but the color of the menu bar mysteriously disappears when I scroll to the right. Any suggestions or ideas on how to fix this issue would be greatly appreciated. The site in quest ...

Ensure the dark mode switch CSS remains consistent when navigating to different pages

I have been working on a code to toggle between dark and light mode on a webpage. While it works fine within the current page, the issue arises when I navigate to another page or refresh the current one - the mode resets back to default (light). How can I ...

Exploring the potentials of REST services using the .Net 2.0 framework in conjunction with integrating REST consumption in JavaScript

According to Wikipedia, REST is defined as REST is considered to be the architectural style of the World Wide Web. It was developed alongside the HTTP/1.1 protocol, while building upon the design of HTTP/1.0 The practices of REST have been in existence ...

Does JSX have an API that can handle self-closing tags, such as <br>, if they are not closed properly?

For my latest project, I've been working on a ReactJS component using JSX and it looks something like this: <script type="text/jsx"> var HelloWorld = React.createClass({ render: function() { return ( < ...

Accept only requests from my Chrome extension

I have successfully set up a NodeJS server with Express on DigitalOcean. My Chrome extension is able to make GET calls to the server without any issues. However, I am looking to enhance the security of the middleware below: // Add headers app.use(function ...

What is the best way to divide widgets in a Wordpress Sidebar?

I am looking to customize the spacing between widgets in my Wordpress sidebar, similar to what is shown on this example site. Currently, my sidebar does not have the desired spacing. I have tried various CSS codes found online, but none of them seem to be ...

Utilizing event.data in the onBeforeInput handler in React without encountering TypeScript errors

When an event is received by the onBeforeInput handler, it is typed as React.FormEvent<HTMLInputElement>. This type is quite general and does not include the data property. However, from what I understand, the events received by onBeforeInput (with ...

Creating a new application to manage and oversee my mathematics classes

As a mathematician with experience in programming, I run a successful (Math) YouTube channel and offer paid math courses. I am looking to create a web application or customize an existing template that will enable me to manage access to my classes (priva ...

Tips for making markers act responsively

Recently, I put together a collection of rain radar images that can quickly show whether rain is on the way or not. As you resize the URL window, the two pictures positioned side by side will also shrink. For an illustration, check it out here ... Each p ...

Clicking on the icon image that features a Background Video will trigger the video to play in a popup window originating from the same location

A background video is currently playing on the site. When the play icon is clicked, the video should start playing in a popup. The video should start playing continuously from the exact moment and position where it is clicked. Check out the example here. ...

Adjust the dimensions of an image post-creation

let displayedImage = Ti.UI.createImageView({ image : somefile.png, height:'100', width:'100' }); The code above shows how I created the image. But now, I am wondering how I can resize the image after its initial creation. ...

Retrieve information from a PHP file and populate it into a textarea using AJAX

In my file named Getsocks.php, I have the following code: <? session_start(); require("../includes/config.php"); $getsocks = mysqli_query($cl,"SELECT * FROM getsocks"); while($socks = mysqli_fetch_array($getsocks)) { echo $s ...

Error message: "Uncaught TypeError: Cannot set property 'state' of undefined in ReactJS"

I am encountering an issue while trying to update the state within my React function. The error message I receive indicates: "Cannot read property 'setState' of undefined." Reviewed below is my code where I have initialized the state in the cons ...

"Is there a way to fix the issue where Express (node.js) res.send is only sending

Encountering unusual behavior with res.send() in the express framework. Below is the function causing the issue: user.register(function(err, result) { console.log(JSON.stringify(err || result)); res.send(JSON.stringify(err || result)); }); The f ...

Avoid changing the size of the button based on the number of words it contains

How can I prevent a button from resizing based on the number of characters inside it? I have noticed that two buttons with the same CSS class are displaying different sizes due to the amount of text inside them. https://i.sstatic.net/BggFW.png https://i. ...

Tips for inserting a logo in the center of a QR code using Node.js

I'm currently working on designing a logo and attempting to incorporate it into the center of a QR code. While I've been successful in generating the QR code, I'm facing challenges in getting the logo to appear in the middle. I've tried ...

Having trouble uploading a file to Laravel using the jQuery .post method?

Here is a snippet of my HTML code: <input type="file" class="form-control" name="file-name" id="file-id"> This is the JavaScript function that is meant to send the file to an API: var file = $('input#file-id').val(); $.post('my/url/ ...

Exciting HTML slot machine game without Javascript functionality

Currently engrossed in the development of an HTML/JavaScript slot machine game. Although confident about the functionality from a mathematical perspective - essentially transplanted it from my Python version and adjusted the format for JavaScript - regrett ...

What could be causing the absence of the input elements on my webpage?

Finally Got It to Work! After troubleshooting, I managed to fix the code on the first page and successfully link it to this second page. Despite using the same CSS and similar HTML, I couldn't figure out why the buttons were not displaying at all. Im ...

Having trouble with loading JavaScript during ng build --prod process

The JavaScript file I'm using for multiple emails (multiple_emails.js plugin) works well with ng serve. Here is my code: (function( $ ){ $.fn.multiple_emails = function(options) { // Default options var defaults = { ...