Expanding child elements to fit parent container using CSS

Currently, I am attempting to make list items stretch across a list.

The following code is relevant for this issue:

#navbar ul
{   
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 100%;
}

#navbar li
{
    display: inline;
    float: left;
    width: 33.33%;
}

This is how it normally appears:

However, there are instances when after leaving the page and returning later (without reloading), the layout distorts as shown below: Adjusting the item width to 33.3% leaves one pixel short, while changing it to 33.333% exacerbates the problem...

Answer №1

You can easily create this design using CSS tables instead, which are widely supported and maintain semantic integrity.

#navbar ul {   
    width: 100%;
    display: table;
    table-layout: fixed; /* ensures all cells have equal width */
}
#navbar li {
    display: table-cell;
}

Check out the code on JSFiddle here!

Answer №2

eliminate padding from the container of "ul"

Answer №3

Just wing it:

#navbar ul li{
   width:33%;
}

#navbar ul li:last-child{
   width:34%;
}

Don't forget to add this style as well:

* { box-sizing: border-box } 

source:

Answer №4

Suggestion:

@Miro, have you considered giving CSS Flexbox layout a try? It can be really helpful for your needs, although it's important to note that it only works in modern browsers.

Exploring CSS Flexbox

CSS Flexible Box Layout Model, known as "flexbox," is a key feature of the CSS3 specification.

It allows for efficient element arrangement on a webpage, ensuring consistent display across various screen sizes and devices
. In many cases, flexbox offers advantages over the traditional block model
by eliminating float usage and avoiding margin collapsing between container and content elements
.

Here's an example implementation:

HTML Structure

<div class="box">
  <div class="A">A</div>
  <div class="B">B</div>
  <div class="C">C</div>
</div>

Style Sheet

html, body{
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
}
.box {
    display: flex;
    flex-flow: row nowrap;
    justify-content: center;
    align-content: center;
    align-items: flex-start;
}

.box div.A {
    order:1;
    flex: 1 1 auto;
    align-self: auto;
    min-width: 0;
    min-height: auto;
}

.box div.B {
    order:2;
    flex: 1 1 auto;
    align-self: auto;
    min-width: 0;
    min-height: auto;
}

.box div.C {
    order:2;
    flex: 1 1 auto;
    align-self: auto;
    min-width: 0;
    min-height: auto;
}

Check out the live demo

This resource link might also provide additional help.

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

CSS for toggling elements visibility

I have been implementing a hide-show feature using JavaScript However, I am encountering an error in the console for the onclick function I'm unsure why this error is happening. PHP CODE <?php $page = '0'; $output.='<div id= ...

Steer clear of using HTML tags in search keywords for indexing

When it comes to indexing keywords in an HTML document, I want to exclude the HTML tags from being indexed. Take this for instance: <div> <!-- this is html code --> <span>You are welcome</span> <!-- simple message for searchi ...

What is the best way to transform the data received from this function into a DataTable that can be used with the Google Charts API?

Is there a way to easily convert the data returned by this function into a format that Google Charts API can read? public function ajax_get_dates(){ $data = ([['date1' => '04/08/2016'], ['date2' => '05/08/2016& ...

Even though I have the image button's ID, I am unable to successfully click on it

I am facing an issue where I can't seem to click on an image button even though I know its ID: driver.findElement(By.id("its Id")).click() Unfortunately, I cannot provide the website link as it is not a public website. However, I have pasted the HTM ...

Swap out the div block with a new one

I am currently facing an issue with my JavaScript code. The code is supposed to remove block1 and replace it with block2 when an onclick function is triggered. function buyerclick() { div = document.getElementById('block2'); div.style.displa ...

Provide an identifier for the anchor tag

I need help passing an id that I am parsing from the url to an anchor tag. The code below shows how I am fetching the id from the url. For example, if the id is 123, then the anchor tag should be constructed like this- <a href="some_url&userId=123 ...

Is there a way to make the first Image Element within the div automatically clickable?

Is there a way to set the first image in a div as the default clicked element? I have a div with multiple images and I want the first one to be clicked by default. This is part of a React functional component that serves as a view. <div className=&quo ...

Issue with displaying dropdown menu icon in Bootstrap 5

Currently utilizing Angular 14 alongside Bootstrap 5. The code snippet below showcases a simple Bootstrap dropdown menu: <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" id=&quo ...

The layout of HTML emails appears to be malfunctioning on Outlook

Having trouble with my HTML email format not displaying properly in Outlook. The code includes Angular and Bootstrap CSS. I've even added inline CSS specifically for Outlook, but the table stretches to full screen when viewed in Outlook. I've tr ...

I am experiencing issues with the functionality of front-page.php on my WordPress website

Seeking guidance in addressing a web development issue: I have encountered a problem with a website that I am currently working on locally using WordPress. I added the following code to the header.php file: <link rel="stylesheet" type="text/css" href= ...

Why won't my picture track the movement of the cursor?

I am trying to make my image follow the cursor using a specific code, but it doesn't seem to be working. I'm having trouble figuring out what's wrong with it. Here is the code snippet that I'm currently using: function followIt(e) ...

Utilizing Modifier Keys in jQuery for Form Submission

Imagine having a form structured like this: [ Animal name input field ] Add button Currently, when a name is entered and the enter key is pressed, a new animal with that name is added to a table. Now, I want to introduce a new feature called "slow add" ...

Tips for rearranging sibling divs while maintaining the order of their child elements

Is there a way to shuffle the order of div classes shuffledv, while maintaining the same order of id's each time the page is refreshed? <div class="shuffledv"> <div id="2"></div> <div id="3"></div> <div id="1">< ...

Tips for transferring data to a different webpage while ensuring that dynamic elements remain active

Is There a Solution to Making Dynamic Elements Persist After Page Load and Transfer Data to the Next Page? I'm struggling with adding a new row that includes both a checkbox and textbox. I've been using the Clone() method to generate a new row, ...

Changing the color of a progress bar in Bootstrap based on a specific percentage level

var elem = document.getElementById("progress-bar"); var progress = 75; elem.style.width = 80 + '%'; if (progress > 80) { elem.style.background = "red"; } #myProgress { height: 5px; } #progress-bar { w ...

Changing the Displayed Content with a Simple Click of a Link

Layout Overview In the process of developing a tool to streamline work tasks, I want to ensure that I am following best practices. My goal is for the website to initially display only column A, with subsequent columns generated upon clicking links in the ...

The horizontal overflow in CSS is ineffective

I am currently working on a project where I need to place multiple divs (represented by black blocks) within a container (depicted as a blue block). The challenge is to fit in as many divs as possible within the container, which has a variable width and di ...

Tips for resizing an image to fit within a col-sm-12 column in Bootstrap

I have a dilemma with two columns - I want to place my image in the first column so that the second column can move down. see image here Here is my HTML code: <div class="container-fluid"> <div class="row"> ...

Incorporating a custom object into two dropdown menus using JavaScript

As a novice in the world of JavaScript, I am attempting to dynamically add values from a text box to two select boxes. Upon entering a value (node name), my goal is to include that name in both select boxes srcNodes and targetNodes. However, I'm enco ...

Tips for selecting specific regions on an Angular SVG map

For my Angular TypeScript project, I included a map. Does anyone know how to create a select region on the map? Click here for StackBlitz Here is the jsFiddle code link CSS styles here p { font-size: 12px; } #core { fill: #ff4f81; animatio ...