Adjusting CSS width of parent or child - html

I had a code using a select (but may be anything else) and that (and others) selects may or may not use a css width (style or class). So I had:
<select>...</select>
...
<select style="width: 100px">...</select>
The first select was using the default width to fit the longer option. So far, so good. But now in the real case (with a web components framework), these selects are generated inside a span like this:
<span><select>...</select></span>
...
<span style="width: 100px"><select>...</select></span>
But this results in different layout. In the second case the select does not adjust its size to the parent width.
If I change the component to generate:
<span style="width: 100px"><select style="width: 100%">...</select></span>
It works for the second case but the first case (without a width in the span) it will not have the default size anymore.
How can I adjust the select so it works like the width applied (or not) to the span would had been applied (or not) directly to the select?

Ok, not sure if this is what you are after, but the below, will take the width of the longest option if no width is applied to the span, or will take the width of the span if a width is applied
.span {
display: inline-block;
}
.select {
width: 100%;
max-width: 100%;
}
<strong>No width span</strong><br>
<span class="span">
<select class="select">
<option>a long option</option>
</select>
</span><br><br>
<strong>Span with width</strong><br>
<span class="span" style="width:200px;">
<select class="select">
<option>a long option</option>
</select>
</span>

Related

Shrunk Chrome extension popup

I created a popup for a Chrome extension. This is the popup source code:
<form>
<label for="email">Email</label>
<input type="email" id="email">
<input type="submit">
<p>Hello, world! This is a paragraph. And this is some text.</p>
</form>
This is how it looks:
And this is how it should look:
As you see, the elements aren't in the right position.
Why does it happen?
How can it be prevented?
Why
According to the source of Chromium on 1/3/23, the default minimum width and height is 25px by 25px:
https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ui/views/extensions/extension_popup.h;l=60
// The min/max height of popups.
// The minimum is just a little larger than the size of the button itself.
// The maximum is an arbitrary number and should be smaller than most screens.
static constexpr gfx::Size kMinSize = {25, 25};
static constexpr gfx::Size kMaxSize = {800, 600};
Your content appears to be out of position, because it's trying to fit within that width of 25px, but overflows instead.
Prevention
Therefore, at least one of the parent elements of your content needs to be styled with a width that will fit your content.
Determine the Parent Element to Style
In your case, the parent / container element <form> could be styled.
Choose a Style Approach
There is more than one way to force the parent element's width to be a certain length, percentage, or keyword value:
In-line CSS within HTML <form> tag
<form style="min-width: max-content !important">...</form>
Internal CSS:
<style> form { min-width: max-content !important; } </style>
External CSS:
form { min-width: max-content !important; }
Mobile
For mobile web development, I would recommend to not use height as another user suggested. Even though it's within a popup, please use min-height instead. Otherwise you might have overlapping container elements, like I did until I used min-height.
This is because the body of the popup is not wide enough to fit this. To change this, you can add super simple CSS to extend the width of the popup.
body {
width: 300px;
height: 300px;
}
You can change this to whatever you would like, just as long as it fits the form.
This happens because the default width of the extension popup is very small, and prefers to stay small. It is encouraged that you change it, to fit your content.

Hiding a block element without using display:none with a modified container with 0 height and width?

https://codepen.io/sayaman/pen/gOmRepQ
Hiding a block element without using display:none?
Ok, so I learned you can't hide a parent div without also hiding the children, although in this case I can also hide the + button we see in the above codepen because it's a child of the parent, I would avoid using hide in the situation where there are many components to hide. Is there a way to hide everything without adding the display:hide styling in every remaining elements that aren't hidden by setting the height and width to 0?
<div class="multiselect" id="countries" multiple="multiple" data-target="multi-0">
<div class="title noselect">
<span class="text">Select</span>
<span class="close-icon">×</span>
<span class="expand-icon">&plus;</span>
</div>
<div class="container">
<option value="us">USA</option>
<option value="fr">France</option>
<option value="gr">Greece</option>
<option value="uk">United Kingdom</option>
<option value="ge">Germany</option>
<option value="sp">Spain</option>
<option value="it">Italy</option>
<option value="ch">China</option>
<option value="jp">Japan</option>
</div>
</div>
<div id="lol" type="button">Click Me</div>
<div style="margin-top: 10px;">
You can select multiple values from the dropdown and delete all of them at the same time, by clicking on the 'x' button.
</div>
The above is the html.
#countries {height: 0px; width:0px;}
.title>span, .title, .expand-icon {
height: 0px;
width: 0px;
}
This is the styling I added to get rid of the former dropdown span. I did this to replace it with a div of my choosing so I can prevent the dropdown span of changing appearance like they do in most dropdown plugins. If you have any creative solutions, share them.

View whole select options

I use select element with fixed width. However, when I have option element nested in select, which has quite long text, then, when this option is being selected, it does not get full background-width (I want the background to be 100%) and also the text is hidden.
Here is the example with the last option being hidden.
.x {
width: 200px;
overflow-x: auto;
}
<select class="x" size="4">
<option class="y" selected>xyz</option>
<option class="y" selected>xyz</option>
<option class="y" selected>xyz</option>
<option class="y" selected>xyzxyzxyzxyzxyzxyzxyzxyzxyzxyzxyz</option>
</select>
You can see the full width using a single css property . Please add a class to solve the issue :
.y{
width: fit-content;
}
Using this style, you could see that the background color gets filled to the full width.

How do I make different types of input elements have the same width?

.form-control, .custom-select {
/* center form controls */
display: inline-block;
/* override Bootstrap's 100% width for form controls */
width: auto;
}
<form action="/" method='post'>
<br>
<div class="form-group">
<select name="show_ID" required="required" class="custom-select">
<option selected>Select something</option>
<option value="Something1">Something 1</option>
<option value="Something2">Something 2</option>
</select>
</div>
</div>
<div class="form-group">
<input class="form-control" name="something2" placeholder="Something 2" type="text"/>
</div>
</form>
What's of interest here are the two Bootstrap elements .form-control and .custom-select. The HTML for each select box looks like this, although the option text in each is of different length:
As you can see, the form boxes in the custom-select boxes do not match each other, nor that of the form-control boxes, even though both have the exact same style specs.
I'm guessing what's happening is that the auto works on text boxes, since there is no pre-existing text to size the box off of, but that auto translates to "based on content" on the boxes that do have pre-existing text (i.e. custom-select).
The auto sizing of form-control works well on one HTML page where only form-control is used, but on another page I need to use both form-control and custom-select, and that's where the size becomes an issue. I've tried setting a specific size for custom-select (eg. 65%), but even if I find a way to match the different box sizes (all custom-select boxes with the form-control boxes) on a desktop, on a mobile device they are off again, since auto means something different than, say, 65% on a cell phone.
How can I make all boxes the same size - both on desktop and mobile devices?
Try this: It is working for me.
[Here You Go:][1]
[1]: https://jsfiddle.net/whr4yc9v/1/

html multiple select inside div stretching entire page

I have a buch of multiple select boxes and I wanted to line them up in a line across the page. I thought I could do this with the help of divs, but for some reason I am running into trouble. Each select is being stretched across the entire width of the page even though I specified in the css that I didn't want that. Why is this happening?
Also, I am giving each select a title by just putting text on top of it. Is there a better way to make a title?
HTML
<div class='bold'>
<div id="parameters">
<div class="section">Program<br> <select multiple="multiple" name="program">
<option value="SGS">SGS</option>
</select>
</div>
<div class="section">School <br><select multiple="multiple" name="school">
<option value="FLH">FLH</option>
<option value="MID">MID</option>
<option value="SUN">SUN</option>
<option value="MNC">MNC</option>
</select>
</div>
<div class="section">Term <br><select multiple="multiple" name="term">
<option value="Fall 2011">Fall 2011</option>
<option value="Late Fall 2011">Late Fall 2011</option>
</select>
</div>
<div class="section">Extension<br> <select multiple="multiple" name="ext">
<option>Something...</option>
</select>
</div>
</div>
</div>
CSS
#parameters{width:100%
height:150px;
border-style:solid;
border-width:2px;
border-color:grey;}
.section{width:50px;}
Divs are block elements so will stack on top of each other by default. If you want them to sit next to each other you will need to float them.
.section{
float: left;
}
JSFiddle
You can try:
.section {
width: 50px;
display: inline-block
}
This is an alternative to float to a degree. It may suit your needs better, but it's difficult to say. display: inline-block comes with rendering drawbacks, as does float of course.
I prefer it sometimes - A lot of people jump at float like it's the only choice, but it can be a real pain too.
JSFiddle sample using display rather than float
As you can see, it preserves your border without using a clearfix, whereas float breaks the border.
edit: If you choose to use float, a good method of making the border wrap around the contained elements is to add overflow: auto to #parameters, as shown in the fiddle below:
Float fiddle with clearfix
I changed your html code a bit in order to use labels in the "tittle" of each select tag.
You can view here: http://jsfiddle.net/SmRzL/4/
In regards to how to align, your elements you need to float each section:
.section{
float: left;
padding: 10px;
}
In regards, to adding a title to each section personally I would do something along the lines of this:
<span class="title">Term </span>
.title {
display:block;
font: bold 1em "Arial Narrow", "Franklin Gothic Medium", Arial;
}
See this fiddle for reference: http://jsfiddle.net/8rQXD/