How can I stack two divs next to one - html

This is what I am trying to go for, where each green line is a parent div.
This is what I have tried so far:
<div id="gym" class="main_container">
<div id="gym_activity_title_hours_pic" class="activity_title_hours_pic">
<!-- <div id="gym_title_hours" class="activity_title_hours"> -->
<div id="gym_title" class="activity_title">
The Gym
</div>
<div id="gym_hours" class="activity_hours">
5 Hours
</div>
<!-- </div> -->
<div id="gym_pic" class="activity_pic">
<img src="./images/gym.jpg" class="pic"/>
</div>
</div>
</div>
This is the CSS
.main_container {
width: 40%;
height: 40%;
display: inline;
margin-left: auto;
margin-right: auto;
overflow: hidden;
}
.activity_title_hours_pic {
display: inline;
}
.activity_title_hours {
float: left;
width: 50%;
}
.activity_title {
/* float: left; */
float: left;
width: 50%;
}
.activity_hours {
/* float: left; */
float: left;
width: 50%;
}
.activity_pic {
float:right;
width: 50%;
}
.pic {
box-sizing: border-box;
margin-top: 1%;
height: 10%;
width: 10%;
margin-left: auto;
margin-right: auto;
border-radius: 6px;
background-repeat:no-repeat;
background-size:cover;
}
However this is the outcome that I am currently getting:
As you can see, the image is under the activity hours, which is not desired.
Thanks for any help!

You're most likely getting bogged down in all the CSS you've written here. You can accomplish what you're asking in very little code. As a starting tip, get rid of your floats - they're all but useless in modern CSS. Use flexbox instead: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
It may be a little tricky understanding at first but it is compatible pretty much everywhere and solves so many CSS pitfalls of the past.
Without going into too much of your CSS, you probably first need to scoot up your 2nd-to-last closing </div> so that it only wraps around #gym_title and #gym_hours - right now your HTML code doesn't describe what you drew in your diagram.
I wrote a little codepen here: https://codepen.io/dustinkeeton/pen/NEgbLe
P.S. codepen.io is a nice way of working on little bits of HTML/CSS/JS code and sharing it.

Just remove float: left from activity_hours class and uncomment that gym_title_hours div.
HTML:
<div id="gym" class="main_container">
<div id="gym_activity_title_hours_pic" class="activity_title_hours_pic">
<div id="gym_title_hours" class="activity_title_hours">
<div id="gym_title" class="activity_title">
The Gym
</div>
<div id="gym_hours" class="activity_hours">
5 Hours
</div>
</div>
<div id="gym_pic" class="activity_pic">
<img src="./images/gym.jpg" class="pic"/>
</div>
</div>
</div>
CSS:
.main_container {
width: 40%;
height: 40%;
display: inline;
margin-left: auto;
margin-right: auto;
overflow: hidden;
}
.activity_title_hours_pic {
display: inline;
}
.activity_title_hours {
float: left;
width: 50%;
}
.activity_title {
/* float: left; */
float: left;
width: 50%;
}
.activity_hours {
width: 50%;
}
.activity_pic {
float:right;
width: 50%;
}
.pic {
box-sizing: border-box;
margin-top: 1%;
height: 10%;
width: 10%;
margin-left: auto;
margin-right: auto;
border-radius: 6px;
background-repeat:no-repeat;
background-size:cover;
}

If I was you I would start with using Grid Systems. Watch a couple fo youtube videos. CSS gets Messy without a grid or flexbox. I would then move onto flexbox once you got a handle on grids.
Vojtone is right about the float take that out but its still messy.
HTML
<div id="gym" class="grid-container">
<div id="gym_title" class="grid-item box1">The Gym</div>
<div id="gym_hours" class="grid-item box2">5 Hours</div>
<div class="grid-item box3">
<img src="https://www.thegymgroup.com/assets/img/components/c050.03-movember rich-text-block/the-gym-group-logo-movember.png" id="gym_pic" class="pic" height=100 width=150>
</div>
</div>
CSS
grids are made up with 12 columns just mess around with the span of the columns and row and you'll get the hang of it.
.grid-container {
display: grid;
border:solid 2px;
}
.box1{
grid-column: 1 / span 11;
border:solid 2px
}
.box2{
grid-column: 1 / span 11;
border:solid 2px
}
.box3{
grid-column: 12 / span 1;
grid-row: 1 / span 2;
}
Codepen
CODE PEN EXAMPLE HERE

Related

How do I layer an image behind a <div>?

I've been trying to make a square image that is layered behind a <div>. For my website, I'm trying to make it look similar to an album with a CD popping out from the slip, but every attempt I've done to make the image square, layered behind and the same size as the <div> seems to not work. It's either on top, or it's overflowing to the bottom of the page.
Here's the HTML I did:
<div class="container">
<div id="navbar">
<div id="nav">
<div>
<i class="fa fa-home" aria-hidden="true"></i>Home
</div>
<div>
<i class="fa fa-address-book" aria-hidden="true"></i>About
</div>
<div>
<i class="fa fa-book" aria-hidden="true"></i>Work
</div>
<div>
<i class="fa fa-question-circle" aria-hidden="true"></i>Extras
</div>
</div>
</div>
<iframe src="home.html" title="Webpage" id="content" name="content"></iframe>
</div>
</div>
<div class="disk">
<img src="/images/cd.svg">
</div>
And the CSS:
:root { /* to get the color just type var(--color) */
--color1:#150F0F; /*DARKEST*/
--color2:#221918;
--color3:#2C221F;
--color4:#423229;
--color5:#58493D;
--color6:#8D7357;
--color7:#BCAA9B;
--color8:#BCAA9B;
--color9:#F5DEAD;
--color10:#FFF9BC; /*BRIGHTEST*/
--border-size:3px;
}
.container {
min-height: 50vh;
width: 70%;
margin: auto;
margin-left:10%;
display: grid;
grid-template-columns: minmax(2vw, 260px) auto;
background-color: var(--color9);
border: 0px;
border-style: solid;
border-top-width: var(--border-size);
border-bottom-width: var(--border-size);
border-color: var(--color10);
z-index: 69;
}
#navbar {
height: auto;
width: auto;
padding: 10%;
float: left;
background-color: var(--color7);
font-family: PopMagic;
font-size: clamp(15px, 4vw, 30px);
}
#content {
height: 100%;
width: 100%;
border:none;
}
.disk {
min-height: 50vh;
max-width: 85%;
margin: auto;
margin-left:10%;
margin-top: -50vh;
position:relative;
overflow:hidden;
padding-bottom:100%;
}
.disk img {
min-height: 50vh;
position: absolute;
}
The site I'm using this on can be found here.
As you may expect there are as many approaches as developers.
You should consider how your layout is constructed at the moment because there is simply not enough space for your disc element.
If I were you I would get rid the following div
<div class="disk">
<img src="/images/cd.svg">
</div>
and try to use ::after psuedo-element instead.
...
.container {
border: none;
position: relative;
padding: 0 160px 0 0;
background-color: #0000;
::after {
right: 0;
content: '';
width: 160px;
height: 100%;
position: absolute;
background-size: cover;
background-position: right;
background-repeat: no-repeat;
background-image: url(/images/cd.svg);
}
}
...
Please don't expect the final solution here but take above code as a bunch of clues which may help to solve your problem.

HTML/CSS buttons work but don't show any effect unless page is refreshed

This is the first time I'm asking around here. Hopefully you guys can help me.
For a client I'm creating a website for a bar/restaurant. A part of the page setup for the menu card. I created 3 buttons underneath to switch between diner, lunch and drinks. Whenever I hit one of the buttons I see the link change. The weird part; I can't figure out why the image doesn't change. If I refresh the page (the changed links let's say) it shows the image it's supposed to show.
I guess my question is. How can I make sure that the image changes with the button press without having to reload the page.
HTML
<span id="menu1"></span>
<span id="menu2"></span>
<span id="menu3"></span>
<div class="image-container">
<img src="img/menu1.jpg" class="slider-image">
<img src="img/menu2.jpg" class="slider-image">
<img src="img/menu3.jpg" class="slider-image">
</div>
<div class="button-container">
</div>
</div>
CSS
.slide-container {
width: 495px;
height: 750px;
margin-left: auto;
margin-right: auto;
overflow: hidden;
text-align: center;
margin-bottom: 30px;
margin-top: 30px;
}
.image-container {
width: 1485px;
height: 700px;
position: relative;
}
.slider-image {
float: left;
margin: 0px;
padding: 0px;
max-width: 495px;
}
.button-container {
margin-top: 10px;
position: relative;
}
.slider-button {
display: inline-block;
height: 10px;
width: 10px;
border-radius: 5px;
background-color: black;
}
#menu1:target ~ .image-container {
margin: 0px;
margin-top: 5%;
}
#menu2:target ~ .image-container {
left: -495px;
margin-top: 5%;
}
#menu3:target ~ .image-container {
left: -990px;
margin-top: 5%;
}
If anyone has any idea and could help. I thank you!
See following code snippet, you need add id to an element and specify that id in anchor tag so that browser will scroll page up to that content on clicking the anchor.
#section1,
#section2,
#section3,
#section4 {
height: 400px;
border-bottom: 1px solid #999;
}
Section 1
Section 2
Section 3
Section 4
<div id="container">
<div id="section1">SECTION 1</div>
<div id="section2">SECTION 2</div>
<div id="section3">SECTION 3</div>
<div id="section4">SECTION 4</div>
</div>

How can you perfectly centre a grid within a container without using CSS Grid or flexbox?

Okay, so I thought that the grid was perfectly aligned to the center, only to realise that it was a few pixels out. I completely stripped all of my attempts at centering and looked online but couldn't find anything.
I know I can use CSS Grids, Flexbox etc. but I am trying to learn how to create websites without using any aid. So I can learn the reasoning behind things.
Fiddle:
https://jsfiddle.net/8L9ye7nj/5/
Grid HTML:
<div class="box-wrapper">
<div class="box-container">
<div class="box" id="stethoscope">
<div class="box-label">
<p>Book an appointment</p>
</div>
</div>
<div class="box" id="prescription">
<div class="box-label">
<p>Request a repeat prescription</p>
</div>
</div>
<div class="box" id="group">
<div class="box-label">
<p>Join the Patient Group</p>
</div>
</div>
</div>
</div>
Grid CSS:
.box {
float: left;
width: 25%;
height: 300px;
background-color: #252625;
color: #FFF;
position: relative;
padding: 15px;
margin: 0.5%;
}
.box-label {
position: absolute;
bottom: 0;
text-align: center;
background-color: rgba(0,0,0,0.5);
width: 100%;
padding: 7px 0;
left: 0;
}
.box-label:hover {
animation: box-stretch 1s forwards ease-in-out;
cursor: pointer;
}
.box-container {
width: 90%;
}
.box-container::after {
content: "";
clear: both;
display: table;
}
.box-wrapper {
background-color: #B21645;
padding: 30px;
}
How can you divide the box and center them?
You can use calc to use mathematical expressions to calculate height, widths etc in css. You can divide the width by three here for the box.
.box {
display: inline-block;
width: calc(100% / 3);
}
Things to consider
Mind the space between inline-block elements. You can read more about
that here.
Avoid using floats as much as possible. Most layouts done with float can be achieved with inline-block. Floats are simply meant to take an element, put it to one side, and let other content flow around it. That’s all.
box-wrapper and box-container either one is only needed to wrap the contents inside.
Code Snippet
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.box-wrapper {
background-color: #b21645;
padding: 20px;
}
.box {
position: relative;
display: inline-block;
width: calc(100% / 3);
padding: 0 10px;
height: 300px;
overflow: hidden;
}
.box img {
height: 100%;
width: 100%;
object-fit: cover;
object-position: left top;
}
.box-label {
position: absolute;
bottom: 0;
width: calc(100% - 20px);
text-align: center;
background-color: rgba(0, 0, 0, .6);
padding: 10px 0;
transition: padding 0.3s;
}
.box-label:hover {
padding: 25px 0;
}
.box-label p {
font-family: Helvetica;
color: white;
font-size: 20px;
}
<div class="box-wrapper">
<div class="box">
<img src="https://images.unsplash.com/photo-1509027572446-af8401acfdc3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef8f839186c5a6055d2802005b575194&auto=format&fit=crop&w=500&q=60" alt="" />
<div class="box-label">
<p>Some Title Here</p>
</div>
</div><div class="box">
<img src="https://images.unsplash.com/photo-1509027572446-af8401acfdc3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef8f839186c5a6055d2802005b575194&auto=format&fit=crop&w=500&q=60" alt="">
<div class="box-label">
<p>Some Title Here</p>
</div>
</div><div class="box">
<img src="https://images.unsplash.com/photo-1509027572446-af8401acfdc3?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef8f839186c5a6055d2802005b575194&auto=format&fit=crop&w=500&q=60" alt="">
<div class="box-label">
<p>Some Title Here</p>
</div>
</div>
</div>

Trouble centering text and image

Im trying to style a div I need to be able to center both lines of text on top of each other with the image (.hand) next them on the right. I can not get this. I must not be understanding how to do this because I've looked up solutions but they are not working for me. Here is my codepen: https://codepen.io/iamgonge/pen/MQvEWY?editors=1100
here is an image of what it should look like:what section should look like.
here is my code:
HTML:
<div class="events">
<h1>You're Cool, We're Cool,</h1>
<p class="moveit">come see us at a event near you.</p>
<img class="hand"src="http://res.cloudinary.com/adamscloud/image/upload/v1518559592/hand_lco9ed.png">
</div>
CSS:
.events {
background: #fbdd37;
height: 150px;
}
.events h1{
margin-top: 0;
position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.moveit{
margin-top: 0;
position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.hand {
width: 8%;
}
Any help here would be greatly appreciated!
You can try using flexbox.
Enclose the h1 and p in a div(.text) and then
add display:flex; in the .events container
also you will need to set the margin of h1 and p since they have a default margin.
p,h1{ margin:10px 20px; }
Please see the sample code below.
.events {
background: #fbdd37;
height: 150px;
padding:0;
margin:0;
display:flex;
justify-content:center;
align-items:center;
}
.text{
text-align:center;
}
.hand {
width: 15%;
}
p,h1{
margin:10px 20px;
}
<div class="events">
<div class="text">
<h1>You're Cool, We're Cool,</h1>
<p class="moveit">
come see us at a event near you.
</p>
</div>
<img class="hand"src="http://res.cloudinary.com/adamscloud/image/upload/v1518559592/hand_lco9ed.png">
</div>
You should put the events and moveit into a container div:
<div class="events">
<div id="container"> <!-- This div added -->
<h1>You're Cool, We're Cool,</h1>
<p class="moveit">come see us at a event near you.</p>
</div>
<img class="hand"src="http://res.cloudinary.com/adamscloud/image/upload/v1518559592/hand_lco9ed.png">
</div>
And then the minimal CSS:
.events {
background: #fbdd37;
height: 150px;
text-align:center;
width:100%;
}
.moveit{
margin-top: 0;
}
#container{
text-align:center;
float:left;
margin-left:500px;
}
.hand {
width: 8%;
float:left;
margin-left:50px;
}
This gets you close to your picture. Of course, adjust the fonts, etc. for a closer match.
This should give you something close. I used flexbox. You would just need to style the font style, boldness, and etc.
--HTML--
<div class="events">
<div class="texts">
<div class="first-line">
<h1>You're Cool, We're Cool,</h1>
</div>
<div class="second-line">
<h2 class="moveit">come see us at a event near you.</h2>
</div>
</div>
<img class="hand"
src="http://res.cloudinary.com/adamscloud/image/upload/
v1518559592/hand_lco9ed.png"/>
</div>
--CSS--
.events {
background: #fbdd37;
height: 150px;
display: flex;
text-align: center;
justify-content: center;
}
.hand {
width: 10%;
height: 40%;
margin: 35px 20px;
}
You can do it like this. It works mainly with inline-blocks and two wrapper DIVs, the inner one wrapping the two text elements, the outer one wrapping the inner wrapper and the image. that outer wrapper is centered with text-align: center, which works since it's an inline-block. The vertical centering is done the usualy way: position: relative, top: 50% and transform: translateY(-50%):
.events {
background: #fbdd37;
height: 150px;
position: relative;
text-align: center;
}
.outer_wrap {
display: inline-block;
position: relative;
top: 50%;
transform: translateY(-50%);
}
.inner_wrap {
display: inline-block;
}
.events h1 {
margin-bottom: 0;
}
.moveit {
margin-top: 0;
}
.hand {
display: inline-block;
width: 120px;
padding-left: 10px;
height: auto;
}
<div class="events">
<div class="outer_wrap">
<div class="inner_wrap">
<h1>You're Cool, We're Cool,</h1>
<p class="moveit">come see us at a event near you.</p>
</div>
<img class="hand" src="http://res.cloudinary.com/adamscloud/image/upload/v1518559592/hand_lco9ed.png">
</div>
</div>
The same in a codepen: https://codepen.io/anon/pen/QQMqOw

Is there a semantically better way to create this layout of text in circles?

I've come up with what seems like a very hacky, non-semantic way to code a design that I'd like to use. Basically, it's a set of 4 equal-sized circles, distributed so their centers are the same as those of equilateral triangles. I've used a bunch of presentational divs to solve two issues: (1) to get the spacing of the circles right, I need their bounding boxes to overlap; and (2) to vertically space text in the circles without changing their size, it seems like I need to use display:table in my CSS.
It works, but I hate it, and I feel like there has to be a better way. I am new to CSS, and this method is the result of a fair amount of research about how to solve this design problem.
The design is at this codepen: http://codepen.io/bhagerty/pen/rejEPZ
(I put borders on a bunch of the elements just to show the structure.)
Here is the HTML:
<body>
<h1 id="home_title">test</h1>
<div id="container_1">
<div id="picture" class="box">
<div class="circle_outer">
<div class="circle_inner">
<div class="inner-text">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/%22In_Which_We_Serve%22_Advertisement_1943.jpg/1024px-%22In_Which_We_Serve%22_Advertisement_1943.jpg" width=100%; />
</div>
</div>
</div>
</div>
<div id="dog" class="box">
<div class="circle_outer">
<div class="circle_inner">
<div class="inner-text">
dog
</div>
</div>
</div>
</div>
<div id="shoes" class="box">
<div class="circle_outer">
<div class="circle_inner">
<div class="inner-text">
shoes
</div>
</div>
</div>
</div>
<div id="dance" class="box">
<div class="circle_outer">
<div class="circle_inner">
<div class="inner-text">
dance
</div>
</div>
</div>
</div>
<div id="footer_1">
Footer<br>
test
</div>
</div>
</body>
Here is the CSS:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
font-size: 16px;
}
h1#home_title {
text-align: center;
font-size: 3rem;
margin: 0;
padding: .1rem 0 .5rem 0;;
background-color: grey;
}
div#container_1 {
border: green solid 5px;
width: 320px;
margin: auto;
position: relative;
}
div.box {
border: red solid 1px;
position: absolute;
width: 53.6%;
text-align: center;
background-color: transparent;
}
/*pseudo-element to give relative height,
per http://jsfiddle.net/simevidas/PFPDU/
and http://www.mademyday.de/css-height-equals-width-with-pure-css.html */
div.box::before {
content: "";
display: block;
padding-top: 100%;
height: 0;
}
/* if inner text has position relative, it influences the size of the containing box */
/*setting all of the positions to zero forces it inside the circle for some reason */
.circle_outer {
position: absolute;
overflow: hidden;
border: black solid 2px;
border-radius: 50%;
/* to create breathing room all around, set top and left to 1/2 of 100% - width (where width = height) */
top: 5%;
left: 5%;
width: 90%;
height: 90%;
}
.circle_inner {
/* border: grey solid 5px; */
display: table;
width: 100%;
height: 100%;
}
.inner-text {
display: table-cell;
/* border: green solid 2px; */
font-size: 2em;
vertical-align: middle;
}
/*First bounding box is at upper left corner */
div#picture {
overflow: hidden;
left: 0;
margin-top: 0;
}
/*Percent positions all based on W, derived from fact
that bounding boxes circumscribe tangent circles, and
circle centers are connected by equilateral triangles */
div#dog {
left: 46.4%;
margin-top: 26.8%;
}
div#shoes {
left: 0;
margin-top: 53.6%;
}
div#dance {
left: 46.4%;
margin-top: 80.4%;
}
div#footer_1 {
border: red solid 2px;
position: relative;
width: 100%;
left: 0;
margin-top: 137%;
text-align: center;
background-color: blue;
}
I much appreciate any thoughts or help. Thanks!
Well, IMO what you've done is really good. I wouldn't be too concerned about the extra divs.
But, it can be done with fewer divs, making use of float and margins.
Codepen is here
html {
font-size: 16px;
}
h1#home_title {
text-align: center;
font-size: 3rem;
margin: 0;
padding: .1rem 0 .5rem 0;;
background-color: grey;
}
div#container_1 {
border: green solid 5px;
width: 320px;
margin: auto;
position: relative;
box-sizing: border-box;
}
div.box {
border: red solid 1px;
position: relative;
float:left;
width: 53.6%;
text-align: center;
background-color: transparent;
box-sizing:border-box;
margin-bottom:-27%;
}
div.box:nth-child(2n) {
float:right;
}
div.box:nth-child(2n+1) {
float:left;
}
/*pseudo-element to give relative height,
per http://jsfiddle.net/simevidas/PFPDU/
and http://www.mademyday.de/css-height-equals-width-with-pure-css.html */
div.box::before {
content: "";
display: block;
padding-top: 100%;
height: 0;
}
/* if inner text has position relative, it influences the size of the containing box */
/*setting all of the positions to zero forces it inside the circle for some reason */
.featuring {
position: absolute;
overflow: hidden;
border: black solid 2px;
border-radius: 50%;
/* to create breathing room all around, set top and left to 1/2 of 100% - width (where width = height) */
top: 5%;
left: 5%;
width: 90%;
height: 90%;
font-size: 2em;
}
.featuring:before {
content:'';
margin-left:-0.25em;
display:inline-block;
vertical-align:middle;
height:100%;
}
/*Percent positions all based on W, derived from fact
that bounding boxes circumscribe tangent circles, and
circle centers are connected by equilateral triangles */
div#footer_1 {
border: red solid 2px;
position: relative;
width: 100%;
left: 0;
margin-top: 137%;
text-align: center;
background-color: blue;
clear:both;
}
<body>
<h1 id="home_title">test</h1>
<div id="container_1">
<div id="picture" class="box">
<div class="featuring">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/58/%22In_Which_We_Serve%22_Advertisement_1943.jpg/1024px-%22In_Which_We_Serve%22_Advertisement_1943.jpg" width=100%; />
</div>
</div>
<div id="dog" class="box">
<div class="featuring">
dog
</div>
</div>
<div id="shoes" class="box">
<div class="featuring">
shoes
</div>
</div>
<div id="dance" class="box">
<div class="featuring">
dance
</div>
</div>
<div id="footer_1">
Footer<br>
test
</div>
</div>
</body>