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

Steps for adding hover color to a div with linear gradient border

My current challenge involves adding a hover color to a specific div, but I'm facing obstacles due to the gradient background that was implemented to achieve border-radius functionality. This task is being carried out within a React Component using c ...

What is the best way to send data from a child component to a parent component in Angular 2?

I want to retrieve data from my child component, which contains a form in a popup. How can I pass all the details to the parent component? Here is the TypeScript file for my parent component: import { Component, OnInit } from '@angular/core' ...

I have a vision of creating a unique Countdown Stopwatch that meets all my

I'm looking to create a custom countdown stopwatch where I can set the time and watch it count down to 0:00. The stopwatch should have three buttons: Start, Stop, and Reset. I've searched multiple websites for what I need but haven't found ...

PHP 5's HTML Form Library

Currently in search of an alternative to QuickForm. I have encountered performance problems with QF, specifically when dealing with a large number of options in combobox. I prefer something that is more object-oriented like Zend_Form, but without the exc ...

Ways to turn off the dragging animation for cdkDrag?

I am facing an issue with cdkDrag where a small preview of the item being dragged shows up. I want to disable this feature and remove any CSS classes related to the dragging state. Can anyone guide me on how to achieve this? https://i.sstatic.net/nYU07.pn ...

What is preventing this code from setting the background color of the main content all the way to the bottom of the

I designed this webpage using HTML and CSS. ... #contents { margin: 0px; padding: 0px; width: 100%; background-color: white; color: black; border-top: 3px solid black; background-image: url(img/CloverImage.png); background ...

Utilizing Angular 2 for dynamic CSS binding: manipulating style.width and style.height properties

I am experiencing some unusual rendering issues when using plain HTML5 canvas and CSS binding with A2. Can anyone please explain the difference between the following code snippets: <canvas height="400" width="1200" #background> </canvas> AND ...

Changing padding of a button causes surrounding elements to move?

Trying to make a button in CSS appear "pushed down" on :active, I decided to increase the padding-top by 2px and decrease padding-bottom by 2px. However, this adjustment seemed to affect the margins of other elements for some reason that eludes me. I am c ...

Oops! Smarty encountered a fatal error that wasn't caught

An error occurred while processing the template file "C:\xampp\htdocs\eventos\libs\templates\teste.tpl" on line 9. The error message states: Fatal error: Uncaught exception 'SmartyCompilerException' with message &apo ...

How can a single item be selected from a multi-select drop-down menu using Python Selenium?

In the web page, there is a button labeled [Add Field] which, when clicked, reveals a drop-down menu. The xpath for this drop-down menu is: '//*[@id="adv_options[3][]' The second array item is empty as shown here: '[]'. I have manag ...

Outlook fails to recognize table cell widths when there is an image present

Although this question has been asked previously, the solutions provided did not solve my issue. I am trying to set the width of the <td> element to 70%, but when I insert an image it is affecting the widths of received emails in Outlook (I use MS O ...

What is the best way to make sure images and text are properly aligned for different screen sizes

I'm a beginner with Bootstrap and could use some assistance. In my code, I have text and images divided into four columns with alignment set based on screen size using media queries in CSS. However, when resizing the window to tablet view (check Scre ...

Dynamically Alter Notification Background

I have created a demo page with some code for a smart notification. The issue I am facing is that even though the CSS is initially set to black, I want to dynamically change it to a random color upon creation. When I try to do this in the inspector, it w ...

Is there a way to automatically send users to a different PHP file once they click "submit"?

I need to redirect to a different php file named index2.php. The username and password values are already set as follows: Username=sid, Password=yeaoklogin. When the user clicks on the Login button, they should be redirected to a page called index2.php. ...

Updating the hyperlink of an html element using css

I am looking to update this A tag <a href="contact.html" >Contact</a> to <a href="tel:+13174562564" >Contact</a> using only CSS, if possible. Alternatively, like this <a href="tel:+13174562564" >+13174562564</a> ...

How to trigger the submission of a form within the submission event of another form using jQuery

I am encountering an issue with having two forms on a single page. Whenever I click on the submit button of the parent form, the parent form's submit event is triggered. In this event, I have included logic to submit the second form using the submit m ...

Issue with HTML and CSS Grid alignment coupled with CSS syntax error on RBRACE

I'm in the process of updating my website indexes and decided to incorporate grids. However, when attempting to implement grids using Dreamweaver, I encountered several issues. Firstly, an error message appeared stating "Expected RBRACE at line 72, co ...

Contrast between the structure of asp.net button and hyperlinks

I want to transfer the styles of a hyperlink in regular html to an Asp.net button. However, I encountered a strange background issue that is disrupting the appearance of the buttons. Hyperlink: Asp.net Button How can I remove the outline that appears o ...

creating unique styles for your Ionic app with Angular using JSON

Having trouble changing the item color. Does anyone know why my CSS isn't working, or if I'm missing something? <ion-view title="Add order to a table"> <ion-content class="has-header has-subheader"> <ion-list> ...

The process of binding two variables in the same select element

Here is the code snippet that I am working with: <div class="form-group "> <label for="field_type"><b>Type</b></label> <div class="input-icon right"> <select required class="form-control" ...