Why two float divs are standing one under another? - html

Why is the second div going under the first? Both of them are "float" elements. When I set a width of the second div, all works well. But I expect these two divs are being located to one row.
.one {
background-color: green;
float: left;
border: 1px solid green;
}
.two {
float: left;
background-color: red;
border: 1px solid red;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>

This is because you didn't define the width's of the floating elements. If you define a max-width, say 50%, they will no longer be on the same line. I recommend giving max-width in contrast to width because, I believe you don't want to give the elements, a static width. Plus, they should be flexible to take as much as space they want, unless they shouldn't mess up with one another, which happens after 50%.
.one {
background-color: green;
float: left;
border: 1px solid green;
max-width: 50%;
}
.two {
float: left;
background-color: red;
border: 1px solid red;
max-width: 50%;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>

You can use flex instead float.
Add display: flex; for a container. And flex: 0 0 auto; for the first div and flex: 1 1 auto; for the second. flex: 0 0 auto; means that element will take as much space as needed. flex: 1 1 auto; means that element will take all available space.
.container {
display: flex;
}
.one {
flex: 0 0 auto;
background: red;
}
.two {
flex: 1 1 auto;
}
<div class="container">
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>
</div>

You can just give them some width options in %, or pixels using calc CSS functions, something like this...
.one {
background-color: green;
float: left;
border: 1px solid green;
display: block;
width: 10%;
}
.two {
width: 70%;
float: left;
background-color: red;
border: 1px solid red;
display: block;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>

Flexbox is what you really want I think, float is great but always felt like a hack as opposed to a real solution.
You can use flexbox across all browsers, IE8 can't handle it but that's not a supported browser anymore. I suggest you read up on it on w3schools they generally have quite nice tutorials.
.flex-container {
display: flex;
}
.one,
.two {
padding: 5px;
}
.one {
background-color: green;
border: 1px solid green;
}
.two {
background-color: red;
border: 1px solid red;
}
html,
body {
margin: 0;
}
<div class="flex-container">
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>
</div>
As to why it's was happening with float, unless you say otherwise anything display block will take up the entire space allotted, which is 100%, with one of the divs taking up 100% there is no room for the other one on the same line.
You could have given it a set width, or changed the display to inline and removed the float left from .two (though that would have been a little odd looking).
.one {
float: left;
background-color: green;
border: 1px solid green;
}
.two {
display: inline;
background-color: red;
border: 1px solid red;
}
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
<div class="one">Menu</div>
<div class="two">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquam architecto beatae delectus eveniet impedit, labore minima nihil nostrum sint voluptates. Animi illum minima officia placeat quo rem repellendus reprehenderit vel.</div>

Related

div table - using summary and details

I made a table with divs, I want to use (summary, details), but when I click on show I want the text to start from the left side under "lala". Please help
Hey, I made a table with divs, I want to use (summary, details), but when I click on show I want the text to start from the left side under "lala". Please help
<div class="div-table dd">
<div class="div-table-row">
<div class="div-table-col">Title</div>
<div class="div-table-col y">Description</div>
<div class="div-table-col"></div>
</div>
<div class="div-table-row">
<div class="div-table-col">lala</div>
<div class="div-table-col x">yy</div>
<details>
<summary></summary>
<div>Lorem ipsum dolor sit amet consectetur adipisicing elit. Corporis id fugit dolorum voluptates saepe, numquam labore quae quia! Laboriosam at natus explicabo minima voluptatibus eligendi neque eum voluptatum aut! Officiis nostrum magni officia minus facilis omnis facere numquam sint porro, nobis aperiam odio commodi, veritatis consequatur, architecto illo accusamus sequi.s</div>
</details>
</div>
</div>
.div-table {
display: table;
width: 90vw;
border: 1px solid #666666;
border-spacing: 5px;
}
.div-table-row {
display: flex;
justify-content: space-between;
margin: 10px;
padding: 10px;
width: auto;
background-color: #ccc;
clear: both;
}
.div-table-col {
float: left;
display: table-column;
width: 33%;
}
.x{
width: 100%;
}
.y{
width: 33%;
}
details{
text-align: right;
}
summary{
padding: 0;
text-align: right;
}
details>summary {
white-space: nowrap;
list-style: none;
}
summary::-webkit-details-marker {
display: none
}
summary::after {
content: 'show ▼';
color: #007bff;
}
details[open] summary:after {
content: "hidden ▲";
}
https://codepen.io/olivier-mazur/pen/XWYdKVw

How to make square image container with css grid

I want to make a modal with a image and other elements to the right, something like this:
I tried making the modal content as a grid with two columns, the problems is when I try to adjust the image to fill all the avaiable space in the modal content, it overflows like this:
My css code looks like this:
<div class="modal">
<div class="content">
<div class="frame">
<img src="https://picsum.photos/400" alt="Example">
</div>
<div class="text">
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quis est mollitia possimus omnis nisi quo
aliquid ad accusantium dignissimos corrupti. Impedit, asperiores magnam corporis iste possimus tempore
quisquam provident dicta. Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quibusdam
consequuntur, incidunt hic dolorum tempora inventore id sint ullam magnam veniam eveniet vitae, harum
dolorem tenetur libero voluptate voluptatibus. Inventore, excepturi!
</div>
</div>
</div>
.modal {
position: fixed;
height: 100%;
width: 100%;
background-color: rgba($color: #000000, $alpha: 0.6);
display: grid;
place-items: center;
}
.content {
background-color: $color-white;
width: 60%;
height: 600px;
border-radius: 5px;
display: grid;
grid-template-columns: auto 300px;
}
.frame > img {
width: 100%;
max-height: 100%;
object-fit: cover;
}
.text {
padding: 2rem 1rem;
}
The modal I tried to do has the problem of the image overflow. I setted the container div to grid with two columns, but the image column overflows the container as we can see in the picture. I want the image to take the space avaiable in the container but maintaining the 1 : 1 aspect ratio, also to be responsive, when the user change the screen size the image always has to maintain the 1:1 aspect ratio and not crop the image.I setted the height as static pixels because I want to add a element with a scrolling bar that otherwise will overflow or expand the container if its height was declared as percentages.
Just specify a max-height to the container(.frame) it will start working fine for 1:1 images because max-height:100% doesn't mean anything if you don't specify a height for its parent
and if you want to work for any ratio then remove the width property from the .frame > img because if you don't specify a width it will adjust itself according to it's height
I think the easiest way would be to use css aspect-ratio like this:
img {
width: 100%;
aspect-ratio: 1;
}
you can read more about it here
I think the problem might be the fact that your image's parent ( .frame ) does not have a set height.Try adding this:
.frame {
height:100%
}
I solved it removing the height of the modal content div. This allow the image to take all the space it needs. For the text to the right to not overgrow the image, what i did was make a div container and inside it a p tag with position absolute and with the widht and height of his parent, but with overflow: scroll. This allowed me to have mi image in 1:1 ratio taking all the space it needs in the card but without the text overflowing things. Here is how I did it.
.modal {
position: fixed;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
display: grid;
place-items: center;
}
.container {
width: 60%;
display: grid;
grid-template-columns: 1fr 30%;
background-color: #f4f4ed;
}
.frame {
width: 100%;
height: 100%;
}
.frame img {
width: 100%;
height: 100%;
object-fit: cover;
}
.content {
display: flex;
flex-direction: column;
align-items: center;
padding: 2em 0.8em;
}
.content h5 {
font-size: 1.4rem;
font-weight: 600;
color: #00f0b5;
}
.text {
flex-grow: 1;
position: relative;
height: 100%;
width: 100%;
overflow-y: scroll;
}
.text p {
font-size: 0.8rem;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.actions {
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 0.5rem;
}
.actions button {
font-family: "Roboto Condensed", sans-serif;
font-weight: 600;
text-transform: uppercase;
border: none;
outline: none;
background-color: #00f0b5;
padding: 0.2em 0;
cursor: pointer;
font-size: 0.5rem;
}
body {
font-size: 62.5%;
box-sizing: border-box;
}
h5{
margin: 0;
line-height: 1;
}
<div class="modal">
<div class="container">
<div class="frame">
<img src="https://picsum.photos/800" alt="Some lorem picsum">
</div>
<div class="content">
<h5>Title</h5>
<div class="text">
<p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Laborum placeat sed voluptatem optio esse facere accusamus tempore, provident dolorem nemo rerum sit accusantium labore quae eveniet ea aliquam. Ipsa, aliquam? Lorem ipsum dolor sit amet consectetur,
adipisicing elit. Laborum placeat sed voluptatem optio esse facere accusamus tempore, provident dolorem nemo rerum sit accusantium labore quae eveniet ea aliquam. Ipsa, aliquam? Lorem ipsum dolor sit amet consectetur, adipisicing elit. Laborum
placeat sed voluptatem optio esse facere accusamus tempore, provident dolorem nemo rerum sit accusantium labore quae eveniet ea aliquam. Ipsa, aliquam?
</p>
</div>
<div class="actions">
<button>Accept</button>
<button>Cancel</button>
</div>
</div>
</div>
</div>

Inside border of image

I have some problem with creating inside border of image. Tried to do it with border, outline and box-shadow but didn't get the result.
HTML:
<div class="item">
<img src="https://i.ytimg.com/vi/vsZDSXlHqI4/maxresdefault.jpg" alt="">
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo blanditiis, distinctio. Odio eveniet vel nobis, consequuntur atque, dolorum debitis quae nesciunt esse quasi beatae, odit repudiandae dolore animi delectus ad nostrum, quas maiores hic labore?
Nisi, expedita sint, qui ullam itaque natus optio error accusantium placeat, culpa reiciendis, quos tempora.</p>
<button>Some action</button>
</div>
CSS:
div.item:hover {
//some code
img {
border-bottom: 5px solid #8cc34b;
margin-bottom: -5px;
}
}
My fiddle: JSFiddle
Here I got the outside border but I want to get inside border of image.
Thanks for help.
Wrap your img in div, and on hover use :after pseudo-element:
div.item {
min-height: 300px;
overflow: auto;
width: 300px;
background-color: white;
border: 2px solid #8cc34b;
border-radius: 5px;
text-align: center;
box-sizing: border-box;
-webkit-box-sizing: border-box;
}
div.item img {
width: 100%;
display: block;
}
div.item p {
text-align: left;
padding-left: 10px;
padding-right: 10px;
}
div.item button {
height: 35px;
width: 120px;
margin: 0 auto;
margin-bottom: 20px;
}
div.item .img-container {
position: relative;
}
div.item:hover h3 {
color: #8cc34b;
}
div.item:hover .img-container:after {
content: '';
position: absolute;
left: 0;
bottom: 0;
height: 5px;
width: 100%;
background-color: #8cc34b;
}
<div class="item">
<div class="img-container">
<img src="https://i.ytimg.com/vi/vsZDSXlHqI4/maxresdefault.jpg" alt="">
</div>
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo blanditiis, distinctio. Odio eveniet vel nobis, consequuntur atque, dolorum debitis quae nesciunt esse quasi beatae, odit repudiandae dolore animi delectus ad nostrum, quas maiores hic labore?
Nisi, expedita sint, qui ullam itaque natus optio error accusantium placeat, culpa reiciendis, quos tempora.</p>
<button>Some action</button>
</div>
I also added display: block on img, because inline img creates extra whitespace below.
Another answer:
To create a color bar like in the image you liked to, I wrapped your image in a DIV with class x and added the following CSS (including a pseudo element):
img {
width: 100%;
}
.x {
display: block;
position: relative;
}
.x:before {
content: '';
position: absolute;
z-index: -1;
bottom: 7px;
left: 0;
right: 0;
height: 5px;
background-color: #8cc34b;
}
Instead of a hover rule for the image, I just make the x DIV visible by changing its z-index:
div.item:hover {
h3 {
color: #8cc34b;
}
.x:before {
z-index: 1;
}
}
Adjust the bottom setting as desired to move it up or down:
https://jsfiddle.net/9wLx9p0f/5/
We can simply do this with outline-offset property:
outline: 1px solid #fff;
outline-offset: -10px;
Try this in the div you wants, if it works for you.

When I resize browsers window, my div gets down to bottom and a big space comes in top

When I resize browsers window, my div gets down to bottom and a big space comes in top. I want that my div should remain in center of page even if resize the size of window lesser than size of div. In my school they haven't teach about this but I want to align this div in center of page
<!DOCTYPE html>
<html>
<head>
<title>CSS centering Done Right.</title>
<style>
* {
margin: 0;
padding: 0;
}
html, body, .m {
height: 100%;
width: 100%;
}
.m {
opacity:1;
font-size: 0;
overflow: auto;
text-align: center;
/*styling>>*/
background-color: #8F1C10;
}
.m::before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.m>div {
display: inline-block;
font-size: 19px;
margin: 20px;
max-width: 320px;
min-height: 20px;
min-width: 300px;
padding: 14px;
vertical-align: middle;
/*styling*/
color:white;
background-color: #140A08;
outline:none;
}
</style>
</head>
<body>
<title>CSS centering Done Right.</title>
<div class="m">
<div contenteditable="true">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis, enim, possimus, voluptates, quia voluptatibus voluptas eum quaerat iure aperiam asperiores debitis fuga itaque quibusdam a ad odio assumenda iusto voluptatem.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem, ex quia consequatur quae quasi amet veniam est quas error quos perferendis ducimus non similique in soluta magnam dolore molestias veritatis.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptate, doloremque iste magni in eveniet ipsa odio mollitia eligendi magnam placeat aliquam hic reprehenderit reiciendis itaque assumenda ratione delectus. Alias, quis.</div>
</div>
</body>
</html>
margin: auto; is what you need. Try this:
<style>
* {
margin: 0;
padding: 0;
}
/*html, body, .m {
height: 100%;
width: 100%;
}*/
.m {
opacity:1;
font-size: 0;
overflow: auto;
text-align: center;
margin: auto;
/*styling>>*/
background-color: #8F1C10;
}
.m::before {
content:'';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.m>div {
display: inline-block;
font-size: 19px;
margin: 20px;
max-width: 320px;
min-height: 20px;
min-width: 300px;
padding: 14px;
vertical-align: middle;
/*styling*/
color:white;
background-color: #140A08;
outline:none;
}
</style>

Add a long black line at the end of a header tag

I'm trying to add a black line after a header tag in HTML using css :after pseudo class. Here is the code:
.container {
max-width: 700px;
margin: 0 auto;
padding: 10px;
letter-spacing: 0.07em;
line-height: 1.5em;
font-size: 130%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
h1:after {
display: inline-block;
content: "";
height: 2px;
background: black;
width: 200px;
margin-left: 5px;
margin-top: 8px;
}
<div class="container">
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Distinctio quia accusamus aperiam magni perspiciatis dignissimos, reiciendis dolor temporibus esse dolores quasi, reprehenderit necessitatibus culpa quas nesciunt quaerat porro! Ab, laborum.</p>
<p>Dolorem eligendi cumque deserunt illo quas aut pariatur inventore, optio provident maxime consectetur, soluta sed, aperiam illum beatae. Quidem beatae aliquid, impedit sit in accusamus rem necessitatibus, velit fugiat! Cupiditate.</p>
</div>
Codepen: http://codepen.io/angelangelesiii/pen/YXwPJb
I want that long black line to span the entire width left by the header itself.
Note: I don't want to try spanning the line to 100% then putting the header in front of it with a white background. It's an illusion but not the answer I'm trying to find. And as much as possible, no JS solutions I'm keeping this as simple as it can be.
Use position: absolute; and width:100% will give you desired output.
Check following.
.container {
max-width: 700px;
margin: 0 auto;
padding: 10px;
letter-spacing: 0.07em;
line-height: 1.5em;
font-size: 130%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
h1::after {
background: none repeat scroll 0 0 black;
content: "";
display: inline-block;
height: 2px;
margin-left: 5px;
margin-top: 8px;
position: absolute;
top: 0.5em;
width: 100%;
}
h1 {
overflow: hidden;
position: relative;
}
<div class="container">
<h1>Lorem Ipsum</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Distinctio quia accusamus aperiam magni perspiciatis dignissimos, reiciendis dolor temporibus esse dolores quasi, reprehenderit necessitatibus culpa quas nesciunt quaerat porro! Ab, laborum.</p>
<p>Dolorem eligendi cumque deserunt illo quas aut pariatur inventore, optio provident maxime consectetur, soluta sed, aperiam illum beatae. Quidem beatae aliquid, impedit sit in accusamus rem necessitatibus, velit fugiat! Cupiditate.</p>
</div>
Hope it helps.
Nevermind I have answered my question (once again)
.container {
max-width: 700px;
margin: 0 auto;
padding: 10px;
letter-spacing: 0.07em;
line-height: 1.5em;
font-size: 130%;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
h1 {
display: -webkit-flex;
display: flex;
width: 100%;
}
h1:after {
-webkit-flex: 3;
-flex: 3;
display: inline-block;
content: "";
height: 2px;
background: black;
margin: auto 0 auto 5px;
}
I have used flexbox to solve this. I know that flexbox have some issues but this works for me so it's good.
I tried to solve it using tables display properties
h1 {
display: table;
white-space: nowrap;
}
h1:after {
display:table-cell;
content: "";
width: 100%;
border-bottom: 2px solid black;
}