HTML/CSS Float Left and Top/Bottom without position absolute/fixed - html

I'm working on my Chemistry application, and I'm struggling with displaying div element how I imagined it could work.
My goal is to have divs floating left as on image: so when hiding red/green div everything stays in order.
Is it even possible without using absolute/fixed positioning? I really need those divs to float left and be aware of each other so I can't solve it by position absolute. I tried experimenting with adding margin, but other div cannot fit into place taken by other element margin.
Thank you for your time spent on reading this post!
Code added:
<div class='container'>
<div class='base-cell'>S</div>
<div class='base-cell'>O</div>
<div class='index-cell'>3</div>
<div class='charge-cell'>2-</div>
</div>
.container{
border: 1px solid red;
height: 1.5em;
text-align: center;
position: relative;
}
.base-cell{
position: relative;
background: red;
height: 1em;
float: left;
margin-top: 0.2em;
font-size: 1em;
border: 1px solid orange;
display: inline-block;
}
.index-cell{
position:relative;
height:0.7em;
margin-top:1.5em;
font-size:0.7em;
display:table;
background: blue;
float:left;
}
.ion-index-cell{
position: relative;
height: 1em;
font-size: 0.7em;
border: 1px solid cyan;
display: table;
background: green;
}
.charge-cell{
height: 1em;
font-size: 0.7em;
border: 1px solid blue;
display: inline-block;
float:left;
}
Edit:
Thank you for your replies, I really don't want to use middle column solution, because of another requirement: sorry for not showing full context before.
As you can see in the picture, all elements flow to the left, and I may need to hide some by using display: none. Thats why I'm looking for parentless solution:

If you flip the diagram on its side then its a lot easier to build using floats. You can use transforms to flip it back up the correct way.
.wrap {
max-width: 100px;
-webkit-transform: rotate(90deg) translate(-170px, -10px);
-moz-transform: rotate(90deg) translate(-170px, -10px);
-ms-transform: rotate(90deg) translate(-170px, -10px);
transform: rotate(90deg) translate(-170px, -10px);
-webkit-transform-origin: 0% 100%;
-moz-transform-origin: 0% 100%;
-ms-transform-origin: 0% 100%;
transform-origin: 0% 100%;
overflow: hidden;
}
.block {
height: 40px;
width: 100%;
float: left;
border: 1px solid #000;
box-sizing: border-box;
margin: 10px 0;
}
.block-left {
max-width: 40%;
border-color: #f00;
}
.block-right {
max-width: 40%;
float: right;
border-color: #0f0;
}
<div class="wrap">
<div class="block block-top"></div>
<div class="block block-left"></div>
<div class="block block-right"></div>
<div class="block block-bottom"></div>
</div>
<div class="wrap">
<div class="block block-top"></div>
<div class="block block-right"></div>
<div class="block block-bottom"></div>
</div>
<div class="wrap">
<div class="block block-top"></div>
<div class="block block-left"></div>
<div class="block block-bottom"></div>
</div>

This may help you somewhat. Its very crude html but I believe does what your looking for. It should at least help you in the direction your looking to go.
<div style="height:100%;">
<div style="float:left; width: 33%;">
Content 1
</div>
<div style="float:left; width: 33%;">
<div style="height:50%">
<div>Content 2</div>
</div>
<div style="height:50%;">
<div>Content 3</div>
</div>
</div>
<div style="float:left; width: 33%;">
Content 4
</div>
</div>

From your question, it looks like you just want to use float:left instead of position:absolute which you are using currently and still want to hide the green and red boxes, while keeping all other boxes intact.
This can be achieved by using float:left; on the boxes while setting the opacity:0; on the red and green boxes (also visibility:hidden work).

So I'm not sure how you are handling the mark up but hopefully you are doing it the proper way. It seems like you have a grid-format in place but you are not applying this on the middle column.
What you should be doing is creating three columns and then when necessary, you can hide the middle column. The red and green box can exist within the middle column. This way if you ever say wanted to add those red/green sections in the left or right column, you can easily do that.
I have created an example below. I have also added a class called hide which can apply to the different columns and/or inner boxes. Like I was mentioning above, you should be adding hide to the middle col if you want to hide everything in the middle column. Apply hide to the inner elements if you want to hide one of those.
I do some absolute positioning in the middle column but you don't actually need to do this -- you can change this to float: left and simply set a margin-top for the bottom box.
.col {
float: left;
width: 100px;
height: 200px;
border: 1px solid black;
margin-left: 10px;
position: relative;
}
.inner {
width: 100px;
height: 75px;
margin-left: auto;
margin-right: auto;
position: absolute;
}
.top {
top: 0;
border: 1px solid red;
}
.bottom {
bottom: 0;
border: 1px solid green;
}
.hide {
display: none;
<div class="col left"></div>
<div class="col middle">
<div class="top inner"></div>
<div class="bottom inner"></div>
</div>
<div class="col right"></div>
EDIT: I notice you posted your CSS and you're using display: table. For that I would like to refer you to this link.
shouldiusetablesforlayout.com
EDIT2: I see you updated your question but the overall concept applies. You are still dealing with columns but I guess in your case now, you kind of want those columns in containers.
.col-container {
float: left;
margin-bottom: 10px;
}
.col {
float: left;
width: 100px;
height: 200px;
border: 1px solid black;
margin-left: 10px;
position: relative;
}
.inner {
width: 100px;
height: 75px;
margin-left: auto;
margin-right: auto;
position: absolute;
}
.top {
top: 0;
border: 1px solid red;
}
.bottom {
bottom: 0;
border: 1px solid green;
}
.hide {
display: none;
}
<div class="col-container">
<div class="col left"></div>
<div class="col middle">
<div class="top inner"></div>
<div class="bottom inner hide"></div>
</div>
<div class="col right"></div>
</div>
<div class="col-container">
<div class="col left"></div>
<div class="col middle">
<div class="top inner hide"></div>
<div class="bottom inner"></div>
</div>
<div class="col right"></div>
</div>
<div class="col-container">
<div class="col left"></div>
<div class="col middle">
<div class="top inner"></div>
<div class="bottom inner"></div>
</div>
<div class="col right"></div>
</div>
If you view it in full page, and shrink the window size, you'll see the 3rd col-container to appear on the second line. If you want to make sure it only has two columns or things break at certain points you can adjust for that by either adding clear to certain elements, distinguishing row classes, etc.

I would use flexbox and justify-content: space-between; should be the thing you are asking for.
<article>
<div>left</div>
<div class="content">
<p>top</p>
<p>bottom</p>
</div>
<div>right</div>
</article>
article {
display: flex;
min-height: 10em;
}
article > div {
flex: 1 1 calc(33.3333% - 1em);
margin: 0.5em;
}
.content {
display: flex;
flex-direction: column;
justify-content: space-between;
}
Codepen sample (w/ -prefix-free, styling and as SCSS)
Simple ;)

Related

HTML/CSS adjust parent div's height/width automatically according to children content

I want to make this:
stacked cards
the html would look like so:
<div class="container>
<div class="top-card>
<div class="card-content">
...
</div>
</div>
<div class="bottom-card>
</div>
</div>
I am having trouble styling this so that the height of the entire card adjusts automatically according to the content inside the top card. Thank you in advance.
you can use a combination of box-shadow and display: inline-block to accomplish what you are trying to do. I have updated the answer. Here is the code:
.grandparent {
display: inline-block;
position: relative;
}
.parent {
display: inline-block;
background: blue;
position: absolute;
top: 0;
left: 0;
}
.child {
width: 100px;
height: 100px;
background: red;
margin: 5px;
}
.shadow {
margin-left: -7px;
margin-top: -7px;
background: pink;
z-index: -100;
border: 1px solid green;
}
.empty {
opacity: 0;
}
<div class="grandparent">
<div class="parent">
<div class="child"></div>
</div>
<div class="parent shadow">
<div class="child empty"></div>
</div>
</div>

How can I set height higher than 100% in a flexbox flex-column class div?

I have created a grid with bootstrap, and in one of the columns I would like to have a grid of divs. Every div should become bigger on hover, and should go over surrounding ones.
Divs have images and text inside. There should be 3 divs on mobile (one above the other, single div in every 'row') ,and 3 'rows' with 3 divs inline on bigger screens. I have achieved that by putting following classes on a bootstrap div that contains previously mentioned: col d-flex flex-column flex-md-row.
<div class="container">
<div class="row">
<div class="col-lg-3">
<p>Place for some other content</p>
</div>
<div class="col-lg-9 ">
<div class="row">
<div class="col d-flex flex-column flex-md-row justify-content-
around">
<div class="image-container">
<div class="left">
<img src="./img/flag.png" alt="">
</div>
<div class="main">
<img src="some image" alt="">
<p>Some text</p>
</div>
<div class="lower">
<button class="btn">Link me</button>
</div>
</div>
/* two more .image-container divs */
</div>
</div>
/* two more times the same: div.row, div.col.d-flex etc.*/
</div>
</div>
</div>
One major point is this: every div with image has two hidden divs on the sides, so when you hover over the div - the div kindda expands (hidden divs get the display: block), AND its content goes over divs on the left and bottom (I have set z-index) without moving those surrounding divs.
Everything works as I wanted, EXCEPT on the mobile where I have set flex-column direction. There divs simply don't expand towards bottom, only on the left. The hidden div on the bottom shows on hover INSIDE the parent, instead bellow and above the following lower positioned element.
SCSS:
.col-lg-3 {
display: none;
#media only screen and (min-width: 992px) {
display: flex;
}
}
.image-container {
margin:15px;
width: 250px;
position: relative;
.main {
padding: 0 10px;
border: 1px solid black;
}
.lower {
display: none;
position: absolute;
background-color: white;
bottom: 0;
right: 0;
width: 100%;
height: 21%;
border: 1px solid black;
border-top: none;
padding: 0 10%;
button {
width: 100%;
background-color: orange;
color: white;
}
}
.left {
display: none;
position: absolute;
background-color: gray;
left: -50px;
top: 0;
height: 100%;
width: 50px;
border: 1px solid black;
border-right: none;
img {
width: 50%;
margin: auto;
}
}
&:hover {
z-index:1;
height: 115%;
.lower, .left {
display: block;
}
}
}
Why is that happening, and how can I make it work the way I intended.
Also, I would be very happy to hear if you have suggestions about other possible solutions for creating these get-bigger-on-hover divs.
If I understood your question correctly, are you looking to increase the size of the tile in-place without affecting the layout of other. If yes, then I have created a small fiddle HERE.
This uses flexbox and scale transformation to increase the size of tile in-place.
.container {
width: 600px;
height: 400px;
background-color: #eee;
border: 1px solid #aaa;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.tile {
border: 1px solid #888;
height: 190px;
width: 190px;
}
.tile:hover {
transform: scale(1.3);
}
<div class="container">
<div class="tile"></div>
<div class="tile"></div>
<div class="tile"></div>
<div class="tile"></div>
<div class="tile"></div>
<div class="tile"></div>
</div>
A better approach would be to use CSS grids. It will allow you to evenly space out tiles around your workspace.

Tic-Tac-Toe board not showing up on webpage [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
For some reason my css and html is not showing up on the webpage. It only shows the h1 title of Tic-Tac-Toe. I've written the code out to match what my homework list but I am missing something and I've started over several times and cannot seem to get what I am missing. maybe I'm reading the homework wrong?
h1 {
text-align: center;
}
.top {
padding: 3em;
float: left;
height: 100px;
width: 100px;
border-bottom: 1px black solid;
}
.middle {
float: left;
height: 100px;
width: 100px;
border-top: 1px black solid;
border-bottom: 1px black solid;
}
.center {
float: left;
height: 100px;
width: 100px;
border-top: 1px black solid;
border-bottom: 1px black solid;
border-left: 1px black solid;
border-right: 1px black solid;
}
.bottom {
float: left;
height: 100px;
width: 100px;
border-top: 1px black solid;
}
.row {
clear: both;
width: 302px;
text-align: center;
}
HTML:
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="app.css">
<div id=b oard>
<h1>TicTacToe</h1>
<div class="row 1">
<div class="top left"></div>
<div class="top middle"></div>
<div class="top right"></div>
</div>
<div class="row 2">
<div class="middle left"></div>
<div class=".center"></div>
<div class="middle right"></div>
</div>
<div class="row 3">
<div class="bottom left"></div>
<div class="bottom middle"></div>
<div class="bottom right"></div>
</div>
</div>
</html>
In your code the first thing I noticed is that you're trying to display items where each one is wider than its container, so the three columns on each row overflows the row total width, so they stack on each other.
Try to use box-sizing: border-box You can read about it here
The important thing is:
It seems that this homework is somewhat old, as it gives tips to build the board using css in a manner we don't use anymore.
The float property is really not the best way to arrange the elements of the page. Since that property was created a long time ago, when the websites were very different, basically texts and images, it was intended mainly to arrange images inside blocks of text. So it produces some really weird results sometimes, if you don't have total knowledge of how it works. see this for details
You could try to use flexbox, once you study and understands the basics of how it works, your life would be a lot easier!
A complete guide to flexbox
With flexbox you could do something like:
div {
box-sizing: border-box;
}
.row {
display: flex;
justify-content: center;
flex-wrap: wrap;
width: 300px;
height: 100px;
background-color: grey;
}
.col {
flex: 1;
margin: 10px;
background-color: blue;
}
<div id= board>
<h1>TicTacToe</h1>
<div class="row">
<div class= "col"></div>
<div class= "col"></div>
<div class= "col"></div>
</div>
<div class="row">
<div class= "col"></div>
<div class= "col"></div>
<div class= "col"></div>
</div>
<div class="row">
<div class= "col"></div>
<div class= "col"></div>
<div class= "col"></div>
</div>
</div>
I used margins and background colors just to make it easier to see the boxes, you can change it as you need.
Other way you could resolve this is by using CSS Grid layout
The essential mistake you made is that the different borders produce different heights and widths for those elements, since the 1px borders are added to the height and width, and therefore some floating elements will go to the next row since there's 1px too little vertical space to make it fit in the same row, messing up the intended layout. (Some elements are 102 high, some 101px)
But if you add box-sizing: border-box, the border width is included in the height and width, so each of your element will really be 100x100px, including the borders.
That's done in my snippet below. The borders obviously are still not correct, but fixing that is up to you now...
* {
box-sizing: border-box;
}
h1 {
text-align: center;
}
.top {
padding: 3em;
float: left;
height: 100px;
width: 100px;
border-bottom: 1px black solid;
}
.middle {
float: left;
height: 100px;
width: 100px;
border-top: 1px black solid;
border-bottom: 1px black solid;
}
.center {
float: left;
height: 100px;
width: 100px;
border-top: 1px black solid;
border-bottom: 1px black solid;
border-left: 1px black solid;
border-right: 1px black solid;
}
.bottom {
float: left;
height: 100px;
width: 100px;
border-top: 1px black solid;
}
.row {
clear: both;
width: 302px;
text-align: center;
}
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="app.css">
<div id=b oard>
<h1>TicTacToe</h1>
<div class="row 1">
<div class="top left"></div>
<div class="top middle"></div>
<div class="top right"></div>
</div>
<div class="row 2">
<div class="middle left"></div>
<div class="center"></div>
<div class="middle right"></div>
</div>
<div class="row 3">
<div class="bottom left"></div>
<div class="bottom middle"></div>
<div class="bottom right"></div>
</div>
</div>
</html>

Center div on wrapping (when it doesn't fit on the line)

I am trying to create this layout using only CSS:
When title fits:
When title doesn't fit:
The btn on the right should be centered if it wraps.
I tried this:
.container {
width: 100%;
border: 1px solid grey;
padding: 5px;
}
.block {
padding: 5px;
border: 1px solid orange;
float: left;
}
.right-block {
float: right;
}
<div class="container">
<div class="block">Logo</div>
<div class="block">Title that is too long</div>
<div class="block right-block">right-btn</div>
<div style="clear: both"></div>
</div>
But obviously, the btn is still on the right after it wraps. Any idea how to center it when it wraps ? And I'd like to avoid javascript.
Fiddle here: http://jsfiddle.net/b7rvhwqg/
Pure CSS solution using a flexbox layout:
Updated Example Here
The trick is to add justify-content: center/flex-wrap: wrap to the parent .container element for horizontal centering. Then adjust the first element's margin-right value to auto in order to prevent the last element from being centered when it's on the same line.
(You may need to resize the browser to see how it adjusts).
.container {
width: 100%;
border: 1px solid grey;
padding: 5px;
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.logo-text {
display: flex;
margin-right: auto;
}
.block {
padding: 5px;
border: 1px solid orange;
}
.center-block {
white-space: nowrap;
margin-right: auto;
}
<div class="container">
<div class="logo-text">
<div class="block logo">Logo</div>
<div class="block text">This title is short.</div>
</div>
<div class="block right-block">right-btn</div>
</div>
<div class="container">
<div class="logo-text">
<div class="block logo">Logo</div>
<div class="block text">This title is slightly longer than the other one. This title is longer than the other one...</div>
</div>
<div class="block right-block">right-btn</div>
</div>
There is an issue to achieve this via Pure CSS. The div is already having a float and you want to have a "long title" to accommodate that float and at the same time, you want the other right float to jump and become center. This is currently not possible. I believe, you need to consider media queries, but again, that will be a dependent solution, but your title looks like independent of expanding/contracting.
is it ok for you if the title will just fit depending on what width u want?.. for example:
{Logo}Title is toooooooooooooooooooooooooooooooooolong {btn}
it will become like this:
{Logo}Title is tooo... {btn}
it will be cut, then only ". . ." will continue
Flexbox is the most suitable for this task:
<div class="container">
<div class="block logo">Logo</div>
<div class="block title">Title that is too long Title that is too long</div>
<div class="block right-block">right-btn</div>
<div style="clear: both"></div>
</div>
.container {
display: flex;
flex-wrap: wrap;
width: 100%
border: 1px solid grey;
}
.block.logo {
flex-grow: 1;
border: 1px solid orange;
}
.block.title{
flex-grow: 10;
border: 1px solid orange;
}
.right-block {
flex-grow: 1;
border: 1px solid orange;
text-align: center;
}
http://jsfiddle.net/gmrash/7b8w982t/
.container {
width: 100%;
border: 1px solid grey;
padding: 5px;
}
.block {
padding: 5px;
border: 1px solid orange;
float: left;
}
.ellipsis{
text-overflow: ellipsis;
/* Required for text-overflow to do anything */
white-space: nowrap;
overflow: hidden; width: 75%;
}
.right-block {
float: right;
}
<div class="container">
<div class="block">Logo</div>
<div class="block ellipsis">Title that is too long Title that is too long Title that is too long that is too long Title that is too long</div>
<div class="block right-block">right-btn</div>
<div style="clear: both"></div>
</div>
jsFiddle

How do I align mixed-size divs within a container?

Updated based on comments
I'm trying to create div sections on a full sized page by making containers that are 30% of the width. Within those, I plan to have 2 or 3 div sizes aligned within them. I have a row with a large box that occupies 100% of the height, and a portion of the width, and then a box that's exactly half of the size. I'd like to have all of those half-size boxes be in the same row as the larger box to create a nice stack. I'm assuming it's an issue of size vs position, but I haven't had much luck and I'm over-thinking the issue.
Fiddle: https://jsfiddle.net/as9hud4k/10/
HTML:
<div class="content_section">
<div class="content_thirdsize">
<div class="content_thirdsize_inner_row">
<div class="content_thirdsize_inner_large"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
<div class="content_thirdsize_inner_small"> </div>
</div>
</div>
</div>
CSS:
.content_thirdsize
{
width: 500px;
height: 500px;
display: inline-block;
text-align: center;
vertical-align: top;
background-color: rgba(83, 35, 128, 0.2);
}
.content_thirdsize_inner_row
{
width: 500px;
height: 105px;
display: inline-block;
background-color: rgba(83, 35, 128, 0.2);
margin: 2px;
}
.content_thirdsize_inner_large
{
position: relative;
width: 100px;
height: 100px;
display: inline-block;
background-color: rgba(83, 35, 128, 0.2);
border: 1px dashed #000;
vertical-align: left;
}
.content_thirdsize_inner_small
{
position: relative;
width: 50px;
height: 50px;
display: inline-block;
background-color: rgba(83, 35, 128, 0.2);
border: 1px dashed #000;
vertical-align: right;
}
I suspect the math may need to be tweaked to account for spacing but flexbox can do a lot of the work here.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
display: flex;
margin: auto;
}
.content-wrap {
display: flex;
flex-wrap: nowrap;
padding: 5px;
background: orange;
}
.small-wrap {
display: flex;
flex-wrap: wrap;
width: 350px;
}
.large,
.small {
width: 100px;
height: 100px;
background: rebeccapurple;
border: 2px dotted white;
}
.small {
width: 50px;
height: 50px;
}
<section>
<div class="content-wrap">
<div class="large"></div>
<div class="small-wrap">
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div>
<div class="small"></div </div>
</div>
</section>
Codepen Demo
I would recommend using position and then align the divs using left, right, top, bottom. Pick a position setting that makes sense in your mind for moving the div and then fiddle with it's position until they line up as you would like.
As paulie_D said in the comments don't use IDs multiple times, they are supposed to be unique. You want to be using a class to apply the same style on multiple objects. My general rule is that classes are for applying style and IDs are for identifying a specific object on the page.