I checked various links to achieve this. One of them was: from stack overflow
Below is my code:
.a-deal {
position: relative;
}
.deal-hd {
float: left;
}
.deal-arw {
float: right;
padding: 10px;
background: grey;
}
.deal-hd:after {
content: '';
width: 100%;
border-bottom: solid 1px #d6d6d6;
position: absolute;
left: 0;
top: 50%;
z-index: 1;
}
.clearfix:before,
.clearfix:after {
content: "";
display: table;
}
.clearfix:after {
clear: both;
}
<div class="a-deal clearfix">
<h2 class="deal-hd">ebay Top Deals</h2>
<!-- <div class="mark"></div> -->
<div class="deal-arw">
˂
˃
</div>
</div>
Requirements:
line to come exactly at the center of two divs using pseudo selectors only
width of line should be scalable. That means in case, if any of the div size increase, the line should take the remaining space only.
You can just use display: flex and flex: 1 on deal-hd so it takes free width and add pseudo-element.
.a-deal {
display: flex;
align-items: center;
}
.deal-hd {
flex: 1;
display: flex;
align-items: center;
margin: 0;
}
.deal-hd:after {
content: '';
height: 1px;
background: black;
flex: 1;
margin: 0 10px;
}
<div class="a-deal clearfix">
<h2 class="deal-hd">ebay Top Deals</h2>
<div class="deal-arw">
˂
˃
</div>
</div>
You can use flex for this. No need to use floats. Using floats you have to write more css to align the items vertically center using transform and position.
And I will also suggest to use :pseudo selector of the parent .a-deal not the heading .deal-hd
Stack Snippet
.a-deal {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
}
.deal-hd {
margin: 0;
background: #fff;
z-index: 9;
padding-right: 10px;
}
.deal-arw {
padding: 10px;
background: grey;
z-index: 9;
padding-left: 10px;
}
.a-deal:after {
content: '';
width: 100%;
border-bottom: solid 1px #d6d6d6;
position: absolute;
left: 0;
top: 50%;
z-index: 1;
}
<div class="a-deal clearfix">
<h2 class="deal-hd">ebay Top Deals</h2>
<!-- <div class="mark"></div> -->
<div class="deal-arw">
˂
˃
</div>
</div>
Related
<!DOCTYPE html>
<html lang="en">
<head>
<style>
*{
margin: 0px;
padding: 0px;
}
.squares {
color: rgb(212, 212, 212);
background-color: #2e2e2e;
width: 40px;
height: 40px;
}
.dropdown-content {
width: 100px;
height: 80px;
background-color: #7e7e7e;
}
#sidebar{
overflow: visible;
width: 40px;
height: 100px;
}
</style>
</head>
<body>
<div id="sidebar">
<div class="squares"></div>
<div class="dropdown-content"></div>
<div class="squares"></div>
</div>
</body>
</html>
How can I make .dropdown-content overflow to the right side of #sidebar without changing size of sidebar? I've tried using floats but that did not work. I've also tried containing the first .squares and .dropdown-content together but that causes the second .squares to be positioned away from the bottom of the first .squares.
You just need to add relative positioning to your .dropdown-content and absolute positioning to the inner sub menu wrapper. And then add top: 0 and left: 100%
Here's the working example for you:
body {
margin: 0;
}
.menu {
display: inline-flex;
justify-content: center;
align-items: center;
flex-direction: column;
background: #212121;
position: fixed;
overflow: visible;
}
.menu-item {
padding: 10px;
color: #fff;
cursor: pointer;
}
.expandable {
position: relative;
display: inline-flex;
justify-content: center;
align-items: center;
}
.expandable:hover > .sub-menu {
display: inline-flex;
flex-direction: column;
width: 200px;
}
.sub-menu {
position: absolute;
left: 100%;
top: 0%;
display: none;
width: 200px;
background: #fff;
color: #000;
border: 1px solid;
cursor: pointer;
}
.sub-menu-item {
padding: 10px;
}
.sub-menu-item:not(:last-child) {
padding: 10px;
border-bottom: 1px solid
}
<div class="menu">
<div class="menu-item">Simple Menu</div>
<div class="menu-item expandable">
Hover to Expand >
<div class="sub-menu">
<div class="sub-menu-item">Sub Menu 1</div>
<div class="sub-menu-item">Sub Menu 2</div>
<div class="sub-menu-item">Sub Menu 3</div>
</div>
</div>
<div class="menu-item">Simple Menu</div>
</div>
In my code .expandable is the one that have sub menus in it, on hovering over the element submenus will be opened on the right side of that menu-item.
The position will be depend upon the hovered element.
Also, here's the code pen link if you wish to tinker it:
https://codepen.io/prathameshkoshti/pen/zYBeWEz?editors=0110
In this one I have used multiple of this.
.dropdown-content {
position: relative;
right: -20px;
width: 100px;
height: 80px;
background-color: #7e7e7e;
}
simple version I do not change the container, and I shift it to the right.
If I understood correctly....
* {
margin: 0px;
padding: 0px;
}
#sidebar {
overflow: visible;
width: 40px;
height: 100px;
}
.squares {
position: relative;
border: 1px solid #fff;
color: rgba(212, 212, 212, 1);
background-color: #2e2e2e;
width: 40px;
height: 40px;
}
.dropdown-content {
position: absolute;
left: 40px;
top: 0px;
width: 100px;
height: 80px;
background-color: #7e7e7e;
display: none; /* sub menus will not be visible as a result */
}
.squares:hover > .dropdown-content {
display: block;
}
<body>
<div id="sidebar">
<div class="squares">1</div>
<div class="squares">
2
<div class="dropdown-content">
</div>
</div>
<!-- create another container to house the .dropdown-content -->
<div class="squares">3</div>
</div>
</body>
insert the .dropdown-content into a .square then add display:none to the styling of .dropdown-content and finally on hover change display: none to block
I'm awfully new to the whole game and approach problems pretty much everyday. Most of the time I solve them with google, learning a lot, but this time I can't find anything.
So, I've got this lovely header that moves to the right and makes some space for another element. I'd like this element (the bonobo head) to appear when I hover over the header.
So, I set the display of the image to none, and block on header:hover, but the image seems glued to the header.
I would like it to appear next to it, in any given location. What do ?
.header {
position: relative;
display: block;
left: 0%;
padding: 15px;
background-color: #54e954;
text-align: center;
width: 100%;
height: auto;
transition: 2s;
border-radius: 8px;
}
.header:hover {
left: 15%;
background-color: #d0f307;
}
.photo {
position: relative;
display: none;
}
.header:hover .photo {
display: block;
}
<div class="header">
<h1 class="h1header">Lorem Ipsum</h1>
<div>
<img src="https://upload.wikimedia.org/wikipedia/commons/4/4b/Bonobo-Head.jpg" class="photo">
</div>
</div>
Is that what you want it to look like ? If you want this, I did it using the display flex structure.
.header {
position: relative;
display: flex; /* added */
justify-content: center;
align-items: center;
left: 0%;
padding: 15px;
background-color: #54e954;
text-align: center;
width: 100%;
height: auto;
transition: 2s;
border-radius: 8px;
}
.header:hover {
left: 15%;
background-color: #d0f307;
}
.photo {
position: relative;
display: none;
}
.header:hover .photo {
display: block;
}
<div class="header">
<h1 class="h1header">Lorem Ipsum</h1>
<div class="img-container">
<img src="https://upload.wikimedia.org/wikipedia/commons/4/4b/Bonobo-Head.jpg" class="photo">
</div>
</div>
Updated : With Javascript, you can better control hover and hoverout operations. Here's an example I've prepared for you. By the way, I had to get the img tag out of the header tag. You can see the additions I've made.
let header = document.querySelector(".header");
let imageContainer = document.querySelector(".img-container");
header.onmouseover = function() {
imageContainer.classList.add("showImage");
}
header.onmouseout = function() {
imageContainer.classList.remove("showImage");
}
.header {
position: relative;
display: flex;
/* added */
justify-content: center;
align-items: center;
left: 0%;
padding: 15px;
background-color: #54e954;
text-align: center;
width: 100%;
height: 80px;
transition: 1s;
border-radius: 8px;
}
.header:hover {
transform: translateX(15%);
/* added*/
background-color: #d0f307;
}
.img-container {
position: relative;
display: none;
}
.img-container.showImage {
position: relative;
display: block;
}
.container {
display: flex;
justify-content: space-between;
}
<div class="container">
<div class="img-container">
<img src="https://upload.wikimedia.org/wikipedia/commons/4/4b/Bonobo-Head.jpg" class="photo">
</div>
<div class="header">
<h1 class="h1header">Lorem Ipsum</h1>
</div>
</div>
With the below code, I am trying to make an overlay for a "progress bar" with text, I need the flex to make it responsive,
BUT the only way I know to make an overlay is to use things like fixed or absolute position, which breaks the layout, is there a way to keep it responsive and have an overlay with dynamic width (for displaying progress).
.master{
display: flex;
}
.item{
border-right: 1px solid black;
padding-right: 5px;
display: inline-block;
text-align: center;
padding: 15px 0;
flex-grow: 1;
flex-basis: 0;
}
<div style='border:1px solid black;margin: 0 auto;'>
<div class='master'>
<span class="item">a</span>
<span class="item">b</span>
<span class="item">c</span>
</div>
<div id='overlay' style='background-color:red;opacity:0.7;width:100%;height:100%;'></div>
</div>
Absolute position will not break the layout...assuming you have set the positioning context on the parent?
.parent {
position: relative;
}
.master {
display: flex;
}
.item {
border-right: 1px solid black;
padding-right: 5px;
display: inline-block;
text-align: center;
padding: 15px 0;
flex-grow: 1;
flex-basis: 0;
}
#overlay {
position: absolute;
background-color: red;
opacity: 0.7;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
<div class="parent">
<div class="master">
<span class="item">a</span>
<span class="item">b</span>
<span class="item">c</span>
</div>
<div id="overlay"></div>
</div>
I am having some trouble keeping this svg, and flexbox buttons from scaling over each other when scaling down. I want to use flexbox so that I can control the buttons and SVG overlay over this hero shot at all time.
Is there a better way to accomplish this, or am I screwed lol?
Any recommendations would be appreciated.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.ob-hero-container {
position: relative;
}
.herobtn-container {
width: 80%;
max-width: 800px;
margin: 0 auto;
position: absolute;
top: 60%;
right: 0;
bottom: 0;
left: 0;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.herosvg-container {
width: 80%;
max-width: 800px;
margin: 0 auto;
position: absolute;
top: -20%;
right: 0;
bottom: 0;
left: 0;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.herobtn-wrapper {
border: 2px solid transparent;
flex-grow: 1;
}
.herobtn1 {
background-color: #000;
color: #fff;
padding: 10px;
/*margin: .1em;*/
text-align: center;
}
/* Tablet */
#media (min-width:30em) and (max-width:50em) {
.herobtn-wrapper {
flex-basis: 50%;
}
}
#media (max-width:50em) {
.herobtn-container {
position: relative;
width: 100%;
}
}
/* Mobile */
#media (max-width:30em) {
.herobtn-wrapper {
flex-basis: 50%;
}
}
<div class="ob-hero-container">
<!-- IMAGE BACKGROUND --><img src="https://s21.postimg.org/jp1vyijrb/sample.jpg" width="100%">
<!-- END IMAGE BACKGROUND -->
<div class="herosvg-container"><img src="http://svgshare.com/i/23M.svg" width="100%"></div>
<div class="herobtn-container">
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP WOMEN'S</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP MEN'S</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP KIDS</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">WATCH VIDEO</div>
</div>
</div>
</div>
You can't allow for the .herosvg-container holding the svg text to be able to grow below the starting point for the buttons.
This change in the .herosvg-container rule will make them not overlap
top: 0; /* changed */
height: 60%; /* changed */
/* bottom: 0; removed */
Stack snippet
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.ob-hero-container {
position: relative;
}
.herobtn-container {
width: 80%;
max-width: 800px;
margin: 0 auto;
position: absolute;
top: 60%;
right: 0;
bottom: 0;
left: 0;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.herosvg-container {
width: 80%;
max-width: 800px;
margin: 0 auto;
position: absolute;
top: 0; /* changed */
height: 60%; /* changed */
/* bottom: 0; removed */
right: 0;
left: 0;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.herobtn-wrapper {
border: 2px solid transparent;
flex-grow: 1;
}
.herobtn1 {
background-color: #000;
color: #fff;
padding: 10px;
/*margin: .1em;*/
text-align: center;
}
/* Tablet */
#media (min-width:30em) and (max-width:50em) {
.herobtn-wrapper {
flex-basis: 50%;
}
}
#media (max-width:50em) {
.herobtn-container {
position: relative;
width: 100%;
}
}
/* Mobile */
#media (max-width:30em) {
.herobtn-wrapper {
flex-basis: 50%;
}
}
<div class="ob-hero-container">
<!-- IMAGE BACKGROUND --><img src="https://s21.postimg.org/jp1vyijrb/sample.jpg" width="100%">
<!-- END IMAGE BACKGROUND -->
<div class="herosvg-container"><img src="http://svgshare.com/i/23M.svg" width="100%"></div>
<div class="herobtn-container">
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP WOMEN'S</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP MEN'S</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP KIDS</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">WATCH VIDEO</div>
</div>
</div>
</div>
You could wrap both the svg container and button container together. This ensures that they won't overlap.
Example:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.ob-hero-container {
position: relative;
}
.overlay {
width: 80%;
max-width: 800px;
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
display: flex;
flex-direction: column;
justify-content: center;
height: 100%;
}
.herosvg-container {
margin-bottom: 1em; /* styling for demo */
}
.herobtn-container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.herobtn-wrapper {
border: 2px solid transparent;
flex-grow: 1;
}
.herobtn1 {
background-color: #000;
color: #fff;
padding: 10px;
/*margin: .1em;*/
text-align: center;
}
/* Tablet */
#media (min-width:30em) and (max-width:50em) {
.herobtn-wrapper {
flex-basis: 50%;
}
}
#media (max-width:50em) {
.herobtn-container {
position: relative;
width: 100%;
}
}
/* Mobile */
#media (max-width:30em) {
.herobtn-wrapper {
flex-basis: 50%;
}
}
<div class="ob-hero-container">
<!-- VIDEO BACKGROUND -->
<!--
<video autoplay="" loop="" preload="auto" width="100%">
<source src="http://media.rackroomshoes.com/uploads/awhitten/obsw/bts-video.mp4" type="video/mp4; codecs=avc1.42E01E,mp4a.40.2" />Your browser does not support the video tag.</video>
-->
<!-- END VIDEO BACKGROUND -->
<!-- IMAGE BACKGROUND --><img src="https://s21.postimg.org/jp1vyijrb/sample.jpg" width="100%">
<!-- END IMAGE BACKGROUND -->
<div class="overlay">
<div class="herosvg-container"><img src="http://svgshare.com/i/23M.svg" width="100%"></div>
<div class="herobtn-container">
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP WOMEN'S</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP MEN'S</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">SHOP KIDS</div>
</div>
<div class="herobtn-wrapper">
<div class="herobtn1">WATCH VIDEO</div>
</div>
</div>
</div>
<!-- /overlay -->
</div>
How can I centre align text over an <img> preferably using FlexBox?
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
}
.center-aligned {
display: box;
display: flex;
box-align: center;
align-items: center;
box-pack: center;
justify-content: center;
}
.background-image {
position: relative;
}
.text {
position: absolute;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg" />
<div class="text">SOME TEXT</div>
</section>
To center text over an image you don't need flexbox. Just use CSS positioning properties.
.height-100vh {
height: 100vh;
position: relative; /* establish nearest positioned ancestor for
absolute positioning */
}
.text {
position: absolute;
left: 50%; /* horizontal alignment */
top: 50%; /* vertical alignment */
transform: translate(-50%, -50%); /* precise centering; see link below */
}
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
display: flex; /* establish flex container */
flex-direction: column; /* stack flex items vertically */
position: relative; /* establish nearest positioned ancenstor for absolute positioning */
}
.text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: white;
font-weight: bold;
}
.center-aligned {
display: flex;
align-items: center;
justify-content: center;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg/revision/latest?cb=20090904155448" />
<div class="text">SOME TEXT</div>
</section>
Revised Codepen
The code above centers the text both vertically and horizontally over the image:
For a more detailed explanation of the centering method see:
Element will not stay centered, especially when re-sizing screen
You can just wrap an image with relatively positioned inline-block <div> and give the text this way:
* {margin: 0; padding: 0; list-style: none;}
.img-holder {position: relative; display: inline-block;}
.img-holder img {display: block;}
.img-holder p {position: absolute; top: 50%; left: 0; right: 0; transform: translate(0, -50%); text-align: center; color: #fff; text-shadow: 0 0 15px;}
<div class="img-holder">
<img src="http://bit.ly/1YrRsis" alt="">
<p>Text Aligned Centrally Vertical & Horizontal.</p>
</div>
Now the <div> is an inline kinda element that you can style.
Preview:
I've added another wrapper div surrounding the img and text as well as using flex to position the text. See this codepen
HTML:
<section class="height-100vh center-aligned">
<div class="wrapper">
<img class="background-image" src="http://bit.ly/1YrRsis" />
<div class="text">SOME TEXT</div>
</div>
</section>
CSS:
#import "bourbon";
body {
margin: 0px;
}
.height-100vh {
height: 100vh;
}
.center-aligned {
#include display(flex);
#include align-items(center);
#include justify-content(center);
}
.wrapper {
position: relative;
}
.text {
justify-content: center;
align-items: center;
display: flex;
color: #fff;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
You also can center align text onto an image using display: inline-block; to the wrapper element.
.height-100vh {
display: inline-block;
position: relative;
}
.text {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
color: #fff;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="http://vignette2.wikia.nocookie.net/uncyclopedia/images/f/f8/Stand-out-in-the-crowd-300x300.jpg" />
<div class="text">SOME TEXT</div>
</section>
I added 2 more divs
<div class="text box" >
<img src="images/woodenbridge.jpg" alt="Wooden Bridge" />
<div class="slide-box flex">
<div class="flex-item"></div>
</div>
</div>
and then styled as follows :
.flex-item {
display: inline;
align-self: center;
}
Responsive centered text content over image
.height-100vh {
height: 100vh;
background: lightgrey;
}
.center-aligned {
display: flex;
align-items: center;
justify-content: center;
}
.background-image {
opacity: .3;
width: 100%;
object-fit:cover;
height: 100%;
}
.text {
position: absolute;
color: white;
font-family: 'Gill Sans', 'Gill Sans MT';
background: darkcyan;
padding: 20px;
}
<section class="height-100vh center-aligned">
<img class="background-image" src="https://www.humanesociety.org/sites/default/files/styles/768x326/public/2018/08/kitten-440379.jpg?h=f6a7b1af&itok=vU0J0uZR" />
<div class="text">I'm in center</div>
</section>