Making div element wide enough for a text string - css

Is it possible to make the width of the absolutely positioned div big enough to contain the text, but not bigger?
I tried inline-block, but it doesn't seem to work, because once I set the position:absolute the div will take the max width of the parent element.
Could you suggest what changes I can make to the child element, so that it float in the center of the parent div and has smallest width possible but fits the text string inside.
Here is the jsfiddle: http://jsfiddle.net/hmmrfmk0/
<div class='grand-parent'>
<div class='parent'>
<div class='child'>
long long long string
</div>
</div>
.grand-parent {
position: absolute;
width:500px;
height:100px;
background-color:red;
}
.parent {
position:relative;
margin: 0;
padding: 0;
width:500px;
height:100px;
}
.child {
position:absolute;
margin:auto;
top:0;
bottom:0;
left:0;
right:0;
background-color: #ccccc0;
border: 1px solid black;
height:15px;
display:inline-block;
}
Thx!

Yes it is. Use white-space:nowrap and remove top, left, right, bottom 0 values and the position the element where you want it. In this case, dead center of the parent div.
.grand-parent {
width: 500px;
height: 100px;
background-color: red;
margin-bottom: 10px;
}
.parent {
position: relative;
margin: 0;
padding: 0;
width: 500px;
height: 100px;
}
.child {
position: absolute;
margin: auto;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #ccccc0;
border: 1px solid black;
height: 15px;
white-space: nowrap;
}
<div class='grand-parent'>
<div class='parent'>
<div class='child'>
long long long string
</div>
</div>
</div>
<div class='grand-parent'>
<div class='parent'>
<div class='child'>
long long long string ong long long string
</div>
</div>
</div>

Your .child class includes left:0 and right:0
This will force the div to be as wide as it's parent, inline-block will not over-ride this.
Remove right:0 from .child and it should work as you want

Try this fiddle. If you display the parent as table-cell and put vertical-align as middle then you can position vertically.
.grand-parent {
position: absolute;
width:500px;
height:100px;
background-color:red;
}
.parent {
position:relative;
margin: 0;
padding: 0;
width:500px;
height:100px;
text-align: center;
display: table-cell;
vertical-align: middle;
}
.child {
background-color: #ccccc0;
border: 1px solid black;
height:15px;
display:inline-block;
vertical-align: middle;
}

Related

Div in div with overaly: scroll

Is it somehow possible to ensure that inner div with class .b can be displayed over the outer div (class .a). For non lot of inner div is hidden. Simple z-index doesn't help and I not able to move inner div element outside the outer one.
I have html file :
<div class="a">
<span class="aa">AAAAA
<div class="b">
1.A
</div>
</span>
...
</div>
And css style like this :
.a {
position:absolute;
width:500px;
height:50px;
border:1px solid red;
overflow-y: auto;
overflow-x: hidden;
}
.b {
display: none;
background: #FFF;
position:absolute;
left: 10px;
top: 10px;
width:150px;
height:150px;
border:1px solid blue;
z-index:10;
}
.aa {
position: static;
}
.aa:hover .b {
display: block;
}
Here is js fiddle with my specification:
Jsfiddle
I have found solution by myself. Removing position: absolute from div with class a helped.
.a {
width:500px;
height:50px;
border:1px solid red;
overflow-y: auto;
overflow-x: hidden;
}

CSS Position Div Over Another Div

I have two divs that I am trying to stack over each other but the one I want on top is not showing. I want the blue background div to lay on top of the red background div. Any advice? The reason why I want to overlay the blue div is because the container is a centered grid and I want the red div to be the background for the first half of the page.
JSFIDDLE
CSS
.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: relative;
background: red;
}
.buddy-content {
position: absolute;
top: -629px;
z-index: 10;
background: blue;
}
.container {
max-width: 1000px;
margin: 0 auto;
overflow:hidden;
position:relative;
padding: 0 10px;}
You have made the second div absolute so you don't need to give the negative value for top. The second div is hiding because you top -629px; Try making the top:0 and see. And also for your current code. Remove the overflow hidden and put z-index like this:
.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: relative;
z-index:9;
background: red;
}
.buddy-content {
position: absolute;
top: -629px;
z-index: 10;
background: blue;
}
.container {
max-width: 1000px;
margin: 0 auto;
width:200px;
height:200px;
position:relative;
padding: 0 10px;
}
.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: absolute;
background: red;
}
.buddy-content {
position: absolute;
z-index: 10;
background: blue;
}
<div class="buddy BlueGradient">
</div>
<div class="container">
<div class="buddy-content">
ROGER
</div>
</div>
https://jsfiddle.net/kt77cp3e/6/
just add z-index : higher to the div that you want to show on top and set z-index low to the other one ..
ant one thing your code is working good just you need to remove " top : -629px;"
that thing is not allowing blue div to be on top just it is showing at the -629 px position..!!!!
If you can update your code like this, it may solve the issue:
Demo:https://jsfiddle.net/kt77cp3e/7/
CSS:
html, body {
height:100%;
width:100%:
}
.container {
width:50%;
height:100%;
background:red;
position:relative;
}
.container>div {
position:relative;
left:0;
right:0;
}
.container>div:first-child {
top:0;
height:50%;
background:blue
}
.container>div:last-child {
bottom:0;
height:50%;
background:green
}
HTML:
<div class="container">
<div></div>
<div></div>
</div>
Update: Considering the latest updated code, I think you should remove overflow:hidden from the container styles. That should do the trick
You should set the dimension on the .container div.
CSS:
.container {
position:relative;
width:100px; //You may modify these values
height:100px
}
Demo: https://jsfiddle.net/kt77cp3e/1/
.buddy { width: 50%; height: 629px; display: inline-block; position: relative; background: red;}
.buddy-content { position: absolute; top: 0px; z-index: 10; background: blue; }
.container {max-width: 1000px; margin: 0 auto; overflow:hidden; position:relative; padding: 0 10px; position: relative;}
<div class="container">
<div class="buddy BlueGradient">
<div class="buddy-content">ROGER</div>
</div>
</div>
This brings the text "Roger" with blue background on top of the red background

Margin-left and position left give different values with the same percentage

I have a div with:
width:100%;
max-width:100%;
position:relative;
overflow:hidden;
An immediate child of this div is:
.my-class {
position:absolute;
bottom:6px;
padding-left:12px;
}
I want the child div to line up with some other content. Nothing outside of this div is effecting it. When I use left:30% I get one number, when I use margin-left:30% I get a different one (which in this case is what I want).
Does margin-left take padding into account and left doesn't?
Or is there some other factor I've not considered?
Yes.Padding is affecting the margin.Take a look at this example:
div, span {
border: 1px solid #000;
height: 80px;
width: 80px;
}
.left, .marginLeft {
background: #aaf;
margin: 10px 0 0 10px;
padding: 10px;
position: relative;
}
.abs {
background: #faa;
position: absolute;
top: 0;
}
.left .abs {
left: 100px;
}
.marginLeft .abs {
margin-left: 100px;
}
<h3>Left</h3>
<div class="left">
parent
<div class="abs">left</div>
</div>
<h3>Margin left</h3>
<div class="marginLeft">
parent
<div class="abs">margin left</div>
</div>

Keeping a DIV at bottom-center of its parent DIV

My HTML structure is basically this -
<div id="header">
<div class="container">
</div>
</div>
<div id="content">
<div class="container">
</div>
</div>
<div id="footer">
<div class="container">
</div>
</div>
Ignore any elements except <div id="header">
I want to align <div class="container"> inside <div id="header"> at exactly bottom center. I'm using the following CSS code-
#header{ width:1062px; height:326px; background-color:#110000; text-align:center; position:relative; }
#header .container{ width:940px; height:262px; background-color:#220000; margin:0px auto; position:absolute; bottom:0px; }
There are height differences between the parent (#header) and child (#header .container) DIVs. Removing position:absolute; from the child centers it but it sticks to the parent's top instead of bottom. Keeping position:absolute; sticks it at the bottom but aligns it to the left.
How do I align it both center AND bottom at the same time?
I tried all the solution above but it didn't work when you resize the browser window. This solution is mostly to be applied when you don't know the element's width. Or if the width is changed on resize.
After making some research I tried the following and it worked perfectly on all screen sizes.
#somelement {
position: absolute;
left: 50%;
bottom: 0px;
-webkit-transform: translateX(-50%);
transform: translateX(-50%)
}
I shared this for anyone still facing this issue.
try in this way:
#header .container{
width: 940px;
height: 262px;
background-color: #220000;
position: absolute;
bottom: 0 ;
left: 50%;
margin-left: -470px;
}
try this
#header .container {
width: 940px;
height: 262px;
background-color: #220000;
margin: 0px auto;
position: absolute;
bottom: 0px;
left: 61px;
}
use this:
#header{
width:1062px; height:262px; background-color:#110000; text-align:center;
position:relative;text-align: center; vertical-align: bottom;padding-top:64px;
}
#header .container{
width:940px;
height:262px;
background-color:#999000;
margin:0px auto;
bottom:0px;
margin-bottom: 0px;
padding-top: 0px;
}
Here the jsfiddle
UPDATE:
As DenisVuyka said in comment, i should add that the above sample was as answer to this particular question with fixed height for DIV.
If you want that height of DIV don't break up things then for example you should use padding-top:10%; in the #header and height:100% in #header .container CSS.
#header{
width:462px; height:262px; background-color:#110000; text-align:center;
position:relative;text-align: center; vertical-align: bottom;padding-top:10%;
}
#header .container{
width:300px;
height:100%;
background-color:#999000;
margin:0px auto;
bottom:0px;
margin-bottom: 0px;
padding-top: 0px;
}
Here is a demo: http://jsfiddle.net/d6ct6/ .
I was trying to get this to work in my project as well. I've edited this jsfiddle:
http://jsfiddle.net/d6ct6/
<div id="header">
<div class="container">
</div>
</div>
#header {
height:100vh;
background-color:#110000;
position:relative;
}
#header .container{
width:300px;
height:40px;
background-color:#999000;
bottom:0px;
position:absolute;
left:calc((100% - 300px)/2);
}
But I've found this only works when the width of .container is fixed.
If the width of .container is not fixed you would need javascript to find it's width and then change that width in the calc.
When the widths are responsive, use this:
HTML
<div id="header">
<div id="container">
</div>
</div>
CSS
#header {
height:100vh;
background-color:#110000;
position:relative;
}
#container{
width:300px;
height:40px;
background-color:#999000;
bottom:0px;
position:absolute;
}
JS
$(document).ready(function () {
var parentWidth = $('#header').width();
var trapWidth = $('#container').width();
var deadCenter = (parentWidth - trapWidth);
var deadHalf = Number( deadCenter / 2 );
$('#container').css("right", deadHalf);
});
In case you care more about having the inside div aligned in the center and can manually set the vertical alignment.
DEMO Height I used was first div height - second div height.
#header .container{ width:940px; height:262px; background-color:red; margin:0 auto; position:relative; top: 64px; }
I would take advantage of CSS table display properties and do the following:
#header {
width:1062px;
height:326px;
background-color:#110000;
text-align:center;
display: table-cell;
vertical-align: bottom;
}
#header .container {
width:900px;
height:262px;
background-color:#cccccc;
color: white;
display: inline-block;
}
Set the #header block to display: table-cell and set vertical-align: bottom to align the child's bottom edge to the bottom edge of the parent.
The child .container element had display: inline-block and this will allow it to respond the text-align: center property of the parent.
This will work regardless of the width of the child .container.
Demo fiddle: http://jsfiddle.net/audetwebdesign/p9CxE/
This same problem was bedevilling me for an hour or so, until I realised I could add an intermediary div; this separated the vertical alignment issue from the centering.
.dparent {
border: 1px solid red;
width: 300px;
height: 300px;
position: absolute;
}
.dchild {
border: 1px solid blue;
margin-left: auto;
margin-right: auto;
width: 200px;
height: 100px;
bottom: 0px;
position: relative;
}
.dmid {
border: 1px solid green;
width: 100%;
position: absolute;
bottom: 0;
<div class="dparent">
<div class="dmid">
<div class="dchild"></div>
</div>
</div>
Do the vertical alignment first, with an absolute position and the 0 bottom. Then do the centering with margin-left and margin-right set to auto.
You might try this solution for any concerned width:
width:100%;
position:absolute;
bottom: 5px;
left:50%;
margin-left:-50%;
Good luck!

Filling sides of a centered div?

I want to fill the sides of a centered div with another div or span on each side.
I'm using margining to center the div as shown in this fiddle.
HTML
<div id='A>
<div id='Ad'>
</div>
</div>
CSS
#A{
z-index: 3000;
position: fixed;
width: 100%;
height: 40px;
background: rgba(0,0,0,0.05);
}
/*
div or span to the left
*/
/*
centered div
*/
#Ad{
z-index: 3000;
width: 400px;
height: 40px;
margin-left: auto;
margin-right: auto;
border-left: solid 1px #ff0000;
border-right: solid 1px #ff0000;
}
/*
div or span to the right
*/
How can I have a div that always takes up the remaining space on the left and another div that takes up the remaining space on the right.
Clarification:
Center column needs to be constant width. Left and Right Columns vary with the window size.
This would achieve what you want - it allows you to have a fixed width central div with left and right columns that fill up the remaining space:
HTML:
<div id="A">
<div id="Ad">Centre</div>
<div id="left">Left</div>
<div id="right">Right</div>
</div>
CSS:
#A {
z-index: 3000;
position: fixed;
width: 100%;
height: 400px;
background: rgba(0, 0, 0, 0.05);
}
/*
centered div
*/
#Ad {
z-index: 3000;
width: 400px;
height: 400px;
margin-left: auto;
margin-right: auto;
border-left: solid 1px #ff0000;
border-right: solid 1px #ff0000;
}
#left, #right {
position:absolute;
left:0;
top:0;
right:50%;
margin-right:200px;
background:#F00;
height: 400px;
}
#right {
left:50%;
right:0;
margin-left:200px;
margin-right:0;
}
The key is that the margin on the left/right is half of the central column's total width, so adjust it to take into account any borders or padding.
Working example: http://jsfiddle.net/2AztF/
I would just use 3 <div>s floated within the main container
HTML:
<div id='A'>
<div id='AdLeft'></div>
<div id='Ad'></div>
<div id='AdRight'></div>
</div>
CSS:
#A { overflow:auto }
#AdLeft { float:left; width:25%; }
#Ad { float:left; width:50%; }
#AdRight { float:left; width:25%; }
Here is a modified jsfiddle.
Make 3 divs :
<div id="A"></div>
<div id="B"></div>
<div id="C"></div>
<div style="clear:both"></div>
CSS:
#A,#B,#C{
float:left;
width:10%;
}
#B{
width:80%;
}
Here, B is you main div.
It is good practice to clear when you use float property.
To fill space on the right and left side of your div code use and make sure you have no margin or padding on those sides.
float:right;
float:left;
HTML:
<div class='container'>
<div class='left'></div>
<div class='center'></div>
<div class='right'></div>
</div>
CSS:
.container { overflow: hidden; margin:0; padding:0; }
.right { float: right; width: 150px; }
.center{ float: right; width:50px; margin-right: 50px; }
.left{ float: left; width: 150px; }
The margin-right of .center will fill the space accordingly.

Resources