Span, Input, Span 100% width CSS issue - html

This is what i'm doing right now. I'm using LESS CSS for my design. I need to put 2 spans between a specified input. all the elements should be 100% width. spans should always 20px width input width can be change according to the screen width. Can anyone help me?
span width: 20px;
<div class="wrapper">
<span class="span-one">span</span>
<input type="text" class="input">
<span class="span-two">span</span>
</div>

You can achieve this with absolute positioning. It will take the spans out of the flow and put them on top of the input. You should also put the input in a div to do this as it doesn't naturally get 100% width when display:block set on it.
HTML
<div class="wrapper">
<span class="span-one">span</span>
<div class="input"><input type="text" class="input"></div>
<span class="span-two">span</span>
</div>
CSS
.wrapper { position: relative; }
div.input { margin: 0 20px; }
input {
width: 100%;
box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box; }
span.span-one {
position: absolute;
width:20px; height:20px;
left:0; top:0;
background-color: red; }
span.span-two {
position: absolute;
width:20px; height:20px;
right:0; top:0;
background-color: red; }
Here's the fiddle: http://jsfiddle.net/ywUeu/1/
Of course the word 'span' in the spans is longer than 20px so it comes out of the span.
Might be best to add 'box-sizing' to input as I've done too.

Positioning is not ideal but you already approved the answer...this is an FYI....
Instead of positioning use: display: table-cell;
http://jsfiddle.net/Riskbreaker/CBC5A/1/

With your HTML:
.wrapper, .text {
width: 100%;
}
.wrapper > span {
display: inline-block;
width: 20px;
}
.wrapper.input {
width: calc(100% - 40px);
}
Better way:
<div class="wrapper">
<div class="input"><input type="text" class="text-input"></div>
</div>
CSS:
/*
.wrapper, .input{ width is 100% by default }
*/
.text-input{
margin: 0 20px;
width: calc(100% - 40px);
}

Related

input element, display bock

Why is input element does not take up 100% of the width of its container automatically after changing its display to block? Are there some other factors which also have an influence on that? Thanks. Demo see below:
some explanation: 1. I comment out width:100% intentionally because block level element is supposed to take up 100% of its container width.
#container {
width: 300px;
margin: auto;
background-color: red;
}
input[type="text"] {
display: block;
opacity:0.5;
/*width:100%;*/
}
<body>
<section>
<div id="container">
<input type="text">
</div>
</section>
</body>
I'm not an expert, but I'm pretty sure it's because you have commented out width:100%. try decommenting that then it should work
#container {
width: 300px;
margin: auto;
background-color: red;
}
input[type="text"] {
display: block;
opacity:0.5;
width:100%;
}
Changed the code check now
#container {
width: 300px;margin: auto;
background-color: red;
}
input[type="text"] {
opacity:0.5;
width:100%;
border-width:0;
padding:0;
}
<body>
<section>
<div id="container">
<input type="text">
</div>
</section>
</body>
The input element by default has a border: 2px and a padding: 1px 0 in google chrome
When you were actually applying a width of 100%, the input actually had a width greater than the actual div outside covering it
width of input(set to width of div) + border + padding > width of div
There is a tiny little white area on the right, in case you uncomment width:100% in your code. That white area actually is the input. If you set the border to zero that's really enough to fix things
#container {
width: 300px;
margin: auto;
background-color: red;
}
input[type="text"] {
display: block;
opacity: 0.5;
width: 100%;
border: 0
}
<body>
<section>
<div id="container">
<input type="text">
</div>
</section>
</body>
Default size of input is 20, so if you do not define size or css rule for your input automatically its size is 20.
The best solution is adding width.
try this code:
#container
{
width: 300px;
margin: auto;
background-color: red;
}
input[type="text"]
{
display: block;
opacity:0.5;
width:100%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
If you want to be responsive it is better to add box-sizing to all element like this:
*
{
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

Two divs inside one div responsive with padding

My problem is that I am trying to use padding in my CSS so that the two divs inside my div are responsive at 50% each. But together they obviously are bigger than 100%. I know this is probably the paddings fault, but I don't know how to fix it.
CSS:
.columns {
max-width:100%;
width:100%;
display:inline-block;
text-align:left;
}
.col1 {
width:50%;
float:left;
padding-left:100px;
}
.col2 {
width:50%;
float:right;
padding-right:100px;
}
HTML:
<div class="columns">
<div class="col1">
</div>
<div class="col2">
</div>
</div>
By default the box model will use padding and border to expand an element beyond a specified width. To keep the paddings/borders from pushing outward, and contain them inward, use box-sizing: border-box;
.columns {
max-width: 100%;
width: 100%;
display: inline-block;
text-align: left;
}
.col1 {
width: 50%;
float: left;
padding-left: 100px;
}
.col2 {
width: 50%;
float: right;
padding-right: 100px;
}
.col1,
.col2 {
box-sizing: border-box;
}
<div class="columns">
<div class="col1">
</div>
<div class="col2">
</div>
</div>
In situations like these, it's useful to put this rule at the beginning of your styles:
* {
box-sizing: border-box;
}
It sets everything to box-sizing: border-box;, which means that the borders and paddings are included in the width/height settings, i.e. a container with width: 200px, border: 1px and padding 10px will really be 200px wide, including borders and padding (and not 222px, as it would be without box-sizing: border-box).

Why can't I select inside these input boxes?

I'm building a dummy form in CodePen and in the middle row I wanted to have two input boxes next to each other taking up 50% each in the row. I was able to do this, however, I cannot click inside either input box to start typing. The only way I can start typing in each input is to start at the first box and press 'tab'. Any suggestions?
HTML:
<div class="wrapper">
<h1>Application for Philadelphia Eagles</h1>
<h2><strong>Position:</strong> Wide Receiver</h2>
<p>An attempt at Input label floats</p>
<form class="form-container">
<div class="form-tr">
<div class="tc-100"><input type="text"></div>
</div>
<div class="form-tr">
<div class="tc-50 flt-l"><input type="text"></div>
<div class="tc-50 flt-r"><input type="text"></div>
</div>
<div class="form-tr">
<div class="tc-100"><input type="text"></div>
</div>
</form>
</div>
CSS:
#import url('https://fonts.googleapis.com/css?family=Josefin+Slab:400,300,700');
body {
font-family: Josefin Slab, sans-serif;
}
h1, h2 {margin: 0.465em}
.wrapper {
margin: 0 auto;
text-align: center;
width: 75%;
}
.flt-l { float:left; }
.flt-r { float:right; }
.form-container {
position:relative;
border: 1px solid #000;
width:40em;
margin: 0 auto;
}
.form-tr {
display: block;
position:relative;
margin-bottom: 0px;
width:100%;
}
.tc-50 {
width:50%;
}
input[type=text] {
width: 100%;
}
input[type=text] {
font-size:1em;
padding:1em;
box-sizing: border-box;
}
My CodePen here: http://codepen.io/mjdeangelis/pen/avpBex?editors=110
Another fix is removing the position:relative property from the form-tr on the CSS file since the outer form container already has it and in this case its making the 2 inputs in the middle overlap.
Before:
.form-tr {
display: block;
position:relative;
margin-bottom: 0px;
width:100%;
}
After:
.form-tr {
display: block;
margin-bottom: 0px;
width:100%;
}
For extra references you can always check out the following links:
http://www.w3schools.com/cssref/playit.asp?filename=playcss_position
http://www.w3schools.com/cssref/pr_class_position.asp
Change your classes like:
.flt-l {
float:left;
}
.flt-r {
display: inline-block;
}
The third row is overlapping with second row, hence not clickable. Provide a height to the second row, so that third row is pushed below it.
Updated code is available here
http://codepen.io/anon/pen/rOjjYE?editors=110
.split-row{
height:50px;
}
Set form-tr height to match the height of your full-width rows.
.form-tr {
display: block;
position: relative;
margin-bottom: 0px;
width: 100%;
height: 55px;
}
If you can remove "position: relative" in class, it also works.
.form-tr {
display: block;
margin-bottom: 0px;
width:100%;
}

Scaling div like an image

I'm doing a list of items, but it has some challenges:
Responsive;
The "title" may have more than one line;
Sometimes a I need to show a icon with a color in the background instead of full image.
This is the image of what I'd expect:
And what I've got: http://codepen.io/caio/pen/ygkfm/
As you can see, I can't set the same scaling to an "image" div when it has a icon. Is there any solution for my problem?
I am assuming your images (exept icons) all have the same aspect ratio as in your example.
In this case, you can use padding bottom to keep the height of the image container. As padding-bottom is calculated according to the width of the container, it will keep it's aspect ratio whatever the content (you will have to position the content with position:absolute; so it doesn't change the dimesions of the container).
Here is a demo Showing what you can do.sorry I'm not into codePen
I also added an other container to center the icons horizontaly.
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.items {
margin: 50px auto 0;
width: 90%;
*zoom: 1;
}
.items:before, .items:after {
content:" ";
display: table;
}
.items:after {
clear: both;
}
.items .item {
border: 1px solid #ddd;
float: left;
width: 32%;
}
.items .item:nth-child(3n+2) {
margin: 0 2%;
}
.items .item .image {
background: #eee;
padding-bottom:50%;
position:relative;
}
.items .item .image .img_in{
position:absolute;
width:100%;
height:100%;
text-align:center;
}
.items .item .image img {
display: block;
width: 100%;
height:100%;
}
.items .item .image img.icon {
height: 80%;
margin:0 auto;
position: relative;
top: 10%;
width: auto;
}
.items .item .title {
margin: 0;
padding: 20px;
}
It's easy
add follwing to .items .item .image
when you have a 'normal' width and height of 200 and 100 Pixels, then 50% represents the 50% of the width (200 * 50% = 100)
{
height: 0;
padding-bottom: 50%;
}
http://codepen.io/HerrSerker/pen/HhjKo?editors=110
edit
You can use SCSS percentage function:
padding-bottom: percentage(100px / 200px);
This is not exactly what you had in mind however it is a very responsive design which I expect to be what you need: http://codepen.io/anon/pen/DwudI
Here's the gist: You probably want to keep the aspect ratio of each main container. The image then scales to at least 80% of the height and no more than 100% in both width and height. The way to create an aspect ratio on a div is by using this fun padding-top trick. When you resize the screen the div's width changes which causes the height to change to (aspect ratio). So if you resize smaller then eventually the div becomes smaller than the image size which will cause the 200x100 to fill the entire div.
So if you want the image to fill the div, then it must be (A) larger than the div and (B) the same aspect ratio as the div.
You mentioned the title might be multiple lines: Right now new lines go below. If you wanted the text to 'float upwards' then that wouldn't be too hard. Simply use position:absolute; bottom:0px on the header and make sure .item has position:relative.
I think you are going about this the wrong way, when everything is based on the width percentages there is no way to know the height unless you use JS, so you need to change the width to something more appropriate to achieve your goal.
changing your CSS to:
.icon {
margin: 0 auto;
padding: 5% 0;
width: 40%;
}
and it will look more like you want. I updated your CodePen
Mainly, I added a max-height and a min-height of the same value to .items .item .image img:
.items .item .image img {
display: block;
width: 100%;
max-height:23%;
min-height:23%;
}
I'm not quite sure what you're trying to achieve but if I got you well then this is what you're looking for, Here is the full code:
HTML
<div class="items">
<a href="#" class="item">
<div class="image">
<img src="http://placehold.it/200x100" />
</div>
<h4 class="title">Hi. I'm a title.</h4>
</a>
<a href="#" class="item">
<div class="image">
<img src="http://placehold.it/80x80" class="icon" />
</div>
<h4 class="title">Hi. I'm a title.</h4>
</a>
<a href="#" class="item">
<div class="image">
<img src="http://placehold.it/200x100" />
</div>
<h4 class="title">Hi. I'm a title.</h4>
</a>
</div>
CSS
* {
#include box-sizing(border-box);
}
.items {
margin: 50px auto 0;
width: 90%;
#include clearfix;
}
.item {
border: 1px solid #ddd;
float: left;
width: 30%;
}
.items .item .image {
background: #eee;
}
.items .item .image img {
display: block;
width: 100%;
max-height:23%;
min-height:23%;
}
.items .item .title {
margin: 0;
padding: 20px;
}
.icon {
height: 80%;
margin: 0 auto;
position: relative;
top: 10%;
width: auto;
}
.items .item:nth-child(3n+2) {
margin: 0 2%;
}
And here is a FIDDLE
I thing this is what you are excepting.
Demo
HTML
<a href="#" class="item">
<div class="image">
<div><img src="http://placehold.it/200x100"></div>
</div>
<h4 class="title">Hi. I'm a title.</h4>
</a>
<a href="#" class="item">
<div class="image">
<div><img src="http://placehold.it/80x80" class="icon"></div>
</div>
<h4 class="title">Hi. I'm a title.</h4>
</a>
<a href="#" class="item">
<div class="image">
<div><img src="http://placehold.it/200x100"></div>
</div>
<h4 class="title">Hi. I'm a title.</h4>
</a>
</div>
SCSS
* { #include box-sizing(border-box); }
.items {
margin: 50px auto 0;
width: 90%;
#include clearfix;
.item {
border: 1px solid #ddd;
float: left;
width: 32%;
&:nth-child(3n+2) { margin: 0 2%; }
.image {
background: #eee;
min-height:100px;
max-height:100px;
display:table;
width:100%;
&> div {
display:table-cell;
text-align:center;
vertical-align:middle;
}
img {
max-width:100%;
height:100%;
margin:0 auto;
&.icon {
height: 80%;
margin: 0 auto;
position: relative;
top: 10%;
width: auto;
}
}
}
.title {
margin: 0;
padding: 20px;
}
}
}
I'd rather go and use those utility classes which I found myself using quite a lot since I found them, basically embedding them on each CSS I write. Clean, easy to read and easy to embed in the HTML.
This small set of classes permits you to have a proportional width/height sizes on elements.
Here's the demo http://siebennull.com/equal_width_height.html
Here's the article explaining it: http://www.mademyday.de/css-height-equals-width-with-pure-css.html
Credit obviously goes to who found this trick :)
CSS
.box{
position: relative;
width: 50%; /* desired width */
}
.box:before{
content: "";
display: block;
padding-top: 100%; /* initial ratio of 1:1*/
}
.content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
/* Other ratios */
.ratio2_1:before{
padding-top: 50%;
}
.ratio1_2:before{
padding-top: 200%;
}
.ratio4_3:before{
padding-top: 75%;
}
.ratio16_9:before{
padding-top: 56.25%;
}
HTML
<div class='box'>
<div class='content'>Aspect ratio of 1:1</div>
</div>
<div class='box ratio16_9'>
<div class='content'>Aspect ratio of 16:9</div>
</div>
You could use an extra element and vertical-padding to force your div to keep the same ratio that it has a 2:1 image or not.
DEMO and basic css:
.image:before {
content:'';
display:inline-block;
vertical-align:middle;
padding-top:50%;/* equals 50% of width of parent */
width:0;
box-shadow:0 0 0 5px red;/* let's see where it stands for demo purpose */
}
In order to have this working in your codepen:
img should turn back to their default display (inline-block), so just remove display:block; and be vertical-alligned in middle to the pseudo element , the gap under img that appears when on baseline, will be no longer here.
.image needs either:
In CSS font-size:0;
In HTML, the code <div><img src=".. should not be indented
In HTML white-space should be commented <div><!-- code indented --><img src="...
to avoid extra white-space and break in 2 lines when img is full width.
I did link in the demo another version where image could be bigger than initial space wanted without breaking the layout (base on idea that elements remain in the flow, no absolute positionning involved): EXTRA
Maybe you could try this jQuery library http://brm.io/jquery-match-height/
To use it you assign data attributes to the elements whose heights you want to match, it then calculated the height of each element to make are they are all the same. It takes in to account padding, margin, border and box-sizing.

How can I vertically align two floated divs?

I have two divs inside a container div. One need to float left the other float right. They also both need to be vertically centered inside their parent. How can I achieve this?
<div id='parent'>
<div id='left-box' class='child'>Some text</div>
<div id='right-box' class='child'>Details</div>
</div>
If no float is applied to either they vertically align to the middle with this css
.child{ display:inline-block; vertical-align:middle; }
However adding #right-box{ float: right; } causes the children to lose their vertical alignment. What am I doing wrong?
Thanks guys
here is the online demo of the solution you needed
it was made with this html:
<div id='parent'>
<div id='left-box' class='child'>Some text</div>
<div id='right-box' class='child'>Details</div>
</div>
and this css:
#parent {
position: relative;
/* decoration */
width: 500px;
height: 200px;
background-color: #ddd;
}
.child {
position: absolute;
top: 50%;
height: 70px;
/* if text is one-line, line-height equal to height set text to the middle */
line-height: 70px;
/* margin-top is negative 1/2 of height */
margin-top: -35px;
/* decoration */
width: 200px;
text-align: center;
background-color: #dfd;
}​
#left-box { left: 0; }
#right-box { right: 0; }
You can try the display:table and display:table-cell styles.
Check this site out for more details http://www.quirksmode.org/css/display.html
NB: if you want the parent div height to be a percent (like 100%), then it will be relative to the height of it's container. If the container is the body, then you will have to set the body and html's height as well, like to 100%.
Here's an example of what the code might look like:
<div id='parent'>
<div id='left-box'>Some text</div>
<div id='right-box'>Details</div>
</div>​
<style>
body,html{
height:100%;
}
#parent{
border:1px solid red;
display:table;
height:100%;
width:100%;
}
#left-box{
background-color:#eee;
display: table-cell;
vertical-align:middle;
padding:3px;
width:50%;
}
#right-box{
background-color:#dddddd;
display: table-cell;
vertical-align:middle;
padding:3px;
width:50%;
}
​</style>