Currently I'm practicing how to code by building a similar website and I got stuck with this step which's adding a search word together with the search icon just like in the shown image.
I did type the code for the word "Sök" which's in Swedish means "Search" but I'm curious how to add the icon so it looks the same.
const icon = document.querySelector('.icon');
const search = document.querySelector('.search').value;
function searchbar() {
icon.style.display = "block";
return
}
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
height: 100vh;
width: 100%;
display: grid;
place-items: center;
font-family: Arial, Helvetica, sans-serif;
background-color: rgb(9, 5, 31);
}
.container {
position: relative;
}
.container .icon {
display: none;
position: absolute;
top: 50%;
right: 10px;
height: 20px;
transform: translateY(-50%);
}
.container .search {
width: 300px;
height: 40px;
border-radius: 9999px;
padding-left: 20px;
}
<div class="container">
<img class="icon" src="https://img.icons8.com/ios-filled/50/000000/search--v1.png" />
<input class="search" oninput="searchbar()" on type="text" placeholder="search">
</div>
if you want to add the search icon next to the searched word just do this:
put the icon and the search bar inside the same parent and set position:absolute; to the icon and position: relative; to the parent that contains both the input and icon.
for the appearance of the search icon when typing:
just set display: none; to it, and use javascript when (input.value > 1) , the display of the icon will be (display: block;).
there are many ways to achieve that
Related
Hello everybody I just started learning HTML and CSS to make my own page from scratch so, for this thread I just need some assistance with the social media bar, essentially what I need is just to make the bar capable of zooming in and out following the mouse wheel scroll of user, meaning that I won't have situations like this:
It should look like this instead (edited on paint):
here it's the code:
html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Community Impact</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
<link href="community-impactstyle.css" rel="stylesheet">
</head>
<body>
<div class="s">
Return to Index<i class="fas fa-hand-point-left"></i>
</div>
<h3>Noah Verner</h3>
<header>
</header>
<main>
<article>
</article>
<aside>
</aside>
</main>
<footer>Made by NOAH VERNER</footer>
</body>
</html>
css:
#font-face{
src: url(Fonts/InputSerifCondensed-Regular.ttf);
font-family: InputSerif;
}
*{
font-family: InputSerif ;
}
body{
background-color: #F5DC00;
margin: 0;
padding: 0;
color: #000000;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
h3 {
position: fixed;
top: -20px ;
left: 0;
right: 0;
font-size: 12px;
background-color: black;
color: white;
font-size: 20px;
text-align: center;
}
footer{
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: black;
color: white;
font-weight: bold;
text-align: center;
}
/*Barra de iconos de redes sociales*/
.s{
position: absolute;
justify-content: center;
height: 100vh;
display: flex;
flex-direction: column;
transform: translate(-1070px,0px);
}
.s a{
color: #F5DC00;
background: rgba(0, 0, 0, 1);
font-size: 20px;
font-weight: 600;
text-decoration: none;
display: block;
margin: 5px;
padding: 20px;
width: 300px;
text-align: right;
border-radius: 50px;
transition: 1s;
transition-property: transform;
}
.s a:hover{
transform: translate(200px, 0);
}
.s i{
margin-left: 10px;
font-size: 30px;
width: 30px;
height: 30px;
}
.s a:nth-child(1) i{
color: #F5DC00;
}
I know the code above doesn't contain all the 5 buttons, but I guess the solution could be applied to the "s" class rather than each button individually, and I tend to think the solution would be applied to the css file, but I'm a noob atm and can't figure it out
any ideas?
You're issue is that you are using the transform property to shift your element out of frame. A more robust way to do this would be to use the left property.
While we probably could find a way to do this in CSS, a more accurate way to do it would be using a touch of JavaScript - like so:
//get all of our social tabs as an array
let socialElem = document.querySelectorAll('.social');
//loop through the array
socialElem.forEach((elem) => {
//define our text element within the tab
let text = elem.querySelector('.hidden');
//get it's dimensions as a JavaScript object
let textSize = text.getBoundingClientRect();
/* now, using the dimensions object above, we can return the 'right' pixel value
of our text element. If we scoot our tab in by the negative of that number it should
perfectly hide our text without using transform.*/
elem.style.left = -textSize.right + 'px';
//we also add 'px' so that CSS can read it
})
:root {
/* set an accent color variable to make my life easier */
--col-acc: #F5DC00;
}
body {
/* reset default body styles */
margin: 0;
height: 100vh;
/* set the background color of the body to the accent color variable */
background-color: var(--col-acc);
/* set the font to something more appealing */
font-family: "Segoe UI Variable Text", system-ui, ui-rounded, sans-serif;
}
.social {
/* set the background color of the social tab to black */
background-color: black;
/* set the text color to our accent color variable */
color: var(--col-acc);
/* resize the tab to be the size of it's content */
width: max-content;
/* round the right side */
border-radius: 0 32px 32px 0;
/* fix the tab (absolute would also work here if you don't care about it staying put when scrolling up and down on the page) */
position: fixed;
/* center it vertically (could be applied to a parent/containter for multiple tabs) */
top: 50%;
/* set it to the left side of the screen */
left: 0;
/* center it just a little more vertically */
transform: translateY(-50%);
/* set the transition to the left property with a durration of 0.5 seconds */
transition: left 0.5s;
}
.social a {
/* give the anchor tag a display of block so we can resize it */
display: block;
/* override the color with our accent color*/
color: var(--col-acc);
/* remove the underline */
text-decoration: none;
/* beef up the font a little */
font-weight: 500;
/* make the font bigger */
font-size: 2rem;
/* add some padding to our tab */
padding: 8px 24px;
}
.social:hover, .social:focus-within {
/* on hover (or focus for accesability) set the left position to 0
I set this property to "important" becase the JavaScript is setting the right value to 0
in the elment styles (which normally overrides the stylesheet), so the important here
re-overrides the elment style, if that makes sense*/
left: 0 !important;
}
<div class="social">
<a href="#">
<span class="hidden">Social</span> 🧔
</a>
</div>
You could do a scroll effect where you take the scrollY and sum it with the boundingClientRect position of the element left and then add some constraints for the unit length in pixels within conditionals. Track the up and down scroll direction and add those to the conditionals for the in and out slide of the social icon and containing text.
const socials = document.querySelectorAll('.socials')
let lastScroll = 0;
window.addEventListener('scroll', function(e) {
let value = window.scrollY
let output = ''
let currentScroll = document.documentElement.scrollTop || document.body.scrollTop;
if (currentScroll > 0 && lastScroll <= currentScroll) {
lastScroll = currentScroll
output = "down"
} else {
lastScroll = currentScroll
output = "up"
}
socials.forEach(icon => {
let rect = icon.getBoundingClientRect()
rect.left < -16 && output === "down" ?
icon.style.left = `${rect.left + value * 0.15}px` :
rect.left > -16 && output === "down" ?
icon.style.left = `0px` :
rect.left > "-192" && output === "up" ?
icon.style.left = `${rect.left - value * 0.15}px` :
rect.left < "-192" && output === "up" ?
icon.style.left = `-192px` : null
})
})
body * {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: sans-serif;
}
#container {
height: 300vh;
position: relative;
}
img {
width: 2rem;
height: 2rem;
}
.socials {
display: flex;
justify-content: space-between;
align-items: center;
width: 15rem;
height: auto;
background: green;
color: limegreen;
border-top-right-radius: 2rem;
border-bottom-right-radius: 2rem;
padding: .5rem;
position: fixed;
top: 2rem;
left: -12rem;
}
<div id="container">
<div class="socials">
Follow me on Rumble
<img src="https://img.utdstc.com/icon/01c/903/01c9032ef30ddf7a9f0346ce3a77b92fd5602e34818f25603962012d2792fab6:200">
</div>
</div>
Everybody, I just found out another solution for my issue, which is even more simple and easy (thanks #calvin-bonner for making me realize it):
In my CSS file: I just typed this:
.s{
position: absolute;
justify-content: center;
height: 100vh;
display: flex;
left: 0;
flex-direction: column;
transform: translate(-82.5%);
}
AS you can see, I only added left: 0; and changed transform: translate(-1070px,0px); to transform: translate(-82.5%);, and now it works pretty well just as I expected
(Actual screenshot of my index after applying my simple solution above)
Note: I think this solution works well because I'm not using nor
planning to add a scroll bar on my website
I'm still a novice at webdev and this is my first question here so please bare with me. I'm currently working on a website for my school and I'm trying to add a search box like this(with the search icon inside the box): click this
I'm following the search bar tutorial from w3schools, but their version is a bit different(search icon on the outside): click this
This is the html code for the search bar:
<input type="text" placeholder="Search..">
I want to put the search icon inside the box like the first picture, but the input tag is a empty tag, so I don't know how to put it inside. Please help me.
i have used font awesome for search icon, u can use local search icon, font awesome icons, material design icons or any icon source just use this below code.
.custom-select {
position: relative;
width: 250px;
outline: none;
height: 50px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
background: none;
padding: 0;
border-radius: 5px;
z-index: 555;
}
.custom-select-search-icon {
position: absolute;
z-index: 2;
height: 22px;
width: 22px;
right: 10px;
opacity: 0.5;
}
.custom-select-input {
padding: 0 10px;
border-radius: inherit;
width: inherit;
height: inherit;
background: transparent;
color: #051833;
}
<!-- Load icon library -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="custom-select">
<input type="text" placeholder="Search please" class="custom-select-input">
<i class="fa fa-search custom-select-search-icon"></i>
</div>
This should give you a basic idea of how it can be done:
document.getElementById('sb').addEventListener("click", function(e){
if(e.target.nodeName =='SPAN') {
let elem = e.target.children[0];
elem.focus();
}
});
.outer {
border: 1px solid gray;
padding: 10px;
border-radius:5px;
}
.outer img {
height: 22px;
width: 22px;
top:5px;
position:relative;
}
input {
/* Tell the input to use all the available space */
flex-grow:2;
/* And hide the input's outline, so the form looks like the outline */
border:none;
}
input:focus {
/* removing the input focus blue box. Put this on the form if you like. */
outline: none;
}
button {
/* Just a little styling to make it pretty */
border:1px solid blue;
background:blue;
color:white;
}
<span class="outer" id="sb">
<input placeholder="Search.."/>
<img src="http://assets.stickpng.com/thumbs/585e4ad1cb11b227491c3391.png">
</span>
I have some checkboxes in a grid and want to use vanilla-css and html to make a custom checkbox. That works fine. The problem is the remaining box of the original checkbox, that stays in my grid and makes it behave in strange ways as it takes a cell. Even when I make it transparent or deactivate it, as it is often suggested.
In the original example they moved it out of the screen area, but I can make it escape the grid.
I think this is the part where it fails to behave like I want to:
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
Here is a minimal example: https://jsfiddle.net/3mzsLj1v/14/
Here is the example I used: https://css-tricks.com/the-checkbox-hack/
Here is the real code I work on: https://codepen.io/vaeng/pen/XWXKoMb
Thanks for your help. I am sure this is very common, but being a beginner, I might not use css in the correct way?
In both your "minimal" and "real code" examples, your "New Checkboxes" comments are not properly opened.
In minimal example:
Line 23: *New Checkboxes*/ s/b /*New Checkboxes*/
In real code example:
Line 123: * New Checkboxes and radio buttons*/ s/b /* New Checkboxes and radio buttons*/
If you fix these lines, your code should work as intended.
Also, I noticed in line 102 that you put // before visibility: hidden;. If you want to comment this line, this syntax is not valid in CSS.
You see, your label and input element are on the same level, and even with position: absolute; your input still a part of the grid. Replaced your input inside the label, added span element and rewrited CSS.
Although in your code was
* New Checkboxes*/
/* Base for label styling */
[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
The first comment was closed incorrect, so next statement didn't work.
.body {
height: 100%;
}
.outer-box {
display: grid;
margin: auto;
background-color: green;
width: 300px;
align-self: center;
grid-gap: 10px;
}
.inner-box {
display: grid;
width: 80%;
background-color: red;
align-self: center;
margin: 5px 5px 5px 5px;
}
/* New Checkboxes*/
/* Base for label styling */
[type="checkbox"] {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked)+span,
[type="checkbox"]:checked+span {
position: relative;
padding-left: 1.95em;
cursor: pointer;
}
/* checkbox aspect */
[type="checkbox"]:not(:checked)+span:before,
[type="checkbox"]:checked+span:before {
content: '';
position: absolute;
left: 0;
top: 0;
width: 1em;
height: 1em;
border: 1px solid grey;
background: transparent;
}
/* checked mark aspect */
[type="checkbox"]:not(:checked)+span:after,
[type="checkbox"]:checked+span:after {
content: '\2713\0020';
position: absolute;
top: .05em;
left: .2em;
font-size: 1.3em;
line-height: 0.8;
color: whitesmoke;
transition: all .2s;
font-family: Arial;
}
/* checked mark aspect changes */
[type="checkbox"]:not(:checked)+span:after {
opacity: 0;
transform: scale(0);
}
[type="checkbox"]:checked+span:after {
opacity: 1;
transform: scale(1);
}
<div class="outer-box">
<div class="inner-box">
Some text
</div>
<div class="inner-box">
<label for="box1" class="container">
<input type="checkbox" id="box1"><span>Selectbox1</span>
</label>
<label for="box2" class="container">
<input type="checkbox" id="box2"><span>Selectbox2</span>
</label>
</div>
</div>
And please, don't use display: grid; for every element. It's very specific setting only for cases, when you really need you use grid.
This question already has answers here:
How to customize <input type="file">?
(18 answers)
Closed 6 years ago.
I'm trying to style a file upload button to my personal preferences, but I couldn't find any really solid ways to do this without JS. I did find two other questions about this subject, but the answers there either involved JavaScript, or suggested Quirksmode's approach.
My major issue with this Quirksmode's approach is that the file button will still have the browser-defined dimensions, so it won't automatically adjust to whatever's used as button that's placed below it. I've made some code, based on it, but it will just take up the space the file button would normally take up, so it won't at all fill the parent div like I want it to.
HTML:
<div class="myLabel">
<input type="file"/>
<span>My Label</span>
</div>
CSS:
.myLabel {
position: relative;
}
.myLabel input {
position: absolute;
z-index: 2;
opacity: 0;
width: 100%;
height: 100%;
}
This fiddle demonstrates how this approach is quite flawed. In Chrome, clicking the !! below the second demo button will open the file dialog anyway, but also in all other browsers, the file button doesn't take up the correct areas of the button.
Is there any more solid way to style the file upload button, without any JavaScript, and preferably using as little 'hacky' coding as possible (since hacking usually brings other problems along with it, such as the ones in the fiddle)?
I'm posting this because (to my surprise) there was no other place I could find that recommended this.
There's a really easy way to do this, without restricting you to browser-defined input dimensions. Just use the <label> tag around a hidden file upload button. This allows for even more freedom in styling than the styling allowed via webkit's built-in styling[1].
The label tag was made for the exact purpose of directing any click events on it to the child inputs[2], so using that, you won't require any JavaScript to direct the click event to the input button for you anymore. You'd to use something like the following:
label.myLabel input[type="file"] {
position:absolute;
top: -1000px;
}
/***** Example custom styling *****/
.myLabel {
border: 2px solid #AAA;
border-radius: 4px;
padding: 2px 5px;
margin: 2px;
background: #DDD;
display: inline-block;
}
.myLabel:hover {
background: #CCC;
}
.myLabel:active {
background: #CCF;
}
.myLabel :invalid + span {
color: #A44;
}
.myLabel :valid + span {
color: #4A4;
}
<label class="myLabel">
<input type="file" required/>
<span>My Label</span>
</label>
I've used a fixed position to hide the input, to make it work even in ancient versions of Internet Explorer (emulated IE8- refused to work on a visibility:hidden or display:none file-input). I've tested in emulated IE7 and up, and it worked perfectly.
You can't use <button>s inside <label> tags unfortunately, so you'll have to define the styles for the buttons yourself. To me, this is the only downside to this approach.
If the for attribute is defined, its value is used to trigger the input with the same id as the for attribute on the <label>.
Please find below a way that works on all browsers. Basically I put the input on top the image.
I make it huge using font-size so the user is always clicking the upload button.
.myFile {
position: relative;
overflow: hidden;
float: left;
clear: left;
}
.myFile input[type="file"] {
display: block;
position: absolute;
top: 0;
right: 0;
opacity: 0;
font-size: 100px;
filter: alpha(opacity=0);
cursor: pointer;
}
<label class="myFile">
<img src="http://wscont1.apps.microsoft.com/winstore/1x/c37a9d99-6698-4339-acf3-c01daa75fb65/Icon.13385.png" alt="" />
<input type="file" />
</label>
The best example is this one, No hiding, No jQuery, It's completely pure CSS
http://css-tricks.com/snippets/css/custom-file-input-styling-webkitblink/
.custom-file-input::-webkit-file-upload-button {
visibility: hidden;
}
.custom-file-input::before {
content: 'Select some files';
display: inline-block;
background: -webkit-linear-gradient(top, #f9f9f9, #e3e3e3);
border: 1px solid #999;
border-radius: 3px;
padding: 5px 8px;
outline: none;
white-space: nowrap;
-webkit-user-select: none;
cursor: pointer;
text-shadow: 1px 1px #fff;
font-weight: 700;
font-size: 10pt;
}
.custom-file-input:hover::before {
border-color: black;
}
.custom-file-input:active::before {
background: -webkit-linear-gradient(top, #e3e3e3, #f9f9f9);
}
<input type="file" class="custom-file-input">
This seems to take care of business pretty well. A fidde is here:
HTML
<label for="upload-file">A proper input label</label>
<div class="upload-button">
<div class="upload-cover">
Upload text or whatevers
</div>
<!-- this is later in the source so it'll be "on top" -->
<input name="upload-file" type="file" />
</div> <!-- .upload-button -->
CSS
/* first things first - get your box-model straight*/
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
label {
/* just positioning */
float: left;
margin-bottom: .5em;
}
.upload-button {
/* key */
position: relative;
overflow: hidden;
/* just positioning */
float: left;
clear: left;
}
.upload-cover {
/* basically just style this however you want - the overlaying file upload should spread out and fill whatever you turn this into */
background-color: gray;
text-align: center;
padding: .5em 1em;
border-radius: 2em;
border: 5px solid rgba(0,0,0,.1);
cursor: pointer;
}
.upload-button input[type="file"] {
display: block;
position: absolute;
top: 0; left: 0;
margin-left: -75px; /* gets that button with no-pointer-cursor off to the left and out of the way */
width: 200%; /* over compensates for the above - I would use calc or sass math if not here*/
height: 100%;
opacity: .2; /* left this here so you could see. Make it 0 */
cursor: pointer;
border: 1px solid red;
}
.upload-button:hover .upload-cover {
background-color: #f06;
}
Any easy way to cover ALL file inputs is to just style your input[type=button] and drop this in globally to turn file inputs into buttons:
$(document).ready(function() {
$("input[type=file]").each(function () {
var thisInput$ = $(this);
var newElement = $("<input type='button' value='Choose File' />");
newElement.click(function() {
thisInput$.click();
});
thisInput$.after(newElement);
thisInput$.hide();
});
});
Here's some sample button CSS that I got from http://cssdeck.com/labs/beautiful-flat-buttons:
input[type=button] {
position: relative;
vertical-align: top;
width: 100%;
height: 60px;
padding: 0;
font-size: 22px;
color:white;
text-align: center;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
background: #454545;
border: 0;
border-bottom: 2px solid #2f2e2e;
cursor: pointer;
-webkit-box-shadow: inset 0 -2px #2f2e2e;
box-shadow: inset 0 -2px #2f2e2e;
}
input[type=button]:active {
top: 1px;
outline: none;
-webkit-box-shadow: none;
box-shadow: none;
}
I just came across this problem and have written a solution for those of you who are using Angular. You can write a custom directive composed of a container, a button, and an input element with type file. With CSS you then place the input over the custom button but with opacity 0. You set the containers height and width to exactly the offset width and height of the button and the input's height and width to 100% of the container.
the directive
angular.module('myCoolApp')
.directive('fileButton', function () {
return {
templateUrl: 'components/directives/fileButton/fileButton.html',
restrict: 'E',
link: function (scope, element, attributes) {
var container = angular.element('.file-upload-container');
var button = angular.element('.file-upload-button');
container.css({
position: 'relative',
overflow: 'hidden',
width: button.offsetWidth,
height: button.offsetHeight
})
}
};
});
a jade template if you are using jade
div(class="file-upload-container")
button(class="file-upload-button") +
input#file-upload(class="file-upload-input", type='file', onchange="doSomethingWhenFileIsSelected()")
the same template in html if you are using html
<div class="file-upload-container">
<button class="file-upload-button"></button>
<input class="file-upload-input" id="file-upload" type="file" onchange="doSomethingWhenFileIsSelected()" />
</div>
the css
.file-upload-button {
margin-top: 40px;
padding: 30px;
border: 1px solid black;
height: 100px;
width: 100px;
background: transparent;
font-size: 66px;
padding-top: 0px;
border-radius: 5px;
border: 2px solid rgb(255, 228, 0);
color: rgb(255, 228, 0);
}
.file-upload-input {
position: absolute;
top: 0;
left: 0;
z-index: 2;
width: 100%;
height: 100%;
opacity: 0;
cursor: pointer;
}
It's also easy to style the label if you are working with Bootstrap and LESS:
label {
.btn();
.btn-primary();
> input[type="file"] {
display: none;
}
}
I used below code for customize the file input type,
<form class="example" action="#">
<!--<input type="file" class="required-entry filename file" name="filename[]" />-->
<div class="fileinputs">
<input type="file" class="file hidden">
<div class="fakefile"><input><img src="ravi/images/button_select.gif" /></div></div>
</form>
style.css
<style type="text/css">
body{
color: black;
font-family: arial;
font-size: 13px;
}
form.example input {
background: url('ravi/images/input_boxes.gif') no-repeat 0 -58px;
border: none;
width: 241px;
height: 20px;
padding-left: 3px;
padding-top: 3px;
}
form.example input:focus {
background-color: transparent;
}
form.example div.fileinputs {
position: relative;
height: 30px;
width: 300px;
}
form.example input.file {
width: 300px;
margin: 0;
}
form.example input.file.hidden {
position: relative;
text-align: right;
-moz-opacity:0 ;
filter:alpha(opacity: 0);
opacity: 0;
z-index: 2;
}
form.example div.fakefile {
position: absolute;
top: 0px;
left: 0px;
width: 350px;
padding: 0;
margin: 0;
z-index: 1;
line-height: 90%;
}
form.example div.fakefile input {
margin-bottom: 5px;
margin-left: 0;
}
</style>
and the page display like below,
when i click select button the browse folder appears and then i select the image file.but in input filed the uploaded image caption not displayed..
i integrated this one from http://www.quirksmode.org/dom/inputfile.html
what is the issue?
You need to add JavaScript code as explained on the page you cited.
If the code you use in testing consists essentially of the HTML and CSS code that you posted, then you are using the “pure CSS” version of the approach described on the page. In the numbered list that explains the basic version, which uses JavaScript too, item 5 describes the feature that is missing from the “pure CSS” version. And that feature is “When the user has selected a file, the visible, fake input field should show the correct path to this file”.
(It won’t actually show the correct path. For security reasons, browsers don’t disclose the real path. But they do let you show the filename part.)