Button centered in flex container is too narrow - html

My project uses a 20 row by 20 column css grid layout (5% of screen for each cell). One of the pages has a button. Originally the page was contained within grid columns 5-8 and grid rows 6-9, and there was no problem with the button itself, but I need to center it within the grid area, so I used flexbox for centering within the css grid.
The button text is "Select file to upload" but problem is that the button is now only the width of a single word; I want the button to be as wide as all four words. Before I added a flex container for centering the button was as wide as all four words but now with the flex container it's not.
The flexbox arrangement works perfectly for all other elements on the page and two other pages, but this button has that problem (on Firefox 64). I may need to use a unique flex-item class for it, different from the flex-item class shown below.
Here are the css classes that apply to this button:
.center_text_grid {
display: grid;
grid-column: 5 / 12;
grid-row: 6 / 19;
display: block;
overflow: auto;
align-items: center;
justify-items: center;
}
.flex-item {
display: flex;
width: 70%;
flex-direction: row;
align-items: center;
justify-content: center;
}
.upload-btn-wrapper {
display: inline-block;
text-align:center;
border-radius: 15px;
}
.btn {
border: 1px solid rgb(117,163,126);
background-color: black;
width: 75%;
padding: 8px 20px;
border-radius: 15px;
font-size: 20px;
font-weight: bold;
font-family: CamphorW01-Thin, sans-serif;
font-size: 13pt;
color: rgb(117,163,126);
cursor: pointer;
}
Here is the html for this button:
<div class="center_text_grid flex-item">
<div class="upload-btn-wrapper">
<button class="btn" style="width: 100%">Select file to upload</button>
<input type="file" name="fileToUpload" />
</div></div><br><br>
I know this is not a complete reproducible example but as this is specific to button styling, I hoped it could be answered with the code above.
Thanks for any help.

Here's how I solved this:
<div class="upload-btn-wrapper center_text_grid flex-item">
<button class="btn">Select file to translate</button>
<input type="file" name="fileToUpload" />
</div><br><br>
As posted in my original question, this html code wrapped the button in two divs. I changed to a single div and added the upload-btn-wrapper class to the single div. I also changed width in the .btn class to 65%.
That leaves only one problem to resolve: the btn class has a hover selector, but the button text is not highlighted on hover:
.btn:hover{
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-webkit-box-shadow: 0px 0px 10px 0px rgba(0, 255, 0, 0.67);
-moz-box-shadow: 0px 0px 10px 0px rgba(0, 255, 0, 0.67);
box-shadow: 0px 0px 10px 0px rgba(0, 255, 0, 0.67);
color: rgb(175,222,162);
}
This is a file selection button so it's handled differently from other buttons, but I don't know how to do it yet. I'll post the answer to that one when I have it. If anyone else knows, please let us know.
Thanks to those who answered.

I copied your code to a jsfiddle and have it below. It works fine on Stack Overflow and jsfiddle. It could your browser not supporting a line of code, causing the code to not work properly. I suggest updating your browser, or switching to a different browser.
Hope this helps!
.center_text_grid {
display: grid;
grid-column: 5 / 12;
grid-row: 6 / 19;
display: block;
overflow: auto;
align-items: center;
justify-items: center;
}
.flex-item {
display: flex;
width: 70%;
flex-direction: row;
align-items: center;
justify-content: center;
}
.upload-btn-wrapper {
display: inline-block;
text-align: center;
border-radius: 15px;
}
.btn {
border: 1px solid rgb(117, 163, 126);
background-color: black;
width: 75%;
padding: 8px 20px;
border-radius: 15px;
font-size: 20px;
font-weight: bold;
font-family: CamphorW01-Thin, sans-serif;
font-size: 13pt;
color: rgb(117, 163, 126);
cursor: pointer;
}
<div class="center_text_grid flex-item">
<div class="upload-btn-wrapper">
<button class="btn" style="width: 100%">Select file to upload</button>
<input type="file" name="fileToUpload" />
</div>
</div><br><br>

Remove styles for both button and input
wrap them in a div.
Add the button css styles to the div.
Example button CSS -
.divButton {
background-color: cyan;
border: none;
color: white;
padding: 12px 30px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
}

Related

Make image appear inline with a div within an outer div using css

I am trying to place an image to the right of a pseudo input box using css to imitate Apple's iMessage app. However, the image keeps displaying below the pseudo input box as follows:
Most answers suggest using display:inline-block and I have put this in the class for both the outter and inner div with no luck. (Have also tried float:left, float:right and display:inline-block on image and there is no difference.)
How can I get the arrow to appear to the right of the pseudo input box as the microphone image is below?
Here is my code for the input field:
.inputbox {
border-radius: 20px;
min-height: 30px;
width: 300px;
padding: 8px 15px;
margin-top: 5px;
margin-bottom: 5px;
border: solid 2px #EEE;
display: inline-block;
}
.inputBoxInner {
border-radius: 20px;
min-height: 30px;
width: 240px;
padding: 8px 15px;
margin-top: 5px;
margin-bottom: 5px;
border: solid 2px #EEE;
height:" auto";
display: inline-block;
}
.inputBoxInner:empty:not(:focus):before {
color: lightgrey;
font-family: helvetica;
content: attr(data-placeholder)
display: inline-block;
}
<div class = "inputBox" contentEditable="true"><div class="inputBoxInner" contenteditable="true" data-placeholder="Start typing"></div><input type="image" id="image" alt="Send"
src="/images/arrow.png" width="30" height="30 style="float:right"; "></div>
The code has a problem:
And this code can resolve the problem:
True code:
inputBox {display: grid; grid-template-columns: 100px 100px}
This should do it for you
.inputbox {
border-radius: 20px;
min-height: 30px;
width: 300px;
padding: 8px 15px;
margin-top: 5px;
margin-bottom: 5px;
border: solid 2px rgb(0, 0, 0);
}
.inputBoxInner {
border-radius: 20px;
min-height: 30px;
width: 240px;
padding: 8px 15px;
margin-top: 5px;
margin-bottom: 5px;
border: solid 2px rgb(131, 131, 131);
height: auto;
display: flex;
justify-content: end;
align-content: center;
}
.inputBoxInner:empty:not(:focus):before {
color: rgb(255, 0, 0);
font-family: helvetica;
}
<body>
<div class = "inputBox" contentEditable="true">
<div class="inputBoxInner" contenteditable="true" data-placeholder="Start typing">
<input type="image" id="image" alt="Send" src="image.png" width="30" height="30" style="float:right">
</div>
</div>
</body>

Flex Container Taking Width of Element

I'm putting a label and an input inside a div that is a flex container and flex-direction is set to column. The container naturally takes the width of the input and not the label. I'm trying to figure out why that is and if there is a way I can get the container to take the width of the label (or what the width of the label should be)
"Relationship to Patient" is the label/input in question.
What I want (on my my extra large screen):
What I'm getting (on my large screen):
HTML (using React and Nextjs):
<div className={styles.inputContainer}>
<label className={styles.label}>Relation to Patient</label>
<select className="form-field">
<option>Self</option>
<option>Child</option>
<option>Spouse</option>
<option>Parent</option>
<option>Other</option>
</select>
</div>
CSS:
:root {
--primary: #50657a;
--secondary: #7a5050;
--tertiary: #797a50;
--lightGray: #bababa;
--darkGray: #4d4d4d;
--extraLightGray: #ededed;
}
.inputContainer {
display: flex;
flex-direction: column;
justify-content: flex-end;
/* width: 200px */
height: 60px;
margin: 0 .5rem;
}
.label {
color: var(--secondary);
}
.formRow {
width: 100%;
display: flex;
margin: .5rem 0;
/* justify-content: space-around; */
}
.form-field {
border: 1px solid #bababa;
line-height: 1.5rem;
height: 2rem;
padding: 5px;
border-radius: 5px;
}
.form-field:focus {
outline: 0px;
-webkit-box-shadow: 0px 0px 3px 3px rgba(80,101,122,.5);
-moz-box-shadow: 0px 0px 3px 3px rgba(80,101,122,.5);
box-shadow: 0px 0px 3px 3px rgba(80,101,122,.25);
}
Parent element:
<div className={styles.formRow} style={{ marginTop: '0' }}>
/* A bunch of other code */
<div className={styles.inputContainer}>
<label className={styles.label}>Relation to Patient</label>
<select className="form-field">
<option>Self</option>
<option>Child</option>
<option>Spouse</option>
<option>Parent</option>
<option>Other</option>
</select>
</div>
</div>
So, reminder, I'm trying to figure out why the flex container chooses the width of the input and not the label and also how I can get the desired results.
I very much appreciate any/all help and answers!!

Can I make the hover background color of a container, larger than its parent container

Basically I am trying to achieve the hover effect as seen in the image below, but I'm not sure on how to make the background color exceed the borders of its parent container without effecting the height of the parent. I've attached a Codepen as well, thank you in advance!
What I want to replicate
/* =============
Base
============= */
#aspo-container {
background-color: #F3F5FF;
padding: 1.5em;
display: flex;
justify-content: center;
}
#aspo-container p {
font-size: rem-calc(20);
font-family: 'Barlow', sans-serif;
}
#aspo-container #aspo-inner-container {
display: flex;
flex-direction: row;
justify-content: space-between;
border: 3px solid #171838;
border-radius: 5px;
width: 50%;
}
#aspo-container #aspo-inner-container div:hover {
background-color: #171838;
color: #FFFFFF;
font-weight: 700;
}
#aspo-container #aspo-inner-container #activities {
text-align: center;
width: 100%;
border-right: 1.5px solid #171838;
cursor: pointer;
}
#aspo-container #aspo-inner-container #staffing {
text-align: center;
width: 100%;
border-left: 1.5px solid #171838;
border-right: 1.5px solid #171838;
cursor: pointer;
}
#aspo-container #aspo-inner-container #participation {
text-align: center;
width: 100%;
border-left: 1.5px solid #171838;
border-right: 1.5px solid #171838;
cursor: pointer;
}
#aspo-container #aspo-inner-container #outcomes {
text-align: center;
width: 100%;
border-left: 1.5px solid #171838;
cursor: pointer;
}
<main>
<section id="aspo-container">
<div id="aspo-inner-container">
<div id="activities" class="active">
<p>Activities</p>
</div>
<div id="staffing">
<p>Staffing</p>
</div>
<div id="participation">
<p>Participation</p>
</div>
<div id="outcomes">
<p>Outcomes</p>
</div>
</div>
</section>
</main>
This could be done with padding-top and a negative margin-top.
Note that I haven't tried very hard to make the example below look very nice, or look like your image, because I didn't see the point – presumably you have the code for most of it already. The snippet is just to demonstrate the method.
Hover over the text to see the effect.
nav {
height: 30px;
width: 340px;
border: 2px solid black;
padding-top: 10px;
font-family: sans-serif;
}
.nav-option {
display: inline-block;
height: 40px;
margin: 6px;
/* These are the important lines */
padding-top: 20px;
margin-top: -30px;
}
.nav-option:hover {
background-color: blue;
color: white;
}
<nav>
<div class="nav-option">Activities</div>
<div class="nav-option">Staffing</div>
<div class="nav-option">Participation</div>
<div class="nav-option">Outcomes</div>
</nav>
I would suggest using transform property of css with scale function to make it bigger on hover e.g transform:scale(1.5) ;
You can adjust number in scale to make it fit whichever size is needed.
I think this is close to what you want. I also added an animated transition, but as-is it's a bit buggy in my browser (leaves behind artifacts). The overall effect is achieved by increasing the padding the same as the negative margin.
A few notes:
Generally you wouldn't set widths on flex children if you want them to be fluid. flex-grow works better.
p elements aren't the best choice for menu items, both semantically and practically. Menu elements aren't usually paragraphs of text, and the default styling for p tags has extras you may wish to reset. So it's easier to use div or span, or the more semantic ul/li tags (although you would have to reset some of that styling also)
Changing font-weight on hover is problematic for fluid width elements, as it changes the element width, which causes jitter.
For this use case, you don't actually want any space-between the menu items, as you expect them to all be right next to each other.
It is a good idea to have the container padding be big enough to contain the hovered elements.
/* =============
Base
============= */
#aspo-container {
background-color: #F3F5FF;
padding: 1.5em;
justify-content: center;
}
#aspo-container #aspo-inner-container {
display: flex;
flex-direction: row;
border: 3px solid #171838;
border-radius: 5px;
}
#aspo-container #aspo-inner-container>div:hover {
background-color: #171838;
color: #FFFFFF;
margin: -0.5em;
padding: 1em;
border-radius: 5px;
}
#aspo-container #aspo-inner-container>div {
text-align: center;
flex-grow: 1;
border-right: 1.5px solid #171838;
border-left: 1.5px solid #171838;
cursor: pointer;
padding: .5em;
font-size: rem-calc(20);
font-family: 'Barlow', sans-serif;
transition: all linear 0.25s;
}
#aspo-container #aspo-inner-container>div:first-child {
border-left: none;
}
#aspo-container #aspo-inner-container>div:last-child {
border-right: none;
}
<main>
<section id="aspo-container">
<div id="aspo-inner-container">
<div id="activities" class="active">
Activities
</div>
<div id="staffing">
Staffing
</div>
<div id="participation">
Participation
</div>
<div id="outcomes">
Outcomes
</div>
</div>
</section>
</main>

CSS :hover selector not working on button

I'm working on an internet layout using CSS grid, and one of my grid areas is a navbar. I have tried to make buttons inside the navbar using divs. I have tried to use the :hover selector on the div to change the mouse to pointer, change the bg color, etc, but it isn't working.
When I force hover in chrome using dev tools it acts as if there were no code for hover on the button at all. I have tried to use the hover on the navbar itself, and it works but the strange thing is that it causes the hover to function correctly on the button as well. So, if I apply the same hover rules to the navbar, I can hover over the navbar or over the button and they both work.
For my assignment I have to use SCSS, so the code below is the compiled CSS code. Can the SCSS cause problems like this?
.myGrid {
display: -ms-grid;
display: grid;
grid-template-areas: "nav nav nav nav nav" background-color: white;
width: 85%;
margin: auto;
margin-top: 0px;
}
.myGrid section {
text-align: center;
-webkit-box-shadow: 0px 15px 20px black;
box-shadow: 0px 15px 20px black;
overflow: hidden;
}
#myNavMenu {
background-color: brown;
-ms-grid-row: 3;
-ms-grid-column: 1;
-ms-grid-column-span: 5;
grid-area: nav;
height: 100px;
margin-top: 20px;
border-top-left-radius: 10px;
border-top-right-radius: 10px;
-webkit-box-shadow: 0px 0px 20px black;
box-shadow: 0px 0px 20px black;
}
.myButton {
background-color: white;
width: 100px;
height: 30px;
padding: 5px;
margin: auto;
border-radius: 8px;
z-index: 2;
}
.myButton :hover {
cursor: pointer;
}
<main class="myGrid">
<section class="navMenu" id="myNavMenu">
<h3>Nav</h3>
<div class="myButton" id="button1"> MyButton </div>
</section>
</main>
the space between .myButton and :hover causes all the mess.
.myButton :hover {
cursor: pointer;
}
Here is a working version https://jsfiddle.net/m0sc9ugb/

How to center text inside anchor tag

I want my anchor tag look like a button, and created this style JsFiddle
.details-button {
background: linear-gradient(to bottom, #FFFFFF 0, #FAB149 2%, #F89406 100%) repeat scroll 0 0 transparent;
border: 1px solid #FAB149;
-ms-border-radius: 3px;
border-radius: 3px;
-webkit-box-shadow: 0 1px 2px #999999;
-ms-box-shadow: 0 1px 2px #999999;
box-shadow: 0 1px 2px #999999;
color: #FFFFFF;
cursor: pointer;
font-family:"Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 13px;
font-weight: bold;
-ms-text-shadow: 0 -1px #099FDF;
text-shadow: 0 -1px #099FDF;
margin: 4px;
height: 28px;
width: 85px;
vertical-align: central;
align-items: center;
text-decoration: none;
font: menu;
display: inline-block;
/*padding: 6px 0 5px 18px;*/
}
It looks as I want, but how to center text horizontally and vertically inside anchor tag?
Add
text-align: center;
line-height: 28px;
working demo: http://jsfiddle.net/3SE8L/2/
line-height and padding work if the height of the element is hard-coded, but I like to use
{
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
}
to keep text centered in a tags where the height may change.
To align your text in the center horizontally and in the middle vertically, try this:
CSS: Example Fiddle
.details-button{
//your existing styles
//height: 28px; <-- remove this entry
text-align: center;
padding: 6px 0;
}
First, remove this style declaration; it is invalid markup:
vertical-align: central;
Next, add this to center your text horizontally:
text-align: center;
Finally, make your height auto and instead of a line-height declaration, set the padding to taste:
height:auto;
padding:3px 0;
That should do it! Be sure to remove the invalid declaration as it can cause your CSS to break in some browsers.
This works:
{
display: grid;
justify-content: center;
}
Simply text-align: center; in your existing class.
So simple, just add  
text-align: center;
 to your targeted class.