Problems with Div and CSS - css

So, trying to figure out how to structure webpages, was trying to make a bodyWrapper to cover entire page using max-height: 100%, max-width: 100%. Then I was going to attempt to put all my other elements using 20% etc... I was hoping it will help when resizing, is that the proper way? Anyways, my CODING problem is my div is only covering one line of the entire page.
My code;
html {
max-height: 100%;
max-width: 100%
}
body {
max-height: 100%;
max-width: 100%;
}
#bodyWrapper {
max-height: 100%;
max-width: 100%;
background-color: black;
}
<meta name="viewpoint" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="myCssTemplate.css">
</head>
<body>
<div id="bodyWrapper">
<p> This is a test.</p>
</div>
</body>

You could set the bodyWrapper min-height to 100vh. That mean it will take 100% viewport height.
#bodyWrapper {
min-height: 100vh;
width: 100%;
background-color: black;
}

What you are trying to achieve is often referred to as "Grid System". The most popular framework for grids is Bootstrap. What Bootstrap does is changing the max-width property on so called media queries (e.g. 100%). Based on that you can create rows and columns. Columns have a relative width to their parent (row and containers), e.g. (like in your example) 20%.
Since this task has been done by multiple frameworks its quite important to understand how it works, but its not important to implement it by yourself. Just use a framework for this task.

When using max-height or max-width you are asking the maximum height of the element to not be over the amount you wrote for (in your case 100%) so technically, impossible to cover all the page. So in your case you'll have to ask for a minimum dimension.
#bodyWrapper
{
height: 100%; /* Can also use min-height: 100%; */
width: 100%; /* Can also use min-width: 100%; */
background-color: black;
}
After that you'll be free to put other element inside your wrapper the width you want.

First you will need to set the height:100% to the root element of the DOM html....
max-height:100% will not work because max-height defines what can be the maximum height of the element if the inner content will be added like below snippet...it will help you to understand max-height concept.
div {
width: 100px;
max-height: 100px;
background: red;
margin-bottom: 20px;
}
<div>Lorem ipsum dolor sit amet</div>
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lacinia, ipsum a posuere consequat, purus erat faucibus orci, vel porta leo odio vitae ligula. Maecenas interdum quis dui eu faucibus.</div>
back to the question you will need to set min-height:100% to the #bodyWrapper instead of max-height and also you don't have to set max-width:100% or width:100% to the html, body, div, because there are block element by default and block element always take width:100% if not specified and width value
html,
body {
height: 100%;
margin: 0;
}
#bodyWrapper {
min-height: 100%;
background-color: black;
}
p {
margin: 0;
color: #ffffff;
}
<html>
<head>
<meta name="viewpoint" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="myCssTemplate.css">
</head>
<body>
<div id="bodyWrapper">
<p> This is a test.</p>
</div>
</body>
</html>

Related

Flexible image sizing inside flexbox container

I have a flexbox layout with an image and a caption (stacked). I'd like the image to take up all the available screen space, minus whatever space the caption needs below. Each image + caption combo should never exceed 100% of the viewport height.
The problem: I'm struggling finding a way to make the image not exceed the height of the available space. (In other words, the image will be larger than the available area, and it needs to scale down to respect the 100vh height set on <section> in my code example below.
Example of desired look
Image space outlined in red, caption space outlined in blue
Demo of problem
I added a max-height to the <img> so you can see the desired effect. (Remove max-height on the <img> to see the problem.)
Ultimately I need it to work like this without setting a literal max-height. Effectively it should be max-height: calc(100vh - (height of caption)), but I'm trying to do this without javascript.
From earlier comment
to keep flex children inside boundaries and use a max-size in %, you need to use overflow:hidden;, so the browser can calculate those size, else content boxes can overflow.
example:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
body {
display: flex;
flex-direction: column;
height: 100vh;
margin: 0;
}
header {
display: flex;
justify-content: space-between;
background: lightblue;
padding: 1vmax;
}
section {
flex: 1;
overflow: hidden;
/* the browser will calculate size */
display: flex;
flex-direction: column;
align-items: center;
}
figure {
padding: 1vmin;
display: flex;
flex: 1 1 auto;
overflow: hidden;
/* the browser will calculate size */
}
img {
margin: auto;
max-height: 100%;
max-width: 100%;
flex-shrink: 1;
/* IE */
}
<header>
<h1>Logo</h1>
<nav>
Link Link Link
</nav>
</header>
<section>
<figure>
<img src="https://placekitten.com/1200/1200" />
</figure>
<aside>
<strong>Lorem Ipsum #1</strong>
<p>Vivamus a neque dui. Nunc tortor risus, mattis quis vulputate at, lacinia vel enim. Nam aliquet ipsum a mi malesuada, nec accumsan felis accumsan.</p>
</aside>
</section>

How to style a blockquote with large curly braces around it

I am trying to style a block of text so that it is surrounded by a large curly brace on each side (so that each brace takes up the whole height of each side of the element). Here is the HTML:
<blockquote>
<span class="braceleft">{</span>
<p class="quotation">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus Pellentesque at neque lorem, vitae aliquet risus.</p>
<span class="braceright">}</span></blockquote>
I should also mention that I am trying to do this in WordPress, which I know can add unwanted tags. If I could get the right CSS for plain HTML, I can hopefully figure out how to strip the unwanted tags.
I can easily change the HTML markup if that would make styling easier.
Remove the <span> and <p> tags. Edit the opening <blockquote> tag to
<blockquote class="addCurlys">. Use this CSS (play with the font-size for the :before and :after pseudoelements):
blockquote {
font-size:1em;
}
blockquote.addCurlys:before {
content: "{";
font-size:10em;
}
blockquote.addCurlys:after {
content: "}";
font-size:10em;
}
Because em is the unit of measurement for the :before and :after pseudoelements, they're linked to the font-size of their parent - the blockquote itself.
I think most browsers now support :: for pseudoelements - I still tend to only use one
Keep in mind that you should restrict content to make it fit inside curly brackets.
You can probably do something like
<blockquote class="clearfix">
<div class="curly-left float-left">
<div class="float-left"> Content here </div>
<div class="curly-right float-left">
</blockquote>
Then in the CSS you set the hight / width and background image for curly left and right.
Float the divs and use clearfix on the blockquote.
I think that should do it.
Here's the code I use for clearfix
/* Clearfix */
.clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; }
.clearfix:after { clear: both; }
.clearfix { zoom: 1; }
<span class="braceleft" style="float:left; padding: 0 10px">{</span>
<p class="quotation" style="float:left; padding: 0; margin: 0">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus Pellentesque at neque lorem, vitae aliquet risus.</p>
<span class="braceright" style="float:left; padding: 0 10px">}</span>
The way to do it without additional tags would be to use CSS's :before and :after to create the 2 braces and then style them accordingly.
Consider this code (or test the fiddle):
HTML:
<blockquote class="addCurlys">I like curly <br> curls <br/><br/> I really do<br/><br/>I really really do</blockquote>
CSS:
BLOCKQUOTE.addCurlys {background: yellow; position: relative; padding: 0.5ex; 1em}
BLOCKQUOTE.addCurlys:before {
content: ''; border: 1px dotted pink;
position: absolute; right: 100%; top: 0; bottom: 0; width: 30px;
background-image: url('http://placekitten.com/g/30/60'); background-size: 100% 100%
}
BLOCKQUOTE.addCurlys:after {
content: ''; border: 1px dotted pink;
position: absolute; left: 100%; top: 0; bottom: 0; width: 30px;
background-image: url('http://placekitten.com/g/30/60'); background-size: 100% 100%
}
Here's how it works (for the opening brace): :before creates a pseudo-element. We need to add the content attribute otherwise it won't be rendered 'properly'. The pink border is only there so you can see where it is.
The BLOCKQUOTE is given a position: relative attribute so that the before and after blocks can be positioned relative to it. We give :before a position: absolute and give it a top and bottom value of 0 so that it gets aligned with the blockquote's top and bottom edges. Then we give it a right: 100% so that it gets pushed all the way to the left of the edge (could use left:0 if you want it inside the blockquote, adjust for your tastes). And a width to our liking.
Finally we add a background (since you wanted the curl to stretch vertically) image and specify that we want it to be sized 100% by 100% of the container (:before, i.e. the opening brace). Feel free to change the kitten images to curly braces, I prefer kittens.
Adjust for your needs.

How would I align a <div> container to the middle of a webpage?

I'm designing a website and this is the last piece I need before I can upload it.
I've searched this site and many others, but I honestly can't find the right solution. I'll be as specific as I can.
www.solarfields.com
This is as good of an example as any. I want to align my content just like his, in the middle of the webpage. He uses Flash, however, and I'd like to keep mine to HTML and CSS.
I have encompassed the entire body of my site in a tag with these attributes:
#body {
position:absolute;
top:13%;
left:25%;
width:350px;
}
It's centered on my monitor (1600x900), but I want it to appear centered on all resolutions.
The size: 525px (h) x 350px (w).
I more than one <div>.
I don't care about browser compatibility.
I don't want visible borders.
There's one table 801px wide. It has two <td>'s, 29px and 762px wide.
I'm a CSS noob, so I'm just giving random information. If you need anything else, don't hesitate to e-mail me # mattjack66#gmail.com.
Thanks so, so much.
Horizontally and vertically centered:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
body div {
position: absolute;
top: 50%;
left: 0;
right: 0;
}
body div div {
position: relative;
top: -262px;
height: 525px;
width: 350px;
margin: 0 auto 0;
background: #000;
}
</style>
</head>
<body>
<div><div>Testing</div></div>
</body>
</html>
If you set margin: 0 auto; on a div that also has a width set, it will center within it's parent container.
To be more specific, take the following css:
div#body {
width:902px; //Or whatever your content width is
margin:0 auto;
padding:0;
}
and apply that class to the outer most div in your site:
<html>
<body>
<div id"body">
...The rest of your content...
</div>
</body>
</html>
What this does is set the top and bottom margins of #body to 0, and the left and right margins to automatically distribute the empty space between them; which centers that div on the page. This applies a left / right center.
Solution: http://jsfiddle.net/magicaj/Xd7sK/5/
Centering horizontally is easy, you just need to set margin:0 auto; on the element you want centered. However getting an element to center vertically is much more difficult. There are CSS only solutions out there that mimic what tables can already do using the vertical-align:middle; CSS. I always strive to use tables only for tabular data but in this case I find tables for layouts is acceptable:
HTML:
<table class="wrapper">
<tr>
<td>
<div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas in lobortis ante. Phasellus rhoncus, magna adipiscing consequat viverra, nibh lectus eleifend neque, eget fermentum massa mauris id felis. Morbi aliquet fermentum quam ut sollicitudin.
</div>
</td>
</tr>
</table>
CSS:
html, body, .wrapper {
height:100%;
}
.wrapper {
margin: 0 auto; /* center horizontally */
}
.wrapper td {
height:100%;
vertical-alignment: middle; /* center vertically */
}
.content {
width: 350px;
background:#ccc;
}

how to wrap float div around absolute position divs? [duplicate]

I know there are a few questions about similar topics but they mostly amount to floating the div/image. I need to have the image (and div) positioned absolutely (off to the right) but I simply want the text flow around it. It works if I float the div but then I can't position it where I want. As it is the text just flows behind the picture.
<div class="post">
<div class="picture">
<a href="/user/1" title="View user profile.">
<img src="http://www.neatktp.com/sites/default/files/photos/BlankPortrait.jpg" alt="neatktp's picture" title="neatktp's picture" />
</a>
</div>
<span class='print-link'></span>
<p>BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah.</p>
<p>BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah.</p>
</div>
Is an example of the HTML
With the CSS being:
.picture img {
background: #fff;
border: 1px #ddd solid;
padding: 2px;
float: right;
}
.post .picture {
display: block;
height: auto;
position: absolute;
right: -10px;
top: -10px;
width: auto;
}
.post {
border: 1px solid #FFF;
border-bottom: 1px solid #e8ebec;
padding: 37px 22px 11px;
position: relative;
z-index: 4;
}
It's a Drupal theme so none of this code is mine, it's just that it's not fully working when it comes to putting a picture there.
I know this is an older question but I came across it looking to do what I believe you were trying to. I've made a solution using the :before CSS selector, so it's not great with ie6-7 but everywhere else you should be good.
Basically, putting my image in a div I can then add a long thing float block before hand to bump it down and the text wraps merrily around it!
img {
float:right;
clear:both;
width: 50% ;
margin: 30px -50px 10px 10px ;
}
.rightimage:before {
content: '' ;
display:block;
float: right;
height: 200px;
}
You can check it out here:
http://codepen.io/atomworks/pen/algcz
Absolute positioning takes the element out of the normal document flow, and therefore it does not interact with the other elements. Perhaps you should revist how to position it using float instead, and ask about it here on Stack Overflow if you get stuck :)
As mentioned by #Kyle Sevenoaks, you are taking absolute positioned content out of the document flow.
As far as I can see, the only way to have the parent div wrap the absolute positioned contents, is to use javascript to set the width and height on each change.
When you position a div absolutely, you're effectively taking it out of the document flow, so the other elements will act as if it's not there.
To get around this, you can instead use margins:
.myDivparent
{
float: left;
background: #f00;
}
.myDivhascontent
{
margin-left: 10px; /*right, bottom, top, whichever you need*/
}
Hopefully that will do the trick :)
In my opinon, the "Absolute" trait is poorly named, because its position is actually relative to the first parent whos position is not static
<div class="floated">
<div style="position: relative;">
<div class="AbsoluteContent">
stuff
</div>
</div>
</div>
I think the best option is to add an additional div after the float content, but still inside the parent to clear previous styles.
<div class="clear"></div>
And CSS:
.clear
{clear:both;}
I needed a similar solution to float a pullout quote (not an image) which would have variable length text inside. The pullout quote needed to be inserted into the HTML at the top (outside the flow of the text) and float down into the content with text that wraps around it. Modifying Leonard's answer above, there is a really simple way to do this!
See Codepen for Working Example: https://codepen.io/chadwickmeyer/pen/gqqqNE
CSS
/* This creates a space to insert the pullout content into the flow of the text that follows */
.pulloutContainer:before {
content: '' ;
display:block;
float: right;
/* The height is essentially a "margin-top" to push the pullout Container down page */
height: 200px;
}
.pulloutContainer q {
float:left;
clear:both;
/* This can be a set width or percent, if you want a pullout that floats left or right or full full width */
width: 30%;
/* Add padding as you see fit */
padding: 50px 20px;
}
HTML
<div id="container">
<div class="pulloutContainer">
<!-- Pullout Container Automatically Adjusts Size -->
<q>Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet, consectetur adipiscing elit Lorem ipsum dolor sit amet.</q>
</div>
<div class="content">
<h1>Sed Aucteor Neque</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam in dui mauris. Vivamus hendrerit arcu sed erat molestie vehicula. Sed auctor neque eu tellus rhoncus ut eleifend nibh porttitor. Ut in nulla enim. Phasellus molestie magna non est.</
...INSERT MORE TEXT HERE...
</div>
</div>
Absolute positioning does not let you wrap text. You have to use float and position using margin or padding.
Here's a trick that might work for some:
if you have a container packed with a lot of objects, and you want that positioned object to appear up high in certain cases, and down lower in other cases (various screen sizes for example), then just intersperse copies of the object multiple times in your html, either inline(-block), or with float, and then display:none the items you dont want to see according to the conditions you need.
Here is a JSFiddle to show exactly what I mean: JSFiddle of right positioning high and low
Note: I added color only for effect. Except for the class names, the subject-1 and subject-2 divs are otherwise exact copies of each other.
There is an easy fix to this problem. It's using white-space: nowrap;
<div style="position:relative">
<div style="position: absolute;top: 100%; left:0;">
<div style="white-space:nowrap; width: 100%;">
stuff
</div>
</div>
</div>
For example I was making a dropdown menu for a navigation so the setup I was using is
<ul class="submenu" style="position:absolute; z-index:99;">
<li style="width:100%; display:block;">
Dropdown link here
</li>
<ul>
Image Examples
Without Nowrap enabled
With Nowrap enabled
Also if you still can't figure it out check out the dropdowns on bootstrap templates which you can google. Then find out how they work because they are using position absolute and getting the text to take up 100% width without wrapping the text.

CSS Dynamic Heights

I have three div tags, a wrapper and two side by side within the wrapper:
<div id="wrapper">
<div id="left"></div>
<div id="right"></div>
</div>
I want to create the condition where the <div id="left"> tag is variable height, stretching the wrapper.
As a result, the <div id="right"> will expand to whatever height the wrapper has become.
What CSS will accomplish this? Thanks!
Perhaps I'm late to the party, but it really is simple to do what you describe with pure CSS. The trick is to make #right absolutely positioned and give it a top and bottom of 0. This will stretch it to whatever height #left is giving #wrapper. Here's a complete working example — #left is green, #right is blue, and #wrapper is red, but never seen because it's completely covered by #left and #right. Try removing the bottom: 0; line to see what it looks like without it.
<html lang="en">
<head>
<title></title>
<style type="text/css">
#wrapper {
background: #fdd;
position: relative;
width: 300px; /* left width + right width */
}
#left {
background: #dfd;
width: 200px;
}
#right {
background: #ddf;
bottom: 0;
position: absolute;
right: 0;
top: 0;
width: 100px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="left">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer augue</p>
<p>massa, scelerisque non viverra sagittis, egestas nec erat. Aliquam vel</p>
</div>
<div id="right">
<p>turpis metus. Sed id lorem eu urna suscipit porttitor. Nullam. </p>
</div>
</div>
</body>
</html>
If you're going to go with Jquery, then you could do this.
$(document).ready(function(){
var leftHt = $('#left').height();
$('#right').css("height",leftHt);
});
It isn't css, but it's pretty simple and should work. CSS just wouldn't be able to easily do this, to my knowledge at least.
If you don't already have the Jquery API, just past this above the Javascript.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="javascript" language"javascript"></script>
Working example here:
http://michaelpstone.net/development/dynamic-height.html
There isn't really a good way for #right to match whatever #left has become. The best way to do this is probably:
<div id="wrapper">
<div id="right"></div> <!-- note that right comes before left -->
<div id="left"></div>
</div>
Then have this style:
#left, #right {
width: 50%; /* Adjust as needed */
#right {
float: right;
}
This way, #right won't affect the page length but #left always will. However, #right still won't stretch to the length of #left. I don't know what reason you have for it needed to stretch to #left, but I assume it's something cosmetic. I would either try to apply it from #left or from #wrapper instead if you want it to repeat all the way down.
For example, if you want the #left white and #right red:
#left {
background: #fff;
}
#wrapper {
background: #f00;
}
The best practical solution is a table - see [this SO question] (Make Two Floated CSS Elements the Same Height).
Of course you may have your own reasons for not using tables...
If you are trying to do backgrounds or something and thus need the same height, I'd recommend tables. It's much easier. If you must use divs and floated divs, particularly, this article presents about the only way I've seen that works well. It's for three columns, but you can modify it for two:
http://matthewjamestaylor.com/blog/holy-grail-no-quirks-mode.htm

Resources