Make div responsive and control text alignment - javascript

I have the following layout (Parent div, two child divs)
wanted to make this layout responsive for browser's width, so anytime the user changes the browser's width this layout should occupies the same area of screen
also, I wanted to middle-text the content, I've tried vertical-align: middle;, display: table-cell;
any suggestions?
Markup here

this is your solution html with css
<html>
<head>
<title>test</title>
<style>
.main{
width:97%;
border:1px solid #000;
height:100px;
padding:1%;
}
.subone{
width:30%;
border:1px solid #000;
float:left;
height:100px;
margin-right:2%;
}
.subtwo{
width:67%;
border:1px solid #000;
float:left;
height:100px;
}
</style>
</head>
<body>
<div class="main">
<div class="subone">
</div>
<div class="subtwo">
</div>
</div>
</body>
</html>

I have created you a fluid layout, with your divs. You do not need to use media queries.
And also to use
vertical-align:middle;
you must put
display:table;
on the parent div and you must not float the divs inside.
You can check this example

will it be helpful???
use diplay:table; for parent and display:table-cell for childs..decalare parents width in percentage with overflow:hidden;
HTML::
<div class="parent">
<div class="first"></div>
<div class="second"></div>
</div>
CSS ::
.parent{
display:table;
width:80%;
border-spacing:4px;
border:2px solid black;
overflow:hidden;
}
.first{
text-align:center;
display:table-cell;
border:2px solid black;
width:100px;
height:200px;
}
.second{
text-align:center;
display:table-cell;
border:2px solid black;
height:200px;
}
FIDDLE

Related

Any way to set parent background-color on the basis of child class using css?

is there any way to set parent background-color on the basis of child class using css?
here is my code
https://jsbin.com/bahodalila/edit?html,css,js,output
if child have red class parent should set background-color red.If child have yellow class parent should set background-color yellow.
<div class="par">
<div class="red">
helli
</div>
</div>
.par{
background-color:red;
}
.par{
background-color:yellow;
}
.par{
width:200px;
height:200px;
border:1px solid blue;
}
.red{
width:100px;
height:100px;
color:red;
border:1px solid;
background-color:green
}
will I use javascript in this case ? is there any way to using css ?
A pseudo element can simulate this:
.par {
width: 200px;
height: 200px;
border: 1px solid blue;
position:relative; /* here and not on child to make it relative to parent*/
z-index:0;
}
.red {
width: 100px;
height: 100px;
color: red;
border: 1px solid;
background-color: green;
}
.red::before {
content:"";
position:absolute;
z-index:-1;
top:0;
left:0;
right:0;
bottom:0;
background:inherit;
}
<div class="par">
<div class="red">
helli
</div>
</div>
<div class="par">
<div class="red" style="background:blue;">
helli
</div>
</div>
CSS and DIV do not use the parent-child methodology. You will need to create each variable in CSS and set each DIV to the CSS variable you want. I did an example using your code to show.
.par{
background-color:red;
width:200px;
height:200px;
border:1px solid blue;
}
.par-yellow{
background-color:yellow;
width:150px;
height:150px;
border:1px solid;
}
.red{
width:100px;
height:100px;
color:red;
border:1px solid;
background-color:green
}
<div class="par">
<div class="par-yellow">
<div class="red">
helli
</div>
</div>
</div>
Unfortunately there are no parent selectors on css. A workaround is only with JS posible. You could do something like:
var redElements = document.querySelectorAll(".red");
for(var i = 0; i < redElements.length; i++){
redElements[i].parentElement.style.backgroundColor = "red";
}

If a div contenteditable is focused?

So Basically I have:
HTML:
<div class="div1">
<div class="as-text-box" contenteditable="true"></div>
</div>
CSS:
.div1{
position:absolute;
height:50px;
width:200px;
background:white;
border:1px solid #ccc;
}
.as-text-box{
position:absolute;
height:30px;
width:90%;
background:#ddd;
top:10px;
left:5%;
}
[contentEditable=true]:focus .div1{
border:1px solid black;
}
To be precise I need to change the border color of div1 to black if as-text-box is focused.
I also tried it with JQuery but still no luck.
JQuery:
$(function(){
if ($(".as-text-box").is(":focus")) {
alert("Has Focus");
} else {
alert("Doesn't Have Focus");
}
});
Jsfiddle: https://jsfiddle.net/2xn5rj2y/
All relies are much appreciated.
You can't navigate to a parent element on a css selector. You'll need Javascript for that.
If you want the parent element to change border when the child one is focused, attach a listener to the blur and focus events of the editable div.
$(".as-text-box").on("focus", function() {
$(".div1").addClass("focusClass");
});
$(".as-text-box").on("blur", function() {
$(".div1").removeClass("focusClass");
});
.div1{
position:absolute;
height:50px;
width:200px;
background:white;
border:1px solid #ccc;
}
.as-text-box{
position:absolute;
height:30px;
width:90%;
background:#ddd;
top:10px;
left:5%;
}
.div1.focusClass {
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="div1">
<div class="as-text-box" contenteditable="true"></div>
</div>
Your selector:
[contentEditable=true]:focus .div1
is trying to select an element with class div1 which is a descendant of a contentEditable element which is focused. This is clearly backwards as the .div1 div is the parent of the contentEditable div.
Simply removing the .div1 part of the selector makes it work:
.div1{
position:absolute;
height:50px;
width:200px;
background:white;
border:1px solid #ccc;
}
.as-text-box{
position:absolute;
height:30px;
width:90%;
background:#ddd;
top:10px;
left:5%;
}
[contentEditable=true]:focus{
border:1px solid black;
outline:none;
}
<div class="div1">
<div class="as-text-box" contenteditable="true">
</div>
</div>
It would also work to reverse the order if you have other reasons for needing to select on div1, but I feel the above solution is the simplest absent other requirements.
I believe you are looking to customize the outline css attribute.
CSS
outline:none;
https://jsfiddle.net/2xn5rj2y/5/
If you are trying to target the parent to have a border, then jquery .parent().css("border","1px solid #000"); would work.
There's no way to do it through css (to select parent), you can do it via jQuery with events focusin and focusout where you can do with parent element what you want.
$('.as-text-box').on('focusin', function(){
/** on focus, set border to a parent **/
$(this).parent().css({'border':'1px solid black'});
}).on('focusout', function(){
/** on lost focus, remove border **/
$(this).parent().css({'border':''});
});
.div1{
position:absolute;
height:50px;
width:200px;
background:white;
border:1px solid #ccc;
}
.as-text-box{
position:absolute;
height:30px;
width:90%;
background:#ddd;
top:10px;
left:5%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div1">
<div class="as-text-box" contenteditable="true">
</div>
</div>
Short & simple. When it has focus border turns black, if it loses focus it goes back to #ccc.
$('.as-text-box').on('focus', function() {
$('.div1').css("borderColor", "black");
$(this).on('focusout', function() {
$('.div1').css("borderColor", "#ccc");
});
});
JSfiddle

How can i make automatically append divs with based on screen resolution both sides of my text

I am trying make it as a name of table besides table name red color dashes is there i want to append this dashes dynamically based on screen size . on both sides of my text . sorry for my bad English. give me a Suggestion how to make it ?
.container{
width:100%;
height:30px;
background:blue;
text-align:center;
}
.container span{
color:#fff;
}
.container .dashes{
display:inline-block;
width:5px;
height:5px;
background:red;
vertical-align:middle;
}
<div class="container">
<div class="dashes"></div>
<span>Name</span>
<div class="dashes"></div>
</div>
Just set the .dashes width as per your screen size using media-query.
.container{
width:100%;
height:30px;
background:blue;
text-align:center;
padding-top: 10px;
}
.container span{
color:#fff;
}
.container .dashes{
display:inline-block;
width:200px;
height:0;
background:red;
vertical-align:middle;
border: 2px dashed blue;
}
<div class="container">
<div class="dashes"></div>
<span>Name</span>
<div class="dashes"></div>
</div>

show div with relative position next to another div

I want to show a textbox next to another div, but it it appears below instead. The position of the textbox must be absolute. This demo demonstrates the issue. Thank you.
css
#show{
border:1px solid gray;
cursor:pointer;
float:left;
}
#textBox{
display:none;
position:absolute;
border:1px solid gray;
width:100px;
height:100px;
float:left;
background-color:pink;
}
#hide{
clear:left;
cursor:pointer;
text-align:center;
}
html
<div id = "show">click me to show test box</div>
<div id = "textBox">text</div>
<div id = "hide">hide text box</div>
js
$('#show').click(function(){
$('#textBox').show();
});
$('#hide').click(function(){
$('#textBox').hide();
});
Most easiest way out would be to replace your css with the below code:
#show{
border:1px solid gray;
cursor:pointer;
float:left;
}
#textBox{
display:none;
position:absolute;
border:1px solid gray;
width:100px;
height:100px;
margin-left:165px;
background-color:pink;
}
#hide{
clear:left;
cursor:pointer;
text-align:center;
margin-left:76px;
}
Check the fiddle:http://jsfiddle.net/6gk0hybj/4/
Another method as mentioned is the comments can be found at:http://jsfiddle.net/6gk0hybj/5/
If you want it more dynamic , we need to change a bit of javascript. Let me know if you want it to be more dynamic

slideDown menubar slides down other document

I made a menubar using a <div> tag. When it is clicked the <div> below it slides down, and another content below those two <div>'s should stay in its place, but it goes down too.
You can see picture here
As you see in this picture when I click to slide down <div> , the other <div> in bottom of that goes down too. How to fix it?
HTML
<div id="slide">click to slide down</div>
<div id="panel">Hello! this is my first slide down example.</div>
<div>this is another div</div>
CSS
#slide{
text-align:center;
border:solid 1px #101010;
width:500px;
height:30px;
background:#cccccc;
}
#panel{
text-align:center;
border:solid 1px #101010;
width:500px;
height:200px;
display:none;
background:#ff0000;
}
jQuery
$(document).ready(function(){
$('#slide').click(function(){
$('#panel').stop().slideToggle(570);
});
});
You can make a few position changes in your CSS, and place your second <div> inside your first. top:100% mean's that your second div will animate from the bottom of your first div:
HTML
<div id="slide">click to slide down
<div id="panel">Hello! this is my first slide down example.</div>
</div>
<div>this is another div</div>
CSS
#slide{
text-align:center;
border:solid 1px #101010;
width:500px;
height:30px;
background:#cccccc;
position:relative;
}
#panel{
position:absolute;
top:100%;
box-sizing:border-box;
text-align:center;
border:solid 1px #101010;
width:100%;
height:200px;
display:none;
background:#ff0000;
}
JSFiddle
Try this:
HTML
<div id="menu_container">
<div id="slide">click to slide down</div>
<div id="panel">Hello! this is my first slide down example.</div>
</div>
<div>this is another div</div>
CSS
#menu_container { position:relative; }
#panel { position:absolute; top:100%; left:0; }
Replace your code with this
#panel{
text-align:center;
border:solid 1px #101010;
width:500px;
height:200px;
display:none;
background:#ff0000;
z-index: 10;
position: absolute;
}

Categories

Resources