I'm currently working on a simple project in HTML/CSS (Bootstrap). Very simply, I have the task of styling links to look like buttons, and getting them to stack in a mobile view.
Requirements: buttons should be side-by-side on regular/desktop view, and should stack on top of each other in iPhone/Android devices.
CodePen: https://codepen.io/anfperez/pen/joqoXG
Here's the code I have so far:
html
<a class="button-a" href="www.google">Link that looks like button</a>
<a class="button-b" href="www.amazon.com">Another link that looks like a button</a>
css (Bootstrap can be included)
.button-a, .button-b {
background-color: blue;
color: white;
text-decoration: none;
padding: 10px;
}
.button-a {
background-color: blue
}
.button-b {
background-color: red;
}
When I try viewing the code in a mobile view, the red button button ends up overlapping on top of the blue button since they're both links. How can I get the red button to clear the blue button? I can't use Bootstrap's btn-group in this case.
You can use Flexbox inside a media query to stack the elements using flex-direction: column.
.button-a, .button-b {
background-color: blue;
color: white;
text-decoration: none;
padding: 10px;
}
.button-a {
background-color: blue
}
.button-b {
background-color: red;
}
#media (max-width: 767px) {
.wrapper {
display: flex;
flex-direction: column;
align-items: flex-start;
}
}
<div class="wrapper">
<a class="button-a" href="www.google">Link that looks like button</a>
<a class="button-b" href="www.amazon.com">Another link that looks like a button</a>
</div>
....really? you can making a have behaviour of div, if you want them to stack, just make the button class having inline-block
.button-a, .button-b {
background-color: blue;
color: white;
text-decoration: none;
padding: 10px;
margin: 20px;
display:inline-block;
}
Edited Codepen
This make the a link has the margin and padding behaviour like div. You just have to setting the width.. So if the media canvas shorter, the link will automatically stacking
.button-a, .button-b {
background-color: blue;
color: white;
text-decoration: none;
padding: 10px;
margin: 20px;
display:block;
}
.button-a {
background-color: blue
}
.button-b {
background-color: red;
}
Related
I'm currently trying to upgrade my HTML and CSS skills by producing a Responsive Navigation Menu and I think I have got into a bit of a mess - here's my problem: when my page width is greater than 768px I wish to hide the div with the class "menu__hamburger" and show an unordered list of items with a DIV with the class "header__navigation". Should the page width be less than 768px I wish to show the div with the class "menu__hamburger" but drop the div "menu__hamburger" so it fills the whole width of the parent DIV with the class header.
Permit me to demonstrate with some images:
Layout greater than 768px
Layout less than 768px (notice how the header__navigation drops to the next row)
The reason I want to show the header__navigation currently is because I am styling the header__navigation. Later I wish to use a hidden checkbox to toggle the display of the header__navigation using css when the user clicks the div with the menu__hamburger class. However I think I have structured my HTML badly as I cannot get the header__navigation to "drop" and "take up a full row".
Here is my HTML so far (a jsbin is here: https://jsbin.com/wecemiyepe/edit?html,css,output):
<div class="header">
<div class="header__logo">
<img src="brand-logo.png">
</div>
<div class="menu__hamburger">
<label for="menuToggle">☰</label>
<input type="checkbox" id="menuToggle">
</div>
<div class="header__navigation">
<ul class="menu">
<li class="menu__item">Home</li>
<li class="menu__item">Company</li>
<li class="menu__item">Services</li>
<li class="menu__item">Products</li>
<li class="menu__item">Careers</li>
<li class="menu__item">Contact</li>
</ul>
</div>
</div>
Here is my CSS
.header {
display: flex;
background-color: black;
border-bottom: 1px solid white;
align-items: center;
}
.header__logo {
flex: 0 1 30%;
padding: 10px 20px;
}
.header__navigation {
flex: 1 1 auto;
align-items: flex-end;
padding: 10px 20px;
}
.menu {
margin: 0;
padding: 0;
color: white;
list-style: none;
display: flex;
}
.menu__item {
flex: 1 1 auto;
}
.menu__hamburger {
color: white;
display: none;
}
input[type=checkbox] + label {
display: none;
}
#media only screen and (min-width: 320px) and (max-width: 768px) {
.menu__hamburger {
color: white;
display: flex;
justify-content: flex-end;
font-size: 40px;
}
input[type=checkbox] {
display: none;
}
/*
ToDo - toggle display of .menu if is checked
input[type=checkbox]:checked
*/
.menu {
/* display: none; */
flex-direction: column;
flex: 1 0 100%;
}
.menu__item {
text-transform: uppercase;
border-bottom: 1px solid #fff;
padding: 10px;
}
}
I'll continue to persevere with my code but if someone can give me a tip to get this working I will be most appreciative. Once again here is a link to a JSbin with the code: https://jsbin.com/wecemiyepe/edit?html,css,output
So first why you need that (min-width: 320px)?
Better delete it, and you want when the user click on the hamburger menu , the unordered list to be displayed?
I'm not sure if I totally understand your ask, but it seems like you are looking to put your navigation into a hamburger menu on smaller screens. I could type out the code but there is a pretty clear explanation on w3schools on how to create this. Hope this helps! 👍
i am trying to make a website, but for some reason i am stuck on the hover. I knew how to do this, but i thing i forgot something.
What i want is that when i hover over the black bar the black turns into white so you can see the text.
This is my code:
div.spoiler1:hover div.spoiler1 {
background-color: white;
}
<div style='display:inline; background-color: black;' class='spoiler1'>hey</div>
I also tried this css:
spoiler1:hover spoiler1 {
background-color: white;
}
div.spoiler1:hover,.spoiler1 {
background-color: white;
}
spoiler1:hover {
background-color: white;
}
Good efforts. The issue is that the inline style overrides the sheet. In general, don't use inline styles (hard to debug/maintain, not reusable):
div.spoiler1 {
background-color: black;
display: inline;
}
div.spoiler1:hover {
background-color: white;
}
<div class='spoiler1'>hey</div>
See this JSFiddle.
I'm trying to change the color of a link on hover of a <div>. Is that possible using just CSS? If not, how would I achieve this?
div {
border: 1px solid black;
padding: 15px;
}
div:hover {
color: red;
}
<div>
<a href='www.google.com'> www.google.com </a>
</div>
You need to style the anchor, not the div. Try this:
div {
border: 1px solid black;
padding: 15px;
}
div:hover a {
color: red;
}
<div>
<a href='www.google.com'> www.google.com </a>
</div>
The div itself has no text, so there's no place to apply the color property. So when you hover a div with nothing to color, nothing happens.
As mentioned in another answer, apply the hover to the anchor element, which contains text.
But your original code would work if instead of color you used background-color or border.
div {
border: 1px solid black;
padding: 15px;
}
div:hover {
color: red; /* won't work; nothing to color */
background-color: aqua; /* this will work */
border: 2px dashed #777; /* this will work */
}
<div>
<a href = 'www.google.com'> www.google.com </a>
</div>
rjdown's answer is correct, but the question is if you still need the div at all.
All a div does is provide a block for you to style. If you style the anchor as block, you have just that. Code bloat is bad for your SEO and headache-freeness. ;-)
Try this:
a:link {
display: block;
/* make it act as the div would */
overflow: auto;
/* or what you want, but good practice to have it */
border: solid 1px black;
}
a:hover,
a:focus,
a:active {
border: solid 1px red;
}
<a href='www.google.com'> www.google.com </a>
Remember to use more than a color change on your hover or the 1 in 12 males with color blindness won't see a thing, potentially, happening. The focus and active additions are for accessibility too. Especially focus is very important for keyboard users.
Good luck.
We can simply assign inherit value to all the CSS properties of anchor tag ,
Thus when you hover above its container DIV element , it will inherit all the new properties defined inside DIV:hover.
div {
display: flex;
align-items: center;
justify-content: center;
text-decoration: none;
height: 100px;
width: 100%;
color: white;
background: blue;
}
a {
text-decoration: inherit;
color: inherit;
}
div:hover {
color: orange;
}
<div>
www.google.com
</div>
I'm working on my new website and am running into something I just can't figure out. Here's the link:
http://www.adamabrams.com/aadc/
When I resize my browser to "mobile width", so that it's narrower than the width of the little row of oval "Tags" just below my site menu, I want the tags to reflow into two rows (or as many as needed). Instead, they stubbornly remain in a single row and jut off the right side of the window.
I haven't given the enclosing div a fixed height... I know this must be something simple, but I'm stumped!
Here's the HTML:
<main class="content" itemtype="http://schema.org/Blog" itemscope="itemscope" itemprop="mainContentOfPage" role="main">
<div class="post_tags">
<a class="tagintro" href="#">Select area of expertise:</a>
<a class="database" title="database projects" href="http://adamabrams.com/aadc/tag/database/">database</a>
<a class="ecommerce" title="ecommerce projects" href="http://adamabrams.com/aadc/tag/ecommerce/">ecommerce</a>
<a class="mobile" title="mobile projects" href="http://adamabrams.com/aadc/tag/mobile/">mobile</a>
<a class="website" title="website projects" href="http://adamabrams.com/aadc/tag/website/">website</a>
</div>
...and here's all the CSS that relates, I think:
/* TAG BUTTONS */
/* Styling of the buttons, everywhere */
.entry-tags a, .post_tags a {
color: #3D3D3D;
font-weight: bold;
background-color: #EECE88;
padding: .35em .5em;
border-radius: .7em;
-moz-border-radius: .7em;
-webkit-border-radius: .7em;
}
.entry-tags a:hover, .post_tags a:hover {
color: black;
background-color: orange;
}
/* TAGS "HEADER" ON TAG.PHP PAGE */
/* Layout of buttons area */
.post_tags {
text-align: center;
margin-bottom: 2em;
}
/* Spacing between buttons */
.post_tags a {
margin-right: 1em;
}
.post_tags a:first-child,
.post_tags a:last-child {
margin-right: 0;
}
/* Currently displayed tag */
body.tag-ecommerce a.ecommerce,
body.tag-database a.database,
body.tag-mobile a.mobile,
body.tag-website a.website {
color: black;
background-color: orange;
}
a.tagintro, a.tagintro:hover {
background-color: transparent;
color: white;
}
Any ideas or suggestions are most welcome!
Thanks,
Adam
I see what you are going for. The best way to get what you are trying to achieve is to change the width and margin of the element at the desired break-point.
So for your "media width" you set your classes to re-size. By doing so the elements will flow naturally down the document.
ex:
#media only screen and (max-width: 480px) { /* Your break-point for mobile */
.ecommerce,
.database,
.mobile,
.website{
width: 48%;
margin-left: 1%;
margin-right: 1%;
}
}
Or some similar measurements that makes sense with your design.
I have a newsfeed which is obviously organized by an . When the user hovers over each of the items, the background is highlighted. I'd also like to have a small "x" in the top right hand corner of each item, only shown when hovered. This "x" would be a delete button to remove that post.
Right now I just have some basic html stating: <div class="hide-button">x</div>
I know that I don't want the "x" displayed in the html, but rather have it in the CSS. So I have the <li> css below for hovering, as well as the CSS for the hide button. I'd like to know the best method to integrate the hide button div into the <li>
.hide-button {
float: right;
margin-top: -13px;
font-size: 11px;
font-family: helvetica;
color: gray;
}
.hide-button a{
text-decoration: none;
color:gray;
}
.hide-button a:hover {
text-decoration: underline;
color:gray;
}
and the list:
.newsfeedlist li {
background: white;
border-bottom: 1px solid #E4E4E4;
padding: 12px 0px 12px 0px;
overflow: hidden;
}
.newsfeedlist li:hover {
background-color: #F3F3F3;
}
Thank you so much!!!!!
Presuming your delete buttons are inside another container you could do something like
.hide-button {
float: right;
margin-top: -13px;
font-size: 11px;
font-family: helvetica;
color: tray;
display: none;
}
... the other bits of CSS ...
.newsfeedlist li:hover .hide-button {
display: block;
}
Modifying the close button to be hidden by default and then when hovering on a list item you set the display back again on the close button.
Hope this makes sense
Tim
You might really be in need of this:
Demo at jsFiddle.net
I modified an example and tushed it up for multiple content areas or images.
But hide-button element in the li and do
.newsfeedlist li:hover .hide-button {
display: inline-block;
}
and add display: none; to .hide-button
Otherwise, there's always javascript.