CSS: Set Inline element inside padding - html

I am looking at the Instagram website. I notice that they put a zoom icon inside the padding of adjacent input. I wonder how this is done, can somebody show me an example
Thanks.

Here is the example for jQuery search box on focus show hide icon as per your reference. I hope this answer will be helpful.
$('.btn-close').hide();
$('.fa-search').show();
$('.input-text').on('focus', function() {
$(this).siblings('.btn-close').show();
$(this).siblings('.fa-search').hide();
});
$('.btn-close').click(function(e) {
$('.fa-search').show();
$('.btn-close').hide();
e.stopPropagation();
});
$('.input-text').on('focusout', function() {
$(this).siblings('.btn-close').hide();
$(this).siblings('.fa-search').show();
});
.input-text {
border: 1px solid #888;
min-height: 40px;
font-size: 16px;
padding: 0 25px 0 5px;
}
.input-box {
position: relative;
display: inline-block;
}
.input-box .fas,
.btn-close {
position: absolute;
right: 0;
padding: 11px 4px;
top: 0;
color: #888;
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="input-box">
<input type="text" class="input-text" placeholder="text">
<i class="fas fa-search"></i>
<a class="btn-close" href="#"><i class="fas fa-times-circle"></i></a>
</div>

The answer is that they don't actually put the icon inside the input box. The just draw a rectangle around both the icon and the <input>. The icon itself is added to the <span> on the line right after the highlighted <input> in the image in the question. Look for the class coreSpriteSearchIcon.
When I inspected that <span>, I saw these styles applied:
background-image:
url(/static/bundles/metro/sprite_core_2x_6ba81dcece9b.png/6ba81dcece9b.png);
}
background-size: 410px 396px;
background-position: -240px -366px;
height: 10px;
width: 10px;
The background-image is the sprite file (an image containing multiple smaller images). background-size ensure that the image isn't stretched. background-position tells you where to find the search icon within the larger sprite image. And, width and height tell you how much of the image to display.
They were able to place it where it is by using absolute positioning:
left: 11px;
position: absolute;
top: 9px;
z-index: 2;

One of the way to achieve this is to use position: absolute and put input into a wrapper. Let me show you:
.input-wrapper {
position: relative;
}
.input-wrapper img {
position: absolute;
top: 5px;
left: 5px;
}
input {
height: 40px;
width: 200px;
padding-left: 35px;
font-size: 20px;
box-sizing: border-box;
}
<div class="input-wrapper">
<img src="https://picsum.photos/30"/>
<input type="text" />
</div>
So basically we use position: relative to move img relatively to it. Also note, that you have to add extra padding(left one in this case) so text won't overlap with icon.
There are a lot of ways to do the same: position: relative, negative margin, background-image, utilising of pseudo-elements, but absolute positioning is the most semantically correct in my opinion.

Related

Align two icons to the far right of a parent div

I am trying to add an svg and another span element container an svg to the far right of an input however, with my current setting they are overlapping:
I have a structure like this:
/* How can I fix this such that the red will be aligned before the arrows? Currently my css is: */
svg,
#mySvg {
cursor: pointer;
position: absolute;
display: inline-block;
opacity: 0.8;
right: 0;
position: relative;
margin-left: -25px;
z-index: 9999 !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex align-items-center input-fields">
<input class="form-control textInput" type="text">
<svg></svg>
<span id="mySvg"></span>
</div>
currently they look like this:
Here is a simple example that I managed to work on for this purpose. You can change your HTML like this:
<div class="input-fields">
<input class="form-control textInput" type="text">
<div id="icons_container">
<svg></svg>
<span id="mySvg">♠</span>
</div>
</div>
and for the CSS part, you do like this:
#icons_container {
position: absolute;
display: flex;
align-items: center;
right: 0;
top: 0;
padding: 0px 5px 0 0;
}
I have wrapped both the icons with div that is positioned as absolute and it's display property is set to flex.
Here is the example in full in CodePen. https://codepen.io/ahmadkarimi2009/pen/ExgpLbe
Note: I haven't used Bootstrap for tackling this issue.
When you using absolute position for elements you can not control overlaping them.
The only thing you can do is that add more right offset to one of them to prevent overlaping.
Example:
svg {
right: 10px;
}
#mySvg {
right: 40px;
}

Dynamically width (and center) input based on length of input

I have absolutely positioned inputs which usually have 1 digit length as their input.
I set the input's width to 8px and everything works great. However, sometimes we can have up to 4 digits in the input. In this case, I want the input to automatically expand to fit, while retaining center alignment.
The inputs are positioned on a grid in a specific fashion and require absolute positioning.
For a simplified example, https://jsfiddle.net/joshuaohana/2var8ftL/1/
In this case I want to be able to type 1234 as an input, the box should expand with the input getting longer, and the center of the input box should remain in the same location.
<div class="container">
<div class="input1">
<input placeholder="1" />
</div>
<div class="input2">
<input placeholder="2" />
</div>
</div>
and the css
.container {
position: relative;
padding: 10px;
border: 1px solid blue;
width: 150px;
height: 150px;
}
.input1 {
position: absolute;
top: 5px;
left: 5px;
}
.input2 {
position: absolute;
top: 50px;
left: 25px;
}
input {
width: 8px;
}
The easiest way to make it happen is to have an element reflecting the value of an invisible input field. There is no way to adjust input width by the content without heavy trickery. That is, if you don't need full edit capabilities, like moving the cursor.
If you do, I would opt for having an invisible <div> element and setting its value to whatever you type in your <input>. Then, you'd read the width of that <div> and set the same width to your <input>. Just remember that they both need to have the same font-family, font-size and any other font-related property.
If you're open to using contenteditable then this should be easy to accomplish
.container {
position: relative;
padding: 10px;
border: 1px solid blue;
width: 150px;
height: 150px;
}
.input1 {
position: absolute;
top: 5px;
left: 5px;
}
.input2 {
position: absolute;
top: 50px;
left: 25px;
transform:translateX(-50%);
}
[contenteditable] {
border: 1px solid;
min-width: 8px;
max-width: calc(8px * 4);
white-space:nowrap;
overflow:hidden;
}
<div class="container">
<div class="input1">
<div contenteditable></div>
</div>
<div class="input2">
<div contenteditable></div>
</div>
</div>

How to place an image on top of text?

the top attribute appears not to be working on a html. I am trying to use the top attribute on image to move an image to the top and place above a text but the top attribute of a css never moves the image Here is snippet
<div class="stl_02">
<div class="stl_03">
<img src=""
alt=""style="top: 4.4538em;" class="stl_04">
</div>
<div class="stl_view">
<div class="stl_05 stl_06">
//other texts here
here are the css rules
.stl_02 {
height: 46em;
font-size: 1em;
margin: 0em;
line-height: 0.0em;
display: block;
border-style: none;
width: 51em;
}
.stl_03 {
position: relative;
}
.stl_04 {
width: 100%;
clip: rect(-0.041667em,51.04167em,66.04166em,-0.041667em);
position: absolute;
pointer-events: none;
}
Please how can push the image to the top using this attribute style="top: 4.4538em;" is a challenge
Your element does have the top attribute applied. This can be seen in the following:
.stl_02 {
height: 46em;
font-size: 1em;
margin: 0em;
line-height: 0.0em;
display: block;
border-style: none;
width: 51em;
}
.stl_03 {
position: relative;
}
.stl_04 {
width: 100%;
clip: rect(-0.041667em, 51.04167em, 66.04166em, -0.041667em);
position: absolute;
pointer-events: none;
}
<div class="stl_02">
<div class="stl_03">
<img src="http://placehold.it/100" alt="" style="top: 4.4538em;" class="stl_04">
</div>
<div class="stl_view">
<div class="stl_05 stl_06">
</div>
</div>
</div>
If you are not seeing this effect, it is possible you have a rule with higher specificity overriding it, or you have cached the style before you applied this rule.
It's also worth noting that top only works on a positioned element. You need to have position: relative, position: absolute or similar on .stl-04 in order to position it with top.
Alternatively, you may be looking for margin-top, which positions vertically based on the containing element.
As an aside, basing margins off of font sizes (with em units) is generally bad practice; you should really use fixed units instead (preferably not going to so many decimal places).

Positioning text inside a button

I have buttons with text in it which has too much space above it. Is looks like this:
How can I position the text a little bit higher so that it doesn't get sliced by the border of the button?
HTML:
<input type="button" value="a">
Here is my CSS so far:
input[type="button"], input[type="submit"] {
min-width: 40px;
height: 25px;
font-size: 30px;
color: #ffffff;
margin-right: 10px;
padding: 0px;
}
The line-height property doesn't change anything, even with the use of !important.
Setting a fixed height will throw the alignment off.
Setting the font size will be enough for the div to auto-size and the text will be centered.
Try this new css:
input[type="button"], input[type="submit"] {
min-width: 40px;
font-size: 30px;
color: #ffffff;
margin-right: 10px;
}
If you really want to preserve the fixed width and height of the button and the font size of the text then you could do something like below.
This is putting the text in a different div so you can position it on top of the button.
If the button is position: relative, the text div is position: absolute, and the HTML is structured so the text div is within the button tag, then it will layer nicely.
Also, when done this way the text will move with the button and won't be displaced (if say you give the button a margin: 100px).
<button value="">
<div id="buttonText">a</div>
</button>
Here is my CSS so far:
<style>
button {
width: 40px;
height: 25px;
margin-right: 10px;
position: relative;
}
#buttonText
{
font-size: 30px;
position: absolute;
top: -9px;
left: 9px;
color: #FFF;
}
</style>

make <input type="file"> element fill up its parent <div>

HTML:
<div>
<img src="some/path/" class="thumbnail" />
<input type="file" class="image_upload" />
</div>
CSS:
div
{
border: 2px solid #ccc;
width: 50px;
height: 50px;
overflow: hidden;
}
.thumbnail
{
width: 100%;
}
.image_upload
{
opacity: 0;
position: absolute;
}
I want <img> and <input type="file"> to overlap with each other and both fill up their parent <div>. How can I fix my CSS to achieve that?
It is not possible to change the size of a file input. You could redesign the file-input and, but the size of the clickable area isn't modifiable.
Edit: Aaron shows a first trick, and I added the second one, so see this fiddle in which the whole image is clickable for the file input.
The trick is to set font-size to a large value, then opacity to zero and finally add overflow: hidden to the parent element.
File input fields don't really play by the rules (or at least as you'd expect). To accomplish what it sounds like you're after, you've gotta get creative. Example: http://jsfiddle.net/ZTPCd/
Its Possible.
Add this css for input type file
.My_CSS {
opacity: 0;
border: none;
border-radius: 3px;
background: grey;
position: absolute;
left: 0px;
width: 100%;
top: 0px;
height: 100%;
}
You'll need to add relative positioning to the parent div, so the input field won't be positioned relatively to the browser window. (Google for more info about absolute/relative positioning).
And you'll have to add some specific positioning (top/left) to the input tag.
http://jsfiddle.net/NbhQY/
(Your outer div will have to be a little bit bigger, though, if it needs to include a file upload.)
Here you need to use some JavaScript. Since I don't see any way to change the CSS for input(type=file) itself, I made it hidden but the <div> responsible for <input type='file'>.
var box = document.getElementById("box");
var file = document.getElementById("file");
box.addEventListener('click', function(){
file.click();
})
#box {
font-size: 30px;
text-align: center;
width: 300px;
height: 200px;
padding: 10px;
border: 1px solid #999;
position: relative;
}
p {
position: absolute;
top: 80px;
color: white;
}
#file {
width: 100%;
height: 100%;
visibility: hidden;
z-index: 100;
}
<div id="box">
<img id="image" src="http://guide.denverpost.com/media/photos/full/mountain_600x600.jpg" width="100%" height="100%"/>
<input type="file" id="file"/>
<p>Click to import</p>
</div>