首页 > 其他分享 >css实现按钮文案垂直水平居中,按钮左侧图标相对文字固定距离展示

css实现按钮文案垂直水平居中,按钮左侧图标相对文字固定距离展示

时间:2024-05-29 11:44:19浏览次数:15  
标签:center height 按钮 图标 margin css left

需求

css实现按钮文案垂直水平居中,按钮左侧图标相对文字固定距离展示,效果如图:

 

实现

方案一:使用 margin-right 来实现按钮和左侧图标的间距

<div class="download-btn">
          <div class="btn-content" :class="{'left-icon': showLeftIcon}">
            <div class="left-icon">
            </div>
            <span class="btn-txt">点击查看</span>
          </div>
</div>
 .download-btn {
    width: 300px;
    height: 48px;
    background-color: #47a0ff;
    border-radius: 4px;
    font-size: 20px;
    margin-top: 30px;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 10px;
    .btn-content {
      width: auto;
      display: flex;
      align-items: center;
      justify-content: center;
      &.left-icon {
        margin-left: -36px;
      }
      .left-icon {
        width: 30px;
        height: 30px;
        margin-right: 6px;
      }
      .btn-txt {
        .line-clamp(1); // 限制按钮文字展示一行
        line-height: 1;
      }
    }
  }

实现逻辑,将按钮和图标放在同一个 div 里,按钮相对右侧文字,加一个 margin-right,按钮和图标的div 再整体向左移动左侧图标的宽度和左侧图标右间距,以保证按钮文字水平居中展示。

方案二:使用相对定位实现

<div class="download-btn">
            <div class="left-icon">
            </div>
            <span class="btn-txt"  :class="{'left-icon': showLeftIcon}">点击查看</span>
</div>
.download-btn {
    width: 300px;
    height: 48px;
    background-color: #47a0ff;
    border-radius: 4px;
    font-size: 20px;
    margin-top: 30px;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 10px;
    cursor: pointer;
    position: relative;
    .btn-content {
      width: auto;
      display: flex;
      align-items: center;
      justify-content: center;
      &.left-icon {
        margin-left: -36px;
      }
      .left-icon {
        width: 30px;
        height: 30px;
        position: relative;
        right: 10px;
      }
      .btn-txt {
        .line-clamp(1);
        line-height: 1;
        position: relative;
        text-align: center;
        &.left-icon{
          right: 10px;
       }
      }
    }
  }

实现逻辑,左侧图标相对右侧文字定位加间距,右侧文字右侧再加定位加间距,让文字水平居中。

标签:center,height,按钮,图标,margin,css,left
From: https://www.cnblogs.com/beileixinqing/p/18219907

相关文章

  • Windows 11系统设置显示桌面图标方法分享
        随着Windows11操作系统的正式发布,许多用户纷纷升级,以体验其新颖的界面和改进的功能。然而,面对全新的用户界面和设置选项,即使是经验丰富的Windows用户也可能会遇到一些挑战。其中一个常见的问题就是如何在Windows11中设置显示桌面图标,这对于习惯了在桌面上组织文......
  • css画箭头,三角形
    箭头.arrow{border:solidblack;border-width:01px1px0;display:inline-block;padding:4px;}//向上.up{transform:rotate(-135deg);-webkit-transform:rotate(-135deg);}//向下.down{transform:rotate(45deg);-webkit-transform:r......
  • css中px和em的区别
    px和em是用来表示元素大小的单位,在CSS中经常被使用。px(pixel)是像素单位,表示屏幕上的一个点。它是一个绝对单位,表示一个固定的大小。例如,一个宽度为200px的元素将始终显示为200个像素宽。em是相对单位,根据继承它的父元素的字体大小来计算实际的大小。默认情况下......
  • SAP: ALV GRID 控制之 单元格按钮
    工作区域(或内部表)“GT_SFLIGHT”不是扁平的,或者包含参考或内部表作为组件。Isnotflat,orcontainsReferenceorinternaltablesascomponentsorinternaltablesascomponents.Ascomponents.解决方式:将SELECT*FROMSFLIGHTINTOTABLEgt_sflightUPTO......
  • css04 CSS Combinators
    https://www.w3schools.com/css/css_combinators.aspCSSCombinatorsAcombinatorissomethingthatexplainstherelationshipbetweentheselectors.ACSSselectorcancontainmorethanonesimpleselector.Betweenthesimpleselectors,wecanincludeaco......
  • css05 CSS Pseudo-classes
    https://www.w3schools.com/css/css_pseudo_classes.asp WhatarePseudo-classes?Apseudo-classisusedtodefineaspecialstateofanelement.Forexample,itcanbeusedto:StyleanelementwhenausermousesoveritStylevisitedandunvisitedlinks......
  • css03 CSS Selectors
    https://www.w3schools.com/css/css_selectors.aspACSSselectorselectstheHTMLelement(s)youwanttostyle.CSSSelectorsCSSselectorsareusedto"find"(orselect)theHTMLelementsyouwanttostyle.WecandivideCSSselectorsintofive......
  • css01 CSS Introduction
    https://www.w3schools.com/css/css_intro.asp CSSisthelanguageweusetostyleaWebpage.WhatisCSS?CSSstandsforCascadingStyleSheetsCSSdescribeshowHTMLelementsaretobedisplayedonscreen,paper,orinothermediaCSSsavesalotofw......
  • css02 CSS Syntax
    https://www.w3schools.com/css/css_syntax.aspACSSruleconsistsofaselectorandadeclarationblock.CSSSyntaxTheselectorpointstotheHTMLelementyouwanttostyle.Thedeclarationblockcontainsoneormoredeclarationsseparatedbysemicolo......
  • html+CSS部分基础运用8
    1.P147实验1,完成页面制作效果。图7-1木兰花令效果图2.P147实验2,完成页面制作效果。项目1<!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <linktype="text/css">  <title>木兰花令</title>......