How do I keep my input fields on one line while centering the DIV that contains them?
Asked Answered
F

10

8

I gave this CSS class to some input fields

.searchField {
    display: inline-block;
}

This is their underlying HTML ...

<div id="searchForm">
Search For Results<br> 
<form id="search-form" action="/races/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
    <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
    <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
    <input type="text" name="my_object" id="my_object" placeholder="Event" size="50" class="searchField">
    <input alt="Search" type="image" src="/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
</form> </div>

However, despite the fact that there is enough horizontal screen real estate, one of them keeps wrapping to teh next line, as this Fiddle illustrates -- https://jsfiddle.net/3mwn14fk/ . How do I keep these items on one line (assuming there is enough browser width)? Note I also want to keep the DIV they are within vertically and horizontally centered.

Edit: This is what I see in the Fiddle. This is on Firefox. Note the text fields are not on one line.

What I see in my Fiddle

Edit 2

Per Monica's Fiddle, this is what I see. Note that the first naem and last name are on one line, but the event text box is on the next line. I would like all three to be on the same line, even if the black box containing them has to expand

What I see from Monica's Fiddle

Fastening answered 30/8, 2016 at 19:47 Comment(11)
works fine for meSmew
what browser are you on? I've added an image into my question of what I see. I tried this on Mac Firefox and Mac Chrome.Fastening
looking at your screenshot, there's not enough room for the third input.. it's doing what it shouldSmew
use display: table for #loginArea and display:table-cell for .searchField it works have a look jsfiddle.net/UserIsMonica/3mwn14fk/1Ineffectual
@aw04, what are you talking about? there is tons of horizontal space. Just because there isn't enough horizontal space in teh black area, the black area shoudl expand to accommodate it because there is horizontal space beyond it.Fastening
the horizontal space is being constrained by the styles you're using to center the black area... not the inner elements. so it works, but only at a certain widthSmew
My question does not say anything about the inner area needing to be constrained -- only that it needs to be centered. If you know an answer that keeps that area centered and allows things to stay on one line, then that is the answer I'm looking for.Fastening
i can try and take time later, but the short of it is it will be a lot easier if you make something a constant width. you can try playing around with the left and translate of the black box thoughSmew
@Mike You have set the position of the #loginArea div to be absolute. By the virtue of the position and the transform properties, it occupies only HALF of the available width. And the input boxes are stacking pretty good in whatever space they have available to them. Try commenting these styles to see what I mean: #loginArea { /*position: absolute; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%);*/ }Hoofbound
@Mike, Also note that default size of the inout elements is 20. So the first two inputs have a size 20, while the third one has a specified size of 50 - more than the combined sizes of the first 2 inputs. This means, for the all inputs to stay on one line, the width of the black area should be AT LEAST 4 times the width of the first input. The width of the first input box as shown by Chrome Inspector comes out to be 173 px. So your black box should be at least 4x173= 692. If we account for some padding and margin between elements, then #loginArea should be min 700px wide.Hoofbound
Just add white-space: nowrap; to the #loginArea css rule.Limassol
C
5

Just to give you an alternative you could use flexbox to achieve what you want. Here's a quick demo:

/* Just some basic CSS declarations to start with */

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-width: 100%;
  min-height: 100%;
}

h4 {
  margin: 5px 0; /* Just to make the demo look nicer */
}

#loginArea {
  border-radius: 25px;
  font-family: 'russo_oneregular';
  font-size: 20px;
  padding: 20px;
  background-color: #CCCCCC;
  color: #ffffff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  display: block;
  width: 80%; /* You could use anything here, depending if you have a wrapping parent etc */
}

#search-form {
  position: relative;
  display: flex;
  align-items: center; /* Not necessary but will center the items */
  flex-direction: row; /* Not necessary but tells that they should be in a row */
  justify-content: flex-start; /* Will align items to left on larger screens */
  flex-wrap: wrap; /* Will allow the flex inputs to wrap on smaller screens */
}

#search-form > * { /* You could give a class or element instead of asterix */
  margin: 0 10px 5px 0; /* Just to make it look nices */
  width: 25%; /* Width of text inputs, you could set them different from each other */
  min-width: 100px; /* Min width to wrap elements when small screen */
  flex: 1;
}

#search-form .search_button {
  max-height: 20px;
  flex: 0 0 20px; /* Keep the magnifying class proportioned/20px wide */
  min-width: 0px; /* Do not use the min-width declared above */
  margin: 0 0 5px 0;
}
<div id="loginArea">
  <div id="searchForm">
    <h4>
      Search For Results
    </h4>
    <form id="search-form" action="/races/search" accept-charset="UTF-8" method="get">
      <input name="utf8" type="hidden" value="✓">
      <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
      <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
      <input type="text" name="my_object" id="my_object" placeholder="Event" size="50" class="searchField">
      <input alt="Search" type="image" src="http://image.flaticon.com/icons/png/128/49/49116.png" class="search_button">
    </form>
  </div>
</div>

I set the login area to a width of 80% but you can alter this in any way you want of course. I set also the min-width of the inputs to 100 pixels - except for the last one - so that it will wrap nicely on smaller screens. I also set the max-width for the inputs so they will not grow infinitely and the search forms justify-content: flex-start; will align them to left on bigger screens.

Also the support for flexbox on browsers can be improved with setting prefixes like -webkit- and -ms- etc to the CSS declarations.

JSFiddle to play around with

Browser support for flexbox

Cute answered 2/9, 2016 at 6:43 Comment(12)
Thanks, the only issue I see with this though is that when I expand teh window ( jsfiddle.net/vbegaf5k/1 ), the containing box has all this extra space to the right of the magnifying glass. I only want the box to take up as much real estate as the elements within it and no more.Fastening
Well it's just about the margin and max width of the elements.. I edited the code snippet so that the search input doesn't have the margin anymore and all the other fields will expand as much as the container. Here's even a new JSFiddle for it: jsfiddle.net/thepio/t8c53ayqCute
Thanks for this updated fiddle. I'm noticing as I expand the width of the window the size of the text fields changes. Ideally, I'd like these to just stay at whatever width "size='x'" comes out to.Fastening
Wasn't that how it worked before my changes? The inputs can have the min and max-width properties which will keep them fixed or allow some width change.Cute
Is there any way to do it where you don't specify "width" properties for the inputs and just let their width be whatever the browser renders "size='x'" to be?Fastening
Yes there actually is. It just won't wrap the elements on smaller screens to a new line if they get too small if you don't have min-width. I mean if you remove the widths all together the inputs will be three even inputs (in width). In this example the widths are gone except from the search input. You should either make it a button or something and then use flex only on the inputs. The search input would otherwise leave those spaces around it that you were talking about. jsfiddle.net/thepio/29ym9wh0Cute
This still isn't right, though. If you look at your Fiddle -- jsfiddle.net/29ym9wh0/1 , the magnifyhing glass is going to the next line even though there is enoguh horizontal screen real estate to keep it on that line. I would like the gray container to expand to accommodate everything. I don't want the items in the container to expand. I lack the proper knowledge to be able to explain what I want better.Fastening
Thank you for explaining. Maybe it's just me that don't understand what you want :P Here's yet another fiddle: jsfiddle.net/thepio/1q4h9zq1 . I must say that without setting the min-width to the inputs the browser itself will not render the inputs under a certain width. This is what is causing the search button to wrap to the next line. See the min-width set in the CSS #search-form > *. I set it to 50px now which I think is now almost too little considering the inputs get so small on smaller screens === not good for the user. Hopefully this explanation helps :)Cute
This totally rocks, there's just one more thing. I tried setting the "size" attributes on the inputs from yoru Fiddle -- jsfiddle.net/1q4h9zq1/1 , but the input sizes seem unaffected. Is there a way to allow those size attributes to be effective and keep the other fixes that you've made?Fastening
Well you know, if you set size="10" to an input it will force the browser to have it at set width and it won't work responsively nor otherwise like in my example...If you want it to be responsive you need to forget the inline size. If you wish to have different size inputs you need to look at flex-grow flex-shrink and flex-basis CSS declarations. Or do it like with the search button or set min-width etc. To sum it up I guess you can't do exactly what you want with CSS unless you do some media queries in your CSS.Cute
Since I'm a novice at CSS, I defer to your expertise. But just so I understand you, you're stating its impossible to specify "size"s on my inputs and keep them on the same line, while keeping the parent container centered unless I specify "width" attributes for the inputs?Fastening
Yes, I would say so. I haven't seen anybody at least in the past 5 years using the size attribute anywhere. It's all done with CSS these days.Cute
A
5

Use inline-block for children with white-space: nowrap on parent. It will prevent children from wrapping in next line.

Note: Please see snippet in full page mode.

#loginArea {
  white-space: nowrap;
  border-radius: 25px;
  font-family: 'russo_oneregular';
  font-size: 20px;
  padding: 20px;
  background-color: #000000;
  color: #ffffff;
  display: inline-block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}

.searchField {
  display: inline-block;
  vertical-align: top;
}
<div id="loginArea">
  <div id="searchForm">
    Search For Results<br> 
    <form id="search-form" action="/races/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
      <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
      <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
      <input type="text" name="my_object" id="my_object" placeholder="Event" size="50" class="searchField">
      <input alt="Search" type="image" src="/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
    </form>
  </div>
</div>
Acrogen answered 2/9, 2016 at 6:15 Comment(7)
Thanks but when I compress the screen so that there is not enough real estate, the elements disappear instead of wrapping like they are supposed to -- jsfiddle.net/3mwn14fk/5 .Fastening
@Mike you can use a media query and move white-space: nowrap there from above you wants the inputs not to wrap.Acrogen
What's a media query? Anyway when I remove the "white-space: nowrap;" directive, things wrap even though there is enoguh screen real estate to dispaly them -- jsfiddle.net/3mwn14fk/6 , which takes me back to the original problem I was trying to solve.Fastening
@Mike media query apply styles based on the width of viewport. you can check the fiddle that i posted in above comment. I hope it will solve your problem.Acrogen
Ah I missed your Fiddle. Why do we need the "min-width" directive? Will this break if viewing on a mobile device?Fastening
@Mike As i told media query apply styles based on viewport width. Search on Google for more information. Styles written inside @media (min-width: 850px) {... } will apply only when your browser screen will be of width equal to 850px or more. On screens lower than this resolution these styles will not be applied.Acrogen
Hey I'm noticing something odd about your Fiddle -- when I compress the screen like so -- jsfiddle.net/3mwn14fk/9 , things are wrapping, even though there is still enough horizontal space to display them all in one line. Note that I don't care about the width of the black containing box, it can expand as needed to accommodate all the elements.Fastening
C
5

Just to give you an alternative you could use flexbox to achieve what you want. Here's a quick demo:

/* Just some basic CSS declarations to start with */

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-width: 100%;
  min-height: 100%;
}

h4 {
  margin: 5px 0; /* Just to make the demo look nicer */
}

#loginArea {
  border-radius: 25px;
  font-family: 'russo_oneregular';
  font-size: 20px;
  padding: 20px;
  background-color: #CCCCCC;
  color: #ffffff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
  display: block;
  width: 80%; /* You could use anything here, depending if you have a wrapping parent etc */
}

#search-form {
  position: relative;
  display: flex;
  align-items: center; /* Not necessary but will center the items */
  flex-direction: row; /* Not necessary but tells that they should be in a row */
  justify-content: flex-start; /* Will align items to left on larger screens */
  flex-wrap: wrap; /* Will allow the flex inputs to wrap on smaller screens */
}

#search-form > * { /* You could give a class or element instead of asterix */
  margin: 0 10px 5px 0; /* Just to make it look nices */
  width: 25%; /* Width of text inputs, you could set them different from each other */
  min-width: 100px; /* Min width to wrap elements when small screen */
  flex: 1;
}

#search-form .search_button {
  max-height: 20px;
  flex: 0 0 20px; /* Keep the magnifying class proportioned/20px wide */
  min-width: 0px; /* Do not use the min-width declared above */
  margin: 0 0 5px 0;
}
<div id="loginArea">
  <div id="searchForm">
    <h4>
      Search For Results
    </h4>
    <form id="search-form" action="/races/search" accept-charset="UTF-8" method="get">
      <input name="utf8" type="hidden" value="✓">
      <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
      <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
      <input type="text" name="my_object" id="my_object" placeholder="Event" size="50" class="searchField">
      <input alt="Search" type="image" src="http://image.flaticon.com/icons/png/128/49/49116.png" class="search_button">
    </form>
  </div>
</div>

I set the login area to a width of 80% but you can alter this in any way you want of course. I set also the min-width of the inputs to 100 pixels - except for the last one - so that it will wrap nicely on smaller screens. I also set the max-width for the inputs so they will not grow infinitely and the search forms justify-content: flex-start; will align them to left on bigger screens.

Also the support for flexbox on browsers can be improved with setting prefixes like -webkit- and -ms- etc to the CSS declarations.

JSFiddle to play around with

Browser support for flexbox

Cute answered 2/9, 2016 at 6:43 Comment(12)
Thanks, the only issue I see with this though is that when I expand teh window ( jsfiddle.net/vbegaf5k/1 ), the containing box has all this extra space to the right of the magnifying glass. I only want the box to take up as much real estate as the elements within it and no more.Fastening
Well it's just about the margin and max width of the elements.. I edited the code snippet so that the search input doesn't have the margin anymore and all the other fields will expand as much as the container. Here's even a new JSFiddle for it: jsfiddle.net/thepio/t8c53ayqCute
Thanks for this updated fiddle. I'm noticing as I expand the width of the window the size of the text fields changes. Ideally, I'd like these to just stay at whatever width "size='x'" comes out to.Fastening
Wasn't that how it worked before my changes? The inputs can have the min and max-width properties which will keep them fixed or allow some width change.Cute
Is there any way to do it where you don't specify "width" properties for the inputs and just let their width be whatever the browser renders "size='x'" to be?Fastening
Yes there actually is. It just won't wrap the elements on smaller screens to a new line if they get too small if you don't have min-width. I mean if you remove the widths all together the inputs will be three even inputs (in width). In this example the widths are gone except from the search input. You should either make it a button or something and then use flex only on the inputs. The search input would otherwise leave those spaces around it that you were talking about. jsfiddle.net/thepio/29ym9wh0Cute
This still isn't right, though. If you look at your Fiddle -- jsfiddle.net/29ym9wh0/1 , the magnifyhing glass is going to the next line even though there is enoguh horizontal screen real estate to keep it on that line. I would like the gray container to expand to accommodate everything. I don't want the items in the container to expand. I lack the proper knowledge to be able to explain what I want better.Fastening
Thank you for explaining. Maybe it's just me that don't understand what you want :P Here's yet another fiddle: jsfiddle.net/thepio/1q4h9zq1 . I must say that without setting the min-width to the inputs the browser itself will not render the inputs under a certain width. This is what is causing the search button to wrap to the next line. See the min-width set in the CSS #search-form > *. I set it to 50px now which I think is now almost too little considering the inputs get so small on smaller screens === not good for the user. Hopefully this explanation helps :)Cute
This totally rocks, there's just one more thing. I tried setting the "size" attributes on the inputs from yoru Fiddle -- jsfiddle.net/1q4h9zq1/1 , but the input sizes seem unaffected. Is there a way to allow those size attributes to be effective and keep the other fixes that you've made?Fastening
Well you know, if you set size="10" to an input it will force the browser to have it at set width and it won't work responsively nor otherwise like in my example...If you want it to be responsive you need to forget the inline size. If you wish to have different size inputs you need to look at flex-grow flex-shrink and flex-basis CSS declarations. Or do it like with the search button or set min-width etc. To sum it up I guess you can't do exactly what you want with CSS unless you do some media queries in your CSS.Cute
Since I'm a novice at CSS, I defer to your expertise. But just so I understand you, you're stating its impossible to specify "size"s on my inputs and keep them on the same line, while keeping the parent container centered unless I specify "width" attributes for the inputs?Fastening
Yes, I would say so. I haven't seen anybody at least in the past 5 years using the size attribute anywhere. It's all done with CSS these days.Cute
P
2

There are several ways to achieve this, with varying degrees of success, depending on the method used:

@Muhammad's option of using display: inline-block; on the inputs and forcing the container to ignore text-wrapping with white-space: no-wrap; does work to acheive your goal of forcing everything on one line;

However, it's a limited solution in that your elements will be whatever default size dictated by the browser and because the containers will be over-sized and out of position on some devices/resolutions/window sizes, etc. (ultimately, the user experience wasn't addressed)

The Flex-box CSS implementation is also another way to go, and browser implementation is getting much closer to the point that it'll be the solid option in the future, and yes, it can be fairly-well implemented in many browsers with certain CSS fallback and/or Javascript Shims (perhaps provided by a framework, etc), but assuming you want bullet-proof coverage that just works, use a little math to calculate some percentage widths, use floats, and add a dash of media queries to achieve your objective:

Here's the CSS and notes to do this, below,

and Here's the Fiddle to try it out. (I've set the container color to change with the breakpoints, so try resizing your window.)

/* Control the box-model: (makes assigned widths predictable) */
* { box-sizing: border-box; }

#loginArea {
    border-radius: 25px;
    font-family: 'russo_oneregular';
    font-size: 20px;
    padding: 20px;
    background-color: #000000;
    color: #ffffff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
}

/* Default Styles (at full width)
   Assign calculable sizes to the form fields:
   We have 3 fields (1 double-sized) and a button:
   So, 1+1+2+1 = 5; Dividing 100% container-width by 5 = 100/5 = 20%;
   we need spacing between (think margin-right),
   so, shave each element down 1% and set the margin-right to 1%;
   then specifically set the double-width entry separately (40% = 39% + 1%)
*/ 
#searchForm input {
   float: left;
   width: 19%;
   margin: .25em 1% .25em 0;
}
#searchForm input#my_object {
   width: 39%;
}

/* Applying Media Queries to give friendlier form layouts
   by using alternative field sizes for better user experience
   at different break-point sizes (indicated by varying background color */

/* Mobile Styles */
@media only screen and (max-width : 480px) {
   #loginArea {background-color:red;}

   /*Full-width, no margin, stacked; */
   #searchForm input {
     width: 100%;
     margin-right: 0;
   }
   #searchForm input#my_object {
     width: 100%;
   }

}

/* Phablet (In-between-sized mobile styles)
   -- in this case, same as mobile for consistency)
*/
@media only screen and (min-width : 481px) and (max-width : 767px) {
   #loginArea {background-color:yellow;}

   /*Full-width, no margin, stacked; */
   #searchForm input {
     width: 100%;
     margin-right: 0;
   }
   #searchForm input#my_object {
      width: 100%;
   }
}

/* Tablet Styles */
@media only screen and (min-width : 768px) and (max-width : 1024px) {
   #loginArea {background-color:green;}

   /* Name side-by-side, Event stacked full-width below;
      (Using 1% padding on both left and right, to keep things balanced)
   */
   #searchForm input {
      width: 48%;
      margin: .25em 1%;
   }
   #searchForm input#my_object {
      width: 98%;
   }
}
Precipitancy answered 3/9, 2016 at 2:9 Comment(16)
Also, a couple other things to note: 1. Your use of the size="50" attribute on the Event field (#my_object) causes it to render as 50 characters wide, but this is based on browser & fonts, etc, which may also be part of why your layout was shifting around. 2. inline-block will allow for wrapping if things get too big (including whitespace between elements, which does take up some space). 3. Using Floats can give this same behavior if you're not using specified widths (and box-sizing:border-box for sanity).Precipitancy
Also, If the #LoginArea container seems to collapse down to the wrong size/disappear/other stuff sliding up too far underneath, etc, fix the float with a clearfix technique more on floats and clearfixes herePrecipitancy
I appreciate this but here's the problem, when I expand the width of the screen -- jsfiddle.net/kwrsmebo/9 , even though there is enough space to display all three, they are not lining up on the same line. The inputs seem to be expanding horizontally, which I don't really want. I'm fine with the fact that size="50" renders as 50 characters wide. I would like to avoid fixed widths on these fields.Fastening
You can always add/remove/adjust the break points as needed to control when the elements snap into line.Precipitancy
If you dont like the scaling field sizes, switch to fixed widths and gutters (pixel widths, etc) [and set arbirary login box widths too for various break points] Character widths are not fixed, however, so size=50 should be overwritten with a calculable css size... As i mentioned above, i assumed you wanted a bigger field size for that one, so i doubled it, but you can re-portion all the field sizes however you want, just dont exceed 100% inner width of the container, and it wont wrap.Precipitancy
ultimately, though, you have to watch the math to make sure all element-widths +margins (assuming border-box) dont total up greater than the inner container width of the login box (excluding its padding)Precipitancy
I used the percentage sizes such that 100% is always used up inside the inner space. And yes, sizes will adjust with a resize of the window, but typically, the user is only going to care that the form was usable at the size at which they initially viewed the page without any resizing, whether mobile or desktop, so as long as fields arent squishing or disappearing/doing wonky things, the scaling + sensible-breakpoints method should work in your favor.Precipitancy
In case it needs stating here, the box model, sizing, whitespace and and display methods used can all affect the space calculations affectong wrapping. Border-box makes sizing behave inuitively: setting a width for an element means that from border-edge to border-edge, the width of the element matches the specified width, and margins take up extra. Vs standar box model where specified width = inner width of the element, and padding +border+margin take up extra spacePrecipitancy
display:inline-block and static or relative positioning allow whitespace to take up space 'in the flow' of the document, so anywhere there is whitespace between elements in the source code can cause a single whitespace to render in browser. position:absolute isnt needed here, so your only solid way to deal with eliminating whitespace is via floating the fieldsPrecipitancy
Floats effectively make elements behave as blocks, but not specifying widths on floated elements is a bad practice. Widths should always be specified on floated elementsPrecipitancy
Is there any way to get it so that the containing box that holds teh three input fields expands to accommodate the inputs instead of the inputs themselves expanding ( jsfiddle.net/kwrsmebo/10 ) ?Fastening
Sort of... You cna typically get away with something like that by setting the widths of the inner floated elements, not specifying width of the outer container and then applying a float to the container as well. The problem is, you have to specify a width somewhere... If you dont, the children are arbitrary based on browser defaults, but they dont know where to wrap because the outer container size is based on the inner contebts at that point, so its mkre likely gonna collapse down or do whatevwr the browser feels like.Precipitancy
You gain the most control by setting all the sizes, but at minimum, the container should have width specified. Thats how to control when the inner elements will wrap based on arbitary sizings. Just make sure that number adds up bigger than the width and margins of the inner elementsPrecipitancy
Specifying fixed pixel widths makes assumptions about the browser on which the user is on, assumptions which I don't want to make. I want things to just display nicely in whatever browser width the user is on. I'm surprised its so complicated to keep thigns on one line without specifyhing a width for things.Fastening
Thats why breakpoints really shine... You target the sizes and any other cas behavior you want deal with at specific conditions/browser sizes etcPrecipitancy
And also, thats why i used percentages originally to fill content area in different ways as the viewport size changes, but the various breakpoints are pixel-based based on popular/common device size thresholds etc (that way you can always do the math again for an upper and lower bounds size and see if youre still happy with the outcome, but you know what the min and max numbers are goong to be)Precipitancy
F
2

Update #2:

Image of working example

Is that how you want it?

Check this fiddle: https://jsfiddle.net/3mwn14fk/18/

  • I use the #loginArea kinda like a wrapper. Vertical align it the same way you did earlier. width: 100%;. Instead of horizontal align with transform I use text-align: center;
  • For the text-align: center; to work #searchForm needs to be inline-block. On the #searchForm you can use text-align: left; if you don't want everything in that divto center.
  • Moved styling like background: #000; and some others to the #searchForm.
  • Removed the searchFieldclass, not needed anymore.

A small update:

My original answer is what I consider to be a better way (maybe some small edits should be done to it) to style it. But if you really want to do it the way you did, it's already working. Like this? Just add a width of like 80% (or something that fit your needs) to #loginArea in your fiddle.


Google CSS reset and read about it.

You should read up on media queries: https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries

Use media queries to edit the style for different screen sizes/devices.


Here is how I would've done it with your code

Using your HTML code with some small edits:

<div id="loginArea">
    <div id="searchForm">
        Search For Results<br> 
        <form id="search-form" action="/races/search" accept-charset="UTF-8" method="get">
            <input name="utf8" type="hidden" value="✓">
            <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
            <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
            <input type="text" name="my_object" id="my_object" placeholder="Event" class="searchField2 btcf">
            <input alt="Search" type="image" src="/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
        </form>
    </div>
</div>
  • Removed the size attribute from the event field.
  • Changed the class on the event field to .searchField2.
  • Added one more class to the event field called .btcf (Clear float class).

And here's the CSS:

#loginArea {
    border-radius: 25px;
    font-family: 'russo_oneregular';
    font-size: 20px;
    padding: 20px;
    background-color: #000000;
    box-sizing: border-box;
    color: #ffffff;
    display: inline-block;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
}
.btcf:after {
    content: "";
    display: table;
    clear: both;
}
.searchField, .searchField2 {
    display: block;
    float: left;
    box-sizing: border-box;
    width: 25%;
}
.searchField2 {
    width: 50%;
}
  • Code for #loginArea is the same.
  • .searchField and .searchField2 are identical except for the width.
  • Added the class btcf:after which is the clear float for the floated fields.

The border-box is needed on the fields as the browsers default styles adds borders, padding and maybe some more stuff.

And here's a fiddle: https://jsfiddle.net/3mwn14fk/16/

Farrahfarrand answered 6/9, 2016 at 23:18 Comment(7)
You've removed the "size" elements from the input boxes but I want them to remain. I don't want the input widths to expand, only the DIV taht contains them. I am happy to let the size="x" attributes determine the width of the inputs. Does that make sense? I seem to have a hard time explaining that to folks in this question.Fastening
It's already working the way you want. oi68.tinypic.com/euq8nd.jpg Set the width of #loginArea to like 80% and you will see that it works.Farrahfarrand
Thanks for your expanded answer. This isn't quite it either though. In your screen shot, the black container occupies much more space than just the elements require. At least in my original Fiddle, it is only as wide as the elements within it, whcih is what I want. I don't want it to be wider than it has to be.Fastening
Updated the answer again. Is this how you want it? jsfiddle.net/3mwn14fk/18Farrahfarrand
Almost but you made the parent container (loginArea) 100% wide. Before it was only taking up as much width as its child elements, which is what I wanted. Now it is taking up the entire screen -- jsfiddle.net/3mwn14fk/19 , which I don't want.Fastening
And why does it matter if it's 100% width or the width as it's child elements?Farrahfarrand
That's the way it has to be, 'cause that's the way I like it.Fastening
U
2

#loginArea {
  white-space: nowrap;
  border-radius: 25px;
  font-family: 'russo_oneregular';
  font-size: 20px;
  padding: 20px;
  background-color: #000000;
  color: #ffffff;
  display: inline-block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translateX(-50%) translateY(-50%);
}
#search-form {
    margin: 0px;
    padding: 0px;
}
.search_button,
.searchField {
    display: inline;
}
<div id="loginArea">


	<div id="searchForm">
	Search For Results<br> 
	<form id="search-form" action="/races/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="✓">
		<input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField" size="10">
		<input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField" size="10">
		<input type="text" name="my_object" id="my_object" placeholder="Event" size="30" class="searchField">
		<input alt="Search" type="image" src="/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
</form>	</div>

</div>
Uppercase answered 7/9, 2016 at 16:43 Comment(6)
Even in your example, the "Search" box appears outside the black container box. I want them all to remain within the black container box. Further, you have specified "white-space: nowrap" on the #loginArea element. I still want things to wrap if there is not enough horizontal real estate, and that attribute will prevent wrapping when the screen is too small.Fastening
Hi, I've edited that code. Check again. It's depending on your number of input box you can increase or decrease the size. For small screen we can write responsive css code.Uppercase
Can you make this work by removing the "width" attributes on the ".searchField" and ".searchForm" classes and adding the size elements to the inputs (10 for the first and last name fields, 30 for the event field)? If so then we're done.Fastening
Please run the above code and check. This input size, we can give inside html tag attribute directly ( if it's your own custom html ) or through Jquery also.Uppercase
I did run your above code, check it out here -- jsfiddle.net/3mwn14fk/21 . Notice that things aren't wrapping when the horizontal screen space is small.Fastening
Please check it out again - jsfiddle.net/3mwn14fk/23 . Now this will wrap according to devices width.Uppercase
H
1

You have to specify the width too. Make hese changes to "searchField" class:

.searchField {
    display: inline-block;
    width:33%;
    box-sizing:border-box;
    float:left;
}

Here is the JSfiddle

Hoofbound answered 30/8, 2016 at 20:20 Comment(1)
I don't want every item to be 1/3 of the box. I would like to specify a size attribute on the text fields and then let the width be determined from there.Fastening
I
1

use table and table -cell instead inline-block

  #loginArea {
    border-radius: 25px;
    font-family: 'russo_oneregular';
    font-size: 20px;
    padding: 20px;
    background-color: #000000;
    color: #ffffff;
    display: table;
    position: absolute;
    top: 50%;
   left: 50%;
   transform: translateX(-50%) translateY(-50%);
  }
 .searchField {
     display: table-cell;
     float:left;
     margin :0px 2px 0px; 
 }

it works have a look http://jsfiddle.net/3mwn14fk/4/

Ineffectual answered 30/8, 2016 at 20:23 Comment(3)
Hey I just opened your Fiddle but it appears as it does in my screen shot -- the third input is going to the next line even though there's enough space for it.Fastening
hi Mike i have tried this in my google Chrome and its works fine. can you please try this one jsfiddle.net/3mwn14fk/4Ineffectual
Hi, I've made a second edit including a photo of what I see with your Fiddle. Notice taht the Event text box is on a line underneath the first name and last name line. I would like them all to be on the same line, even if that means expanding the black area containing them.Fastening
E
1

If you want to keep one input box bigger and other two have same width then change the class of the input box for which you want to add the width to "searchField1".

And add the following styles

#loginArea {
border-radius: 25px;
font-family: 'russo_oneregular';
font-size: 20px;
padding: 20px;
background-color: #000000;
color: #ffffff;
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
text-align: center;
}

.searchField {
display: inline-block;
width: calc(50% - 100px);
box-sizing:border-box;
float:left;
}

.searchField1 {
display: inline-block;
width: 200px;
box-sizing:border-box;
float:left;
}

If you want to specify width for all input boxes this will still work but dont forget to add a
tag just before the image input so that it is placed in the next line

 .searchField {
 display: inline-block;
 width: 100px; // add your width here
 box-sizing:border-box;
 float:left;
 }

 .search_button{
 clear:both;
 }

I hope this helps,

Extravagant answered 31/8, 2016 at 19:0 Comment(1)
I don't know how else I can say this -- I don't want to specify any widths. I would like the containers to automatically understand there is enoguh width on the screen to display everything in one line, and if there is not, then it is ok to wrap. But in the Fiddle I have, there is enough width, yet it is still not keeping things in one line.Fastening
B
1

You have to insert a div inside the form so that, all the three text fields should be enclosed within that div tag. Now set the style of that div to:

    display:inline-flex;

make sure this style overwrites the default display:block of your newly added div tag. Your code should look like this:

    <div id="loginArea">
        <div id="searchForm">Search For Results<br> 
            <form id="search-form" action="/races/search" accept-charset="UTF-8" method="get">
                <input name="utf8" type="hidden" value="✓">
                <div style="display:inline-flex">
                    <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
                    <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
                    <input type="text" name="my_object" id="my_object" placeholder="Event" size="50" class="searchField">
                </div>
                <input alt="Search" type="image" src="/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
            </form>
        </div>
    </div>

If you also want to place your search icon in the same line, then you don't have to insert the new div tag. Instead, set the style of the form to:

     display:inline-flex;

Then everything will get aligned in the same line. Hopefully, this will work out.

Bucaramanga answered 2/9, 2016 at 6:7 Comment(3)
Maybe I'm not understanding your solution, but when I apply the CSS style, the elements aren't wrapping when there's not enoguh horizontal screen real estate. See my updated Fiddle with your solution -- jsfiddle.net/3mwn14fk/10 .Fastening
In that case, you can use the responsive design (@media Rule) as mentioned earlier by @Muhammad to make the content wrap according to the screen size.Bucaramanga
That would be great, but that's not working. Look at this fiddle with the media rule -- jsfiddle.net/3mwn14fk/14 . Note the abundance of horizontal space but the elements are wrapping.Fastening
T
1
<div class="main-container">
<div id="searchForm">
   Search For Results<br> 
  <form id="search-form" action="/races/search" accept-charset="UTF-8" method="get">
    <input name="utf8" type="hidden" value="✓">
    <input type="text" name="first_name" id="first_name" placeholder="First Name" class="searchField">
    <input type="text" name="last_name" id="last_name" placeholder="Last Name" class="searchField">
    <input type="text" name="my_object" id="my_object" placeholder="Event" size="50" class="searchField">
    <input alt="Search" type="image" src="/assets/magnifying-glass-0220f37269f90a370c3bb60229240f2a4e15b335cd42e64563ba65e4f22e4.png" class="search_button">
  </form> 
</div>
</div>

Now, you can write Css as,

.main-container {display:block; text-align:center; }
#searchForm { display:inline-block; }
#searchForm form input { float:left; margin-right:5px; width:100px;}
Theirs answered 6/9, 2016 at 9:20 Comment(1)
YOu are hard-coding a width for the #searchForm element, but as I said in my other comments, this is exactly what I want to avoid. I want the containing element to be the wdith of the elements within it. If there is not enough horizontal space to contain those elements, then it is ok for them to wrap.Fastening

© 2022 - 2024 — McMap. All rights reserved.