I'm wondering how to connect two div elements with a line which is even distanced kind of like the one in this website:
JSFiddle link: https://jsfiddle.net/mcbvb8m2/
How would you do this for horizontal and vertical divs?
Any help would be appreciated!
Thanks.
You can create a div with a class of something like connector and style this to look like the connector with the following CSS:
.connector {
border: 6px solid #333;
border-right: 0;
border-top-left-radius: 8px;
border-bottom-left-radius: 8px;
height:50px;
width: 10px;
}
You can change the appearance of this by playing around with the border thickness, colour and border-radius. This takes care of the styling.
To position it correctly, you can use absolute or relative positioning. In this case to use absolute positioning, apply position:absoluteto the connector class. To position it use properties such as top, bottom, left and right. The absolute position will absolutely position the element relative to the entire page so I'd recommend adding position:relative to its parent container so it is positioned relative to this.
.container{
height:800px;
width:100%;
padding:50px;
background:#eeeeee;
position:relative;
}
.box-1{
height:300px;
width:300px;
background:blue;
color:#fff;
margin-bottom:30px;
}
.box-2{
height:300px;
width:300px;
background:red;
color:#fff;
}
.connector {
position:absolute;
top: 335px;
left: 35px;
border: 6px solid #333;
border-right: 0;
border-top-left-radius: 8px;
border-bottom-left-radius: 8px;
height:50px;
width: 10px;
}
<div class="container">
<div class="box-1">
Box 1
</div>
<div class="box-2">
Box 2
</div>
<div class="connector"></div>
</div>
Related
I would like to layout my website header with an image to the left and some text aligned at the bottom left ...
...or bottom right.
...or even better, have the image DIV height perfectly fit in the parent DIV :
...
Unfortunately I cannot get past this :
I am approaching this in two parts. The first part is to float my two inner elements left with a simple :
float:left;
on both inner DIVs.
Then with the second inner DIV I am playing around with position
position:fixed;
right:0;
However if I also add bottom:0 then I get something like this :
The second DIV has jumped outside of the outer DIV. How do I make it so that the positioning is relative to the parent DIV? I have tried position:inherit/absolute/fixed and nothing seems to work. Do I have to set something on the parent DIV?
Here is my JSFiddle: http://jsfiddle.net/c0gjq2fb/8/
Is there something I am not understanding right? Is there a take home message? I never seem to understand HTML/CSS layouts no matter how hard I try :(
You can use Flexbox
.outer {
border: 5px solid red;
display: flex;
justify-content: space-between;
align-items: flex-start;
}
.inner2 {
align-self: flex-end;
border: 5px solid green;
}
img {
border: 5px solid blue;
}
<div class="outer">
<img src="http://www.frontangle.com/resources/FrontAngle_For_Site_PNG24.png">
<div class="inner2">Some text</div>
</div>
The border is messed up cause of the height: 100%; value. If you remove the border, it's fine.
http://jsfiddle.net/j9p78a91/
add
position:relative;
top:220px;
to css class .inner2.
.outer {
border-style: solid;
border-width: 5px;
border-color: red;
width: 100%;
height: 280px
}
.inner1 {
border-style: solid;
border-width: 5px;
border-color: blue;
float:left
}
.inner2 {
border-color: green;
border-style: solid;
border-width: 5px;
float:right;
position:relative;
right:0;
top:220px;
}
<div class="outer">
<div class="inner1">
<img src="http://www.frontangle.com/resources/FrontAngle_For_Site_PNG24.png">
</div>
<div class="inner2">
Some text
</div>
</div>
I am puzzled by nested DIV's with Position set to absolute, perhaps someone could guide me :)
My goal is: 1 navbar with 2 div's for points and life, both containing text and 1 icon, i want to absolute position the div's so i can later manipulate them via jQuery.
my problem is, when i nest elements within my points and life div, they go all bananas for me, the overlap and top/right does not seem to have any effect, also the elements within the div's does not comply to my position:absolute, i've tried all sort of combinations with relative, absolute, display:block etc. but without any result.
i compiled a Fiddler to help visualize my problem:
Fiddler link
if i change the div's within points/life the icon seems to behave, but the h2 text is still giving me problems ?...
any idea/solution to my nested div chaos :D. .
<div class="gameWrapper">
<div class="navBar">
<div class="points">
<h2>points</h2>
<div class="imageClass"><div>
</div>
<div class="life">
<h2>life</h2>
<div class="imageClass"><div>
</div>
</div>
</div>
.gameWrapper{
position: absolute;
overflow:hidden;
width:100%;
height:100%;
background-color: #eee;
}
.gameWrapper .navBar{
position:relative;
overflow: hidden;
height:69px;
width:100%;
top:0;
left:0;
border: 1px solid #00f;
}
.gameWrapper .navBar .points,
.gameWrapper .navBar .life{
position: absolute;
overflow:hidden;
top:15px;
}
.gameWrapper .navBar .points .imageClass,
.gameWrapper .navBar .life .imageClass{
position: absolute;
top:0;
right:0;
width: 30px;
height:30px;
background: #fff url(http://drytech.dk/wp-content/uploads/2013/03/pdf-icon.png) no-repeat 0;
}
.gameWrapper .navBar h2{
position: absolute;
top:0px;
}
.gameWrapper .navBar .points{
right:140px;
width:150px;
height:30px;
border: 1px solid #0f0;
}
.gameWrapper .navBar .life{
right:50px;
width:80px;
height:30px;
border: 1px solid #f00;
}
I would start by closing your div tags for your imageClass elements.
Making the h2 tags inline would also help. You can either do this with display: inline; or changing them into a default inline element, like a span.
Here is the fiddle
Changing the h2 tags into span tags, like I did for the fiddle, removes some of the default styling. Something to consider if you choose that route.
I'm trying to create a "tile" (square block) with content centered perfectly in the middle of it.
Here's the HTML
<div class="tile-facebook">
<h5>Facebook</h5>
<div class="tile-notification">4</div>
<h4>notifications</h4>
</div>
And the CSS
.tile-facebook{
width:175px;
height:175px;
background: #3b5998 ;
text-align: center;
border-radius: 10px;
border-style: solid;
border-color: #ccc;
border-width:1px;
color: white;
}
.tile-notification{
font-size:80px;
font-weight:bold;
padding:10px 0px;
}
I've got the text in the middle of the block, however I want it to be directly in the middle with the same padding from the top and bottom. Thoughts?
You might not set the height , but use a pseudo or extra element to draw your square from any width of your boxe.
vertical padding at 100% + inline-block is a way to draw a square and add content in its middle.
<div class="tile-facebook">
<div class="wrapper">
<h5>
Facebook
</h5>
<div class=" tile-notification ">
4
</div>
<h4>
notifications
</h4>
</div>
</div>
.tile-facebook {
width:175px;
background: #3b5998;
text-align: center;
border-radius: 10px;
border-style: solid;
border-color: #ccc;
border-width:1px;
color: white;
}
.tile-notification {
font-size:80px;
font-weight:bold;
}
.tile-facebook .wrapper * {
margin:0;
}
.tile-facebook:before {
padding-top:100%;
content:'';
}
.tile-facebook:before,
.wrapper {
display:inline-block;
vertical-align:middle;
}
http://jsfiddle.net/cnq82/
some explanation about vertical % padding or margin : http://www.w3.org/TR/CSS2/box.html#padding-properties & http://www.w3.org/TR/CSS2/box.html#propdef-margin-top
So, (hope it makes it a bit more clear : ) )
If you give a vertical padding of 100% its height will be equal to width of parent.
If you want height to be taller of 20px you can do : padding:100% 0 20px 0 ; or padding:20px 0 100% 0;
If you want a box with a ration of 4:3 , just do padding-top:75%; or padding:50% 0 25% 0;.
pseudo or extra element can be floatting, or inline-block for vertical alignment.
You do not need to set a width in parent's CSS.
This fix tequires that the contents height never changes, and you need to add another <div>.
<div class="tile-facebook">
<div class="center">
<h5>Facebook</h5>
<div class="tile-notification">4</div>
<h4>notifications</h4>
</div>
</div>
And add the CSS:
.title-facebook {
position: relative;
}
.center {
position: absolute;
top: 50%;
margin-top: -[half height];
left:0;
width: 100%;
}
Where [half height] is half the height of the .center div.
Add margin: -30px; to your CSS here:
.tile-notification {
font-size:80px;
font-weight: bold;
padding: 10px 0px;
margin: -30px;
}
I want to set solid border width like as picture. What can I do? Thanks for answers
|
|
----- -------
|
|
You can visually achieve this result if you apply border-radius to the parent container.
The child container needs to have a solid background (that matches the background of the wrapper element). Like so: http://jsfiddle.net/skip405/JgsKM/
It's not easy to do borders like that just by using CSS border properties. You'd have to resort to some kind of trickery to cover over the borders at each corner.
You are better off using a background image for this task. Prepare an image with the right and bottom lines and place it on the background of those rectangles, positioned bottom right on each one. (Obviously you'd prepare the background image so that those gray lines don't meet at the bottom right corner—just as in the image you posted.)
This is assuming that you have a fair idea of the width and height of the elements, so it's not a perfect solution, but will get you pretty close.
img {
border:solid rgb(165, 162, 162)
border-top:none;
}
I think you should add 4 child divs for mask borders parent div.
Html
<ul class="list">
<li>
<div id="holder"> <!--parent div-->
<div id="maskTopLeft"></div> <!-- child 1 for mask border on top-left -->
<div id="maskTopRight"></div> <!-- child 2 for mask border on top-right -->
<div id="maskBottomLeft"></div> <!-- child 3 for mask border on bottom-left -->
<div id="maskBottomRight"></div> <!-- child 4 for mask border on bottom-right -->
</div>
</li>
</ul>
css
#main { width:100%; margin:10px; }
.list {
margin: 0px;
padding: 0px;
list-style-type: none;
position: relative;
}
.list li {
float:left;
margin-right: -0.5px;
margin-top: -0.5px;
}
.list li:first-child #holder {
border-right: 0.5px solid #000;
}
.list li:last-child #holder {
border-left: 0.5px solid #000;
}
#holder {
border: 1px solid #000;
height: 250px;
width: 200px;
position:relative;
}
#maskTopLeft {
position: absolute;
top:-1px;
left:-1px;
width:21px;
height:20px;
background-color:#fff;
}
#maskTopRight {
position: absolute;
top:-1px;
left:180px;
width:21px;
height:20px;
background-color:#fff;
}
#maskBottomRight {
position: absolute;
top:230px;
left:180px;
width:21px;
height:21px;
background-color:#fff;
}
#maskBottomLeft {
position: absolute;
top:230px;
left:-1px;
width:21px;
height:21px;
background-color:#fff;
}
demo on cssdeck
You can use background-image properties in css the best and easy way
http://www.w3schools.com/cssref/pr_background-image.asp
set a backgroud image to the div-container where the images is in it. this background image is white and has the to borders (right side, bottom side) as you want. i think that's the solution for your problem, because otherwise the borders will look different
Somehow I can't figure out what I'm missing...
I try to position a number of absolute divs between two fixed bars (header and footer). Header contains some tabs and footer contains an copyright. I want to use the window's scrollbar and not an overflowed div and I know it should be possible!
Every absolute positioned div should carry an extra margin, so that the bottom of that div does not disappear behind the footer.
It should become something like this:
A snippet of my problem is available here on jsfiddle.
My HTML:
<ul class="cf tabs">
<li>Tab 1</li>
<li>Tab 2</li>
</ul>
<div style="margin-top: 40px; padding-bottom: 30px; position:relative">
<div style="position:absolute;top:300px; height:100px; width: 250px; left:200px; border: 1px solid purple;">aaa</div>
<div style="position:absolute;top:0px; height:100px; width: 100px; left:100px; border: 1px solid purple;">bbb</div>
<div style="position:absolute;top:450px; height:100px; width: 250px; left:400px; border: 1px solid purple;">ccc</div>
</div>
<div class="cf footer">Copyright ©</div>
The stylesheet I'm using:
ul.tabs {
list-style-type: none;
list-style-position: outside;
padding:5px;
margin: 0;
position:fixed;
top:0;
z-index: 999;
background-color: white;
left:0;
right:0;
border-bottom: 1px solid green;
opacity: 0.7;
}
ul.tabs li {
float: left;
margin:1px;
padding: 4px 10px 2px 10px;
border: 1px solid black;
}
div.footer {
position: fixed;
bottom: 0;
left: 0;
right:0;
background-color:#DEDEE9;
border-top: 3px outset #BBBBBB;
padding: 5px;
opacity: 0.6;
}
.cf:before,
.cf:after {
content: " ";
display: table;
}
.cf:after {
clear: both;
}
Do you guys have any hints?
Extra info
As you can see in the attached image the purple border of squared div at bottom right is overlapping the fixed footer. I do not want this. There should be given a bottom margin somewhere, so that every div carries an extra margin so it should match the top of the footer
Here's the solution I've come up with. Wrap the bottommost absolutely positioned div inside with another div, on which put bottom margin equal to footer height and the border. I gave it class .inner.
See my fiddle.
Add bottom padding to the document body equivalent to the height of the footer:
body {
padding-bottom: 31px;
}
(JSFiddle doesn't seem to let you modify styling of the body element, so I can't post a fiddle. It should work, though.)
Instead of using absolute positioning, do something like:
margin - left : 800px
Margin - top: 500px