CSS - how do I make a 1/4 circle that is 100vh? - html

I want something like this (the pink circle): CSS quarter circle 100vh example.
So far, I have a half-circle (see CSS below), but when I try to make it 100vh, it stretches and I can't figure out how to keep it proportional.
.circle {
height: 180px;
width: 90px;
border-radius: 0 90px 90px 0;
-moz-border-radius: 0 90px 90px 0;
-webkit-border-radius: 0 90px 90px 0;
background: red;
margin: 100px;
position: absolute;}
Any insights greatly appreciated. Thanks

I modified the code to only use 200vh to calculate both width and height of circle. This will give you a perfect circle at any screen size.
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100vw;height:100vh}
.box {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.box > .circle {
height: 200vh;
width: 200vh;
position: absolute;
bottom: -100vh;
right: -100vh;
border-radius: 50%;
background: hotpink;
}
<div class="box">
<div class="circle"></div>
</div>

You can do it like this:
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100vw;height:100vh}
.box {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.box > .circle {
height: 200vh;
width: 125.5vw; /* if exactly 16:9 vw/vh aspect ratio */
position: absolute;
bottom: -100vh;
right: -56.250vw; /* if exactly 16:9 vw/vh aspect ratio */
border-radius: 50%;
background: hotpink;
}
<div class="box">
<div class="circle"></div>
</div>
Conclusion: This works perfectly if the screen viewport is exactly at 16:9 aspect ratio (see it inside the editor (not "Run code snippet") without Menu Bar & Bookmarks Toolbar but better to see it in full screen), anything else than that fails so I wouldn't recommend using viewport units for this task. If anyone can prove me wrong or do it better, go ahead.
And with px:
* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100%}
.box {
position: relative;
width: 100vw;
height: 100vh;
overflow: hidden;
}
.box > .circle {
height: 180px;
width: 180px;
position: absolute;
bottom: -90px;
right: -90px;
border-radius: 50%;
background: hotpink;
}
<div class="box">
<div class="circle"></div>
</div>

you can start to keep the box into the ratio you want and center it when it doesn't fill the whole screen (=> downscaling to fit within the screen)
to size things, you can relay on %, vw,vh,vmax and or vmin units.
basicly you can start with size and max-size using the viewport as reference for the main box:
height:100%;
width:100%;
max-width:179vh;/* height viewport*/
max-height:56vw;/* width viewport*/
to align content you can use the flex display and margins and position.
To draw that 1/4 circle, you need a square that is at least the height of your box if it is to be drawn from border-radius. ( else a radial-gradient would do just fine).
for the vertical text, you may take a look at writing-mode.
A mix of CSS3 rules and positionning method can allow to do something that is about fluid.
Run the snippet below in fullpage and resize your window (heigh/width/both) your browser to see behavior.(or play with the codepen)
html {
height: 100%;
display: flex;
background: #ccc;
}
body {
margin: auto;
background: linear-gradient( to right, rgb(231, 231, 231) 25%, rgb(225, 207, 207) 25%);
height: 100%;
width: 100%;
max-width: 179vh;
max-height: 56vw;
display: flex;
align-items: center;
overflow: hidden;
position: relative;
box-shadow: 0 0 0 2px;
/* debug , see me */
}
/* make div be squares */
div:before {
content: "";
display: inline-block;
padding-top: 100%;
vertical-align: top;
}
.small {
border-radius: 50%;
background: rgb(101, 112, 168);
width: 25%;
margin-left: 13%;
box-shadow: 20vmin 20vmin 40vmin;
}
.big {
background: linear-gradient(to top, rgb(195, 90, 131), rgb(195, 90, 131)) no-repeat 0 0;
width: 56%;/* according to the ratio choosen */
border-top-left-radius: 100%;
flex-shrink: 0;/* avoid flex to shrink it */
margin: auto 0 0 auto;
}
/* position piece of text via absolute */
p {
margin: 0;
position: absolute;
bottom: 1%;
right: 5%;
font-size: 15vmin;
white-space: nowrap;
color: white;
}
p span {/* this rules might need to be tune to specific font-family*/
width: 0.25em;
display: inline-block;
vertical-align: -0.1em;
font-size: 0.155em;
writing-mode: vertical-lr;
transform: scale(-1);/*=> writing-mode:sideways-lr; not avalaible everywhere*/
}
h1,
body:before {
top: 0;
color: rgb(101, 112, 168);
position: absolute;
width: 5em;
font-size: 2vmin;
margin: 12vmin 0 5vmin 5vmin;
}
body:before {
content: "2017";
top: auto;
bottom: 0;
font-weight: bold;
}
h1:before {
content: "HB";
color: rgb(195, 90, 131);
border-radius: 50%;
position: absolute;
bottom: 150%;
font-size: 2.5em;
width: 0.75em;
height: 0.75em;
line-height: 0.75em;
letter-spacing: -0.35em;
border: solid 1px;
text-indent: -0.35em;
overflow: hidden;
}
<h1>VISUAL EXPLORATION</h1>
<p><span>BACK TO</span>BASIS</p>
<div class="small"></div>
<div class="big"></div>

By using the same view port unit for both height and width, you can achieve a perfect circle. Then putting it within a container which is fixed to the height of the view port and hiding the rest of the overflow will allow you to remove any unnecessary scroll bars and still allow for content below the circle.
The below demo will show the circle proportionally correct, always 100% of the view port height while making sure it is always a perfect circle.
It will obviously look better in full screen.
* {
margin: 0;
padding: 0;
}
.container {
width: 100%;
height: 100vh;
position: relative;
overflow: hidden;
background: #dedede;
}
.dark {
background: #777777;
}
.circle {
width: 200vh;
height: 200vh;
border-radius: 100%;
position: absolute;
right: -100vh;
background: pink;
}
<div class="container">
<div class="circle"></div>
</div>
<div class="container dark">
</div>

Related

HTML CSS can't make image fit screen diagonally

I don't have much knowledge about html and css and I couldn't find the answer on the internet so I am here.
Problem:
I am trying to make an image fill top part of the screen but one thing stops me from it and it's the default margin of the <body>. I've managed it by using margin: -10px; But now the image can't fill the screen by 20px, probably because there is no margin, image still thinks screen is that big.
html,body {
width: 100%;
height: 100%;
margin: -10px;
padding: 0;
overflow: hidden;
}
img {
width: 1600px;
height: 300px;
opacity: 70%;
object-fit: cover;
object-position: top 10px;
}
.cont {
position: relative;
text-align: center;
padding: 10px;
}
.main-text {
font-size: 100px;
color: white;
position: absolute;
top: 100px;
left: 70px;
}
<body>
<div class="cont">
<img src="https://i.stack.imgur.com/DWZAk.jpg">
<div class="main-text">Big Ass Title</div>
</div>
</body>
NOTE: If you have any questions or didn't understand anything about the question, please ask because I am ready for any answer. :) Thanks.
If your image is meant to be a decoration(design), then background is fine to use.
.cont can be a flex or grid element, to avoid position absolute and possible unwanted sides effects.
here an example with a background and grid:
body {
margin: 0;
min-height: 100vh; /* optionnal if it does not have a purpose */
}
.cont {
height: 300px; /* guessed from your code */
display: grid; /* default make a single column*/
background: url(https://picsum.photos/id/1015/600/300) 0 0 / cover; /* background covering */
}
.main-text {
margin-block: auto; /* vertical-centering in its row from here */
margin-inline-start:70px;
font-size: 100px; /* from your code */
color: white; /* from your code */
font-weight: normal; /* you looked for this? */
text-shadow: 0 0 1px #000; /*Optionnal increase readability*/
}
<div class="cont">
<h1 class="main-text">Big Ass Title</h1><!-- if that's a title, then make it a title ;) -->
</div>
Generally to eliminate all the margins and paddings you can add:
* {
margin: 0;
padding: 0;
}
By the way I attached a snippet where it's working as you requested. Is better to eliminate the margins than adding a negative margin, if you want to do it that way you must to compensate it in the width to achieve the 100% width intended.
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
img {
width: 100%;
margin: 0;
height: 300px;
opacity: 70%;
object-fit: cover;
}
.cont {
position: relative;
text-align: center;
}
.main-text {
font-size: 100px;
color: white;
position: absolute;
top: 100px;
left: 70px;
}
<html>
<body>
<div class="cont">
<img src="https://images2.alphacoders.com/941/thumb-1920-941898.jpg">
<div class="main-text">Big Ass Title</div>
</div>
</body>
</html>

Stretch fixed to bottom parent div to div child's width

So, I have a main container that shows like the following:
I want to be able to adapt the parent div to the number of child's it receives. Let's say we remove div2. The result should be something like this:
Instead, the parent div does not stretch to the width of the div child's
Here's my code:
HTML:
<div class="main-container">
<!-- Card container -->
<div class="card-container">
<div class="card">div1</div>
<div class="card">div2</div>
<div class="card">div3</div>
</div>
<!-- Footer container -->
<div class="footer">i am a footer</div>
</div>
CSS:
.main-container {
position: fixed;
margin: 0 auto;
left: 0;
right: 0;
bottom: 0;
width: 100%;
max-width: 400px;
box-shadow: 0 0 15px #B3B3B3;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
text-align:center;
}
.card-container {
color: #3B3D3D;
height:105px;
float: left;
width: 100%;
}
.footer {
color: #FFFFFF;
background: #0095D3;
height: 45px;
float: left;
width: 100%;
}
.card {
width:100px;
float:left;
}
What am I doing wrong here? I've tried the display: inline-block; solutions out there but since the parent div must be fixed to the bottom, I am not seeing the desired result.
Any help will be precious.
Thanks in advance.
Try this https://jsfiddle.net/2Lzo9vfc/136/
You can try to remove one .card on click and see what hapens here https://jsfiddle.net/2Lzo9vfc/138/
CSS
.main-container {
position: fixed;
margin: 0 auto;
left: 50%;
bottom: 0;
box-shadow: 0 0 15px #B3B3B3;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
text-align:center;
display: inline-block;
transform: translateX(-50%);
}
.footer {
color: #FFFFFF;
background: #0095D3;
height: 45px;
width: 100%;
}
.card {
width:100px;
height:105px;
display: inline-block;
}
HTML
<div class="main-container">
<div class="card">div1</div>
<div class="card">div2</div>
<div class="card">div3</div>
<div class="footer">i am a footer</div>
</div>
Here you go: http://codepen.io/n3ptun3/pen/PPgWNb
You don't need to use display: inline-block.
I've left your HTML alone, and simplified some of your CSS: .card-container and .footer don't need float: left; and width: 100%;. They are both block-level elements so they will take up 100% of the width, and they don't need anything to wrap around them.
On the .main-container, you can't set margin: 0 auto; and position: fixed;. position: fixed; removes the ability for centering via margin. left: 0; and right: 0; were stretching the size of the main container, so those need to be removed. width: 100%; and max-width: 400px; were trying to fix the width issue, but that wouldn't allow resizing based on content.
Instead you need to set left: 50%; (places left edge of element at 50% of the parent's width, i.e. the viewport width, in this case) and then transform: translate(-50%); to bring the element back toward the left by 50% of its width. Thus bringing the element to the center of the window/viewport.
Now, if you remove one of the "cards," it will resize the "main-container," while keeping everything fixed to the bottom and centered.
.main-container {
position: fixed;
bottom: 0;
left: 50%;
transform: translate(-50%);
box-shadow: 0 0 15px #B3B3B3;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
text-align: center;
}
.card-container {
color: #3B3D3D;
height: 105px;
}
.card {
width: 100px;
float: left;
}
.footer {
color: #FFFFFF;
background: #0095D3;
height: 45px;
}
EDIT: Based on your new information (re: the increased width or added "cards"), I've found that the issue lies with the left position on the .main-container. When you position the element by 50% and its width is more than 50% of the parent, it runs into the right side of the parent div, and you get the stacking. To fix this, you can instead remove the float: left; on .card and add display: flex; on .card-container. This will allow you to increase the width of the "cards" while keeping them from stacking.
I've updated the code here: http://codepen.io/n3ptun3/pen/PPgWNb
.main-container {
position: fixed;
bottom: 0;
left: 50%;
transform: translate(-50%);
box-shadow: 0 0 15px #B3B3B3;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
text-align: center;
}
.card-container {
color: #3B3D3D;
height: 105px;
display: flex;
}
.card {
width: 100px;
// float: left;
}
.footer {
color: #FFFFFF;
background: #0095D3;
height: 45px;
}

Responsive triangles on both sides of the element

I have a project where there will be two triangles, one of each side, as the images below shows, I am having difficulty in, first of all, getting each one on correct side, I have been trying using floats but it is not working.
And then making them align with the white zone irrespective of screen size i.e. responsive.
#anim {
position: relative;
height: 100%;
min-height: 100%;
background-image: url("http://i.imgur.com/rxks29H.jpg");
background-image: no-repeat;
background-size: 100%;
}
#anim img {
position: relative;
height: 100%;
width: 100%;
}
.arrow-left {
position: absolute;
width: 0;
height: 0;
width: 200px;
border-top: 200px solid transparent;
border-bottom: 200px solid transparent;
z-index: 3;
top: 30%;
border-left: 200px solid green;
}
.arrow-right {
position: absolute;
width: 200px;
float: right;
z-index: 3;
border-top: 200px solid transparent;
border-bottom: 200px solid transparent;
top: 30%;
border-right: 200px solid blue;
}
<section id="anim">
<img src="http://i.imgur.com/ucQ3ZXl.png">
<div class="arrow-right">
</div>
<div class="arrow-left">
</div>
</section>
Why not make the arrows part of the background image to insure that it's always in place no matter the screen size, and then the content in each arrow can be positioned on top an if it moves a little it wont break the background itself. I created the background really fast to illustrate what I mean, feel free to re-create the image yourself if needed.
to position the text in each arrow change the CSS to this:
float: left or float: right don't work with position: absolute you need to use the left and right properties.
#anim {
position: relative;
height: 100%;
min-height: 100%;
background-image: url("http://i.imgur.com/rxks29H.jpg");
background-image: no-repeat;
background-size: 100%;
}
#anim img {
position: relative;
height: 100%;
width: 100%;
}
.arrow-left {
padding: 2.5% 15px;
text-transform: uppercase;
position: absolute;
width: 13%;
left: 0;
z-index: 3;
top: 36%;
}
.arrow-right {
padding: 2.5% 15px;
text-transform: uppercase;
position: absolute;
width: 13%;
right: 0;
z-index: 3;
top: 36%;
}
.arrow-right h2 {
font-size: 28px;
color: #FFF;
}
.arrow-left h2 {
font-size: 28px;
color: #FFF;
}
<section id="anim">
<img src="http://i.stack.imgur.com/Fbhc4.png">
<div class="arrow-right">
<h2>Scouting For Companies</h2>
</div>
<div class="arrow-left">
<h2>Seeking For Ideas</h2>
</div>
</section>
You will need to add some rules for smaller screens and really large ones if you are making the site responsive.
** Edit **
I added the animation really quick just to illustrate what you need to do and give you a good head start on it.
Here is a JSFIDDLE.

How do I crop and center a full-height image when I don't know the container's dimensions?

There are a few questions out there that show how to crop and center images, but I haven't found one that matches these requirements:
The visible part of the image must be square.
The image should be scaled so that the full height is displayed and fills the height of the container.
The size of the container is variable and determined by the width of it's container.
The image must be centered.
The end-goal is to have a grid with 3 square images in a row that shrink depending on the browser width.
Here's what I have so far.
.i-om-section-content {
max-width: 800px;
border: 3px solid blue;
margin: 0 auto;
padding: 0 32px;
padding: 0 3.2rem;
position: relative;
z-index: 2;
}
.i-om-item {
overflow: hidden;
margin: 10px;
position: relative;
width: 32.5%;
height: 0;
padding-bottom: 32.5%;
border: 1px solid;
float: left;
}
img {
position: absolute;
left: -100%;
right: -100%;
top: 0;
bottom: 0;
margin: auto;
min-height: 100%;
min-width: 100%;
}
<div class="i-om-section-content">
<div class="i-om-item">
<img src="http://onetaste.us/wp-content/uploads/2015/10/DSC2641.png" />
</div>
<div class="i-om-item">
<img src="http://onetaste.us/wp-content/uploads/2015/10/smita.png" />
</div>
</div>
JSFiddle
Generally speaking, if you want more advance cropping/positioning/sizing of images, it's much easier to work with them as background images. background-size:auto 100% means "auto width, full height," the rest of it was what you already had.
<div class="i-om-section-content">
<div class="i-om-item one">
</div>
<div class="i-om-item two">
</div>
</div>
--
.i-om-section-content {
max-width: 800px;
border: 3px solid blue;
margin: 0 auto;
padding: 0 32px;
padding: 0 3.2rem;
position: relative;
z-index: 2;
}
.i-om-item {
overflow: hidden;
margin: 10px;
position: relative;
width: 32.5%;
height: 0;
padding-bottom: 32.5%;
border: 1px solid;
float: left;
background-size:auto 100%;
background-size:center center;
}
.one{
background-image:url("http://onetaste.us/wp-content/uploads/2015/10/DSC2641.png");
}
.two{
background-image:url("http://onetaste.us/wp-content/uploads/2015/10/smita.png");
}
https://jsfiddle.net/ammsh4y5/
See this updated fiddle.
It uses jQuery to set the height and width of the container to be the same (make it square). It then sets the image height to the height of the div. Lastly, it centers the image by getting the difference of the widths of the image and the div, dividing it by two, and moving it that much left (absolute positioning).
Here's the jQuery code (CSS and HTML were modified as well):
function updateImage() {
$("img").each(function() {
var parent = $(this).parent();
parent.height(parent.width());
$(this).height(parent.height());
$(this).css("left", -($(this).width()-parent.width())/2);
});
}
// call on window resize and on load
$(window).resize(function() {
updateImage();
});
updateImage();
It's not the most elegant solution but it does the job and is pretty intuitive. (But I do like #DylanWatt's background-image solution: much more creative).
.i-om-section-content {
max-width: 800px;
border: 3px solid blue;
margin: 0 auto;
padding: 0 32px;
padding: 0 3.2rem;
position: relative;
z-index: 2;
}
.i-om-item {
overflow: hidden;
margin: 10px;
position: relative;
width: 32.5%;
height: 0;
padding-bottom: 32.5%;
border: 1px solid;
display:inline-block;
background-position:center center;
}
.one{
background-image:url("http://onetaste.us/wp-content/uploads/2015/10/DSC2641.png");
}
.two{
background-image:url("http://onetaste.us/wp-content/uploads/2015/10/smita.png");
}
<div class="i-om-section-content">
<div class="i-om-item one">
</div>
<div class="i-om-item two">
</div>
</div>

minus 20 pixels at top and bottom of div

As a bare bones examples I have those 2 really simple divs:
(the green one is inside the red one)
Now how can I subtract 20 pixels from the bottom and the top of the green div?
html:
<div id="container">
<div id="rows">
</div>
</div>
css:
#container {
width: 200px;
height: 300px;
background: red;
}
#rows {
width: 50%;
height: 100%;
/* margin-top: 20px;
margin-bottom: 20px; */
/* padding-top: 20px;
padding-bottom: 20px; */
/* top: 20px;
bottom: 20px;
height: auto; */
background: green;
}
http://jsfiddle.net/clankill3r/2L6c2bLf/1/
Add padding to #container
padding: 20px 0px;
Edit:
as suggested by #Adam you should contain also
box-sizing: border-box;
to stylesheet if you want to preserve box height
Here is a way to do it without adding padding to the parent div or using a calc in the child div.
JSFIDDLE
#container {
width: 200px;
height: 300px;
background: red;
position: relative;
}
#rows {
position: absolute;
width:50%;
top: 20px;
bottom: 20px;
background: green;
}
This may be the solution:
height: calc(100% - 40px);
JSFiddle
Another solution is
padding:20px 0px;
box-sizing:border-box;
for your container. box-sizing:border-box; preserves the height changing of the container.
Here's a solution:
#rows {
width: 50%;
height: 260px;
margin-top: 20px;
margin-bottom: 20px;
float: left;
background: green;
}
http://jsfiddle.net/2L6c2bLf/5/