I am trying to make something like this...
where the blue is the div, and the black is the logo. how is something like this achieved? I was messing around with the transform but this also does one side.
This is a great opportunity to use CSS shapes!
If you think of the blue shape as "a blue rectangle that is immediately followed by a blue downward-pointing triangle, with no gap between them", then we just need to figure out how make that triangle and put it in the right place.
Let's start with your current HTML & CSS (I'm basing this on the screenshot, and assuming the logo element is outside the blue <div>):
.pointy {
background-color: #0086FD;
height: 285px;
}
.logo {
background-color: #000;
height: 200px;
margin: 0 auto;
transform: translateY(-30%);
width: 200px;
}
<div class="pointy"></div>
<div class="logo"></div>
No need to modify your HTML here. We're going to use the :after pseudo element to add the triangle shape after the div.
I used the handy CSS Triangle Generator to get a triangle started using border properties.
A few other details:
adding position: relative to the div, so that...
we can position the triangle at the bottom with position: absolute and top: 100%
we're applying width: 100vw to the div, because...
since the triangle is created using borders, and borders can't be a percentage width, we can set the two relevant border widths to 50vw, and they'll be exactly half the width of the 100vw parent
Let's make the triangle red for the moment, so you can see it clearly.
.pointy {
background-color: #0086FD;
height: 285px;
position: relative;
width: 100vw;
}
.pointy:after {
border-color: #f00 transparent transparent transparent;
border-style: solid;
border-width: 50px 50vw 0 50vw;
content: '';
display: block;
height: 0;
position: absolute;
top: 100%;
}
.logo {
background-color: #000;
height: 200px;
margin: 0 auto;
transform: translateY(-30%);
width: 200px;
}
<div class="pointy"></div>
<div class="logo"></div>
Final solution
Now that we have created and positioned our triangle, let's make it the same color as the div. (I've also tweaked the vertical positioning of .logo to achieve the desired effect.)
Voila: pointy blue div, no extra HTML needed.
.pointy {
background-color: #0086FD;
height: 285px;
position: relative;
width: 100vw;
}
.pointy:after {
border-color: #0086FD transparent transparent transparent;
border-style: solid;
border-width: 50px 50vw 0 50vw;
content: '';
display: block;
height: 0;
position: absolute;
top: 100%;
}
.logo {
background-color: #000;
height: 200px;
margin: 0 auto;
transform: translateY(-20%);
width: 200px;
}
<div class="pointy"></div>
<div class="logo"></div>
Related
I am given a rectangular picture of width=300px and height=300px.
I want to show this picture inside a circle and came up with the following CSS:
img {
width: 300px;
height: 300px;
background-color: white;
border-radius: 50%;
border: 2px solid black;
}
The issue that I am having is that the picture doesn't really seem to fit nicely inside the circle. It seems way to big and I think that's because the circular form is cropping off parts of the image. So, how could I avoid this? I guess I could simply upload a smaller image. An image of size 200px x 200px, however, would likely be distorted since the circle is too big for it and thus the image would be enlarged.
Any suggestions?
Here is an example of the picture I am adding:
And here it is in a circle:
I would do this with a background image, which gives you more control over how it sits in its container (in this case a <div>).
You can set the background size and position to accommodate a non-symmetric image such as yours.
div {
width: 300px;
height: 300px;
background-color: white;
border-radius: 50%;
border: 2px solid black;
background-image: url(https://i.imgur.com/z7YIJI6.png);
background-size: 80% 80%;
background-position: 50% 70%;
background-repeat: no-repeat;
}
<div></div>
Results:
Suggestion using <div> as wrapper:
img {
z-index: -1;
width: 300px;
height: 300px;
margin: auto;
display: block;
margin-top: 45px;
position: relative;
}
div {
width: 350px;
height: 350px;
display: block;
border-radius: 50%;
border: 2px solid black;
}
<div>
<img src="https://i.stack.imgur.com/oWAqE.png">
</div>
At the moment I'm trying to make a system where once something happens, there will be a big div and error message over the current div to say that the user can no longer interact with the parent div.
The way I tried doing this (in the child element to the parent) was adding a position: absolute; and top: 0; left:0 but that just made everything weirdly aligned and kind of ruined the flow and build of the divs.
So my question is, is there a better way to display a div over another div where, for example, I could make it's opacity 0.8, and be able to see through it, over the entire div behind it, kind of like a z-index: 1;.
JSFiddle: https://jsfiddle.net/331nr1L0/
You can just make the parent position relative then set absolute positioning on the child. I'd need to see more examples of how everything got weirdly aligned like you mentioned.
.parent {
width: 100%;
height: 200px;
background-color: lightgrey;
border: solid black 1px;
border-radius: 3px;
position: relative;
}
.child {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: hsla(270, 100%, 50%, .4);
}
<div class="parent">
<div class="child">
</div>
</div>
Updated your jsfiddle here: https://jsfiddle.net/331nr1L0/1/
You can use the transparency as you mentioned if you would like, just remember that css changes made to the parent div will fall through to the child div, unless you overwrite them by specifying a different value for the same option in the child. EG
.parent {
width: 100%;
height: 200px;
background-color: lightgrey;
border: solid black 1px;
border-radius: 3px;
opacity:0.5;
}
.child {
opacity:1;
}
If you want it to overlap the child over the parent, I'd adjust the margins as well.
Change the colours you want and this should do the trick (as an alternative to the other responses)
Here, the child has a dashed white border (change it to solid, any colour you want)
.parent {
width: 100%;
height: 200px;
background-color: lightgrey;
border: solid black 1px;
border-radius: 3px;
}
.child {
border: 5px dashed white;
border-radius: 3px;
background-color: red;
opacity: 0.4;
height: 210px;
width: 110%;
top: -10px;
left: -10px;
margin: -10px;
}
<div class="parent">
<div class="child">
</div>
</div>
Need background color for child container extending till width of view point. Properties needed should be applied to child container only.
I tried giving huge border to child container till parent container, but it did not work in high resolution screens.
Background color should applied only to the area of text.
.outer{
height: auto;
border: 1px solid red;
width: 420px;
}
.inner{
margin-left: 100px;
margin-right: 100px;
width: 200px;
height: 300px;
border: 1px solid green;
}
<div class="outer">
<div class="inner">Need background color for child container extending till width of view point. properties needed have to be given only within child container.
</div>
</div>
Here is the jsfiddle link to better understand the scenario:
https://jsfiddle.net/5qp1a3um/
You can set a wrapper to contain the desired color like this:
.bg-wrapper{
background: #54BCDF; /*change to desired color*/
}
.outer {
height: auto;
border: 1px solid red;
width: 420px;
margin: 0 auto;
}
.inner {
margin-left: 100px;
margin-right: 100px;
width: 200px;
height: 300px;
border: 1px solid green;
}
<div class="bg-wrapper">
<div class="outer">
<div class="inner">Need background color for child container extending till width of view point. properties needed have to be given only within child container.</div>
</div>
</div>
Bearing in mind that you can only change the css of child container you can try with this modification of the answer of Rick to extend the background:
.outer{
height: auto;
border: 1px solid red;
width: 420px;
}
.inner{
margin-left: 100px;
margin-right: 100px;
width: 200px;
height: 300px;
border: 1px solid green;
}
.inner:before {
content: '';
background: #54BCDF; /*change to desired color*/
position: absolute;
width: 100%;
height: 300px;
left: 0;
z-index: -1;
}
<div class="outer">
<div class="inner">
Need background color for child container extending till width of view point. properties needed have to be given only within child container.
</div>
</div>
But to center .outer I'm afraid that you'll have to add the style margin: 0 auto
Set the parent's position to relative:
.outer {
position: relative;
}
Then create a pseudo-element on the child, which covers the extent of the parent (width and height 100%).
Give it a negative z-index so its background won't hide the content:
.inner:before {
content: '';
background: lightgreen;
position: absolute;
width: 100%;
height: 100%;
left: 0;
z-index: -1;
}
Fiddle 1
Update based on the fact that you can't style the parent
Set the child's position to relative:
.inner {
position: relative;
}
The pseudo-element's width should now be 100% plus the difference between the child's and parent's widths.
You'll need to move the pseudo-element left to account for the child's left margin:
.inner:before {
content: '';
background: lightgreen;
position: absolute;
height: 100%;
width: calc(100% + 220px); /* parent's width - child's width = 220 */
left: -100px; /* account for left margin */
z-index: -1;
}
Fiddle 2
Not completely sure if I understand what you are trying to do, but I gave it a shot.
In the CSS if you change the margin to padding, then any background color on the child element will extend to the parent. background color is includes the padding but not the margin.
I also had to bump up the width of the child 20px for it to fill properly.
Updated fiddle: https://jsfiddle.net/5qp1a3um/1/
.inner{
padding-left: 100px;
padding-right: 100px;
background-color: blue;
width: 220px;
height: 300px;
border: 1px solid green;
}
Use padding instead of margin, that's the right way to do it.
padding: 0 100px;
https://jsfiddle.net/5qp1a3um/2/
Basically I am trying to cut the background of a div off at an angle. Please see the image below for an example: You will notice that the navigation section is cut off and also the header image below.
http://imgur.com/fsT4R9T
Is there a way to create this effect using CSS3? I have tried this:
.site-header {
background-color: #0c3063;
width: 960px;
height: 300px;
#include clearfix;
position: relative;
z-index: 1;
&:after {
content: "";
width: 100px;
position: absolute;
top: -162px;
right: -150px;
width: 300px;
height: 300px;
background-color: black;
transform: rotate(45deg);
z-index: 2;
}
}
In the example above it overlaps but I know how to fix this, basically just use before instead of after. However is there a better way of doing this?
My example seems I can not put the :after content before its parent so this solution is not ideal :( any ideas?
EDIT: https://jsfiddle.net/6x3yenge/1/
Thanks
Though the following code does not achieve what you want for the image, it surely does the job for the top menu. Using borders to create triangles simplies things a lot.
JSfiddle link: https://jsfiddle.net/qvjhptpy/
HTML:
<body>
<div class="menu">
<div class="main"></div>
<div class="cut"></div>
</div>
</body>
CSS:
body
{
background: yellow;
}
.menu
{
background: transparent;
font-size: 0px; /*In case a gap is coming between .main & .cut nested DIVs (Due to whitespace in HTML code)*/
}
.main
{
display: inline-block;
width: 200px;
height: 100px;
background: red;
}
.cut
{
display: inline-block;
border: 50px solid transparent;
border-top: 50px solid red;
border-left: 50px solid red;
}
I have a div with a border of 1 px. I have a square transparent-in-parts png image much smaller than the div 48px * 48px.
I'd like to position the square image such that it overlays the top left border of the div giving the appearance of both top and left borders going underneath the image.
Using background-image 'left top' puts the image inside the div borders which is not what I'm looking for. Wish I could show an example but I don't have any. Hope my question describes it well.
Here's the JSFiddle: http://jsfiddle.net/9sn22/1/
<div id='mybox'>text</div>
#mybox {
text-indent: 0.5in;
background-image:url('http://aerbook.com/site/images/quote-mark-icon-black.png');
border-radius:3px;
border: 1px solid #cccccc;
height: 300px;
font-weight: 200;
text-indent: 0.35in;
padding: 20px;
background-repeat:no-repeat;
background-position: left top;
}
Not quiet getting your question as there are no images or any demo for the desired effect you are trying to achieve, but from what I understood, you can use position: relative; for the container div and use a literal img tag inside the div and use position: absolute; with top: -1px; and left: -1px; respectively.
If you are trying to make the background-image move out of the element area than it's not possible...you need to use img for this
<div>
<img src="#" />
</div>
div {
position: relative;
border: 1px solid #000;
}
img {
position: absolute;
left: -1px;
top: -1px;
}
Update: (After you added a demo)
Do you need something like this?
do you mean something like this? http://jsfiddle.net/q44k5/
html:
<div> </div>
css:
div{
border: 1px solid red;
height: 100px;
width: 100px;
position: relative;
margin: 50px;
}
div:before{
content: '';
height: 20px;
width: 20px;
position: absolute;
top: -10px;
left: -10px;
background: green;
}
try this css below
#cLeft{
position:absolute;
}
background: #ffffff url('http://spikyarc.net/images/down_Arrow.png') no-repeat top left;
try this html below
<img id="cLeft" src="http://spikyarc.net/images/down_Arrow.png" />
<div class="content">
Your Text here.
</div>