Centering a div in the remaing space left by a floating div? - html

Right, so I have the site in a 100% width wrapper. Inside are two divs. One is floated left with a fixed width of 900px, and... I'm trying to get the second one to be centered in the remaining space between the first div and the right of the screen.
I've tried variations of floating left/right relative/absolute positioning, margin auto etc but none of it is working.
Could anyone help please?

Floated elements do not take up space inside their container by definition, it's therefore impossible to center 'the other content'.
In general, don't use float for content that isn't actually floating, like images inside an article. For layout you should usually prefer absolute positioning or other more flexible constructs.
You could for example just position the sidebar absolutely and give the container a margin equal to the size of the sidebar, which would solve your problem instantly.
If this isn't possible, for example because the elements are both flexible height, you could position 2 containers next to eachother with either float:left, display:inline-block or display:table-cell. Centering inside the right container is then trivial.

If my understanding is correct, you are trying to achieve something like this:
<div class="container"><div class="first"></div><div class="second"></div></div>
CSS:
.first {
width: 100px;
height: 50px;
background-color: black;
}
.second {
position: absolute;
height: 50px;
background-color: red;
left: 100px;
right: 0;
top: 0;
}
.container {
width: 500px;
height: 100px;
background-color: green;
position: relative;
}
Here's a fiddle. If I misunderstood and you don't want to have the second div stretched to full remaining width, instead you just want to center it, then simply modify the css like this:
.second {
position: absolute;
width: 100px;
height: 50px;
background-color: red;
left: 100px;
right: 0;
top: 0;
margin-left: auto;
margin-right: auto;
}

Here is an example using CSS table settings on the divs. It stays together well when resized, and when content is added. An additional benefit is that both the left and right columns will stay the same height, and will wrap around the content. If you want the left and right columns to appear to have different heights, just insert and style nested divs.
In the first example, the right div has text-align:center. The nested div within is display:inline-block which recognizes the text-align:center on the parent. Therefore, the nested div is truly centered. Inline-block will also wrap around its content's width, and that's helpful if your centered element has a set width, or a small variable width. If your content fills its parent like the example, just set a max-width.
In the second example the right div has equal padding on the left and right to "center" its contents.
Note: If you support IE6 and IE7 - CSS table settings and inline-block have excellent browser compatibly, except for IE6 and IE7. For the css table settings, there is a polyfill. Also IE6 and IE7 don't recognize inline-block, but they do recognize display:inline. Just use *display:inline; *zoom:1; for IE6 and IE7.
Give it a good test and see what you think.
JSFiddle Example
CSS Table Browser Compatibility Chart
Inline-block Browser Compatibility Chart
CSS
.table-holder {
display:table;
width:100%;
}
.table-row {
display:table-row;
}
.table-cell-right {
display:table-cell;
width:90px; /* small px used for example */
background-color:#ccffcc;
padding:.5em;
vertical-align:top;
}
.table-cell-left {
display:table-cell;
background-color:#ccffff;
text-align:center;
}
.center-nested {
width:85%; /*set to desired width */
display:inline-block;
padding:.5em;
text-align:left;
vertical-align:top;
background-color:#ffffcc;
}
/* ------------ center using padding -------------*/
.padded-table-cell-left {
display:table-cell;
background-color:#ccffff;
padding:0em 2em 0em 2em;
}
.padded-center-nested {
padding:.5em;
background-color:#ffffcc;
}
HTML
<p>Table using inline-block to center</p>
<div class="table-holder">
<div class="table-row">
<div class="table-cell-right">Left Container set at 90px</div>
<div class="table-cell-left">
<div class="center-nested">
This is a CSS table. The blue parent cell has text-align:center. The yellow nested div is display:block, which responds to the text-align:center on the parent: therefore, the yellow div is truely centered. Lorem ipsum dolor sit amet, usu ad fugit indoctum molestiae, inermis mediocrem in quo, sed affert sadipscing no. Per solum rebum forensibus id, his prima everti epicuri te. Vis diam tation ei, audire tritani adipiscing eam at. Ea modo nonumy feugiat usu, pri an ubique electram. Aeque adversarium ea vim. Pri te novum veritus scriptorem, vero blandit mei eu.
</div>
</div>
</div>
</div>
<p>Table using padding to center</p>
<div class="table-holder">
<div class="table-row">
<div class="table-cell-right">Left Container set at 90px</div>
<div class="padded-table-cell-left">
<div class="padded-center-nested">
The yellow centered cell appears centered because the parent has equal left and right padding. - Lorem ipsum dolor sit amet, usu ad fugit indoctum molestiae, inermis mediocrem in quo, sed affert sadipscing no. Per solum rebum forensibus id, his prima everti epicuri te. Vis diam tation ei, audire tritani adipiscing eam at. Ea modo nonumy feugiat usu, pri an ubique electram. Aeque adversarium ea vim. Pri te novum veritus scriptorem, vero blandit mei eu.
</div>
</div>
</div>
</div>

Do you have a padding on the divs?
if you have you can set box-sizing to border-box and float the first box to the left and leave the second one as is.
Do you want to achieve something like this?
http://jsbin.com/zenedabiyoyu/1/edit

One possible solution is to nest the right div inside of another div with margin-left: 900px and then use margin: 0 auto on the inner div to center it.
Check out the technique in this example: http://jsfiddle.net/r15fL0de/
Note: For the sake of the fiddle I used 300px instead of 900px, but the principle is the same

Related

css - Postion:fixed works on desktop but not mobile

I am creating a page for comments, which containers users' comments and a comment input, the comment input is fixed at the bottom.
The problem is it works fine on Desktop, but when I try on my iPad iOS 11 the comment input box scrolls with the page, not fixed at the bottom.
Here is my code: JSFiddle
CSS
.xi-ipad-scroll {
height:500px;
overflow:hidden;
background:green;
}
.xi-comment {
width: 40%;
bottom: 0;
position: fixed;
}
.xi-comment-box {
width: 100%;
font-size: 15pt;
font-weight: 700;
}
.xi-comment-send {
bottom: 15px;
position: fixed;
}
HTML
<div class="xi-ipad-scroll">
<div class="xi-main-title">Bình luận</div>
<div class="xi-comment-list">
<ul>
<li>
<div>Quang Anh Nguyễn</div>
<div>Lorem ipsum dolor sit amet, illum prompta sadipscing cu sit. Ea mei lorem erroribus honestatis, laoreet torquatos eu mel, nam dicant labitur tractatos et. Cu est alia altera consulatu, vim falli detracto reformidans in, novum forensibus eu sit. At etiam erroribus prodesset qui, eam veniam laoreet at. Ea mei natum erant.</div>
</li>
<li>....</li>
</ul>
</div>
<div class="xi-box xi-comment">
<textarea type="text" placeholder="Comment..." class="resizable xi-comment-box" rows="1"></textarea>
</div>
</div><!--iPad-->
I searched on internet and I got solutions like putting -webkit-backface-visibility: hidden; or z-index:100 but none of them works
If I'm understanding what you are trying to do correctly, your "position:fixed" is actually what is causing this. Fixed position will always show on the screen. More info on fixed position. If you want it to be at the bottom of all content you will need to remove the fixed from both -send and -comment.
.xi-comment {
width: 40%;
bottom: 0;
***position: fixed;***
}
.xi-comment-send {
bottom: 15px;
***position: fixed;***
}
This now leads to a new problem, you have set an absolute height and have hidden anything outside of that height. You either need to extend the height, remove the hidden, or move the comment section outside of that div.
I put together a codepen to show this. I think you actually are having the same issue on desktop, I just don't believe you had enough content for you to realize it.

How to change the background color of TEXT in css

Let's take for example :
<div id="div1">This is a sentence.</div>
The <div> fill up 100% of the line.
The text itself, just a little portion of that.
I want to give the text a background color WITHOUT giving the whole line a background color.
I can change the html and add a span of course. (And use this span with background-color)
<div id="div1"><span>This is a sentence.</span></div>
But is it possible in pure css, without changing the html ?
PS : I'm making a css code for hyper terminal.
SOLUTION (for all div) :
div{display:table;}
This totally destroy tmux behavior (inside hyper) but maybe i'm asking for too much...
echo -e "\n\n" also becomes a problem. :p
SOLUTION FOR THIS PROBLEM : (empty div take no space)
div:after{content: " ";}
You can use :before to apply CSS on
#div1 {
color: blue; // hide original text
background-color: blue;
position: relative;
}
#div1:before {
content: 'This is a sentence.';
background-color: purple;
display: inline;
color: white;
position: absolute;
}
<div id="div1">This is a sentence.</div>
If you do not need #div1 to be full width, than simply apply #div1 {display: inline-block} to it and it will act as span element
You can use display:table-cell
table-cell: Let the element behave like a <td> element - W3Schools
Also, unless you want to go back to the dark ages, this is well-supported
#div1 {
background: red;
color: white;
display: table-cell;
}
<div id="div1">This is a sentence.</div>Lorem ipsum dolor sit amet, pri cu quod audiam molestie, sit an modo probo conceptam, vim nemore quodsi no. Postea possit ne pro. Ne mel mollis oportere laboramus. Eu dico eius omnes ius. Id vis nibh adipiscing, maiorum suscipit ius eu. Sonet viris antiopam
nec in, est id equidem omnesque cotidieque, tritani detraxit qui cu.
[Block element always takes the width as 100% while inline block takes the width of the element itself. so try using inline block CSS to the element inside a div]
Display:inline-block;
1
As for eg:
[https://codepen.io/nazarbecks/pen/GEMdaG][1]
Codepen link
you can set #div1 as inline-block.
#div1 {
display: inline-block;
background-color: color;
}

Div is not floating left

I'm still relatively new to html and css, and I'm not able to figure this out. I have three divs with some text and I want them to be next to each other. I set them to float: left but they are not doing so.
.threethings {
width: 20%;
}
.threethings div {
text-align: center;
position: relative;
float: left;
}
<div class="threethings">
<div><span>Style</span>
<br>
<p>Lorem ipsum dolor sit amet, ius te ullum indoctum, sanctus consequat eum te. Nemore recteque necessitatibus et eos.</p>
</div>
<div><span>Style</span>
<br>
<p>Lorem ipsum dolor sit amet, ius te ullum indoctum, sanctus consequat eum te. Nemore recteque necessitatibus et eos.</p>
</div>
<div><span>Style</span>
<br>
<p>Lorem ipsum dolor sit amet, ius te ullum indoctum, sanctus consequat eum te. Nemore recteque necessitatibus et eos.</p>
</div>
</div>
try this
.threethings {
width : 100%;
}
.threethings div {
width: 20%;
text-align: center;
position: relative;
float : left;
}
so what happens is divs are floated in your case but not side by side as you are giving 20% width to parent , due which children cant fload side by side.
what i did was give parent some large width and children some small width, so that parent can accomodate children floating side by side.
well as div is block. width:100% might not be necessary
You're floating the parent class - not the divs containing paragraphs. Add the .threethings class to the 3 nested divs to instruct them to float their proceeding divs up alongside them.
Note that you may also need to give the divs a fixed width, as the paragraphs you have on there may exceed the length of your html body, which in turn would force them onto a new line.
Try this solution
.threethings {
width:100%;
}
.threethings div {
width:33%;
position: relative;
float : left;
}

Using CSS to place element over another

I need to place an <input> exactly over an <a>, have the edges of the <input> link up with the outer boarder of the <a>, and not change any other presentation. Please see https://jsbin.com/cotogimaqo (script duplicated below).
The following information is known about a.link and can (but not must) be used in the CSS.
width 39px
height 17px
outerWidth w/o margin 99px
outerWidth w/ margin 159px
outerHeight w/o margin 77px
outerHeight w/ margin 137px
In Attempt 1, I thought I could use position:absolute and pin the top, right, bottom, and left positions, however, it doesn't work.
In Attempt 2, Attempt 3, and Attempt 4, I tried several other things, but still it doesn't work.
Attempt 5 is pretty accurate, but I have no idea why it is, and was accomplished by trial and error.
How can this be accomplished? Please provide explanation on "why" it works, and not just a quick fix.
PS. Note that I asked a similar question How do I place one element precisely over another element?, however, did not get any answers, and that question was related to JavaScript. This question is very different and only deals with CSS.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Testing</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js" type="text/javascript"></script>
<style type="text/css">
.box{margin:5px;padding:5px;border: 1px solid black;width:200px;}
.link{margin:30px;padding:20px;border: 10px solid black;}
.divWrapper{margin:0;padding:0;display:inline; position:relative;}
#wrapper{width:600px;}
#info{float:right;}
.divWrapper input{margin:0;border: 1px dashed red;cursor:pointer;} /*.divWrapper input{z-index:9999;opacity:0;} */
/* this is the part I need help with.
Known information about a.link:
width 38px, height 18px, outerWidth w/o margin 98px, outerWidth w/ margin 158px, outerHeight w/o margin 78px, outerHeight w/ margin 138px
*/
#div1 input{
position:absolute;top:0;right:0;bottom:0;left:0;
}
#div2 input{
position:absolute;top:0;left:0;
width:98px;height:78px;
}
#div3 input{
position:absolute;top:0;left:0;
width:98px;height:78px;
margin:30px;
}
#div4 input{
position:absolute;top:30px;left:30px;
width:98px;height:78px;
}
#div5 input{
position:absolute;top:-30px;left:30px;
width:98px;height:78px;
}
</style>
<script type="text/javascript">
/*The following JavaScript is just to provide link information and is not related to the question */
$(function () {
var link=$('#div1 a.link');
var tableRows=$('#info tr');
tableRows.eq(0).find('td').eq(1).text(link.css('width'));
tableRows.eq(1).find('td').eq(1).text(link.css('height'));
tableRows.eq(2).find('td').eq(1).text(link.outerWidth(false)+'px');
tableRows.eq(3).find('td').eq(1).text(link.outerWidth(true)+'px');
tableRows.eq(4).find('td').eq(1).text(link.outerHeight(false)+'px');
tableRows.eq(5).find('td').eq(1).text(link.outerHeight(true)+'px');
$('.divWrapper').click(function(){alert('input clicked.');return false;})
});
</script>
</head>
<body>
<div id="wrapper">
<div id="info">
<h2>a.link info</h2>
<table>
<tr><td>width</td><td></td></tr>
<tr><td>height</td><td></td></tr>
<tr><td>outerWidth w/o margin</td><td></td></tr>
<tr><td>outerWidth w/ margin</td><td></td></tr>
<tr><td>outerHeight w/o margin</td><td></td></tr>
<tr><td>outerHeight w/ margin</td><td></td></tr>
</table>
</div>
<div id="div1" class="box"><p>Attempt 1</p>Vel labitur sanctus antiopam at. <div class="divWrapper"><a class="link" href="javascript:void(0)">LINK</a><input type="file" name="bla"></div>. Ludus temporibus et duo. Nullam consequuntur comprehensam id eos, nec ad quot mucius oportere.</div>
<div id="div2" class="box"><p>Attempt 2</p>Vel labitur sanctus antiopam at. <div class="divWrapper"><a class="link" href="javascript:void(0)">LINK</a><input type="file" name="bla"></div>. Ludus temporibus et duo. Nullam consequuntur comprehensam id eos, nec ad quot mucius oportere.</div>
<div id="div3" class="box"><p>Attempt 3</p>Vel labitur sanctus antiopam at. <div class="divWrapper"><a class="link" href="javascript:void(0)">LINK</a><input type="file" name="bla"></div>. Ludus temporibus et duo. Nullam consequuntur comprehensam id eos, nec ad quot mucius oportere.</div>
<div id="div4" class="box"><p>Attempt 4</p>Vel labitur sanctus antiopam at. <div class="divWrapper"><a class="link" href="javascript:void(0)">LINK</a><input type="file" name="bla"></div>. Ludus temporibus et duo. Nullam consequuntur comprehensam id eos, nec ad quot mucius oportere.</div>
<div id="div5" class="box"><p>Attempt 5</p>Vel labitur sanctus antiopam at. <div class="divWrapper"><a class="link" href="javascript:void(0)">LINK</a><input type="file" name="bla"></div>. Ludus temporibus et duo. Nullam consequuntur comprehensam id eos, nec ad quot mucius oportere.</div>
</div>
</body>
</html>
Change every div -> input to:
input {
height: 41px;
left: 13px;
position: absolute;
top: -12px;
width: 58px;
}
It happens because div.divWrapper is wrapped around a.link, and takes on the width of the child element, but only the height of the inline child element without the padding, margin, or border.
So, when one uses absolute positioning on the <input>, it needs to be raised by the padding and border amount.
As for horizontal positioning, left 0 would place it at the outer left margin of <a>, but since it should be placed at the border edit, it needs to be moved right by the margin.
The final CSS should be:
#divWorking input{
position:absolute;
top:-30px; /* padding (20px) plus border (10px) */
left:30px; /* same as margin (30px) */
width:98px; /* text in <a> width plus <a> padding and border width */
height:78px; /* text in <a> height plus <a> padding and border height */
}

i want my image size to be larger than my div size

I have a div that is a specific width (80%) and i have an image in that div. i want the image to stretch to 100% of the page, which would overflow the 80% div. how can i do this. would i set the image width to 140% ? i dont know how to go over the containing div that the image is in. i have tried using VW and float, and various positioning, but no luck yet.i want the image above everything else and not inside or behind.
<section style="background:linear-gradient(#F5F1FD 70%, #ffffff 30%);">
<div id="bigbtn" class='bigbtn' style="height:800px; width:1600px; overflow:hidden; cursor:pointer;">
<div style="width:2400px; height:800px; float:left; position:absolute;" id="clkcont" class="clkcont">
<div id="bgdsply2" style="float: left; display: inline-block; height:800px; width: 1600px;">
<img src="admin/showroom/clocks/<?php echo $filename ?>" style="width:1600px; height:800px;" alt="" id="bigclk"/>
</div>
<div style="display: inline-block; width: 300px; margin-top: 200px; margin-left: 200px;">
Quo ne facer impedit euripidis, inermis nonumes vis ex, fabulas menandri postulant ad nam. Animal disputationi ad qui, case natum cotidieque ei mel, et diam prima posse vel. Usu admodum lobortis inciderint eu, oratio tritani et vis, ea eum nemore deseruisse. Dicam conceptam interpretaris sed ea. Ex mei everti abhorreant disputationi.
</div>
</div>
</div>
</section>
"section" is set to 80%width. bigbtn i want to reach the left side of the visible bvrowser window and the right side also, so it will reach across 100% of the visible window while the rest of the page is at 80% width. so far no solutions have worked
I don't know if it's what you want but:
<style>
div {
width:80%;
}
img {
width: 100%;
position: absolute;
}
</style>
<div>
<img src="/img/foo.jpg">
</div>
Let me know if it solves your issue.
You can set the width of the image in pixels, and (assuming that pixel value is wider than the width of the div) the image will overflow.
Check out This fiddle:
body { width: 500px; }
div {
border: solid gray 1px;
width: 80%;
}
img { width: 500px; }