How to make divs same height? - html

I have one parent-div which contains three divs and I would like to make them same height but it is not working. The first and third div contains each an image. The second div contains three divs with content.
Here is the HTML:
<div class="container">
<div class="column1">
<img src="http://placehold.it/300x318">
</div>
<div class="column2">
<div class="row1">
<div class="text">UNIKE GUSTAVIANSKE STILMØBLER.</div>
<div class="text">VI SELGER HÅNDVERK ETTER 1700-</div>
<div class="text">OG 1800-TALLS TRADISJONER.</div>
</div>
<div class="row2"></div>
<div class="row3">
<div class="text">
Åpningstider:<br>
Man - Fre 11 -17 Lør 11- 15
</div>
</div>
</div>
<div class="column3">
<img src="http://placehold.it/300x318">
</div>
</div>
The .container has the css-rule display:flex;. When I apply this also to .column1, .column2 and .column3, the layout breaks.
I am trying to achieve that the images height increase and decrease depended to .column2. Unfortunately, I have not the possibility to change the HTML or use JS.
Here you can see a JS-Fiddle. I have commented out the CSS-rules.
Many thanks for your help!

In the question you mention that you apply display:flex to .column1, .column2 and .column3.
Instead just apply this to .column1 and .column3 and leave .column2 as display:block.
This should resolve your problem (works in your JS-Fiddle).

You just need to apply a height and max-width to the images. They will distort to fit into the space and make your images look weird. If you choose images that are this dimensions, they will look better.
.container {
width: 100%;
display: flex;
}
.container img{
height: 100%;
max-width: 100%;
}
.row1 {
background-color: #fff;
padding-top: 40px;
}
.row1 .text {
font-size: 30px;
text-align: center;
line-height: 50px;
font-weight: 300;
}
.row2 {
height: 150px;
background-color: #e4e8eb;
}
.row3 {
background-color: #c7cacf;
padding: 10px 0px;
}
.row3 .text {
font-size: 25px;
text-align: center;
line-height: 25px;
}
<div class="container">
<div class="column1">
<img src="http://placehold.it/300x318">
</div>
<div class="column2">
<div class="row1">
<div class="text">
UNIKE GUSTAVIANSKE STILMØBLER.</div>
<div class="text">VI SELGER HÅNDVERK ETTER 1700-</div>
<div class="text">OG 1800-TALLS TRADISJONER.
</div>
</div>
<div class="row2"></div>
<div class="row3">
<div class="text">Åpningstider:
<br>Man - Fre 11 -17 Lør 11- 15</div>
</div>
</div>
<div class="column3">
<img src="http://placehold.it/300x318">
</div>
</div>

you can add this lines for row css
.row {
display: flex;
justify-content: center; /* align horizontal */
align-items: center;
}

You just need to specify the height and width of image tag.Just add width:100% and height 100% for image tag.
.container {
width: 100%;
display: flex;
}
.container img{
height: 100%;
max-width: 100%;
}
.row1 {
background-color: #fff;
padding-top: 40px;
}
.row1 .text {
font-size: 30px;
text-align: center;
line-height: 50px;
font-weight: 300;
}
.row2 {
height: 150px;
background-color: #e4e8eb;
}
.row3 {
background-color: #c7cacf;
padding: 10px 0px;
}
.row3 .text {
font-size: 25px;
text-align: center;
line-height: 25px;
}
.imgsize{
width: 100%;
height: 100%;
}
<div class="container">
<div class="column1">
<img src="http://placehold.it/300x318" class="imgsize">
</div>
<div class="column2">
<div class="row1">
<div class="text">
UNIKE GUSTAVIANSKE STILMØBLER.</div>
<div class="text">VI SELGER HÅNDVERK ETTER 1700-</div>
<div class="text">OG 1800-TALLS TRADISJONER.
</div>
</div>
<div class="row2"></div>
<div class="row3">
<div class="text">Åpningstider:
<br>Man - Fre 11 -17 Lør 11- 15</div>
</div>
</div>
<div class="column3">
<img src="http://placehold.it/300x318" class="imgsize">
</div>
</div>

If I'm understanding properly you can add this to your styles:
img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
Your columns are already filling to the parent height. Using the above css styles the images in this will fill the parent div height and basically overflow the width. If you do something else like set height of the images to 100% it will distort the image.
Here is a similar question with same (but more detailed) answer. https://stackoverflow.com/a/26967278/3366016

Related

How to put blocks in line in html

I have the following code, where I wanted to align logo(picture) and one block, which includes Username, his level and progress bar. And used flex for it, but it ate my progress bar, how can I do it so error bar's width would be 100%?
<div class="class" id="user">
<img src="user.svg">
<div>
<h2>username</h2>
<h2> Уровень </h2>
<div id="container">
<div id="progress"> 80% </div>
</div>
</div>
</div>
The CSS of the page:
#user {
display:flex;
}
#container {
background-color: white;
width: 100%;
}
#progress{
background-color: #fd6a72;
width: 100%;
}
give your elements some width.
#user {
display: flex;
align-items: center;
}
#container {
background-color: white;
width: 100%;
}
#progress {
background-color: #fd6a72;
width: calc(100vw - 25px - 8px);
height: 20px;
}
img{
height:25px;}
<div class="class" id="user">
<img src="https://via.placeholder.com/25">
<div>
<h2>username</h2>
<h2> Уровень </h2>
<div id="container">
<div id="progress"> 80% </div>
</div>
</div>
</div>

Center parent div vertically based on child element

I am hoping to center my parent div height based on my child div height. My goal is to have 3 boxes with a shorter, but wider rectangle centered vertically behind it. Right now I have my parent div shorter and wider than the children, however I cannot seem to center it vertically.
Here is the ideal outcome:
Here is my current version (Please ignore minor differences with text and box colors). :
.content {
width: 80%;
margin: 0px auto;
}
#container .col {
border: 1px solid #00acd4;
background-color: white;
padding-top: 2em;
padding-bottom: 2em;
position: relative;
}
#parent {
background-color: #f0f9fb;
max-height: 80px;
}
#container {
margin-top: 50px;
margin-bottom: 50px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<div id="container">
<div id="parent">
<div class="content">
<div class="row">
<div class="col ">
<h3>$500</h3>
</div>
<div class="offset-1 col">
<h3>$3500</h3>
</div>
<div class="col offset-1">
<h3>50%</h3>
</div>
</div>
</div>
</div>
</div>
Don't use a negative margin unless absolutely necessary. In this case, it is not. Use flex on parent with align-items: center;
.content {
width: 80%;
margin: 0px auto;
}
#container .col {
border: 1px solid #00acd4;
background-color: white;
padding-top: 2em;
padding-bottom: 2em;
position: relative;
}
#parent {
background-color: #f0f9fb;
max-height: 80px;
display: flex;
align-items: center;
}
#container {
margin-top: 50px;
margin-bottom: 50px;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<div id="container">
<div id="parent">
<div class="content">
<div class="row">
<div class="col ">
<h3>$500</h3>
</div>
<div class="offset-1 col">
<h3>$3500</h3>
</div>
<div class="col offset-1">
<h3>50%</h3>
</div>
</div>
</div>
</div>
</div>
Without a sketch of what you are trying to do, I believe this is what you are wanting... You can just set a negative margin in the col divs in order to take them outside of the parent...
#container {
margin-top: 50px;
margin-bottom: 50px;
}
#parent {
background-color: #f0f9fb;
}
.content {
width: 80%;
margin: 0px auto;
}
#container .col {
border: 1px solid #00acd4;
background-color: white;
padding-top: 2em;
padding-bottom: 2em;
position: relative;
margin-top: -20px;
margin-bottom: -20px;
}
<div id="container">
<div id="parent">
<div class="content">
<div class="row">
<div class="col">
<h3>$500</h3>
</div>
<div class="offset-1 col">
<h3>$3500</h3>
</div>
<div class="col offset-1">
<h3>50%</h3>
</div>
</div>
</div>
</div>
</div>
Forked your fiddle: https://jsfiddle.net/jstgermain/o6xhL92s/
*** RECOMMEND BELOW SOLUTION ***
#Betsy, I would recommend simplifying your HTML and using flexbox over the previous solution to your fiddle. You will want to make sure your behavior is consistent across browsers and devices. You can use media queries to change the size to eht col items for smaller devices.
#container {
margin-top: 50px;
margin-bottom: 50px;
}
#parent {
background-color: red;
/*#f0f9fb;*/
display: flex;
justify-content: space-evenly;
}
.col {
border: 1px solid #00acd4;
background-color: white;
padding: 1em;
width: 25%;
margin: -20px auto;
}
<div id="container">
<div id="parent">
<div class="col">
<h3>$500</h3>
</div>
<div class="col">
<h3>$3500</h3>
</div>
<div class="col">
<h3>50%</h3>
</div>
</div>
</div>

3 column layout will not fill width of screen

I'm fairly new to html/css and I'm having a problem with a project. I am trying to create a 3 column layout for the bottom portion of my page. What I have right now is close to what I want, but it does not fill the width of the screen. It's all bunched on the left side and does not stretch to match the screen. I went back and followed the example from w3schools and it still didn't work. What am I missing?
HTML
<div class="row">
<div class="column">
<h1>FOLLOW ME ON <br> INSTAGRAM</h1>
</div>
<div class="column">
<h2>contact me</h2>
</div>
<div class="column">
<h1>SUBSCRIBE</h1>
</div>
</div>
CSS
.column {
float: left;
padding: 10px;
height: 300px;
text-align: center;
position: relative;
}
.column.side {
width: 25%;
}
.column.middle {
width: 50%;
}
.row:after {
content: "";
display: table;
clear: both;
}
If you wanna have padding "inside" element, then you need to set box-sizing: border-box.
.row {
width: 100%;
}
.column {
float: left;
padding: 10px;
height: 300px;
text-align: center;
box-sizing: border-box;
}
.column.side {
width: 25%;
}
.column.middle {
width: 50%;
}
<div class="row">
<div class="column side">
<h1>FOLLOW ME ON <br> INSTAGRAM</h1>
</div>
<div class="column middle">
<h2>contact me</h2>
</div>
<div class="column side">
<h1>SUBSCRIBE</h1>
</div>
</div>
Two things:
1.) As #chojnicki wrote in a comment, you need to add the classes defined in the CSS (.side, .middle) to your HTML
2.) To include the padding in the width in order to get a sum of exactly 100% overall (and not more than that to avoid the last column to go under the second one) for the added widths (25% + 50% + 25%), you need to add box-sizing: border-box; to everything (using the *selector):
* {
box-sizing: border-box;
}
.column {
float: left;
padding: 10px;
height: 300px;
text-align: center;
position: relative;
}
.column.side {
width: 25%;
}
.column.middle {
width: 50%;
}
h1 {
font-size: 24px;
}
h2 {
font-size: 18px;
}
<div class="row">
<div class="column side">
<h1>FOLLOW ME ON <br> INSTAGRAM</h1>
</div>
<div class="column middle">
<h2>contact me</h2>
</div>
<div class="column side">
<h1>SUBSCRIBE</h1>
</div>
</div>
Why you don't use .col-xs-4?
and you shouldn't use width to your cols so why do you use col?

Div Table Responsive - Header Repeats

I have a DIV table, it is fully responsive. The table's head should repeat in each section when we view it in a small screen. Right now it shows in the first section but the second section is missing the header.
How can I make the header show in each section on a small screen? that looks like this sample: https://codepen.io/geoffyuen/pen/FCBEg (this sample is a tr td table, my table is a DIV table)
Note: The browser needs to be minimized to see the table responsive.
DEMO: http://jsfiddle.net/w04g2qj9/2/
CSS
.header {
width: 100%;
float: left;
}
.header1 {
width: 33%;
float:left;
background: #bbb;
height: 25px;
}
.row1, .row2 {
width: 33%;
float:left;
background: #ddd;
height: 25px;
}
#media all and (max-width: 480px)
{
.header
{
width:40%;
height: 100%;
float:left;
background: red !important;
}
.header1, .row1, .row2 {width: 100% !important}
.row {
width: 60% !important; margin-bottom: 5px; float:left
}
}
HTML
<div class="table">
<div class="header">
<div class="header1">
Header 01
</div>
<div class="header1">
Header 02
</div>
<div class="header1">
Header 03
</div>
</div>
<div class="row">
<div class="row1">
row 01-1
</div>
<div class="row1">
row 02-1
</div>
<div class="row1">
row 03-1
</div>
</div>
<div class="row">
<div class="row2">
row 01-2
</div>
<div class="row1">
row 02-2
</div>
<div class="row1">
row 03-2
</div>
</div>
</div>
Div Table Image
If you run the code snippet you've provided, this is the result. I'm assuming that this is what you wanted, however, I do see a lot of redundancies in your css. For example, why have two classes row1 and row2 when they are the same thing? Unless you plan to change it so they are different at a later time.
Using Flexyou can achieve this> See Sample code below..Hope it helps
.footer_mainDIV {
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.header {
width: 100%;
float: left;
}
.header1 {
width: 33%;
float: left;
background: #ccc;
height: 25px;
}
.row1,
.row2 {
width: 33%;
float: left;
background: #ddd;
height: 25px;
}
#media all and (max-width: 480px) {
.footer_mainDIV {
flex-direction: row;
flex-wrap: nowrap;
text-align: center;
}
.header {
height: 100%;
float: left;
background: red !important;
flex: 1;
/* Add other alignment related styles*/
}
.header1,
.row1,
.row2 {
width: 100% !important
}
.row {
flex: 1;
margin-bottom: 5px;
background: green !important;
}
}
<div class="footer_mainDIV">
<div class="header">
<div class="header1">
Header 01
</div>
<div class="header1">
Header 02
</div>
<div class="header1">
Header 03
</div>
</div>
<div class="row">
<div class="row1">
row 1-1
</div>
<div class="row1">
row 1-2
</div>
<div class="row1">
row 1-3
</div>
</div>
<div class="row">
<div class="row2">
row 2-1
</div>
<div class="row1">
row 2-2
</div>
<div class="row1">
row 3-3
</div>
</div>
</div>

Div not centering with CSS

I'm trying to put some divs on the website, so it looks neat. There's one big div "container" and inside we have div "head", below it there's an empty div just to separate things and underneath it there's a div "content". My problem is: "head" is easily centered with "margin: 0 auto;", but the same line doesn't work in "content".
#container
{
background-color: darkorange;
width: 70%;
height: 800px;
margin-left: auto;
margin-right: auto;
}
#head
{
background-color: lightblue;
width: 95%;
height: 80px;
margin: 0 auto;
}
.space
{
width: auto;
height: 10px;
background-color: red;
}
#content
{
background-color: forestgreen;
width: 95%;
height: 600px;
margin: 0 auto;
}
<div id="container">
<div id="head">
<div id="reg_welcome">
</div>
<div id="logo">
</div>
<div id="login_out">
</div>
</div>
<div class="space">
</div>
<div id="content">
</div>
<div class="space">
</div>
<div id="footer">
</div>
</div>
Any idea why? (the colors are there just to see how it looks, please just ignore them)
And the second question, similar problem. Inside "head" there are three small divs that I want to float to left. Two of them behave as I want, the first one isn't even showed on the website. Here's the css for those three bits:
#reg_welcome
{
background-color: royalblue;
float: left;
width: 100px;
height: 75px;
margin-right: 40px;
}
#logo
{
background-color: springgreen;
width: 300px;
height: 75px;
float: left;
}
#login_out
{
background-color: teal;
float: left;
width: 120px;
height: 75px;
margin-left: 40px;
}
<div id="container">
<div id="head">
<div id="reg_welcome">
</div>
<div id="logo">
</div>
<div id="login_out">
</div>
</div>
<div class="space">
</div>
<div id="content">
</div>
<div class="space">
</div>
<div id="footer">
</div>
</div>
The oddest thing is here it all seems to work perfectly, but when ran on localhost, I have the problems I mentioned. Any idea why it happens?