Make a div the size of its content - html

I'm working on some HTML where some divs are all lined up when the screen is wide, then they stack and float left or right when the screen is small.
They have some space above and below when the screen is wide, but the problem is that top and bottom margin seems to disappear when the screen is thinner and they stack. When I inspect the element in Firefox, it says the outer divs are smaller than the content, which I'm guessing has something to do with the problem.
Here's a jsfiddle of my code. Change the Result window width to see it change.
Here's the code
<div id="controls">
<div id="control_left">
<div id="play_button" class="button">play</div>
<div id="step_button" class="button">step</div>
</div>
<div id="control_right">
<div id="stop_button" class="button">stop</div>
<div id="restart_button" class="button">restart</div>
</div>
<br/>
</div>
<div id="instruction1" class="instruction top_instruction">
<div class="instructionNumber"></div>
<div class="instructionType">
<FORM NAME="myform">
<SELECT NAME="mylist">
<OPTION VALUE="m1">VAL1
<OPTION VALUE="m2">VAL2
<OPTION VALUE="m3">VAL3
</SELECT>
</FORM>
</div>
<input class="reg" type="text" />
<input class="reg" type="text" />
<input class="reg" type="text" />
</div>
#play_button {
float: left;
}
#step_button {
float: right;
}
#stop_button {
float: left;
}
#restart_button {
float: right;
}
.button {
text-align: center;
width: 45%;
background-color: #aaa;
cursor: pointer;
box-sizing: border-box;
}
#controls {
margin-top: 5px;
margin-bottom: 5px;
box-sizing: border-box;
height: auto;
width: auto;
}
#control_left {
box-sizing: border-box;
height: auto;
width: auto;
}
#control_right {
box-sizing: border-box;
height: auto;
width: auto;
}
#media screen and (min-width: 600px) {
#control_left {
float: left;
width: 45%;
}
#control_right {
float: right;
width: 45%;
}
}
.instruction{
width: 100%;
font-family:courier;
font-weight:bold;
margin-right: auto;
margin-left: auto;
border-right: 2px solid #000000;
border-left: 2px solid #000000;
border-bottom: 2px solid #000000;
display: inline-block;
}
.top_instruction{
border-top: 2px solid #000;
}
How do I get space below beneath the controls div, and beneath each button when they stack?

Just give the div a margin.
div {
margin:2%;
}
Demo
more apt will be applying margin for the button class
.button{
margin:2%;
}

add the margin attribute to the button class
.button {
text-align: center;
/*border-radius: 1%;*/
width: 45%;
background-color: #aaa;
cursor: pointer;
box-sizing: border-box;
margin-top: 5px;
margin-bottom: 5px;
}

You can use 'margin' to create space between buttons as they stack. Add this line off CSS to your buttons.
.button {
margin: 2%; /*you can increase or decrease this percentage*/
}
This will add space between your buttons.

Related

CSS/HTML: input bar width and button width not the same even though I'm setting it as the same

My assignment is to make a login page, and the inputs and buttons have to have a 320px width, including a 2px border and 15px padding. I create two classes for the inputs and button, and in CSS I am specifying these widths for both but the button keeps coming out shorter. Right now it's coming out like this: 1
I'm fairly new at this so I apologize if my code is messy/this might seem like a silly question.
Here's my code:
HTML
<form class="signup" action="/signup/" method ="post">
<fieldset name="sign-up">
<legend>Sign up</legend>
<div class="input">
<label for="email">Email</label></br>
<input class="inputbar" placeholder="foo#bar.com" type="email" name="email" id="email" required/></br>
<label for="password">Password</label></br>
<input class="inputbar" placeholder="1234passw0rd" type="password" name="password" id"password" required/></br>
</div>
</fieldset>
<button type="signupbutton">Sign up</button>
</form>
CSS
.signup {
width: 320px;
padding: 40px;
margin-top: 30px;
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: white;
}
.signup fieldset{
border: none;
}
.input{
text-align: left;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 250px;
}
button{
background-color: mediumseagreen;
color: white;
}
Thanks everyone!
It's best to set the container width and then the elments inside to 100%. Using box-sizing: border-box; is key here.
* {
box-sizing: border-box;
}
.signup {
width: 320px;
padding: 40px;
margin-top: 30px;
margin-left: auto;
margin-right: auto;
text-align: center;
background-color: white;
}
.signup fieldset{
border: none;
padding: 0;
margin: 0;
}
.input{
text-align: left;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 100%;
}
button{
background-color: mediumseagreen;
color: white;
}
.inputbar, button{
border: 2px solid lightgrey;
border-radius: 2px;
padding: 15px;
width: 250px;
}
It appears you have set the width of the button to 250px. Try changing that to 320px.
Remove the text align on th input class
.input{
text-align: left;
}
and do this
.signup .input label {
text-align: left;
display: inline-block;
width: 100%;
padding-left: 20px;
}
Aside that the everything is working alright in chrome
what browser are you using to run your test?
this is whati have on my browser

Inline block element, not inline in this case

I have set my search bar div element as inline-block, as well as the img.
However, the div element is below the image, instead on the same horizontal line.
Anyone can advise me why this is happening?
body {
margin: 0;
}
#header {
width: 100%;
height: 60px;
background-color: #f1f1f1;
}
#header img {
height: 56px;
display: inline-block;
margin-left: 20px;
}
#search {
display: inline-block;
}
#search input {
display: inline-block;
width: 584px;
height: 38px;
border: 1px solid #d9d9d9;
}
#search input:hover {
border: 1px solid black;
}
<div id="header">
<img src="http://orig04.deviantart.net/1d83/f/2013/087/5/6/google_icon_by_slamiticon-d5z7lrp.png" />
<div id="search">
<form>
<input type="text" name="search" />
</form>
</div>
</div>
Your styles are working as they should; the #search input element's width is just too wide! Try looking at the result on a wider screen, and you will see the elements appear inline as expected.
Anticipating your next question, you can prevent wrapping of inline elements using the rule (on the container, e.g. #header):
white-space: nowrap
Anticipating your next question (if I may be so bold), you will probably want to set CSS rule:
vertical-align: middle
on both #header img and #search to get the look you want.
Here is the code snippet with what you want. The problem here is width of input. Since its more it bring the div to next line.
body {
margin: 0;
}
#header {
width: 100%;
height: 60px;
background-color: #f1f1f1;
}
#header img {
height: 56px;
display: inline-block;
margin-left: 20px;
}
#search {
display: inline-block;
height: 100%;
position: relative;
top: -22px;
}
#search input {
display: inline-block;
width: 480px;
height: 38px;
border: 1px solid #d9d9d9;
}
#search input:hover {
border: 1px solid black;
}
<div id="header">
<img src="http://orig04.deviantart.net/1d83/f/2013/087/5/6/google_icon_by_slamiticon-d5z7lrp.png" />
<div id="search">
<form>
<input type="text" name="search" />
</form>
</div>
</div>
The total width of image and search input is greater than full width of screen. Try with lesser width either of image or search input.
try this one
body {
margin: 0;
}
#header {
width: 100%;
height: 60px;
background-color: #f1f1f1;
}
#header img {
height: 56px;
display: inline-block;
margin-left: 5px;
}
#search {
display: inline-block;
height: 100%;
position: relative;
top: -22px;
}
#search input {
display: inline-block;
width: 450px;
height: 38px;
border: 1px solid #d9d9d9;
}
#search input:hover {
border: 1px solid black;
}
<div id="header">
<img src="http://orig04.deviantart.net/1d83/f/2013/087/5/6/google_icon_by_slamiticon-d5z7lrp.png" />
<div id="search">
<form>
<input type="text" name="search" />
</form>
</div>
</div>

CSS, two columns - left with dynamic width (input), right with fixed (button)

can somebody please point me to a solution for this?
HTML
<div class="container">
<input type="text" class="left" />
<button class="right">Some button</button>
</div>
CSS
.container {
display: table;
width: 100%;
background-color: red;
}
.left, .right {
display: table-cell;
}
.right { width: 100px; }
Here is code pen sample: http://codepen.io/be-codified/pen/qdRRBY
Input field should be stretchable, button should be fixed positioned on right.
Thank you in advance.
// edit
I can not use table tag because layout needs to be responsive.
I gave the input a width of calc(100% - 110px) and the button a float:right which resulted in the following. Is that what you need? The input type you want to use is, as far as I know, not stretchable by the user.
CSS
.container {
display: table;
width: 100%;
background-color: red;
}
.left, .right {
display: table-cell;
}
.right {
width: 100px;
float: right;
}
input.left {
width: calc(100% - 110px); //This makes sure the input area is as wide as possible, while also leaving space for the button. Play with the exact measurements to get what you need.
}
I suggest you to put the form elements into <div>, so don't change their default display properties, and then set the left input box to 100% width as needed.
.container {
display: table;
width: 100%;
background-color: red;
}
.left, .right {
display: table-cell;
}
.right {
width: 100px;
}
.left input {
width: 100%;
box-sizing: border-box;
}
<div class="container">
<div class="left"><input type="text" /></div>
<div class="right"><button>Some button</button></div>
</div>
In fact, both left and right can have dynamic width, so right column always get the minimum width based on the button length.
.container {
display: table;
width: 100%;
background-color: red;
}
.left, .right {
display: table-cell;
white-space: nowrap;
}
.left {
width: 100%;
}
.left input {
width: 100%;
box-sizing: border-box;
}
<div class="container">
<div class="left"><input type="text" /></div>
<div class="right"><button>Some button</button></div>
</div>
Here is full responsive solution.
HTML
<div class="container">
<div class="input-flied-box">
<form>
<input type="text" required="required" placeholder="Right Some thing" />
<button type="submit" class="submit-button">Send</button>
</form>
</div>
</div>
CSS
/* RESPONSIVE CSS */
.container{
width: 100%;
}
.input-flied-box{
width: 100%;
}
.input-flied-box input{
padding: 6px 12px 6px 12px;
}
.submit-button{
top: inherit;
right: inherit;
position: relative;
margin-bottom: 15px;
}
#media (min-width: 768px){
.container{
width: 750px;
}
.input-flied-box{
width: 600px;
}
.input-flied-box input{
padding: 6px 101px 6px 12px;
}
.submit-button{
top: 14px;
right: 14px;
position: absolute;
margin-bottom: 0;
}
}
#media (min-width: 992px){
.container{
width: 960px;
}
}
#media (min-width: 1200px){
.container{
width: 1170px;
}
}
/* RESPONSIVE CSS END */
*:after,
*:before{
box-sizing: border-box;
}
*{
box-sizing: border-box;
}
.container:after,
.container:before{
display: table;
content: " ";
clear: both;
}
.container{
margin-left: auto;
margin-right: auto;
}
.input-flied-box {
background-color: #666666;
margin-left: auto;
margin-right: auto;
padding-left: 15px;
padding-right: 15px;
position: relative;
}
.input-flied-box input {
background-color: #ffffff;
border: 1px solid #cccccc;
height: 40px;
margin-bottom: 15px;
margin-top: 15px;
width: 100%;
border-radius: 4px;
}
.submit-button {
background-color: #fc3850;
border: medium none;
border-radius: 4px;
color: #ffffff;
font-family: Arial;
line-height: 1;
padding: 13px 30px;
text-transform: uppercase;
}
https://jsfiddle.net/bL3wgrv9/

CSS beginner, help creating this layout?

In the image below, on the left is the output of my html/css, on the right is what I would like the layout to look like.
I'm pretty clueless as to:
how to Center the header
why the 'upper right' text and button are being forced to the next line by the header (as opposed to orienting in the upper right
how to align the text area so that it is to the right of the image
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="outer_border">
<div class="inner_border">
<!--just use a div to represent the image -->
<div class ="image">
</div>
<span class="upper_left_text">
upper left
</span>
<span class ="header">
<h2>
Header
</h2>
</span>
<span class="upper_right_text">
upper right
</span>
<button class="button1">Button</button>
<textarea class="text_area">Text Area</textarea>
<button class="button2">Button 2</button>
</div>
</div>
</body>
</html>
.outer_border {
border: 1px solid black;
width: 600px;
height: 500px;
}
.inner_border {
border: 3px solid black;
width: 400px;
height: 300px;
float: right;
}
.image {
border: 1px solid black;
display: inline-block;
width: 50px;
height: 100px;
margin: 5px;
float: left;
}
.the_header {
text-align: center;
display: inline-block;
}
.button1 {
float: right;
}
.button2 {
float: right;
width: 80px;
height: 60px;
}
.text_area {
clear: both;
display: block;
width: 100%;
height: 150px;
margin: 5px;
/*I have no idea how to position this*/
}
.upper_left_text {
float: left;
}
.upper_right_text {
float: right;
}
I made a jsfiddle, check this one, should get you started :)
https://jsfiddle.net/fazbyxyq/
html5
<div class="right">
<div>upper left</div>
<div>header</div>
<div>upper right</div>
<div><textarea>textarea</textarea></div>
<div>button2</div>
</div>
css3
*{
box-sizing:border-box;
-moz-box-sizing:border-box;
}
.left{
float:left;
width:10%;
height:100px;
border:1px solid #000;
}
.right{
float:left;
width:89%;
margin-left:1%;
}
.right div{
float:left;
width:33%;
border:1px solid #000;
}
.right div:nth-child(2){
text-align:center;
}
.right div:nth-child(3){
text-align:right;
}
.right div:nth-child(4),.right div:nth-child(5){
width:99%;
border:0;
}
.right div:nth-child(4) textarea{
width:100%;
height:100px;
margin:10px 0;
}
.right div:nth-child(5){
text-align:right;
}
Peace out!
well, Your code was wrong in many lvl's. I have fixed it to look like in your image... but it's just a fix. Maybe not what you are looking for.
As a resume: You want a container with an image looks like a column and the rest of the html stay as another column.
Then, as you did, the image container is floating left with a fixed width of 50px but we have to add 10px more as you have given the container 5px margin (5px right and left = 10px),
Then I just add a container which will take the rest of the html. THen it's easy to give the container a float left and as its width 340px so the total of your layout is, as you want, 400px.
I have added both box-sizing: border-box; to make the border be inside the containers and not messing with the fixed widths.
Then I just have added .header {float:left;} as basically ion your code you have a class named the_headerwhich is not even used in the html. and then a bit of margin to the h2 to separete it from upper left
here you have the fiddle
The key lays in treating your layout as a layout with 2 columns. I believe the markup should look something like this:
<div id='demo'>
<div class='col1'>
<img src='http://www.placehold.it/50x100' />
</div>
<div class='col2'>
<div class='header'>
<span class='left'>left</span>
<span class='right'>
<button>button</button>
right
</span>
<h2>center</h2>
</div>
<textarea>Lorem ipsum</textarea>
<button>button</button>
</div>
</div>
to achieve the result in your image, you should add the following css:
#demo {
border: 2px solid black;
padding: 10px;
}
#demo .col1, #demo .col2 {
display: inline-block;
vertical-align: top;
}
#demo .col2 {
width: calc(100% - 60px);
}
#demo .left {
float: left;
}
#demo .right {
float: right;
}
#demo .header {
text-align: center;
}
#demo textarea {
width: 100%;
min-height: 100px;
margin: 8px 0;
}
#demo button {
float: right;
margin-left: 8px;
}
Note that I've used as little fixed dimesions as possible. Just cause it will make your layout adapt easier to different content and different screen sizes.
I've put your code next to my proposal in a fiddle. I think the code should be fairly easy and self explanatory, but feel free to ask if anything isn't clear.
Here is another fiddle that uses the "calc" operation to set the textarea the remaining width of the div.
Here is the fiddle http://jsfiddle.net/SteveRobertson/tyokk1qj/
I wrap this image in and set the height to 100% and then modify the rest of the elements to the right use CSS
.outer_border {
border: 1px solid black;
width: 600px;
height: 500px;
}
.inner_border {
border: 3px solid black;
width: 400px;
height: 300px;
}
#tall{
height:100%;
float:left;
}
.image {
border: 1px solid black;
display: inline-block;
width: 50px;
height: 100px;
margin: 5px;
float: left;
}
.the_header {
text-align: center;
display: inline-block;
}
h2 {
display:inline;
}
.button1 {
float: right;
}
.button2 {
width: 80px;
height: 60px;
display: block;
float:right;
}
.text_area {
clear: both;
display: inline;
width:auto;
height: 150px;
margin-right: 0;
}
.upper_left_text {
float: left;
}
.upper_right_text {
float: right;
}
.text_area{
width:calc(100% - 70px);
}

Center Form Elements on Page with CSS

I've tried every way of centering elements in CSS that I know but I cannot get these form elements to center on the page.
My code: http://jsfiddle.net/VJLst/1/
<style>
#divQuizFillBlank {
margin-left: auto;
margin-right: auto;
}
#textQuizFillBlankUserAnswer {
border: 2px solid #000;
font-size: 16px;
font-weight: bold;
width: 80%;
height: 40px;
float: left;
box-sizing: border-box;
vertical-align: middle;
}
#buttonQuizFillBlankSubmit {
border: 2px solid #000;
font-size: 20px;
font-weight: bold;
width: 10%;
height: 40px;
float: left;
margin-left: 5px;
}
</style>
<div id="divQuizFillBlank">
<input type="text" id="textQuizFillBlankUserAnswer">
<input type="button" id="buttonQuizFillBlankSubmit" value=">">
</div>
You need to set the width for #divQuizFillBlank.
The rule margin: auto; doesn't work for elements without a width. Moreover, it works only for block elements and those elements take up full available width.
#divQuizFillBlank {
margin-left: auto;
margin-right: auto;
width: 80%;
}
Updated Fiddle: http://jsfiddle.net/praveenscience/VJLst/2/