I'm stuck here with an easy css problem:
The problem is to align "World" text inside the div element at the bottom right.
Here is a fiddle:
http://fiddle.jshell.net/0p6w3x14/2/
<div id="container">
<div id="tableElement">
<table> <!-- this table needs to be here, it's containing more info -->
<tr>
<td>
Hello
</td>
</tr>
</table>
</div>
<div id="element">
World
</div>
</div>
#container
{
width: 200px;
border: 1px solid black;
}
#tableElement
{
border: 1px solid black;
display: inline-block;
}
table
{
border: 1px solid red;
height: 100px;
}
#element
{
display: inline-block;
float: right;
border: 1px solid green;
}
Update your css like this:
#container
{
width: 200px;
border: 1px solid black;
position:relative; // add this line
}
#element
{
display: inline-block;
position:absolute; //add this line
bottom:0; //add this line
right:0; //add this line
border: 1px solid green;
}
and remove float:right
Working fiddle here
Check this Fiddle.
I didn't use
float: right
and
display: inline-block
But instead I set a defined width, set its Left property to 100% and then use margin to adapt it to the container
#element{
width: 45px;
top:100%;
left: 100%;
margin-left: -45px;
}
This could be simply done by giving #element the styles position:absolute;, bottom:0; and right:0. And then giving #container the style position:relative; like this:
#container
{
width: 200px;
border: 1px solid black;
position:relative; // Parent needs relative positioning if child will have absolute positioning
}
#element
{
border: 1px solid green;
position:absolute;
bottom:0;
right:0;
}
JSFiddle Demo
Related
My HTML
<div id="wrapper">
<div id="div1">The two divs are</div>
<div id="div2">next to each other.</div>
</div>
My CSS
#wrapper {
border: 1px solid blue;
}
#div1 {
display: inline-block;
width:49%;
height:120px;
border: 1px solid red;
}
#div2 {
display: inline-block;
width:49%;
height:160px;
border: 1px solid green;
}
A JSFiddle
So, when as you can see the width of each div is 49%, that's the only way I'm getting it to work. If you set the width of each to 50%, the divs aren't displayed next to each other anymore... Why is that?
Because of two things
Border size so you need to change box-sizing to border-box
White space on inline-block elements
* {
box-sizing: border-box;
}
#wrapper {
border: 1px solid blue;
}
#div1 {
display: inline-block;
width: 50%;
height: 120px;
border: 1px solid red;
}
#div2 {
display: inline-block;
width: 50%;
height: 160px;
border: 1px solid green;
}
<div id="wrapper">
<div id="div1">The two divs are</div><div id="div2">next to each other.</div>
</div>
You need to remove the line break between the <div> tags and box-sizing:border-box;
The two divs arenext to each other.
Another approach would be to use float
#wrapper {
border: 1px solid blue;box-sizing:border-box;
}
#div1 {
float:left;
width:50%;
height:120px;
background:green;
box-sizing:border-box;
border:1px solid #909090;
}
#div2 {
float:left;
width:50%;
height:160px;
background:green;
box-sizing:border-box;
border:1px solid #909090;
}
The other option is to use Flex.
#wrapper {
border: 1px solid blue;
display: flex;
}
#div1 {
width:50%;
height:120px;
border: 1px solid red;
}
#div2 {
width:50%;
height:160px;
border: 1px solid green;
}
This is because you have added a border of 1px to wrapper. Your border with take 2px of total width of your page.
If you wanna keep the border and still keep the width of each div as 50%, you can refer to #NenadVracar 's answer
Another option is to use calc() and calculate the width to be 50% - 2px. I'm just listing that as an option #Nenad Vracar has the right answer
I've got this shape made:
https://jsfiddle.net/5vue1buj/1/
However, the way I'm doing this is by inserting:
<br /><br /><br /><br />
in between the top and bottom. How do I do this more elegantly?
Remove all inline styling.
DEMO
HTML
<div>
<div id="top">
<div class="triangle-down-right">
<!--empty-->
</div>
<div class="triangle-down-left">
<!--empty-->
</div>
</div>
<div id="bottom">
<div class="triangle-up-right">
<!--empty-->
</div>
<div class="triangle-up-left">
<!--empty-->
</div>
</div>
</div>
Then add this CSS:
#top, #bottom {
float: none;
overflow: hidden;
}
#top {
margin-bottom: 10px;
}
By using css styles margin/padding you can achieve this.
In your case you have to clear the space between the two containers [top and bottom]. By default div elements are left aligned. I have added an empty divwhich will remove the space in between the two container [using clear:both. height and overflow is added for Cross browser compatibility]
please check this Fiddle.
By using minimal of html and css
You can use only two div and two its psuedo elements :after and :before
.bottom {
position:absolute;
width:210px;
top:180px;
}
.upper {
position:absolute;
width:210px;
top:20px;
}
.upper:before {
content:'';
position:absolute;
left:0;
width: 0;
height: 0;
border-bottom: 100px solid #4679BD;
border-left: 100px solid transparent;
}
.upper:after {
content:'';
position:absolute;
right:0;
width: 0;
height: 0;
border-bottom: 100px solid #4679BD;
border-right: 100px solid transparent;
}
.bottom:before {
content:'';
position:absolute;
left:0;
width: 0;
height: 0;
border-top: 100px solid #4679BD;
border-left: 100px solid transparent;
}
.bottom:after {
content:'';
position:absolute;
right:0;
width: 0;
height: 0;
border-top: 100px solid #4679BD;
border-right: 100px solid transparent;
}
<div class="upper"></div>
<div class="bottom"></div>
Here's another way, with much less CSS...
#top, #bottom {
height: 200px;
position: relative;
}
.right, .left {
height: 0;
width: 0;
display: inline-block;
}
#top {
margin-bottom: 10px;
}
.left {
margin-right: 10px;
}
#top .left {
border-top: 200px solid transparent;
border-right: 200px solid #4679bd;
}
#top .right {
border-top: 200px solid transparent;
border-left: 200px solid #4679bd;
}
#bottom .left {
border-bottom : 200px solid transparent;
border-right: 200px solid #4679bd;
}
#bottom .right {
border-bottom: 200px solid transparent;
border-left: 200px solid #4679bd;
}
<div>
<div id="top">
<div class="left"></div><div class="right"></div>
</div>
<div id="bottom">
<div class="left"></div><div class="right"></div>
</div>
</div>
Just for fun, here another example.
It uses pseudo elements and some new css3 properties to minimize the html markup down to only one div. This div is relatively positioned, but could as well be positioned absolutely for easily placing it wherever you like on the page.
A sophisticated jsfiddle can be found here where you can play around if the values easily (using Sass).
#shape{
position:relative;
background:#4679BD;
width:200px;height:200px;
transform:rotate(45deg);
margin-top:50px;margin-left:50px;
overflow:hidden;
}
#shape::before,#shape::after{
content:"";display:block;
position:absolute;
width:300px;height:10px;
background:white;
transform:rotate(45deg);
transform-origin:5px 5px;
left:-5px;top:-5px;
}
#shape::after{
transform:rotate(-45deg);
bottom:-5px;top:auto;right:-5px;
}
<div id="shape"></div>
Size is easily adjustable by adjusting the width of the pseudo elements like: (dim of shape + 5) * 1,414 and the height determines the gap between the triangles.
I have the following html structure:
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
The parent is positioned absolutely, child1 and child2 are displayed side-by-side using inline-block.
I need this whole thing to be responsive based on the width of the 2 children divs. the problem is, if I increase the width of any of them, the parent's width remains the same. Changing its position to relative fixes this, but I have to have it in absolute.
Is there anyway to get it to be responsive?
EDIT:
I was hoping for this to be simple, but apparently not so much... :(
here's the actual HTML:
<div class="action_container">
<div class="action_inner">
<div class="action_title">Format Text</div>
<div class="action_body">
<div class="action_args_section"></div>
<div class="action_output_section"></div>
</div>
</div>
</div>
And the CSS:
<style>
.action_container {
display: block;
position: absolute;
}
.action_inner {
border: 1px solid black;
}
.action_inner {
min-width: 120px;
min-height: 50px;
background-color: white;
border: 1px solid #666;
border-radius: 5px;
}
.action_title {
font-size: 12px;
font-weight: bold;
text-align: center;
border-bottom: 1px solid #ccc;
padding: 3px;
}
.action_args_section {
display: inline-block;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 3px;
}
.action_output_section {
display: inline-block;
width: 50px;
vertical-align: top;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 3px;
}
</style>
.parent{
position: absolute;
display: table;
}
.child{
position: relative;
display: table-cell;
}
Use this trick to set children in single line and parent to get width from them. Don't apply floats to nothing. And remember about white-space: nowrap; if You need to keep single line in child elements.
Here is fiddle.
.parent {
position:absolute;
height:50px;
border:1px solid red;
}
.child1 {
width:100px;
height:30px;
border:1px solid green;
}
.child2 {
width:150px;
height:30px;
border:1px solid blue;
}
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
Is this what you're looking for?
JSFiddle
.parent{
position:absolute;
left : 60px;
top : 60px;
width : auto;
height:auto;
border:1px solid black;
}
.parent .child{
display:inline-block;
border:1px solid blue;
}
<div class="parent">
<div class="child">aaaaaassssssssssssss</div>
<div class="child">sssssssccccccccccccccccccc</div>
</div>
Try use a max-width to set a maximum width for the parent div so it doesn't get wider than specified.
I did this easily. Changing the width of the divs changes the parent as well.
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
<style>
div{border:1px solid black;}
.parent{
position:absolute;
width:auto;
height:auto;
}
.child1{
display:inline-block;
width:40px;
height:40px;
}
.child2{
display:inline-block;
width:30px;
height:40px;
}
</style>
If you want a responsive design, make sure you're using percentages, and not pixel values because the size of the divs will be calculated by the viewport width.
If you just want the parent to resize based on the absolute sizes of the child divs, add height:auto; width:auto to the parent. Then, change the child divs to display:block; float:left. The parent will resize accordingly.
Updated CodePen Demo
CSS
.action_container {
display: block;
position: absolute;
height:auto;
width:auto;
}
.action_inner {
border: 1px solid black;
}
.action_inner {
min-width: 120px;
min-height: 50px;
background-color: white;
border: 1px solid #666;
border-radius: 5px;
}
.action_title {
font-size: 12px;
font-weight: bold;
text-align: center;
border-bottom: 1px solid #ccc;
padding: 3px;
}
.action_args_section {
display: block;
float:left;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 3px;
width:300px;
border: 1px solid red;
}
.action_output_section {
display: block;
float:left;
width: 150px;
vertical-align: top;
box-sizing: border-box;
-moz-box-sizing: border-box;
padding: 3px;
border: 1px solid blue;
}
see the sample solution here in jsfiddle link
using this css:
.parent{
position:fixed;
background-color:blue;
height:auto;
width:auto;
}
.child1{width:200px;background-color:black;height:200px;float:left;}
.child2{width:200px;background-color:red;height:200px; float:left;}
if it is not what you're looking for,you can edit your css here then we can help
.parent{
float: left;
posetion: absolute;
background-color: yellow;
width:auto;
height: auto;
}
.parent div{
float: left;
display: inline-block;
width: 100px;
height: 100px;
background-color: red;
}
<div class="parent">
<div class="child1">this</div>
<div class="child2">this</div>
</div>
Here's The Code You Need :)
I'm trying to set a simple page grid. Each row consists of an optional left column, plus a main content right column. I want the right column to remain the same size at the same position even if the left column isn't present.
I figured that floating the left column and using position: relative with left: on the right column would give me the behaviour I want.
My HTML looks like this:
<div class="row">
<div class="sidebar">I'm a sidebar!</div>
<div class="main">
<p>I'm main!</p>
</div>
</div>
and my CSS looks like this:
.sidebar {
float: left;
width: 200px;
border: 1px solid red;
}
.main {
position: relative;
left: 220px;
width: 500px;
border: 1px solid green;
}
Here's a fiddle: http://jsfiddle.net/ttr5k/1/
To my surprise, the content of .main is shifted right (as if .main had padding-left) seemingly due to the sidebar. Why is this, and how could I solve it?
I also suspect this isn't the best way to build a grid, is there a better approach?
Add position absolute instead of relative
http://jsfiddle.net/ttr5k/2/
As you can see the text aligns left again
.sidebar {
float: left;
width: 200px;
border: 1px solid red;
}
.main {
position: absolute;
left: 220px;
width: 500px;
border: 1px solid green;
}
I recommend doing something like this:
.row {
background:#eee;
width:90%;
overflow:auto;
border:1px solid #ccc;
margin:20px auto;
}
.sidebar {
float: left;
width: 200px;
border: 1px solid red;
}
.main {
float:left
width: 500px;
border: 1px solid green;
overflow:auto;
clear:right;
}
Now you will be able to remove the sidebar whenever you want without adding new CSS
DEMO: http://jsfiddle.net/ttr5k/5/
OR------
if you want that space even if no sidebar and still want to content to overflow:
http://jsfiddle.net/ttr5k/7/
.row {
background:#eee;
width:600px;
overflow:auto;
border:1px solid #ccc;
margin:20px auto;
}
.sidebar {
float: left;
width: 200px;
border: 1px solid red;
}
.main {
float:right;
width: 396px; /* This is due to box-model adding border as width */
border: 1px solid green;
overflow:auto;
clear:right;
}
Here is the FIDDLE on how I would do it: http://jsfiddle.net/mikea80/zJa5P/
<div class="row">
<div class="main">
<p>I'm main!</p>
</div>
<div class="sidebar"><p>I'm a sidebar!</p></div>
</div>
.row {
margin: 0 auto;
width:704px;
clear:both;
}
.main {
display:inline-block;
float:right;
width: 500px;
border: 1px solid green;
}
.sidebar {
display:inline-block;
float: right;
width: 200px;
border: 1px solid red;
}
With the row being 700px this code will center it
You have to add position absolute to sidebar class.
CSS:
.sidebar {
position: absolute;
float: left;
width: 200px;
border: 1px solid red;
}
.main {
position: relative;
left: 220px;
width: 500px;
border: 1px solid green;
}
Trust me, this way, you can add other row class without any problem. Here is the FIDDLE:
http://jsfiddle.net/asubanovsky/bVr6r/
I'm trying to make two squares overlap eachother in a parent div. The squares are using position:inherit. Please note that the number of squares will be dynamic. Also note that the parent div is using margin-left: 30%. Is this possible?
<div style="border: 1px solid Black; width: 300px; height:300px; margin-left:30%;">
<div style="height:40px; width:40px; border: 1px solid Black; position:inherit; left:0px; top: 0px;"></div>
<div style="height:40px; width:40px; border: 1px solid Black; position:inherit; left:0px; top: 0px;"></div>
</div>
http://jsfiddle.net/AzYUn/1/
Get rid of the position: inherit; and use position: relative;.
Using the top, right, bottom and left properties you can move an element and making it overlap.
CSS
div.parent {
border: 1px solid black;
margin-left: 30%;
height: 300px;
width: 300px;
}
div.parent > div.box {
height:40px;
width:40px;
border: 1px solid black;
}
div.parent > div.box.overlap {
position: relative;
top: -40px;
}
HTML
<div class="parent">
<div class="box"></div>
<div class="box overlap"></div>
</div>
The only way I can think of is to use other position value - relative or even absolute to reach that goal.