I'm trying to figure out the best way to fix this problem. There are 4 divs, and when you shrink the browser window, the 3rd div appears on the right and the 4th div underneath. I know this is because the 1st div has more text, and an auto height means the height is larger than the other divs next to it. I don't want to give it a fixed height. Clearing the float on the 3rd div puts the 3rd div in the right place but the 4th div then sits below the 3rd div.
How can I make the 3rd and 4th divs sit next to each other when shrinking the browser window? And is there an easy way of making the 1st and 2nd divs the same height, even though the content is different in each div?
Thanks for the help!
.thing {
width: 50%;
height: auto;
background-color: pink;
box-sizing: border-box;
border: 1px solid red;
display: block;
float: left;
}
.title {
display: block;
}
a {
display: block;
}
<div class="thing">
<span class='title'>the excess text here causes the problem as the height is increased</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing">
<span class='title'>title two</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing thingy">
<span class='title'>title three</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing">
<span class='title'>title four</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
You can use clear:both on the third DIV:
.thing {
width: 50%;
height: auto;
background-color: pink;
box-sizing: border-box;
border: 1px solid red;
display: block;
float: left;
}
.title {
display: block;
}
a {
display: block;
}
.thing.thingy {
clear: both;
}
<div class="thing">
<span class='title'>the excess text here causes the problem as the height is increased</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing">
<span class='title'>title two</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing thingy">
<span class='title'>title three</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing">
<span class='title'>title four</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
About the second question: You can use flex to achieve that: Wrap a container around everything and apply display: flex; and flex-wrap: wrap; to it:
.thing {
width: 50%;
height: auto;
background-color: pink;
box-sizing: border-box;
border: 1px solid red;
display: block;
}
.title {
display: block;
}
a {
display: block;
}
.wrap {
display: flex;
flex-wrap: wrap;
}
<div class="wrap">
<div class="thing">
<span class='title'>the excess text here causes the problem as the height is increased</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing">
<span class='title'>title two</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing thingy">
<span class='title'>title three</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing">
<span class='title'>title four</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
</div>
Use flexbox.
You'll need to put the .things in a container and set it to display flex. Remove the float and you're done. Easy.
Its supported by basically every modern browser including IE10 and up and it easy to implement. See this excellent guide to flexbox for tips.
.container {
display: flex;
flex-wrap: wrap;
}
.thing {
width: 50%;
height: auto;
background-color: pink;
box-sizing: border-box;
border: 1px solid red;
display: block;
}
.title {
display: block;
}
a {
display: block;
}
<div class="container">
<div class="thing">
<span class='title'>the excess text here causes the problem as the height is increased</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing">
<span class='title'>title two</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing thingy">
<span class='title'>title three</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
<div class="thing">
<span class='title'>title four</span>
<a href='#'>News</a>
<a href='#'>Audacity</a>
<a href='#'>Thisisalongerword</a>
</div>
</div>
Related
I am new to using css grid. I am attempting to make a responsive grid of images that differ in sizes. The example that I came up with works perfectly but it seems like I need to define a size repeat(auto-fit, minmax(84px, max-content)); instead of something like repeat(auto-fit, minmax(min-content, max-content)); if I use auto-fit. This makes the a href attribute extend the 84px even if the image itself isn't 84px.
Is there a way to use auto-fit while also having the rows and columns fit perfectly like they do now, without having to define the size as 84px? Or is there a better way to do this while keeping it simple?
a {
border: 2px dashed pink;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(84px, max-content));
align-items: center;
grid-gap: 2rem;
}
.ef {
background: url(https://fakeimg.pl/84x84/) no-repeat 0px 0px;
width: 84px;
height: 84px;
}
.eft {
background: url(https://fakeimg.pl/16x16/) no-repeat 0px 0px;
width: 16px;
height: 16px;
}
.ytt {
background: url(https://fakeimg.pl/44x44/) no-repeat 0px 0px;
width: 44px;
height: 44px;
}
<p>
The grid is fine but the a href extends too far for some reason.
</p>
<section class="grid">
<a href="/ytt">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
</section>
Simply change justify-items. By default items will get stretched to fill the grid area since the default alignment is stretch. You already changed this on the cross axis using align-items. We do the same for the main axis using justify-items. You can notice that both properties accept stretch as value
a {
border: 2px dashed pink;
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(84px, max-content));
align-items: center;
justify-items:center;
grid-gap: 2rem;
}
.ef {
background: url(https://fakeimg.pl/84x84/) no-repeat 0px 0px;
width: 84px;
height: 84px;
}
.eft {
background: url(https://fakeimg.pl/16x16/) no-repeat 0px 0px;
width: 16px;
height: 16px;
}
.ytt {
background: url(https://fakeimg.pl/44x44/) no-repeat 0px 0px;
width: 44px;
height: 44px;
}
<p>
The grid is fine but the a href extends too far for some reason.
</p>
<section class="grid">
<a href="/ytt">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="ef"></div>
</a>
<a href="/ef">
<div class="eft"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
<a href="/ef">
<div class="ytt"></div>
</a>
</section>
Please note that to simplify the code I removed some list items but they all have class="footerstyle"
This is what the list currently looks like:
This is what I want it to look like however they have to be in the centre of a bootstrap grid:
This is my code:
.footer {
position: absolute;
bottom: 0;
width: 100%;
height: 50px;
}
footer {
position: relative;
}
.list-unstyled {
margin-bottom: 20px;
}
.footerstyle {
font-size: 18px;
text-align: center;
}
.footerstyle:hover {
border-radius: 25px;
text-align: center;
border-bottom:solid 1px transparent;
}
.dropright {
flex-wrap: wrap;
}
.col-md {
height: 200px;
display: flex;
}
<div class='footer'>
<footer class="page-footer font-small pt-4">
<div class="row">
<div class="col-sm">
<ul class="list-unstyled">
<div class="col-sm">
</div>
<div class="col-sm">
<li class="footerstyle">
<a>Homepage</a>
</li>
</div>
<div class="col-sm">
<div class="dropdown footerstyle">
<a class="footer-dropdown dropdown-toggle" id="dropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Team
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" >Team members</a>
<a class="dropdown-item" >Our Story</a>
<a class="dropdown-item" >Join the team</a>
</div>
</div>
</div>
</ul>
</div>
<div class="col-sm socialmedia">
<a href="">
<img src="linkedin.png">
</a>
<a href="">
<img src="twitter.png">
</a>
</div>
<div class="col-sm cookies">
A
</div>
</div>
<div class="footer-copyright text-center py-3">
<a> B </a>
</div>
</footer>
</div
The other items in the footer are in the right place and don't have this problem as they aren't list items. How can I make it so that the list items align to the left whist still being on the centre of the bootstrap grid?
To align text left just add this code to your css:
.page-footer a {
text-align:left;
}
This question already has answers here:
Align 3 unequal blocks left, center and right
(4 answers)
Closed 3 years ago.
My probleme is difficult to explain but i will try.
I develop a web site. For my nav-bar i have 3 div in a header (burger-menu / loogo / social-networks). I use flex for display with justify-content: between.
My probleme is i want to center my second div by the viewport and not by my two other divs. Someone know how to do ?
(Please excuse my bad English)
<header>
<div class="burger">
<img src="assets/img/cutlery.svg" alt="icône couteau cuillère burger menu">
</div>
<div class="logo">
<img src="assets/img/bio.svg" alt="">
</div>
<div class="network">
<a href="#">
<img src="assets/img/facebook.svg" alt="">
</a>
<a href="#">
<img src="assets/img/linkedin.svg" alt="">
</a>
<a href="#">
<img src="assets/img/instagram.svg" alt="">
</a>
<a href="#">
<img src="assets/img/france.svg" alt="">
</a>
<a href="#">
<img src="assets/img/english.svg" alt="">
</a>
</div>
</header>
You can add
position: absolute;
width: 100%;
display: flex;
justify-content: center;
to your second div. Look here:
header{
display: flex;
width: 100%;
justify-content: space-between;
}
.logo{
position: absolute;
width: 100%;
display: flex;
justify-content: center;
}
<header>
<div class="burger">
Burger
</div>
<div class="logo">
Logo
</div>
<div class="network">
<a href="#">
Fb
</a>
<a href="#">
Ln
</a>
<a href="#">
I
</a>
<a href="#">
Fr
</a>
<a href="#">
En
</a>
</div>
</header>
Just tested the code below, and it works. This is to give an idea; CSS attributes have to be in separate file of course:
<div style="display:flex;flex-direction:row;width:100%">
<div style="flex:1 1 0px">
<span style="float:right;background-color:lime">Left Element</span>
</div>
<div style="background-color:blue">Central element</div>
<div style="flex:1 1 0px">
<span style="background-color:red">Right 1</span>
<span style="background-color:gray">Right 2</span>
<span style="background-color:magenta">Right 3</span>
</div>
</div>
Or you can have like this.
header{
display: flex;
width: 100%;
justify-content: space-between;
}
.right-menu{
display: flex;
flex: 0 0 55%;
justify-content: space-between;
}
.right-menu ul{
list-style:none;
margin:0;
}
.right-menu ul li{
display:inline-block;
}
<header>
<div class="burger">
Burger
</div>
<div class="right-menu">
<div class="logo">
Logo
</div>
<ul>
<li>
<a href="#">
Fb
</a>
</li>
<li>
<a href="#">
Ln
</a>
</li>
<li>
<a href="#">
I
</a>
</li>
<li>
<a href="#">
Fr
</a>
</li>
<li>
<a href="#">
En
</a>
</li>
</ul>
</div>
</header>
Give all the child elements of the flexbox a flex: 1 to give them the same width, then give your .logo div a text-align: center.
header {
display: flex;
width: 960px;
background: lightblue;
}
header>* {
flex: 1;
}
header .logo {
text-align: center;
}
header .network {
text-align: right;
}
<header>
<div class="burger">
<img src="http://placekitten.com/40/40" alt="icône couteau cuillère burger menu">
</div>
<div class="logo">
<img src="http://placekitten.com/200/100" alt="">
</div>
<div class="network">
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
<a href="#">
<img src="http://placekitten.com/50/50" alt="">
</a>
</div>
</header>
This answer well be helpful.
header {
display: flex;
flex-direction: row;
justify-content: space-between;
flex: 1;
}
header > div {
display: inline-flex;
flex-basis: 33.33%;
align-items: center;
}
.burger {
justify-content: flex-start;
background-color: #AAAAAA;
}
.logo {
justify-content: center;
background-color: #DDDDDD;
}
.network {
justify-content: flex-end;
background-color: #AAAAAA;
}
<header>
<div class="burger">
<span>01</span>
<img src="assets/img/cutlery.svg" alt="">
</div>
<div class="logo">
<span>02</span>
<img src="assets/img/bio.svg" alt="">
</div>
<div class="network">
<a href="#">
<span>03</span>
<img src="assets/img/facebook.svg" alt="">
</a>
<a href="#">
<span>04</span>
<img src="assets/img/linkedin.svg" alt="">
</a>
<a href="#">
<span>05</span>
<img src="assets/img/instagram.svg" alt="">
</a>
<a href="#">
<span>06</span>
<img src="assets/img/france.svg" alt="">
</a>
<a href="#">
<span>07</span>
<img src="assets/img/english.svg" alt="">
</a>
</div>
</header>
I would like to create a webpage with a section that scrolls horizontally using flexbox. However, the code results in each box being reduced in size to fit the screen, rather than overflowing and enabling for horizontal scrolling.
Code:
.main {
flex-direction: row;
-webkit-flex-direction: row;
overflow: scroll;
width: 100%;
height: 100vh;
}
.portfolio_item {
width: 50%;
}
.flex {
display: flex !important;
display: -webkit-flex !important;
}
<div class="main flex">
<div class="portfolio_item willow">
<a class="link" href="https://aubergewillowinn.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Willow Inn</h3>
</div>
</a>
</div>
<div class="portfolio_item bellevue">
<a class="link" href="http://www.bellevuemtl.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Bellevue Condominiums</h3>
</div>
</a>
</div>
<div class="portfolio_item willow">
<a class="link" href="https://aubergewillowinn.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Willow Inn</h3>
</div>
</a>
</div>
<div class="portfolio_item bellevue">
<a class="link" href="http://www.bellevuemtl.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Bellevue Condominiums</h3>
</div>
</a>
</div>
</div>
If you add a min-width of 50% (as well as the width of 50%) to your child portfolio items, your code works (with overflow and scrolling).
Hope this helps
.main {
flex-direction: row;
-webkit-flex-direction: row;
overflow: scroll;
width: 100%;
height: 100vh;
}
.portfolio_item {
min-width: 50%;
width:50%;
}
.flex {
display: flex;
display: -webkit-flex;
}
<div class="main flex">
<div class="portfolio_item willow">
<a class="link" href="https://aubergewillowinn.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Willow Inn</h3>
</div>
</a>
</div>
<div class="portfolio_item bellevue">
<a class="link" href="http://www.bellevuemtl.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Bellevue Condominiums</h3>
</div>
</a>
</div>
<div class="portfolio_item willow">
<a class="link" href="https://aubergewillowinn.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Willow Inn</h3>
</div>
</a>
</div>
<div class="portfolio_item bellevue">
<a class="link" href="http://www.bellevuemtl.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Bellevue Condominiums</h3>
</div>
</a>
</div>
</div>
Not sure if using flex is necessary, since flex is to help avoid the overflow situation amongst other things.
Here is a very simple example without flexbox utilising the overflow-x: scroll; and white-space: nowrap; attributes. The first allows the div to be scrollable, and the second prevents white space from wrapping around to a new line
.container {
overflow-x: scroll;
white-space: nowrap;
}
.box {
display: inline-block;
border: 1px solid black;
margin: 5px;
height: 200px;
width: 200px;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="test.css">
<title></title>
</head>
<body>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</body>
</html>
I'm not sure flexbox is actually the tool for this job. The premise behind flexbox is that your elements flex to fit inside the box. However, you specifically want them to overflow your box, not fill the available space in your box.
From css-tricks.com:
The main idea behind the flex layout is to give the container the ability to alter its items' width/height (and order) to best fill the available space [...] A flex container expands items to fill available free space, or shrinks them to prevent overflow.
It might make more sense to enable horizontal scrolling by using a layout based on inline-block and white-space: nowrap like this:
.main {
overflow-x: auto;
white-space: nowrap;
}
.portfolio_item {
display: inline-block;
margin: 0 30px;
}
<div class="main">
<div class="portfolio_item willow">
<a class="link" href="https://aubergewillowinn.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Willow Inn</h3>
</div>
</a>
</div>
<div class="portfolio_item bellevue">
<a class="link" href="http://www.bellevuemtl.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Bellevue Condominiums</h3>
</div>
</a>
</div>
<div class="portfolio_item willow">
<a class="link" href="https://aubergewillowinn.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Willow Inn</h3>
</div>
</a>
</div>
<div class="portfolio_item bellevue">
<a class="link" href="http://www.bellevuemtl.com/">
<div class="filter flex">
<h3 class="portfolio_item-text">Bellevue Condominiums</h3>
</div>
</a>
</div>
</div>
I have the following code and want the anchor everywhere in the box not only at the image:
HTML:
<p style="clear:left" />
<div class="reference_container">
<div class="reference_box reference_box_geraete">
<a href="google.de">
<img src="http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/Hans.NETMVCJeffAtwoodandhistechnicalteam_1349C/stackoverflow-logo-250_3.png"/>
</a>
</div>
<div class="reference_box reference_box_geraete">
<a href="google.de">
<img src="http://www.johndscomputers.com/wp-content/uploads/2014/02/200px-Apple_logo_black.svg_.png"/>
</a>
</div>
<div class="reference_box reference_box_geraete">
<a href="google.de">
<img src="http://www.johndscomputers.com/wp-content/uploads/2014/02/200px-Apple_logo_black.svg_.png"/>
</a>
</div>
<div class="reference_box reference_box_geraete">
<a href="google.de">
<img src="http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/Hans.NETMVCJeffAtwoodandhistechnicalteam_1349C/stackoverflow-logo-250_3.png"/>
</a>
</div>
</div>
<p style="clear:left" />
CSS:
.reference_container {
display: flex;
}
.reference_box {
border: 1px solid #ddd;
border-radius: 5px;
margin: 1%;
position:relative;
overflow: hidden;
padding: 15px;
white-space: nowrap;
display:inline-block;
}
.reference_box_geraete {
width: 22%;
/*box-sizing: border-box;*/
text-align: center;
display: flex;
align-items: center;
justify-content: center;
}
.reference_box_geraete img {
vertical-align: middle;
max-width: 100%;
max-height: 100%;
}
The problem is that I don't get the anchor to stretch to the whole surrounding div.
I tried to remove the div and make the anchor as block-element, but then the images are not stretched correctly (doesn't keep aspect-ratio) when resizing the browser window.
How can I achieve this.
set width , height and display:block for element a
check this:
<p style="clear:left" />
<div class="reference_container">
<a class="reference_box reference_box_geraete" href="google.de">
<img src="http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/Hans.NETMVCJeffAtwoodandhistechnicalteam_1349C/stackoverflow-logo-250_3.png"/>
</a>
<a href="google.de" class="reference_box reference_box_geraete">
<img src="http://www.johndscomputers.com/wp-content/uploads/2014/02/200px-Apple_logo_black.svg_.png"/>
</a>
<a href="google.de" class="reference_box reference_box_geraete">
<img src="http://www.johndscomputers.com/wp-content/uploads/2014/02/200px-Apple_logo_black.svg_.png"/>
</a>
<a href="google.de" class="reference_box reference_box_geraete">
<img src="http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/Hans.NETMVCJeffAtwoodandhistechnicalteam_1349C/stackoverflow-logo-250_3.png"/>
</a>
</div>
<p style="clear:left" />
Demo: http://www.cssdesk.com/p5Cqd