Ways to assign dynamic widths to inner divs within a parent div automatically

I am working with divs

<div id='top' >
    <div id='top-border' > </div>
    <div id='top-menu' > <jdoc:include type="modules" name="top-menu" style="well" /></div>
</div>

and adjusting styles

#top {
    width:100%;
    height:40px;
    overflow: hidden;
}
#top-border {
    background: url(../images/border.png) repeat-x scroll 0px 18px;
    position: relative;
    /*width:74%;*/
    height:40px;
    float:left;
    overflow: hidden;
}
#top-menu {
    margin:0;
    float:right;
    height:40px;
    padding-top:6px;
}

My challenge is making the content in top-menu adjust its width automatically based on its content. The remaining width should be allocated to top-border, which has a background image. Although setting a fixed width for top-border works well, I require a flexible width instead. Any suggestions or ideas?

Answer №1

One alternative method is to utilize a table with a width of 100% instead of an outer div. By replacing the top-border and top-menu with td elements, you can achieve the desired effect. To style the top-border td, use the CSS properties "width: 1px; white-space: nowrap;"

Answer №2

In order to show a div with just a background image, you need to define a width for the element or add content to it.

Therefore, to display the #top-border div, you must either include some content in it or specify a width.

Answer №3

To achieve the desired layout, you can utilize the CSS Property word-wrap in conjunction with the width attribute.

For a demonstration of the solution, please refer to this link.

HTML Code:

<div id="top">
    <div id="top-border">abc</div>
    <div id="op-menu"> <jdoc:include type="modules" name="top-menu" style="well" />def</div>
</div>

CSS Code:

#top {
    width:100%;
    height:40px;
    overflow: hidden;
}
#top-border {
    background: url(../images/border.png) repeat-x scroll 0px 18px red;
    position: relative;
    /*width:74%;*/
    height:40px;
    float:left;
    overflow: hidden;
    width: 20px;
    word-wrap: break-word;
}
#top-menu {
    margin:0;
    float:right;
    height:40px;
    padding-top:6px;
}

I trust this information proves beneficial.

EDIT

If a dynamic width is required, consider this solution.

HTML Code:

<div id="top">
    <div id="top-border">The Quick Brown Fox jumps Over The Lazy Dog. The Quick Brown Fox jumps Over The Lazy Dog. The Quick Brown Fox jumps Over The Lazy Dog. The Quick Brown Fox jumps Over The Lazy Dog. The Quick Brown Fox jumps Over The Lazy Dog. </div>
    <div id="op-menu"> <jdoc:include type="modules" name="top-menu" style="well" />def</div>
</div>

CSS Code:

#top {
    width:100%;
    height:40px;
    overflow: hidden;
}
#top-border {
    background: url(../images/border.png) repeat-x scroll 0px 18px red;
    position: relative;
    /*width:74%;*/
    height:40px;
    float:left;
    overflow: hidden;
    width: 97%;
    word-wrap: break-word;
}
#top-menu {
    margin:0;
    float:right;
    height:40px;
    padding-top:6px;
}

This updated solution should address any concerns.

EDIT - 2

Take a look at this alternative approach, which adapts the box size based on its content rather than preset dimensions.

HTML Code:

<div id="top">
    <div id="top-border">The Quick Brown Fox jumps Over The Lazy Dog. The Quick Brown Fox jumps Over The Lazy Dog. The Quick Brown Fox jumps Over The Lazy Dog. The Quick Brown Fox jumps Over The Lazy Dog. The Quick Brown Fox jumps Over The Lazy Dog. </div>
    <div id="top-menu"> <jdoc:include type="modules" name="top-menu" style="well" />def</div>
</div>

CSS Code:

#top {
    width:100%;
    height:40px;
    overflow: hidden;
    display:table-row;
}
#top-border {
    background: url(../images/border.png) repeat-x scroll 0px 18px red;
    position: relative;
    /*width:74%;*/
    height:40px;

    display:table-cell;
}
#top-menu {
    margin:0;
    height:40px;
    padding-top:6px;
    background:green;
    display:table-cell;
}

Hopefully, this revised approach meets your needs.

Answer №4

It appears that the top-border div is unnecessary and should not be used for purely presentational purposes. Instead, consider adding a top border to the top-menu div or adjusting padding-top and background image styles. Floating the top-menu may cause it to only be as wide as its content, similar to using display: inline-block; or display: table.

With regards to your question, here is a simple solution without using an image (although you can incorporate one): http://codepen.io/pageaffairs/pen/ECFny

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8>

<style media="all">
body {margin: 0; padding: 0;}

#top {
    width:100%;
    overflow: hidden;
    background: #f7f7f7;
}

#top ul {
    margin:0;
    list-style: none;
    float:right;
    position: relative;
    padding: 0;
}

#top li {
    margin: 0 0 0 20px;
    float: left;
}

#top li a {
    display: block;
    line-height: 40px;
}

#top ul:before {
    content:" ";
    height: 0;
    width:999em;
    right:100%;
    border-top: 2px dashed #000;
    position:absolute;
    top:19px;
}

</style>

</head>
<body>

<div id='top'>
    <ul> 
        <li><a href="">Link 1</a></li>
        <li><a href="">Link 1</a></li>
        <li><a href="">Link 1</a></li>
    </ul>
</div>

</body>
</html>

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

Newbie inquiries regarding the process of migrating a CRUD application to PhalconPHP

I have recently delved into using PhalconPHP 1.3.1 for an application related to my master's thesis. While it is still a work in progress, I am currently focusing on implementing CRUD functionality and refining the user interface. This endeavor marks ...

How can I design an avatar image within a button similar to Facebook's style?

I'm currently working on a project that involves adding an avatar and a dropdown menu for account settings to my navigation bar. I've already created the dropdown, but I'm having trouble styling the avatar within the button. The button is ta ...

I am experiencing issues with my JavaScript not functioning properly in conjunction with my HTML and CSS. I am uncertain about the root cause of the problem (The console is displaying an error message:

I am facing challenges in creating a content slider and encountering issues with its functionality. Specifically, when testing locally, I noticed that the current-slide fades out and back in upon clicking the arrows left or right, but the slide content is ...

Gone Without a Trace: Where Did the WP Admin

I am brand new to creating WordPress themes and I have just started working on my very first one. I have managed to get some content up and running, and the home page is functioning properly. However, when I log into WordPress and view the site, I noticed ...

Can JavaScript be used to continuously monitor a window variable object in real-time?

Is there a way to dynamically control a variable in JavaScript? I'm currently working on a code that needs to display a button when it reaches the last signature of an automatic request process. The code for activating/deactivating the button is show ...

How to position one element relative to another using CSS

I need to strategically place four div elements in relation to another element. I have a rectangular div, and my goal is to insert four div elements at each of its corners. While I am aware of the CSS attribute "position: relative", it only allows me to ...

The page background appears to be incomplete in its coloring

As I work on creating a web application for personal use, my goal is to ensure it is mobile-friendly. However, I've encountered an issue where the background of my language page isn't displaying in full blue color as intended. Despite trying diff ...

What is the best way to conceal a sticky footer using another element?

I have a footer that remains fixed at the bottom of both long and short pages. Below is the CSS code for this footer: .footer{ position: absolute; bottom: 0; width: 100%; height: 100px; background-color: white; } My goal is to achieve a 'r ...

Submit data from one form to another form located within an iframe

I am currently using a JX Browser that allows content to be displayed in an iframe. My goal is to automatically transfer the username and password of a user logging into my ticketing software to another form within an iframe. The page within the iframe is ...

Having trouble with SVG background image not displaying properly and positioning correctly?

I need help fitting an svg as a background into an element that is 80% of the screen width. Here is the desired final result: The svg effect only works correctly when I reduce the width, but then it breaks in two. I want the svg to appear at 80% width a ...

Expanding Gridview Width within a Container in ASP.Net: A Step-by-Step Guide

https://i.stack.imgur.com/ZaJE7.jpg After viewing the image above, I am facing a challenge with stretching and fixing a gridview to contain the entire div where it is placed. The issue arises when the gridview adjusts automatically based on the content&ap ...

Rendering images in Next.js version 10

Just recently, Next.js version 10 was launched featuring the latest Image element, making it a great asset for websites that heavily rely on images! When I receive an HTML response from the server, it looks something like this: HTML = "<div> &l ...

Styling a Fixed Header in CSS/HTML - Scroll Effect

Is it possible to hide only the upper part of the nav-bar, similar to the behavior in WhatsApp? I am using material-ui for this particular use-case. Currently, my implementation causes the app-bar to extend only when the scroll position is less than 48px, ...

Overlapping Image Arrows with CSS Styling

After receiving a design with what appeared to be a simple feature from a designer, I discovered that implementing it was more complex than expected. Specifically, I needed to create a CSS3 arrow with an image as the fill color in order to overlap the unde ...

When a div is covered by an if statement

One of the challenges I am facing is managing a view with multiple projects, some of which are active while others are not. In my function, if a project's deadline has passed, it displays '0' days left on the view, indicating that these are ...

AngularJS's support for html5mode on GitHub pages is now available

Is it feasible for GitHub pages to accommodate AngularJS in html5mode? I came across a source online suggesting that it can be done with a fallback page for 404 errors. However, this solution seems flawed as it may result in multiple 404 errors, which wou ...

Dependencies in Scala.js for CSS styling

I am currently having an issue implementing Scala.js with the bootstrap library. Including the js file was a simple and straightforward process: jsDependencies +="org.webjars" % "bootstrap" % "3.3.7-1" / "bootstrap.js" minified "bootstrap.min.js" However ...

JS Issue with Countdown functionality in Internet Explorer and Safari

I am having an issue with a JavaScript countdown not working on Internet Explorer and Safari, despite being tested on Windows 7. It works fine on Chrome and Firefox. I am unable to switch to a jQuery countdown due to certain restrictions on the website, so ...

Change the class of an element only within the div that is directly below it

Trying to achieve: Visit this link In my navigation menu, I have two folders with subpages under them. The CSS for displaying the page titles within the folder is as follows: .main-nav .subnav ul {display:none; transition: all 100ms ease-in-out; } .mai ...

Creating a CSS grid with uniform row heights, each adjusting independently

I'm currently working on creating a CSS grid for a product display. My goal is to have rows with equal heights, but each row should adjust its height based on the tallest column within that specific row. Right now I have all rows set to be the same he ...