Why does font-size increase an input's width - html

I'm wondering why setting the font-size CSS attribute on an HTML input automatically increases its width?
I understand why it increases the height, but I'd like an input to remain the same base width when changing the font-size.
The reason I ask, is because it is breaking a flex layout I'm building, in which there is an input. When I increase the font-size, the input completely breaks out of the layout.
Here's a (react) reproduction:
<div
style={{
backgroundColor: 'blue',
display: 'flex',
flexDirection: 'column',
maxWidth: 400,
padding: 20,
alignItems: 'center',
}}
>
<div style={{ display: 'flex' }}>
<span>Some text</span>
<input style={{ 'font-size': 20 }} />{' '}
</div>
<div style={{ display: 'flex' }}>
<span>Some text</span>
<input style={{ 'font-size': 50 }} />
</div>
</div>
Is there a clean way to solve this?

<input> tag is one of the replaced elements and they often have intrinsic dimensions.
In flexbox you can override the intrinsic size by adding:
input {
min-width: 0;
}
Or, give it a smaller size than the default value 20:
<input size="1">
Then set the desire size by using flex / flex-grow / flex-basis, or width as needed.

You can use 100% width or a fixed width on the inputs. Also to get this working in IE, you need to remove alignItems: 'center' from the outer most div
<div
style={{
display: 'flex',
flexDirection: 'column',
maxWidth: 400,
padding: 20,
}}
>
<div style={{ display: 'flex' }}>
<span>Some text</span>
<input style={{ 'font-size': 20, width: "100%", boxSizing: "border-box" }} />
</div>
<div style={{ display: 'flex' }}>
<span>Some text</span>
<input style={{ 'font-size': 50, width: "100%", boxSizing: "border-box" }} />
</div>
</div>
For firefox you have to wrap the input in a container and apply flex: 1 1 auto; to the container.
Hope that helps

Related

Overflow restricts width of children

I have a div element that I set to have a particular width, say 600px. In that div, I have a row of elements that I would like to each have a width greater than the width of that parent div. These elements are encased in a container div to hold the elements of that row as such:
<div className="App">
<div style={{ overflow: "scroll" }}>
<div style={{ display: "flex", flexDirection: "row" }}>
<div style={{ width: "600px" }}>Test 1</div>
<div style={{ width: "600px" }}>Test 2</div>
<div style={{ width: "600px" }}>Test 3</div>
<div style={{ width: "600px" }}>Test 4</div>
</div>
</div>
</div>
However, this just constrains the width of the "Test" divs so that all four divs fit on the screen. I've tried setting the width of the row container div, and this allows the width of the children elements to expand to 600px.
I imagine this has something to do with flexbox, but I'm lost there and would like to know how I can implement the scroll function without restricting the width of the divs.
I figured it out. All I needed to do was set the min-width of the children to the desired width.
Just change your width style into min-with.
Example below:
<div className="App">
<div style={{ overflow: "scroll" }}>
<div style={{ display: "flex", flexDirection: "row" }}>
<div style={{ minWidth: "600px" }}>Test 1</div>
<div style={{ minWidth: "600px" }}>Test 2</div>
<div style={{ minWidth: "600px" }}>Test 3</div>
<div style={{ minWidth: "600px" }}>Test 4</div>
</div>
</div>
</div>

DIV shows behind Google Maps Div rather than under it

I have a react component that renders a google map as follows:
<div style={{ textAlign: "center" }}>
<h3>This is header text.</h3>
<div>
<LocationHeatMap
center={{ lat: 43.589, lng: -79.644 }}
zoom={11}
positions={data}
/>
</div>
<div style={{ textAlign: "center" }}>
This is some more text. PROBLEM IS WITH THIS.
</div>
</div>
The LocationHeatMap container returns the following
<div className="map-container">
<Map
google={this.props.google}
style={{width: '100%', height: '80%', position: 'relative'}}
className={"map"}
zoom={this.props.zoom}
initialCenter={this.props.center}
onReady={this.handleMapReady}
>
<HeatMap
gradient={gradient}
positions={this.props.positions}
opacity={1}
radius={10}
maxIntensity={50}
/>
</Map>
</div>
But when it renders the problem text appears behind the map div, rather than under it. I have tried position: relative on both the map div, the text div, tried virtually every display and block settings in the browser, but none of them do it. The only thing that works is if I add a top class in css for the text div, but that has its own problems in responsive design. And I am also pretty sure that the top is being applied relative to the header text and not the google map. What can I do to make the problem text appear relatively under the google map.
I know from past experience you can run into issues with a maps div by not using an explicit height. I have included an adjustment below. Hope it helps :)
<div style={{ textAlign: "center" }}>
<h3>This is header text.</h3>
<div class="row">
<div className="map-container row">
<div class="map" google={this.props.google} style={{width: '100%', height: '80%', position: 'relative'}} className={"map"} zoom={this.props.zoom} initialCenter={this.props.center} onReady={this.handleMapReady}>
<HeatMap gradient={gradient} positions={this.props.positions} opacity={1} radius={10} maxIntensity={50} />
</div>
</div>
</div>
<div class="row" style={{ textAlign: "center" }}>
This is some more text. PROBLEM IS WITH TH IS.
</div>
</div>
<style>
.row {
width: 100%;
background-color: lightblue;
}
.map {
width: 100%;
min-height: 200px;
background-color: yellow;
}
</style>
The other thing is making sure the divs are full width:
You can check out an example here:
CodePen Example

Displaying nested divs on the same line

I am working with a react component that produces nested divs. I'd like to style force the divs to be on the same line but can only declare styles on the outer container div. Is there a way to achieve this without styling the inner divs? If only I can pass style to the "hello" div.
Currently:
hello
secondLine thirdLine
Desired:
hello secondLine thirdLine
<div style={{ textAlign: "left", whiteSpace: "nowrap" }}>
<div style={{ display: "inline" }}>
<div>hello</div>
</div>
{` `}
<div style={{ display: "inline" }}> secondDiv </div>
<div style={{ display: "inline" }}> thirdDiv </div>
</div>
make the outer div display: 'flex'
<div style={{ textAlign: "left", whiteSpace: "nowrap",display:"flex" }}>
<div style={{ display: "inline" }}>
<div>hello</div>
</div>
{ }
<div style={{ display: "inline" }}> secondDiv </div>
<div style={{ display: "inline" }}> thirdDiv </div>
</div>
For more reference :details
if you have access and can change the HTML change the inner <div> to <span> or any inline element, the parent div have display:inline and inner div will take full width ignoring parent having inline display, because the inner div have default display block:
<div style="textAlign:left, whiteSpace:nowrap">
<div style="display:inline">
<span style="display:inline">hello</span>
</div>
<div style="display:inline"> secondDiv </div>
<div style="display:inline"> thirdDiv </div>
</div>
for your jsx code it should be like:
<div style={{ textAlign: "left", whiteSpace: "nowrap" }}>
<div style={{ display: "inline" }}>
<span>hello</span>
</div>
{` `}
<div style={{ display: "inline" }}> secondDiv </div>
<div style={{ display: "inline" }}> thirdDiv </div>
</div>

div alignment is not coming properly

I want - box to come exactly below + box as shown in the attached image
I have tried this code given below but does not work. Can someone tell me what changes I need to make in the css. This is part of reactjs application.
<div style={{width:"20%",float:"left"}}>
<input type="text" style={{width:"50px",height:"60px",marginLeft:"20px",marginTop:"20px",float:"left"}}/>
<div style={{fontSize:"25px",fontWeight:"bold", width:"30px",float:"left",border:"1px solid #eee", marginTop:"20px",marginLeft:"5px",textAlign:"center"}}><a href="#" style={{textDecoration:"none"}}>+</a></div>
<div style={{fontSize:"25px",fontWeight:"bold", width:"30px",float:"left",border:"1px solid #eee", marginTop:"20px",marginLeft:"5px",textAlign:"center"}}><a href="#" style={{textDecoration:"none"}}>-</a></div>
</div>
Wrap your +/- boxes in a div and use display: flex with flex-direction: column styles on the wrapper div like
<div style={{width:"20%",float:"left"}}>
<input type="text" style={{width:"50px",height:"60px",marginLeft:"20px",marginTop:"20px",float:"left"}}/>
<div style={{display: 'flex', flexDirection: 'column' }}>
<div style={{fontSize:"25px",fontWeight:"bold", width:"30px",float:"left",border:"1px solid #eee", marginTop:"20px",marginLeft:"5px",textAlign:"center"}}><a href="#" style={{textDecoration:"none"}}>+</a></div>
<div style={{fontSize:"25px",fontWeight:"bold", width:"30px",float:"left",border:"1px solid #eee", marginTop:"20px",marginLeft:"5px",textAlign:"center"}}><a href="#" style={{textDecoration:"none"}}>-</a></div>
</div>
</div>
Wrapp em in div with flex
<div style={{ display: 'flex', flexDirection: 'column' }}>
</div>

How to make images in grid to grow vertically when changing browser width?

Here is the example I am trying to achieve
I want left and right image to always be the same height, and height of the block in the middle should be equal to the height of the left image.
Even more I need images to grow vertically when I change browser width.
I am stuck and not sure how to achieve such result. Instead of images I could utilize div elements with the background.
Specifically I am not sure how to make image heights of the inner block to grow in height with the browser width, and I do not want to use JavaScript to achieve such result
In my current efforts I can make all the columns to be the same height, however, when I change browser width there is more space added between columns.
So I was able to achieve meaningful result with the div blocks and background images
const drDefaultStyles = {
container: {
},
header: {
borderBottom: 'solid 1px #dddddd',
color: '#898989',
textAlign:'left',
marginBottom: '20px',
},
header_title: {
fontSize: '14px',
fontStyle: 'italic',
fontFamily: 'Platino',
textTransform: 'capitalize',
margin: 0,
fontWeight: 100,
},
tall: {
height: '250px',
},
half_tall: {
height: '120px',
},
image_container: {
position: 'relative',
width: '100%',
height: '100%',
background: 'rgb(245, 245, 245)',
padding: '10px',
},
image: {
width: '100%',
height: '100%',
backgroundSize: 'cover',
backgroundPosition: 'center center',
backgroundRepeat: 'no-repeat',
},
title: {
position: 'absolute',
display: 'inline-block',
bottom: '25px',
background: 'rgba(245, 245, 245, .85)',
textTransform: 'uppercase',
padding: '7px 15px',
},
}
<section className="container" style={ _s.container }>
<div className="row" style={ _s.header }>
<h2 style={ _s.header_title }>Destination races</h2>
</div>
<div className="row" style={ _s.container }>
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4" style={{ ..._s.tall, paddingLeft: 0, paddingRight: '5px' }}>
<div style={ _s.image_container }>
<div style={{ ..._s.image, backgroundImage: 'url(/images/cities/leadville.jpg)' }}></div>
<span style={ _s.title }>Leadville</span>
</div>
</div>
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4" style={{ padding: 0 }}>
<div className="col-lg-12 col-md-12 col-sm-12 col-xs-12" style={{ ..._s.half_tall, marginBottom: '10px', padding: 0 }}>
<div style={ _s.image_container }>
<div style={{ ..._s.image, backgroundImage: 'url(/images/cities/san-francisco.jpg)' }}></div>
<span style={ _s.title }>San Francisco</span>
</div>
</div>
<div className="col-lg-5 col-md-5 col-sm-5 col-xs-5" style={{ ..._s.half_tall, float: 'left', padding: 0, paddingRight: '5px' }}>
<div style={ _s.image_container }>
<div style={{ ..._s.image, backgroundImage: 'url(/images/cities/miami.jpg)' }}></div>
<span style={ _s.title }>Miami</span>
</div>
</div>
<div className="col-lg-7 col-md-7 col-md-7 col-xs-7" style={{ ..._s.half_tall, float: 'left', padding: 0 }}>
<div style={ _s.image_container }>
<div style={{ ..._s.image, backgroundImage: 'url(/images/cities/chicago.jpg)' }}></div>
<span style={ _s.title }>Chicago</span>
</div>
</div>
</div>
<div className="col-lg-4 col-md-4 col-sm-4 col-xs-4" style={{ ..._s.tall, paddingLeft: '5px', paddingRight: 0 }}>
<div style={ _s.image_container }>
<div style={{ ..._s.image, backgroundImage: 'url(/images/cities/new-york.jpg)' }}></div>
<span style={ _s.title }>New York City</span>
</div>
</div>
</div>
</section>