I want 3 elements in line. First two being aligned to the left and the third one being align to right.When the window is smaller i want the second going under the first while the third remains in the right, when the window gets even smaller all three under each other.
Here is an example:
https://www.backyardburgers.com/
I want to make the red thing under the header with those three elements an h2 a p and an anchor wich is the button.
I would like if you could show me what css to use if the html is the following:
<section class="location">
<div class="content">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, copiosae perpetua voluptaria in pro, laboramus scriptorem instructior in usu, duo expetenda delicatissimi in. </p>
<a class="button" href="#">Sale sonet</a>
</div></section>
Example code with 3 divs
CSS ::::
div {
height: 200px;
width: 200px;
border: 1px solid white;
display: inline-block;
}
#media only screen and (max-width: 500px) {
div {
width: 100%;
}
}
#media screen and (max-width: 900px) and (min-width: 500px) {
div {
display: block;
}
.first, .second {
width: calc(100% - 200px);
}
.third{
position: fixed;
top: 8px;
right: 5px;
width: 200px;
}
}
#media screen and (min-width: 900px) {
div{
display: inline-block;
}
.first {
float: left;
}
.second {
width: calc(100% - 410px);
}
.third {
float: right;
}
}
Here is a simple turnaround :
.location {
background: #d00807;
color: #fff;
padding: 20px;
position: relative;
}
.location h2 {
display: inline-block;
margin: 0px;
vertical-align: middle;
}
.location p {
display: inline-block;
margin: 0px;
margin-left: 10px;
vertical-align: middle;
}
.location a.button {
background: transparent;
border: 1px solid #fff;
text-decoration: none;
color: #fff;
padding: 10px;
position: absolute;
right: 20px;
top: 50%;
margin-top: -20px;
}
#media (max-width:768px) {
.location h2 {
display: block;
margin-bottom: 10px;
}
.location p {
width: 50%;
margin-left: 0px;
}
}
<section class="location">
<div class="content">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, copiosae perpetua voluptaria in pro, laboramus scriptorem instructior in usu, duo expetenda delicatissimi in. </p>
<a class="button" href="#">Sale sonet</a>
</div>
</section>
Related
sorry for my English...
I want to make sidebar 100% height of the page. I set html:100% and then height: 100%. but as you see in screenshot the sidebar does not working. and the height is equal to the height of content inside that.
this is css code i wrote for that. I also use viewpoints for sidebar..............
html {
box-sizing: border-box;
height: 100%;
}
*,
*::before,
*::after {
margin: 0;
padding: 0;
box-sizing: inherit;
}
body {
font-family: Tahoma, sans-serif;
}
body p {
font-family: 'Trebuchet MS', sans-serif;
}
.main {
width: 80%;
height: 100%;
background-color: #243E36;
color: #F1F7ED;
float: left;
position: relative;
}
header {
text-align: center;
margin: 20px auto;
border-bottom: 2px solid #E0EEC6;
width: 30%;
padding-bottom: 15px;
}
header h1 {
color: #C2A83E;
}
header p {
margin-top: 10px;
text-transform: uppercase;
word-spacing: 10px;
letter-spacing: 5px;
}
section {
color: #212121;
padding: 0 20px;
}
article {
width: 50%;
margin-bottom: 30px;
float: left;
}
article h2 {
padding: 15px;
}
article p {
text-align: justify;
padding: 0 15px;
}
footer {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
background-color: #7CA982;
text-align: center;
}
aside {
width: 20%;
min-height: 100%;
max-height: 100%;
background-color: #E0EEC6;
float: left;
}
.group:before,
.group:after {
content: "";
display: table;
}
.group:after {
clear: both;
}
.group {
zoom: 1;
}
<body>
<div class="container group">
<div class="main">
<!-- Header -->
<header>
<h1>My Personal NoteBook</h1>
<p>Welcome to my page</p>
</header>
<!-- Notes -->
<section class="group">
<article>
<h2>Title 1</h2>
<p>Posted on 2020/07/23</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolorum, sequi iusto! Facere, quos
tempore veritatis sit ratione iste perferendis quod possimus voluptatem, quam, non similique
labore quas adipisci corporis libero!</p>
</article>
</section>
<!-- Footer -->
<footer>
<p>©2020 My Nootbook</p>
</footer>
</div>
<!-- Sidebar -->
<aside style="height:100vh;">
<h1>SideBar</h1>
</aside>
</div>
</body>
One way you can do this is by using inline CSS in your sidebar div element
`
<div class="sidenav" style="height:100vh;position: fixed;">
About
Services
Clients
Contact
</div>"
`
I hope this example will give you a better idea.
You issues is you have used 2 floats elements, and float elements won't respect height:100%.
Solution #1: Float 1 div, and another using margin-left
html,body {
height: 100%;
}
.c-container {
height: 100%;
}
.c-content {
float: left;
width: 80%;
}
.c-sidebar {
margin-left: 80%;
height: 100%;
background: red;
}
<div class="c-container">
<div class="c-content ">
content
</div>
<div class="c-sidebar">
sidebar
</div>
</div>
Flex solution:
html,body {
height: 100vh;
}
.c-container {
display: flex;
height: 100%;
}
.c-content {
flex: 1 1 auto;
}
.c-sidebar {
width: 200px;
background: red;
}
<div class="c-container">
<div class="c-content ">
content
</div>
<div class="c-sidebar">
sidebar
</div>
</div>
I'm trying to create a same-height column which was 2 boxes to the left and 1 box to the right. Something on this line where 01 & 02 combined together has the same height as 03:
Demo: https://jsfiddle.net/bptdqx67/2/
HTML
<div class="row">
<div class="col-7">
<div class="h-100">
<div class="thankyou__box blue">
<div class="thankyou__number">
<h4>01</h4>
</div>
<div class="thankyou__content">
<p>This is the first box. This is the first box. This is the first box. This is the first box. This is the first box. This is the first box. This is the first box. </p>
</div>
</div>
<div class="thankyou__box green">
<div class="thankyou__number">
<h4>02</h4>
</div>
<div class="thankyou__content">
<p>This is the second box. This is the second box. This is the second box. This is the second box. This is the second box. This is the second box. This is the second box. </p>
</div>
</div>
</div>
</div>
<div class="col-5">
<div class="thankyou__box green thankyou__height">
<div class="thankyou__number">
<h4>03</h4>
</div>
<div class="thankyou__content">
<p>This is the third box. This is the third box. This is the third box. This is the third box. This is the third box.</p>
</div>
</div>
</div>
</div>
CSS:
* {
box-sizing: border-box;
}
.row::after {
content: "";
clear: both;
display: table;
}
.col {
/*flex: 0 0 20%;
*/
width: 20%;
float: left;
padding: 1px;
}
[class*="col-"] {
float: left;
padding-left: 5px;
padding-right: 5px;
}
.col-1 {
width: 8.33%;
}
.col-2 {
width: 16.66%;
}
.col-3 {
width: 25%;
}
.col-4 {
width: 33.33%;
}
.col-5 {
width: 41.66%;
}
.col-6 {
width: 50%;
}
.col-7 {
width: 58.33%;
}
.col-8 {
width: 66.66%;
}
.col-9 {
width: 75%;
}
.col-10 {
width: 83.33%;
}
.col-11 {
width: 91.66%;
}
.col-12 {
width: 100%;
}
.h-100 {
height: 100%;
}
.thankyou__box {
padding: 15px;
border-radius: 4px;
margin-bottom: 10px;
background-size: 50px;
background-repeat: no-repeat;
background-position: 98% 95%;
}
.thankyou__box.blue {
border: 2px solid #c9f5ff;
}
.thankyou__box.green {
border: 2px solid #c6dc9c;
}
.thankyou__number {
width: 33px;
display: inline-block;
vertical-align: top;
float: left;
}
.thankyou__content {
width: calc(90% - 40px);
display: inline-block;
}
.thankyou__content p {
margin: 0;
}
.thankyou__height {
height: 100%;
background-size: 100px;
background-position: 95% 100%;
}
.thankyou__height .thankyou__content {
width: calc(100% - 33px);
}
Adding display:flex to .row would solve this problem.
Using Grid will greatly simplify your layout rules. Here is a straightforward approach with no dependencies. Once we declare the parent as a grid container, we can create rules for how the children behave.
.right {
grid-column: 2; /* begin at column two */
grid-row: 1 / 3; /* span from the first grid line to the third */
}
With this in place the right column's height will always be the sum of the children in the first column.
.grid {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: auto auto;
grid-gap: 2px;
}
.right {
grid-column: 2;
grid-row: 1 / 3;
background-color: red;
}
.grid * {
padding: 1em;
}
.grid *:not(.right) {
background-color: #ccc;
}
<div class="grid">
<div class="top">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quis nemo doloremque tempora porro quos natus odio est tenetur facilis commodi voluptatum reprehenderit, in, consequatur pariatur. Voluptatum quam, modi sit. Eos. lorem</div>
<div class="btm">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cum ut maiores perspiciatis temporibus quae magni enim quas rerum deserunt expedita voluptas deleniti, labore dolore possimus quis officiis ducimus vel repellat.</div>
<div class="right"></div>
</div>
jsFiddle
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 500px;
width: 100%;
}
.container__inner {
display: flex;
}
.container__col:nth-child(1) {
width: 60%;
margin-right: 30px;
}
.container__col:nth-child(1) .container__item {
height: 100px;
}
.container__col:nth-child(2) {
width: 40%;
}
.container__col:nth-child(2) .container__item {
height: 100%;
}
.container__item {
display: block;
border-radius: 4px;
border: 2px solid green;
margin-bottom: 30px;
width: 100%;
}
.container__item:last-of-type {
margin-bottom: 0;
}
<div class="container">
<div class="container__inner">
<div class="container__col">
<div class="container__item"></div>
<div class="container__item"></div>
</div>
<div class="container__col">
<div class="container__item"></div>
</div>
</div>
</div>
I am trying to have 2 divs on the same row, one with a title and a <hr> and the other one with an image that can be clicked to go to another page(even if I remove the link the problem persists). My expectations would be that executing this code I would get the first one starting in the top left corner followed on right by the image, but instead the first <div> just starts randomly.
#zonaTitlu {
width: 300px;
height: 200px;
display: inline-block;
background-color: red;
}
#zonaImagine {
width: 400px;
height: 400px;
display: inline-block;
}
.LinieTitlu {
display: block;
margin-top: 0.5em;
margin-bottom: 0.5em;
margin-left: auto;
margin-right: 50px;
border-style: inset;
border-width: 2px;
border-color: darkgrey;
}
.logo {
width: 400px;
height: auto;
}
<body style="background-color:LightGray">
<div id="zonaTitlu">
<p>
<h3>Welcome to LOL Tournaments</h3>
</p>
<hr class="LinieTitlu">
</div>
<div id="zonaImagine">
<a href="https://eune.leagueoflegends.com/ro/">
<img class="logo" src="lol-logo.jpg" alt="LOL logo">
</a>
</div>
</body>
If you want both div side by side mean try this and change the image width to 100%
.zona{
width: 100%;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
#zonaTitlu {
width: 50%;
height: 200px;
display: inline-block;
background-color: red;
}
#zonaImagine {
width: 50%;
height: 400px;
display: inline-block;
}
.LinieTitlu {
display: block;
margin-top: 0.5em;
margin-bottom: 0.5em;
margin-left: auto;
margin-right: 50px;
border-style: inset;
border-width: 2px;
border-color: darkgrey;
}
.logo {
width: 400px;
height: auto;
}
<body style="background-color:LightGray">
<div class="zona">
<div id="zonaTitlu">
<h3>Welcome to LOL Tournaments</h3>
<hr class="LinieTitlu">
</div>
<div id="zonaImagine">
<a href="https://eune.leagueoflegends.com/ro/">
<img class="logo" src="lol-logo.jpg" alt="LOL logo">
</a>
</div>
</div>
</body>
While I like the other solutions (flexbox especially). OP seems to have a simple problem to fix - inline-block alignment default to baseline (bottom) and the content within the first div is what's being aligned with the bottom of the image. You see the hr is lined up with the bottom of the image.
Adding vertical-align: top; on either inline-block element fixes this and they will align at the top. Here is a good SO post on all the above behavior:
My inline-block elements are not lining up properly
I don't know what you're trying to do with fixed widths so I'm not going to offer other solutions with responsive design and whatnot - this is the straight answer to your question.
#zonaTitlu {
width: 200px;
height: 200px;
display: inline-block;
background-color: red;
vertical-align: top; // added this
}
#zonaImagine {
width: 140px;
height: 100px;
display: inline-block;
}
.LinieTitlu {
display: block;
margin-top: 0.5em;
margin-bottom: 0.5em;
margin-left: auto;
margin-right: 50px;
border-style: inset;
border-width: 2px;
border-color: darkgrey;
}
.logo {
width: 400px;
height: auto;
}
<body style="background-color:LightGray">
<div id="zonaTitlu">
<p>
<h3>Welcome to LOL Tournaments</h3>
</p>
<hr class="LinieTitlu">
</div>
<div id="zonaImagine">
<a href="https://via.placeholder.com/350x150">
<img class="logo" src="https://via.placeholder.com/140x100" alt="LOL logo">
</a>
</div>
Now you can adjust all things separately. TH HR is a bit outdated. It also will force a line break. I add a underline to the h3 which would do same i thing. But for completeness i add also a HR which can now be placed also individual by css.
Same for the image. - It depends a bit on the image size how to do adjustments.
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<style>
#zonaTitlu {
width: 400px;
height: 200px;
display: inline-block;
background-color: red;
display: inline-block;
position: absolute;
top: 0px;
left: 0px;
z-index: -1;
}
#zonaImagine {
width: 400px;
height: 400px;
display: inline-block;
}
.LinieTitlu {
display: block;
margin-top: 0.5em;
margin-bottom: 0.5em;
margin-left: auto;
margin-right: 50px;
border-style: inset;
border-width: 2px;
border-color: darkgrey;
}
.underlined {
text-decoration: underline;
}
.logo {
position: relative;
width: 400px;
height: auto;
top: 5px;
}
.spacer {
display: inline-block;
width: 30px;
}
.hr {
position: relative;
top: 60px;
width: 400px;
left: 0px"
}
.para {
position: relative;
width: 380px;
top: 15px;
}
</style>
<style type="text/css">
body.c1 {
background-color: LightGray
}
</style>
</head>
<body class="c1">
<h3 class="underlined">Welcome to LOL Tournaments <a class="logo" href="https://eune.leagueoflegends.com/ro/"><img class="logo" src="lol-logo.jpg" alt="LOL logo"></a></h3>
<div id="zonaTitlu">
<hr class="hr">
</div>
<p class="para">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Tu autem, si
tibi illa probabantur, cur non propriis verbis ea tenebas? Mihi enim satis est, ipsis non satis. <i>Eiuro,
inquit adridens, iniquum, hac quidem de re;</i> Duo Reges: constructio interrete.</p>
</body>
</html>
Looks right to me . You can try using a table if required.The div can be removed
here is a working example using flex-box
<body style="background-color:LightGray">
<div id="container">
<div id="zonaTitlu" class="box">
<p>
<h3>Welcome to LOL Tournaments</h3>
</p>
<hr class="LinieTitlu">
</div>
<div id="zonaImagine" class="box">
<a href="https://eune.leagueoflegends.com/ro/">
<img class="logo" src="lol-logo.jpg" alt="LOL logo">
</a>
</div>
</div>
</body>
<style>
/*
#zonaTitlu {
width: 300px;
height: 200px;
display: inline-block;
background-color: red;
}
#zonaImagine {
width: 400px;
height: 400px;
display: inline-block;
}
*/
#container {
display: flex;
flex-direction: row;
}
.box {
width: 400px;
height: 400px;
border: 1px solid black;
}
.LinieTitlu {
display: block;
margin-top: 0.5em;
margin-bottom: 0.5em;
margin-left: auto;
margin-right: 50px;
border-style: inset;
border-width: 2px;
border-color: darkgrey;
}
.logo {
width: 400px;
height: auto;
}
</style>
This question already has answers here:
How to align 3 divs (left/center/right) inside another div?
(21 answers)
Closed 6 years ago.
How can i make 3 divs side by side. where div1 would be extreme left ,div3 would be extreme right and div2 in the middle.
I know this can be done by display:flex and justify-content:space-between ,but i am looking for an approach without flex.
Here is my approach ,but could not achieve it successfully.
I tried to make all div's display:inline-block and float:left and float:right to the two extreme divs and for the middle one i tried margin:auto,but looks like it is not respecting it
Please help
.container {
border: 1px solid;
}
.container div {
height: 50px;
width: 50px;
background: red;
display: inline-block;
}
#div1 {
float: left;
}
#div3 {
float: right;
}
#div2 {
margin: auto;
}
<div class="container">
<div id="div1"></div>
<div id="div2">he</div>
<div id="div3"></div>
</div>
try this one. position: absolute;
.container {
border: 1px solid;
position: relative;
overflow: hidden;
}
.container div {
height: 50px;
width: 50px;
background: red;
display: inline-block;
}
#div1 {
float: left;
}
#div3 {
float: right;
}
#div2 {
position: absolute;
left: 0;
right: 0;
margin: auto;
}
<div class="container">
<div id="div1"></div>
<div id="div2">he</div>
<div id="div3"></div>
</div>
this is what your code will be
.container {
border: 1px solid;
}
.container div {
height: 50px;
width: 50px;
background: red;
display: inline-block;
}
#div1 {
float: left;
background-color:red;
}
#div3 {
float: left;
background-color:green;
}
#div2 {
float: left;
background-color:yellow;
}
and
<div class="container">
<div id="div1">div 1</div>
<div id="div2">div 2</div>
<div id="div3">div 3</div>
</div>
Kindly check below CSS see if this is what you want:
.container {
float: left;
width: 300px;
border: 1px solid;
}
#div1 {
float: left;
width: 100px;
background-color: red;
}
#div2 {
float: left;
width: 100px;
background-color: green;
}
#div3 {
float: left;
width: 100px;
background-color: blue;
}
<div class="container">
<div id="div1">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ullamcorper eleifend volutpat. Class aptent taciti sociosqu ad litora torquent per conubia nostra.</div>
<div id="div2">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
<div id="div3">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur ullamcorper eleifend volutpat.</div>
</div>
.container {
border: 1px solid;
position: relative;
}
.container div {
height: 50px;
width: 50px;
background: red;
display: block;
}
#div1 {
float: left;
}
#div3 {
position: absolute;
right: 0;
top: 0;
}
#div2 {
margin: auto;
}
<div class="container">
<div id="div1"></div>
<div id="div2">he</div>
<div id="div3"></div>
</div>
Don't use float and inline-block together. This will work:
<div class="container">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
</div>
.container {
width:100%;
}
#div1, #div2, #div3 {
height: 50px;
background: red;
display: inline-block;
width:33.33%;
margin-right:-4px;
}
If you want to style the divs individually then target them individually
I have 3 columns. left, middle and right.
left and middle are fixed-width, right should fill the remaining space.
How can I accomplish this?
Current HTML
<div id="menu">
<div id="left">
</div>
<div id="middle">
</div>
<div id="right">
</div>
</div>
LESS
#menu {
width: 100%;
#left {
width: 20%;
float: left;
}
#middle {
width: 300px;
float: left;
}
#right {
overflow-x: hidden;
float: right;
}
}
#menu {
width: 100%;
#left {
width: 20%;
float: left;
}
#middle {
width: 300px;
float: left;
}
#right {
width: -webkit-calc(100% - 20% - 300px);
width: -moz-calc(100% - 20% - 300px);
width: -o-calc(100% - 20% - 300px);
width: calc(100% - 20% - 300px);
float: right;
}
}
You must know that calc is still an experimental technology, I suggest you to use javascript instead if you are looking for browser compatibility.
Here is a fiddle with it all working.
It's "small-screen-first" so - if you aren't into that... then you can just delete the #media rule.
It looks like all you really need, is a width auto - and to make sure it's not floated.
HTML
<aside class="container left"><h2>Left</h2></aside>
<section class="container main-content"><h2>Main content</h2></section>
<aside class="container right">
<h2>Right</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Et beatae quam quibusdam dolor dolorum vero harum commodi vel quidem quasi sed dolores iusto is</p>
</aside>
CSS
* {
box-sizing: border-box;
}
.container {
width: 100%;
float: left;
}
#media (min-width: 800px) {
.left {
width: 20%;
}
.main-content {
width: 400px;
}
.right {
width: auto;
float: none;
}
}
I think
#right{
width:auto;
float:right;
overflow-x: hidden;
}
has to work.
#middle {
width: 300px;
float: left;
position: relative; /* Show above #right */
}
#right {
width: 80%;
margin-left: -300px; /* equal to width of #middle in px */
padding-left: 300px; /* equal to width of #middle in px */
-moz-box-sizing: border-box;
box-sizing: border-box;
overflow-x: hidden;
float: right;
}